1

It appears that my pyramid is smaller in the front and bigger in the back.

-(void)drawRect:(NSRect)dirtyRect
{
    // get the dimensions of the window
    NSSize dim = [self frame].size;

    // clear the background with color
    glClearColor(0.0, 0.0, 0.0, 0.4);
    glViewport(0, 0, dim.width, dim.height);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // cube position data
    GLfloat vertices[] = {0.0, 100.0, 0.0, -50.0, 0.0, 50.0, 50.0, 0.0, 50.0, 50.0, 0.0, -50.0, -50.0, 0.0, -50.0};

    // cube indices data
    GLubyte indices[] = {0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 1, 1, 4, 3, 1, 3, 2};

    // cube color data
    GLfloat color[] = {0.0, 1.0, 0.0, 1.0,
    0.0, 0.3, 0.8, 1.0, 
    1.0, 0.0, 0.0, 1.0, 
    0.5, 0.0, 0.8, 1.0, 
    0.5, 0.6, 0.3, 1.0};

    // bind each array of data to separate buffers
    // bind cube position data to the first buffer
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    // bind the cube color data to the second buffer
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(color), color, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    // enable the shader program
    GLuint programID = [self loadShaders];
    glUseProgram(programID);

    // enable vertex attributes
    // enable cube position attributes
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[0]);
    glEnableVertexAttribArray(VERTEX_POS_INDEX);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    // enable cube color attributes
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[1]);
    glEnableVertexAttribArray(VERTEX_COLOR_INDEX);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    // point to the enabled attribute data
    glVertexAttribPointer(VERTEX_POS_INDEX, VERTEX_POS_SIZE, GL_FLOAT, GL_FALSE, 0, vertices);
    glVertexAttribPointer(VERTEX_COLOR_INDEX, VERTEX_COLOR_SIZE, GL_FLOAT, GL_FALSE, 0, color);

    GLfloat offset[] = {0.0, 0.0, -200.0};
    GLint offsetUnif = glGetUniformLocation(programID, "offset");
    GLint zNearUnif = glGetUniformLocation(programID, "zNear");
    GLint zFarUnif = glGetUniformLocation(programID, "zFar");
    GLint frustumScaleUnif = glGetUniformLocation(programID, "frustumScale");

    glUniform3fv(offsetUnif, 1, offset);
    glUniform1f(frustumScaleUnif, 1.0f);
    glUniform1f(zNearUnif, 0.1);
    glUniform1f(zFarUnif, 1000.0);

    // draw the elements
    glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, indices);

    glDisableVertexAttribArray(VERTEX_POS_INDEX);
    glDisableVertexAttribArray(VERTEX_COLOR_INDEX);
    glUseProgram(0);

    // flush buffer
    glFlush();
    [[self openGLContext] flushBuffer];

}

Here is the vertex shader:

#version 120

attribute vec3 position;
attribute vec4 inColor;

uniform vec3 offset;
uniform float zNear;
uniform float zFar;
uniform float frustumScale;

varying vec4 outColor;

void main()
{
    vec4 cameraPos = vec4(position.x, position.y, -position.z, 1.0) + vec4(offset.x, offset.y, offset.z, 0.0);
    vec4 clipPos;

    clipPos.xy = cameraPos.xy * frustumScale;

    clipPos.z = cameraPos.z * (zNear + zFar) / (zNear - zFar);
    clipPos.z += 2 * zNear * zFar / (zNear - zFar);

    clipPos.w = -cameraPos.z;

    gl_Position = clipPos;
    outColor = inColor;
}

When I run the code I get this image, note it looks as though the pyramid with bigger in the back than the front:

Pyramid

I circled the "front" corners (though I should be seeing this from the other side thus they are actually the rear corners). Any thoughts or suggests as to where the problem may lie?

4

1 回答 1

1

看来,您没有在着色器中应用透视投影矩阵,因此缺乏视角/深度,导致某种正交投影。

您要在着色器中应用的矩阵是gl_ModelViewMatrix(对象和相机变换)、gl_ProjectionMatrix(投影)或gl_ModelViewProjectionMatrix(对象和相机变换和投影)。由于这些内置制服已被弃用,您需要自己传递它们。

于 2013-03-16T13:44:31.837 回答