0

我对opengl es 2相当陌生,因为我正在编写IOS应用程序,所以我想试试看!所以我在 xcode 4 中使用 GLKit 模板加载了一个默认的 opengl 程序,在摸索提供的代码之后,我开始修改它,以便我可以绘制多种不同类型的顶点数组对象......问题只是对 glClearColor 的调用似乎产生了任何结果-整个屏幕现在只是灰色的:-(我不知道出了什么问题,因为我在代码的关键位置检查了许多 glGetError 调用,它似乎总是返回 0 ......而且我也,据我所知,请按照使用 VAO 绘制所需的步骤,因为当我最初修改默认代码以显示它工作正常的飞机时......所以如果你们能帮助我,我将非常感激新手!:)

这是代码不同相关部分的发布:

//init VAOs:
glGenVertexArraysOES(NUM_VAO, _vertexArrayIDS);
glGenBuffers(NUM_VAO, _vertexBufferIDS);
glGenBuffers(NUM_VAO, _indexBufferIDS);

//init gl object for player:
//setupGLObject(VAO_PLAYER, gCubicVertexData, gCubicIndices, GL_STATIC_DRAW);   
glBindVertexArrayOES(_vertexArrayIDS[VAO_PLAYER]);

glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferIDS[VAO_PLAYER]);
glBufferData(GL_ARRAY_BUFFER, sizeof(gCubicVertexData), gCubicVertexData, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferIDS[VAO_PLAYER]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(gCubicIndices), gCubicIndices, GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(12));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(24));

//NSLog(@"glError after player init : %d", glGetError());
//init gl object for player wall:
//setupGLObject(VAO_PWALL, gPlayerWallVertexData, gPlayerWallIndices, GL_DYNAMIC_DRAW);
glBindVertexArrayOES(_vertexArrayIDS[VAO_PWALL]);

glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferIDS[VAO_PWALL]);
glBufferData(GL_ARRAY_BUFFER, sizeof(gPlayerWallVertexData), gPlayerWallVertexData, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferIDS[VAO_PLAYER]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(gPlayerWallIndices), gPlayerWallIndices, GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(12));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(24));

//init gl object for wall/floor plane:
//setupGLObject(VAO_WALL_FLOOR_PLANE, gPlanePyVertexData, gPlanePyIndices, GL_STATIC_DRAW);
glBindVertexArrayOES(_vertexArrayIDS[VAO_WALL_FLOOR_PLANE]);

glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferIDS[VAO_WALL_FLOOR_PLANE]);
glBufferData(GL_ARRAY_BUFFER, sizeof(gPlanePyVertexData), gPlanePyVertexData, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferIDS[VAO_PLAYER]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(gPlanePyIndices), gPlanePyIndices, GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(12));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), BUFFER_OFFSET(24));

glBindVertexArrayOES(0);

绘图功能 - glkView:(GLKView *)view drawInRect:(CGRect)rect

for (NSString *currentKey in g_renderables) {
    GameObject *currentRenderable = [g_renderables objectForKey:currentKey];

    if (currentRenderable.go_type == LC_FLOOR) {
        glBindVertexArrayOES(_vertexArrayIDS[VAO_WALL_FLOOR_PLANE]);

        glUseProgram(_program);

        glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, currentRenderable.go_mvm.m);
        glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, _normalMatrix.m);

        NSLog(@"glError : %d", glGetError());

        glDrawElements(GL_TRIANGLE_STRIP, sizeof(gPlanePyIndices)/sizeof(gPlanePyIndices[0]), GL_UNSIGNED_INT, 0);
    }
    else if (currentRenderable.go_type == LC_WALL) {
        glBindVertexArrayOES(_vertexArrayIDS[VAO_WALL_FLOOR_PLANE]);

        glUseProgram(_program);

        glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, currentRenderable.go_mvm.m);
        glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, _normalMatrix.m);

        glDrawElements(GL_TRIANGLE_STRIP, sizeof(gPlanePyIndices)/sizeof(gPlanePyIndices[0]), GL_UNSIGNED_INT, 0);
    }
    else if (currentRenderable.go_type == LC_PLAYER) {
        glBindVertexArrayOES(_vertexArrayIDS[VAO_PLAYER]);

        glUseProgram(_program);

        glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, currentRenderable.go_mvm.m);
        glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, _normalMatrix.m);

        glDrawElements(GL_TRIANGLE_STRIP, sizeof(gCubicIndices)/sizeof(gCubicIndices[0]), GL_UNSIGNED_INT, 0);
    }

