jwl*_*ton 11 opengl glsl osx-elcapitan
我有一个OpenGL(4.1)应用程序,我在Xcode中构建,在升级之前运行正常,但现在没有向OpenGL视图写入任何内容.应用程序构建时没有错误或警告,GLSL着色器编译和链接没有错误.
我运行了一些测试,看看着色器是否基本正常工作:
如果片段着色器的最后一行是:
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
Run Code Online (Sandbox Code Playgroud)
OpenGL窗口应该是完全红色的.我认为这也证实了顶点数据正确传递.我可以动画视图围绕任何轴旋转,我再次可以看到正确传递顶点数据(包括深度).
如果片段着色器的最后一行是:
fragColor = vec4(texCoord[0], 0.0, 0.0, 1.0);
Run Code Online (Sandbox Code Playgroud)
要么
fragColor = vec4(texCoord[1], 0.0, 0.0, 1.0);
Run Code Online (Sandbox Code Playgroud)
我在x或y方向上得到一个红色渐变,向我指示纹理坐标也正确传递.
所以我认为纹理数据本身由于某种原因没有被传递到采样器中.这是相关的代码.
创建纹理的代码是:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures( 1, appController->appDelegate->wispTexture);
glBindTexture(GL_TEXTURE_2D, appController->appDelegate->wispTexture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, (GLsizei)TEXTUREWIDTH, (GLsizei)TEXTUREHEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, appController->appDelegate->wispTexture[0]);
Run Code Online (Sandbox Code Playgroud)
glTexImage2D调用的数据源现在为null,因为我稍后在应用程序中加载它.应用程序构建纹理图像数据,但现在只是为了测试着色器的操作我将它加载到纯白色:
for(int i = 0; i < NUMTEXTUREPOINTS; i++)
{
appController->textureManager->wispBitmapData[i].red = 255;
appController->textureManager->wispBitmapData[i].green = 255;
appController->textureManager->wispBitmapData[i].blue = 255;
appController->textureManager->wispBitmapData[i].alpha = 255;
}
Run Code Online (Sandbox Code Playgroud)
其中wispBitmapData声明为:
AlphaPixelBytes wispBitmapData[NUMTEXTUREPOINTS];
Run Code Online (Sandbox Code Playgroud)
和AlphaPixelBytes被定义为一个结构:
typedef struct
{
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char alpha;
} AlphaPixelBytes;
Run Code Online (Sandbox Code Playgroud)
绘制图像的代码是:
glBindTexture(GL_TEXTURE_2D, appDelegate->wispTexture[0]);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, (GLsizei)TEXTUREWIDTH, (GLsizei)TEXTUREHEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, appController->textureManager->wispBitmapData);
glBindBuffer(GL_ARRAY_BUFFER, appDelegate->wispVertexBuffer[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(wispVertices), wispVertices);
glBindBuffer(GL_ARRAY_BUFFER, appDelegate->wispTexCoordBuffer[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(wispTextureCoordinates), wispTextureCoordinates);
glBindBuffer(GL_ARRAY_BUFFER, appDelegate->wispHitsBuffer[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(wispScreenHits), wispScreenHits);
glBindBuffer(GL_ARRAY_BUFFER, appDelegate->wispNormalBuffer[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(wispNormals), wispNormals);
glUseProgram(appDelegate->wispShaders);
glBindBuffer(GL_ARRAY_BUFFER, appDelegate->wispVertexBuffer[0]);
glVertexAttribPointer(appDelegate->wispPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(appDelegate->wispPositionLoc);
glBindBuffer(GL_ARRAY_BUFFER, appDelegate->wispTexCoordBuffer[0]);
glVertexAttribPointer(appDelegate->wispTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(appDelegate->wispTexCoordLoc);
glBindBuffer(GL_ARRAY_BUFFER, appDelegate->wispHitsBuffer[0]);
glVertexAttribPointer(appDelegate->wispHitsLoc, 1, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(appDelegate->wispHitsLoc);
glBindBuffer(GL_ARRAY_BUFFER, appDelegate->wispNormalBuffer[0]);
glVertexAttribPointer(appDelegate->wispNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(appDelegate->wispNormalLoc);
glUniform1i(appDelegate->wispFilterModeLoc, coloringdata->filteringMode);
glUniform1i(appDelegate->wispMaxHitsLoc, maxScreenHits);
glUniform1i(appDelegate->wispMaxNumSamplesLoc, coloringdata->maxNumSamples);
glUniform1f(appDelegate->wispSampleSizeFactorLoc, coloringdata->sampleSizeFactor);
glUniform1i(appDelegate->wispDisplayModeLoc, coloringdata->displayMode);
glUniform1i(appDelegate->wispLightOnLocation, coloringdata->lightingOn);
glUniformMatrix4fv(appDelegate->wispModelMatrixLocation, 1, GL_FALSE, appDelegate->modelMatrix.m);
glUniformMatrix4fv(appDelegate->wispViewMatrixLocation, 1, GL_FALSE, appDelegate->viewMatrix.m);
glUniformMatrix4fv(appDelegate->wispProjectionMatrixLocation, 1, GL_FALSE, appDelegate->projectionMatrix.m);
glUniformMatrix3fv(appDelegate->wispNormalMatrixLocation, 1, GL_FALSE, appDelegate->normalMatrix.m);
glUniform3fv(appDelegate->wispLightPositionLocation, 1, coloringdata->lightPosition);
glActiveTexture(GL_TEXTURE0);
glUniform1i(appDelegate->wispTextureLoc, 0);
glBindTexture(GL_TEXTURE_2D, appDelegate->wispTexture[0]);
// *********************************** Draw the Image to The Screen
glBindVertexArray(appDelegate->wispVertexArray[0]);
glBindVertexArray(appDelegate->wispTexCoordArray[0]);
glBindVertexArray(appDelegate->wispHitsArray[0]);
glBindVertexArray(appDelegate->wispNormalArray[0]);
glDrawArrays(GL_POINTS, 0, NUMSCREENPOINTS);
glDisableVertexAttribArray(appDelegate->wispPositionLoc);
glDisableVertexAttribArray(appDelegate->wispTexCoordLoc);
glDisableVertexAttribArray(appDelegate->wispHitsLoc);
glDisableVertexAttribArray(appDelegate->wispNormalLoc);
[[appController->wispView openGLContext] flushBuffer];
Run Code Online (Sandbox Code Playgroud)
任何人都可以看到这个代码有什么问题?我已经多次参与其中,并检查了我能想到的文档.
在片段着色器中,有一些声明:
in vec2 texCoord;
uniform sampler2D Texture;
out vec4 fragColor;
Run Code Online (Sandbox Code Playgroud)
但
fragColor = texture(Texture, texCoord);
Run Code Online (Sandbox Code Playgroud)
呈现黑屏.
我保留了关于着色器简短的代码发布,因为它们实际上非常复杂.
我现在已经创建了非常简单的新测试着色器:
顶点着色器:
#version 410
in vec4 Position;
in vec2 TexCoord;
uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ProjectionMatrix;
out vec2 texCoord;
void main()
{
texCoord = TexCoord;
gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * Position;
}
Run Code Online (Sandbox Code Playgroud)
片段着色器:
#version 410
in vec2 texCoord;
uniform sampler2D Texture;
out vec4 fragColor;
void main()
{
fragColor = texture(Texture, texCoord);
}
Run Code Online (Sandbox Code Playgroud)
我仍然可以通过上述各种测试获得相同的结果.
我还在每一步都跟踪了GL错误的代码测试,但没有.
我很欣赏那些为堆叠溢出贡献自己的时间和经验的优秀人才,我敢肯定,赏金中提供的声誉点对于任何可以帮助我理顺这一点的人来说都没有任何意义.只要明白我就知道并感谢你的帮助.
我正在回答我自己的问题,因为没有其他答案,我最终解决了我所有的问题。我只是对此做一些总结。
追踪代码后,我发现 OpenGL 代码没问题,但某些调用的计时存在问题。
我将所有应用程序初始化代码移至:
- (void)applicationWillFinishLaunching:(NSNotification *)aNotification
Run Code Online (Sandbox Code Playgroud)
到:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
Run Code Online (Sandbox Code Playgroud)
这解决了所有问题(包括我现在理解的其他一些棘手问题),并且应用程序再次正常运行。
| 归档时间: |
|
| 查看次数: |
244 次 |
| 最近记录: |