简体   繁体   English

OpenGL纹理无法渲染

[英]OpenGL texture doesn't render

The texture simply doesn't render, the geometry is all black. 纹理根本无法渲染,几何图形全为黑色。

Screenshot: http://i.imgur.com/ypMdQY4.png 截图: http//i.imgur.com/ypMdQY4.png

Code: http://pastebin.com/SvB8rxxt 代码: 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. 我还将链接到我要渲染的纹理和Transformations.py模块,但是我没有声誉,不能放置两个以上的链接。 Googling "modern opengl 02" will get you the tutorial from which I got the former, and "transformations py" will get you the latter. 谷歌搜索“现代opengl 02”将为您提供从中获得前者的教程,而“ transformations py”将为您提供后者。

Search for "TEXTURE STUFF START" to find where texture stuff is set up. 搜索“ TEXTURE STUFF START”以查找设置纹理的位置。

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. 我不是python专家,所以也许我缺少了一些东西。

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). 法线的错误属性(例如如果它们全为0)。

Since your shader is a straight finalColor = texture(tex, fragTexCoord); 由于您的着色器是直接的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: 在iOS中,当我绑定纹理时,我必须执行以下操作:

    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. 也许texturebuffer.bind()为您执行了这些步骤之一。 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. 设置不正确的制服可能会为该制服返回0。

Somewhere you have to connect `uniform sampler2D tex' from your shader to the texture you're binding when rendering. 渲染时,必须将着色器的“ uniform sampler2D tex”连接到要绑定的纹理。

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. 我还认为调用您的纹理ID tex (将其返回给glGenTextures然后又称为统一的“ tex”)是令人困惑的,并且形式可能很糟。这是两种不同的情况。一是纹理id,另一是统一变量。

First thing I'd do is change the name of the id from tex to texID to keep them straight in your mind. 我要做的第一件事是将id的名称从tex更改为texID以使它们texID 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. 然后,您可能会发现,在代码中没有任何地方可以获取glsl统一变量tex的统一位置(glGetUniformLocation),顺便说一下,必须编译着色器之后进行此操作。 Maybe there's some helper there in python I'm not seeing. 也许我没有看到python中的一些帮助器。

But when you do texture buffer.bind where is that connecting things to the uniform variable 'tex'? 但是,当您执行texture buffer.bind ,将事物连接到统一变量'tex'的位置在哪里?

* UPDATE 1 * *更新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: 它将统一的位置ID传回您的应用程序:

u_tex = glGetUniformLocation(shader, "tex")

Change that texture id variable to texID like this: 将纹理id变量更改为texID如下所示:

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. 而且我不得不承认我不知道texturebuffer.bind()作用。 So probably try the 3 lines above either before or after that line. 因此,请尝试在该行之前或之后的三行。

* UPDATE 2 * *更新2 *

Just noticed that you're also missing glEnable(GL_TEXTURE_2D) which should be right there along with your glTexParameter stuff. 只是注意到您还缺少glEnable(GL_TEXTURE_2D) ,它应该与glTexParameter东西一起放在那里。 Add it above those lines. 将其添加到这些行上方。

* UPDATE 3 * *更新3 *

Just as a test in your shader replace finalColor = texture(tex, fragTexCoord); 就像在着色器中进行测试一样,替换finalColor = texture(tex, fragTexCoord); with finalColor = vec4(1.0,0.0,0.0,1.0); 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. 因为现在我想到了,所以我不知道finalColor是什么。 It should be gl_FragColor = . 应该是gl_FragColor =

* UPDATE 4 * *更新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. 我没有意识到您使用的是OpenGL 3.0。 I was thinking 2.0. 我当时在想2.0。 The output variable name depends on your glsl version. 输出变量名称取决于您的glsl版本。 You've set the version to 1.3. 您已将版本设置为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? 请参阅这篇文章,并确保您也正在正确地执行该部分: 片段着色器如何知道要用于像素颜色的变量?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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