矩阵计算:

float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 1000.0f);


for (NSString *currentKey in g_renderables) {
    GLKMatrix4 thisMvm = GLKMatrix4Identity;
    GameObject *currentRenderable = [g_renderables objectForKey:currentKey];

    if (currentRenderable.go_hasVisual) {

        thisMvm = GLKMatrix4Translate(thisMvm, currentRenderable.go_origin.x, currentRenderable.go_origin.y, currentRenderable.go_origin.z);
        if (currentRenderable.go_type == LC_WALL || currentRenderable.go_type == LC_PLAYER) {
            if (currentRenderable.go_type == LC_PLAYER) {
                if (currentRenderable.go_orientation == VV_MINUS_X) {
                    thisMvm = GLKMatrix4RotateY(thisMvm, GLKMathDegreesToRadians(90.0f));
                }
                else if (currentRenderable.go_orientation == VV_PLUS_X) {
                    thisMvm = GLKMatrix4RotateY(thisMvm, GLKMathDegreesToRadians(-90.0f));
                }
                else if (currentRenderable.go_orientation == VV_PLUS_Z) {
                    thisMvm = GLKMatrix4RotateY(thisMvm, GLKMathDegreesToRadians(180.0f));
                }
            }
            else {
                if (currentRenderable.go_orientation == VV_MINUS_X) {
                    thisMvm = GLKMatrix4RotateZ(thisMvm, GLKMathDegreesToRadians(90.0f));
                }
                else if (currentRenderable.go_orientation == VV_PLUS_X) {
                    thisMvm = GLKMatrix4RotateZ(thisMvm, GLKMathDegreesToRadians(-90.0f));
                }
                else if (currentRenderable.go_orientation == VV_PLUS_Z) {
                    thisMvm = GLKMatrix4RotateX(thisMvm, GLKMathDegreesToRadians(-90.0f));
                }
                else if (currentRenderable.go_orientation == VV_MINUS_Z) {
                    thisMvm = GLKMatrix4RotateX(thisMvm, GLKMathDegreesToRadians(90.0f));
                }
            }
        }
        if (currentRenderable.go_type != LC_LINKED_WALL) thisMvm = GLKMatrix4Scale(thisMvm, currentRenderable.width, currentRenderable.height, currentRenderable.depth);
        thisMvm = GLKMatrix4Multiply(GLKMatrix4MakeLookAt(g_currentCam.go_origin.x, g_currentCam.go_origin.y, g_currentCam.go_origin.z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f), thisMvm);
        //NSLog(@"%f", thisMvm.m[10]);
        thisMvm = GLKMatrix4Multiply(projectionMatrix, thisMvm);

        currentRenderable.go_mvm = thisMvm;

    }
}

就是这样。请帮忙!!!

4

1 回答 1

0

我认为您理解 glclearcolor 的方式有些混乱。

主要思想是,对于每一帧,您至少应该通过 glclear 清除颜色位,这就像在您完成绘图后清洁木板一样。

glclearcolor 指示 OpenGL 在清除后应该为整个屏幕使用哪种颜色。

这方面的一个例子如下:

glClearColor(1.0f,0.0f,0.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

这个例子告诉 OpenGL 在需要 color_buffer_bit 的清除颜色时使用红色。

我希望这在某种程度上有所帮助。

于 2012-04-11T14:28:19.147 回答