I'm not a python guy so maybe I'm missing something.
Having all fragments rendering black has to be one of these things:
- Texture data isn't loaded
- Texture isn't bound to the sampler for that shader
- Something wrong with lighting math
- Uniforms aren't being sent correctly to the shader
- Bad attributes for Normals (like if they were all 0's).
Since your shader is a straight finalColor = texture(tex, fragTexCoord);
that rules out the lighting math and the uniforms. So something has to be wrong with your texture sampler.
In iOS when I bind a texture I have to do the following:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(glUniforms[U_textureSampler], 0);
And I would assume that you need to apply the same steps. Maybe texturebuffer.bind()
does one of more of those steps for you. But otherwise somewhere you need to be activating the texture sampler, binding it, and setting up the uniform.
An non activated texture could appear black. A uniform that isn't set up right is probably going to return 0 for that uniform.
Somewhere you have to connect `uniform sampler2D tex' from your shader to the texture you're binding when rendering.
I also think it's confusing and probably in bad form to call your texture id tex
(the one that gets returned with glGenTextures
and then also call the uniform `tex'. These are two different things. One is an texture id, the other is a uniform variable.
First thing I'd do is change the name of the id from tex
to texID
to keep them straight in your mind. Then you might see that no where in your code are you getting the uniform location (glGetUniformLocation) of the glsl uniform variable tex
, which by the way has to be done after the shader is compiled. Maybe there's some helper there in python I'm not seeing.
But when you do texture buffer.bind
where is that connecting things to the uniform variable 'tex'?
* UPDATE 1 *
A suggestion on how to get that uniform location and use it. Do this after the shader is compiled. It passes a uniform location id back to your app:
u_tex = glGetUniformLocation(shader, "tex")
Change that texture id variable to texID
like this:
texID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texID)
Then in your display loop when you bind the texture to use it do:
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, texID)
glUniform1i(u_tex, 0)
And I have to admit that I don't know what texturebuffer.bind()
does. So probably try the 3 lines above either before or after that line.
* UPDATE 2 *
Just noticed that you're also missing glEnable(GL_TEXTURE_2D)
which should be right there along with your glTexParameter stuff. Add it above those lines.
* UPDATE 3 *
Just as a test in your shader replace finalColor = texture(tex, fragTexCoord);
with finalColor = vec4(1.0,0.0,0.0,1.0);
and see if the box turns red. Cause now that I think of it I don't know what finalColor
is. It should be gl_FragColor =
.
* UPDATE 4 *
Do the above test and make sure you can set the color of the fragment manually. I didn't realize that you were in OpenGL 3.0. I was thinking 2.0. The output variable name depends on your glsl version. You've set the version to 1.3. See this post and make sure you're doing that part right also: How does the fragment shader know what variable to use for the color of a pixel?