0

The texture simply doesn't render, the geometry is all black.

Screenshot: https://i.stack.imgur.com/QE1QU.png

Code: http://pastebin.com/SvB8rxxt

I'd also link to the texture I'm trying to render and the transformations.py module, but I don't have reputation to be allowed to put in more than two links. Googling "modern opengl 02" will get you the tutorial from which I got the former, and "transformations py" will get you the latter.

Search for "TEXTURE STUFF START" to find where texture stuff is set up.

I tried poking at all the parts (image loading, texture loading, texture coordinate data, shaders) but I don't know of a way to really determine which is at fault, and all seem fine as far as I can tell.

Update: Updated code with some potential problems fixed, but still exhibitng the same issue.

genpfault
  • 51,148
  • 11
  • 85
  • 139
orost
  • 361
  • 2
  • 6

1 Answers1

1

I'm not a python guy so maybe I'm missing something.

Having all fragments rendering black has to be one of these things:

  1. Texture data isn't loaded
  2. Texture isn't bound to the sampler for that shader
  3. Something wrong with lighting math
  4. Uniforms aren't being sent correctly to the shader
  5. 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?

Community
  • 1
  • 1
badweasel
  • 2,349
  • 1
  • 19
  • 31
  • I think you're right that I'm not setting the uniform properly. Could you explain how to do that? `glGetUniformLocation(shader, "tex")` (which is what goes in the first argument of `glUniform1i`, right?) just returns 0. – orost Jun 19 '14 at 01:01
  • I read up some more and apparently 0 is a valid value for a uniform location, but glUniform1i fails with "invalid operation 1282" which I can't figure out. – orost Jun 19 '14 at 01:19
  • You're waiting to do that glGetUniformLocation until after the shader is compiled right? I'll add my suggestion to the answer so I can format it. – badweasel Jun 19 '14 at 01:42
  • Yes, I'm passing the shader as an argument to `glGetUnformLocation`, so I'm not even sure how I could do that before I create it. – orost Jun 19 '14 at 01:45
  • Your update is basically the same thing that I added myself after your first response - and unfortunately, `glUniform1i` fails with that "invalid operation" error code. Googling, this is most often a problem with using the texture ID instead of the uniform location, but I _am_ using the uniform location (from `glGetUniformLocation(shader, "tex")`, and anyway, they both have the same value of `0`. – orost Jun 19 '14 at 01:54
  • They can have the same value. But as a test maybe add a second texture id to make sure things are working. A second texture id should not also be 0 – badweasel Jun 19 '14 at 01:57
  • Like do texID2 = glGenTextures(1) and see what that value is – badweasel Jun 19 '14 at 01:59
  • Do you mean change `glGenTextures(1)` to `glGenTextures(2)` and see what the second texture ID is? It's `2` and using it instead makes no difference. But the texture ID doesn't go into `glUniform1i`, am I misunderstanding? – orost Jun 19 '14 at 02:03
  • No I meant as a test add it after the 1 just to see that it gives you a different value. Sort of grasping at straws though – badweasel Jun 19 '14 at 02:03
  • Right but you said that texID and u_tex have the same values. If the glUniform1l is erring the u_tex must be wrong – badweasel Jun 19 '14 at 02:06
  • Yes, the texture IDs are properly unique, `1` and `2`. Do you think I should open another question about `glUniform1i`? You've helped me narrow it down a lot. – orost Jun 19 '14 at 02:06
  • I did say that, but that was wrong, sorry. Texture IDs start with 1, not 0. – orost Jun 19 '14 at 02:08
  • hey.. you're also missing glEnable(GL_TEXTURE_2D) – badweasel Jun 19 '14 at 02:10
  • True, but I've added it since then. Here's how the code looks now: http://pastebin.com/re1n2c0J – orost Jun 19 '14 at 02:13
  • You didn't do what I suggested. Read my suggestion again. Where I say "when you bind the texture" I mean in the display loop. That's what it's not working. You bunched all those calls up together. They have to be where I said to put them. – badweasel Jun 19 '14 at 02:19
  • I was confused because you said "when you bind the texture", and I wasn't really binding the texture anywhere. If you mean `texturebuffer`, that's just the texture coordinates. Sorry about that. The program now runs, but the texture still isn't rendering (still black). To be sure if I undrestand: the last three lines go into the display loop, and everything else into one-time setup, correct? – orost Jun 19 '14 at 02:28
  • should yeah. I'd also look here http://pyopengl.sourceforge.net/context/tutorials/nehe6.html to make sure you're not missing something else. I'm about out of ideas since I'm not a python coder. Maybe someone else will post an answer. – badweasel Jun 19 '14 at 04:11
  • Check my latest update. It's often 3 or 4 different things at the same time when OpenGL stuff doesn't work. – badweasel Jun 19 '14 at 04:15
  • Re updats: That was one of the test I managed to do myself before posting the question - the fragment shader is working, at least. Re link: the NeHe tutorial is extremely outdated and mostly irrelevant towards the version of openGL I'm using unfortunately. Anyway, thanks a lot for the help, you did manage to help me narrow it down a lot. – orost Jun 19 '14 at 11:08