   Beginner question on shaders
  I have a beginner question on shaders. I'm trying to texture a model but can't see a thing, everything is black.Here's the vertex shader:varying vec2 texture_coordinate;void main(){texture_coordinate = vec2(gl_MultiTexCoord0);gl_Position = ftransform();}And the fragment shader:varying vec2 texture_coordinate; uniform sampler2D ado;void main(){    gl_FragColor = texture2D(ado,texture_coordinate);}And here is the code for the rendering:void Render(){glUseProgramObjectARB(program);int i,uloc;glActiveTexture(GL_TEXTURE0);glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, texID);uloc = glGetUniformLocation(program, "ado");glUniform1i(uloc, texID);glActiveTexture(GL_TEXTURE0);glPushMatrix();glMultMatrixf(*matrix);glBegin(GL_TRIANGLES);int j;for (i=0; i&lt;triList.size(); i++){for (j=0; j&lt;3; j++){glTexCoord2fv(&texCoords[triList.index[j]].x);glNormal3fv(*normals[triList.index[j]]);glVertex3fv(*vertices[triList.index[j]]);}}glEnd();glPopMatrix();glUseProgramObjectARB(NULL);}So what's going on there?   
  When you see nothing, either the pixel shader results in black colors, or (more likely) the modelview projection matrix is wrong or you forgot to set it. That will make the vertices transform to a place where they *don't* belong :)To make sure your fragment shader is ok, just let it render pure white pixels.//gl_FragColor = texture2D(ado,texture_coordinate);gl_FragColor = 1;If you do see something now, either your texture is not loaded/not passed properly, or the texcoords are wrong. Ow, and give the result color alpha value also '1'. Just to make sure you don't render a 100% transparent model (in case transparency or blending was enabled).If you still see nothing, focus on the vertex shader. gl_Position = ftransform();I don't know glsl, neither the ftransform function. But ussually you multiply the vertex world positions with the modelview projection matrix. I believe this is a reserved matrix in glsl, so you can always use it. Pick a simple glsl demo, and you will probably see that matrix.In your OpenGL code you already do a matrix multiplication as well with glMultMatrixf(*matrix);I suppose you render an object at a certain position/scale/rotation? Anyway, you could first try to render the object at its origin, so without that line. Point your camera at the origin, use the modelviewmatrix in your shader, and then look again if you can finally see your object.Greetings,Rick   ;Quote:Original post by spekWhen you see nothing, either the pixel shader results in black colors, or (more likely) the modelview projection matrix is wrong or you forgot to set it. That will make the vertices transform to a place where they *don't* belong :)To make sure your fragment shader is ok, just let it render pure white pixels.//gl_FragColor = texture2D(ado,texture_coordinate);gl_FragColor = 1;If you do see something now, either your texture is not loaded/not passed properly, or the texcoords are wrong. Ow, and give the result color alpha value also '1'. Just to make sure you don't render a 100% transparent model (in case transparency or blending was enabled).If you still see nothing, focus on the vertex shader. gl_Position = ftransform();I don't know glsl, neither the ftransform function. But ussually you multiply the vertex world positions with the modelview projection matrix. I believe this is a reserved matrix in glsl, so you can always use it. Pick a simple glsl demo, and you will probably see that matrix.In your OpenGL code you already do a matrix multiplication as well with glMultMatrixf(*matrix);I suppose you render an object at a certain position/scale/rotation? Anyway, you could first try to render the object at its origin, so without that line. Point your camera at the origin, use the modelviewmatrix in your shader, and then look again if you can finally see your object.Greetings,RickI can see something when saying this:gl_FragColor = 1;And the texture is loaded correctly.   ;  Yippie :) But is it also what you want to see? I mean, is the shape correct? Ifso, your vertex shader is fine, so then check this:- texcoords ok?- output color alpha == 1? (I suppose you don't want transparency for now)- is the texture passed to the shader?In order to check your model, try to show the texcoords:gl_FragColor.rg = texcoord.xy;gl_FragColor.b  = 0;gl_FragColor.a  = 1;You should see a funky red/greenish disco model now, since your texcoords are between 0 and 1 (are they?). If everything is black again, your texcoords are not passed properly.You can also try this trick with the normal, in case you would like to test them. Pass the normals or texcoords from the vertex shader to the fragment shader, and place them in the output color. Be aware that negative normals result in black pixels.Your OpenGL code seems to be ok, so I suspect the test from above will be ok. In that case your texture might be on the wrong texture slot. But first, to be sure, check if its loaded properly by rendering it without any shaders. If that works, you must checkuloc = glGetUniformLocation(program, "ado");glUniform1i(uloc, texID);It seems to be ok though... Well, first check if your model is ok. When you see only white pixels its hard to tell, so map the normals or texcoords for a better view. If its deformed, something goes wrong with the matrices.Rick;  The texcoords are ok. I do a print on uloc, and it's 0, and gleLastError() returns 0.The geometry is not distorted. Rendering without shaders looks fine.   ;  >> The geometry is not distorted. Rendering without shaders looks fine.Ok, but is the shape the same with the vertex shader enabled? If not -> matrices, ifso -> texture.You said the texcoords are ok. I'm sure they are ok in your listing in the OpenGL code. But are they also received properly in the fragment shader?texture_coordinate = vec2(gl_MultiTexCoord0);I would say they are, since you callglActiveTexture(GL_TEXTURE0);But its always wise to check them anyway. I can't point you a bug in your shaders or GL code, so I'm trying to exclude problems. That's why you could render the model with the texcoords as resulting color, just to check if you see something. You tried that?A test table I always use:- Anything at all visible? Use white pixels to render- Alpha problem? Disable blending/transprency, output fragment color.a as '1'- Matrices ok? Render object without matrix transformations (in OpenGL code, stay at the origin), check if the shape is ok.- Passed data ok? Output normals/texcoords/tangents/.../ on your model as color to see if they are not black or all the same- Texture ok? Render without a shader on a quad or something- Textured passed ok? Make sure you call the right parameter, and/or the texture is set on the right channel (texture channel 0..15)- Still doubts about the texcoords? Render a known color from the texture on your model (for example, the center pixel . If that does work (model has the same color everywhere, but the correct known color), the texcoords are not ok, or not received properly in the shader.<br><br>I can't think of more problems… Unless it is actually a bug with calling the GLSL shader. I can't help you with that though, since I never used GLSL.<br><br>Rick;Quote:Original post by spek>> The geometry is not distorted. Rendering without shaders looks fine.Ok, but is the shape the same with the vertex shader enabled? If not -> matrices, ifso -> texture.You said the texcoords are ok. I'm sure they are ok in your listing in the OpenGL code. But are they also received properly in the fragment shader?texture_coordinate = vec2(gl_MultiTexCoord0);I would say they are, since you callglActiveTexture(GL_TEXTURE0);But its always wise to check them anyway. I can't point you a bug in your shaders or GL code, so I'm trying to exclude problems. That's why you could render the model with the texcoords as resulting color, just to check if you see something. You tried that?A test table I always use:- Anything at all visible? Use white pixels to render- Alpha problem? Disable blending/transprency, output fragment color.a as '1'- Matrices ok? Render object without matrix transformations (in OpenGL code, stay at the origin), check if the shape is ok.- Passed data ok? Output normals/texcoords/tangents/.../ on your model as color to see if they are not black or all the same- Texture ok? Render without a shader on a quad or something- Textured passed ok? Make sure you call the right parameter, and/or the texture is set on the right channel (texture channel 0..15)- Still doubts about the texcoords? Render a known color from the texture on your model (for example, the center pixel . If that does work (model has the same color everywhere, but the correct known color), the texcoords are not ok, or not received properly in the shader.<br><br>I can't think of more problems… Unless it is actually a bug with calling the GLSL shader. I can't help you with that though, since I never used GLSL.<br><br>Rick<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>- I use white pixels to render, the geometry looks fine, no cracks or distorsion, it's what i expect to see.<br>- There's no alpha problem.<br>- Matrices seem to be ok.<br>- I output texCoords and they seem tyo be ok<br>- I render without a shader and the texture is ok<br>- I think texture is passed ok<br><br>I made something and it worked: changed from glUniform1i to glUniform1f and now it shows the texture ok. But i can't manage 3 textures in the fragment shader. I have a gf 6600gt so i can handle 3 textures at &#111;nce. I pass 3 textures to the fragment shader diffuse, bump, and specular, but when i say this: <br>    gl_FragColor = texture2D(specID,vec2(texture_coordinate));<br>in the fragment shader the model is textured with the diffuse texture.<br>The diffuse and spec have different values.<br><br>All this is pretty strange :D;  That brings is closer to the real problem. So it seems to be a problem with passing the parameter with 'glUniform1i'. I don't know which functions there are in GLSL. Cg for example has seperated function for passing integers, floats, matrices, and texture handles. A texture handle is an integer number, so its a little bit strange that 'glUniform1f'(pass a float) does work.Anyway, I looked at a GLSL program, and it works a little bit different there. First of all, you don't have to pass the texture parameters if you know on which texture channel the textures are set. For example:    // Bind the textures:    glActiveTextureARB(GL_TEXTURE2_ARB);    glBindTexture(GL_TEXTURE_2D, normalMap);    glActiveTextureARB(GL_TEXTURE1_ARB);    glBindTexture(GL_TEXTURE_2D, specularMap);    glActiveTextureARB(GL_TEXTURE0_ARB);    glBindTexture(GL_TEXTURE_2D, diffuseMap);... activate shader & render ...// shaderuniform sampler2D diffuseMap  (= channel 0)uniform sampler2D specularMap (= channel 1)uniform sampler2D normalMap   (= channel 2)I think the order in which you declare the samplers is important here. But maybe you can also explicitely declare which channel to use. In Cg for example, you can write:sampler2D myTexture : TEXUNIT0I guess GLSL can do this as well, but I don't know the command. Anyway, if I read your problem I think textures are placed on another channel than the shader expects them to be, which causes the wrong mix.By the way, maybe this is useful (or maybe you was already using this site)http://www.lighthouse3d.com/opengl/glsl/index.php?textureMultiThe demo I looked at was a HDR demo on www.delphi3d.net. OpenGL + GLSL, in Delphi code though (althoug the OpenGL commands are exaclty the same as in C++).Rick   ;  That brings it closer to the real problem. So it seems to be a problem with passing the parameter with 'glUniform1i'. I don't know which functions there are in GLSL. Cg for example has seperated function for passing integers, floats, matrices, and texture handles. A texture handle is an integer number, so its a little bit strange that 'glUniform1f'(pass a float) does work, and glUniform1i not.Anyway, I looked at a GLSL program, and it works a little bit different there. You don't have to pass the texture parameters if you know on which texture channel the textures are set. For example:    // Bind the textures:    glActiveTextureARB(GL_TEXTURE2_ARB);    glBindTexture(GL_TEXTURE_2D, normalMap);    glActiveTextureARB(GL_TEXTURE1_ARB);    glBindTexture(GL_TEXTURE_2D, specularMap);    glActiveTextureARB(GL_TEXTURE0_ARB);    glBindTexture(GL_TEXTURE_2D, diffuseMap);... activate shader & render ...... No glUniform1i/f here for passing the textures! (only for vectors and scalars)// shaderuniform sampler2D diffuseMap  (= channel 0)uniform sampler2D specularMap (= channel 1)uniform sampler2D normalMap   (= channel 2)I think the order in which you declare the samplers is important here. But maybe you can also explicitely declare which channel to use. In Cg for example, you can write:sampler2D myTexture : TEXUNIT0I guess GLSL can do this as well, but I don't know the command. Anyway, if I read your problem I think textures are placed on another channel than the shader expects them to be, which causes the wrong mix.By the way, maybe this is useful (or maybe you was already using this site)http://www.lighthouse3d.com/opengl/glsl/index.php?textureMultiThe demo I looked at was a HDR demo on www.delphi3d.net. OpenGL + GLSL, in Delphi code though (althoug the OpenGL commands are exaclty the same as in C++).Rick   ;  I finally solved like this:glActiveTexture(GL_TEXTURE0);glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, texID);uloc = glGetUniformLocation(program, "diffuse");glUniform1i(uloc, 0);glActiveTexture(GL_TEXTURE1);glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, bumpID);uloc = glGetUniformLocation(program, "bump");glUniform1i(uloc, 1);glActiveTexture(GL_TEXTURE2);glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, specID);uloc = glGetUniformLocation(program, "specID");glUniform1i(uloc, 2);I'm passing 0,1, and 2 instead of texID, bumpID, specID which have the following values: 1,2,3.This is kind of weird... guess i have to specify the texture unit index...   