Skip to main content
GameDev.net gamedev.net
🔒 Locked

OpenGL Texturing and Cg

Started by TThirion Mar 10, 2005 at 5:30 PM 4 replies 1.6k views
Original Post
TThirion
TThirion
I can't seem to read textures properly in Cg. I'm confused about the proper state for OpenGL. I lookup into two textures in a Cg fragment program. I first read the two textures into memory, generating a unique ID for each and then passing that ID to the fragment program as a uniform parameter. However, doing this only allowed me to access the last texture I loaded. I figured that I needed to enable multitexturing and load a single texture into two different texture units. So I did that and now I get no geometry at all. Can someone clue me in as to how to setup OpenGL to be able to read two textures in a Cg fragment program? EDIT: Also, Cg seems to quit when I use GLEW and enable multitexturing. Even my shaders that don't access textures won't work. T
Evil Bachus
Evil Bachus
Bind the first texture on GL_TEXTURE0_ARB, and the second texture on GL_TEXTURE1_ARB. Then in the fragment program use "uniform samplerWHATEVER tex0 : TEXUNIT0, uniform samplerWHATEVER tex1 : TEXUNIT1" and replace WHATEVER with 1D, 2D, or whatever you want the handle to the texture object to be. Then you you can use tex2D/texRECT/whatever on tex0 and tex1 to lookup the texture values.

That *should* work.
TThirion
TThirion
No luck.

I load the images:
fclose(file);image = auxDIBImageLoad("noise.bmp");glActiveTextureARB(GL_TEXTURE0_ARB);glEnable(GL_TEXTURE_2D);glGenTextures(1, &g_noiseTexture);glBindTexture(GL_TEXTURE_2D, g_noiseTexture);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->sizeX, image->sizeY,	 0, GL_RGB, GL_UNSIGNED_BYTE, image->data);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);free(image->data);free(image);image = 0;


and similar for the second.
The rendering does:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glTranslatef(0.0f, -1.5f, -g_cameraZ);glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);glRotatef(g_phi, 1.0f, 0.0f, 0.0f);glRotatef(g_theta, 0.0f, 0.0f, 1.0f);g_currentVP->SetStateMatrix("modelViewProj",			    CG_GL_MODELVIEW_PROJECTION_MATRIX,		            CG_GL_MATRIX_IDENTITY);g_currentVP->SetStateMatrix("modelViewIT",                            CG_GL_MODELVIEW_MATRIX,                            CG_GL_MATRIX_INVERSE_TRANSPOSE);glutSolidTeapot(1.0f);glFlush();


The vertex shader is:
void main(float4 position		: POSITION,		  float4 normal		: NORMAL,		  float2 texCoord	: TEXCOORD0,		  out float4 oPosition	: POSITION,		  out float3 oNormal	: TEXCOORD1,		  out float2 oTexCoord	: TEXCOORD2,		  		  uniform float4x4 modelViewProj,		  uniform float4x4 modelViewIT){	oPosition = mul(modelViewProj, position);	oNormal   = mul(modelViewIT, normal).xyz;	oTexCoord = texCoord;}


void main(float4 position	        : TEXCOORD0,		  float3 normal		: TEXCOORD1,		  float2 texCoord0	: TEXCOORD2,		  		  out float4 oColor	: COLOR,		  uniform float3	Kd,		  uniform float3	lightPosition,		  uniform float3	eyePosition,		  uniform float		ambientIntensity,		  uniform float		contrastExponent,		  uniform sampler2D	contrastTexture : TEXUNIT0,		  uniform sampler2D	randomTexture   : TEXUNIT1){	float3 N = normalize(normal);	float3 L = normalize(lightPosition - position.xyz);	float diffuseLight = max(dot(N, L), 0.0f);	float illumination = max(diffuseLight + ambientIntensity, 0.0f);	float contrastIllumination = pow(illumination, contrastExponent);	float2 texCoord1 = float2(0.0f, contrastIllumination);		float4 random = tex2D(randomTexture, texCoord0);	texCoord1.y += random.y;	float4 contrast = tex2D(contrastTexture, texCoord1).xxxx;	float4 color = contrastIllumination.xxxx;	float4 smudgedColor = (contrast + color) * 0.5f;	oColor = smudgedColor;}


There's no output and no errors reported in OpenGL nor in Cg.

T
silvermace
silvermace
your rendering code isnt right, you need to bind each texture to each texture unit per frame:

glLoadIdentity();glTranslatef(0.0f, -1.5f, -g_cameraZ);glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);glRotatef(g_phi, 1.0f, 0.0f, 0.0f);glRotatef(g_theta, 0.0f, 0.0f, 1.0f);g_currentVP->SetStateMatrix("modelViewProj",			    CG_GL_MODELVIEW_PROJECTION_MATRIX,		            CG_GL_MATRIX_IDENTITY);g_currentVP->SetStateMatrix("modelViewIT",                            CG_GL_MODELVIEW_MATRIX,                            CG_GL_MATRIX_INVERSE_TRANSPOSE);glActiveTexture(GL_TEXTURE0_ARB);glBindTexture(texture1);glActiveTexture(GL_TEXTURE1_ARB);glBindTexture(texture2);glutSolidTeapot(1.0f);glFlush();
TThirion
TThirion
No, that didn't do it.

T
Roquqkie
Roquqkie
Hi,

I've just skimmed your code, but one thing hit my eye... Try not to use glutSphere() and instead just draw a simple triangle or quad with texture-coordinates and normals.
It could be a problem, if glutSphere dosen't send normals and texture-coordinates down the pipeline, but then again, I don't know the inner work of glutSphere ;)

edit:
Btw. Where do you bind your shaders? Enable your shader-profile?
Another thing: You can't output TEXCOORD1 before you output TEXCOORD0.

edit edit:
You have to specify ...ARB after each extension like multiple textures. GLEW screws this up once in a while.
Try to cout your opengl function-pointers, they will probably return NULL.

Best regards,
Roquqkie

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.