简体   繁体   中英

OpenGL texture doesn't render

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

Screenshot: http://i.imgur.com/ypMdQY4.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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM