1

尝试进行手动透视转换后,我无法显示我的立方体我的代码如下。我怀疑这可能是我的远近飞机号码。

-(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);

    // variables
    GLfloat cubeHeight = 90.0;
    GLfloat cubeWidth = 90.0;
    GLfloat cubeLength = 200.0;
    //GLfloat alpha = 0.0;
    //GLfloat beta = 0.0;
    //GLfloat gamma = 0.0;

    // cube position data
    GLfloat cubePositionData[] = {0.0, 0.0, 0.0,
        0.0, 0.0, cubeLength,
        cubeWidth, 0.0, cubeLength,
        cubeWidth, 0.0, 0.0,
        0.0, cubeHeight, 0.0,
        0.0, cubeHeight, cubeLength,
        cubeWidth, cubeHeight, cubeLength,
        cubeWidth, cubeHeight, 0.0};

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

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

    // array to hold buffer IDs
    GLuint vertexBuffers[2];

    // 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(cubePositionData), cubePositionData, 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(cubeColorData), cubeColorData, 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, cubePositionData);
    glVertexAttribPointer(VERTEX_COLOR_INDEX, VERTEX_COLOR_SIZE, GL_FLOAT, GL_FALSE, 0, cubeColorData);

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

    glUniform2fv(offsetUnif, 1, offset);
    glUniform1f(frustumScaleUnif, 1.0f);
    glUniform1f(zNearUnif, 1.0);
    glUniform1f(zFarUnif, 3.0);

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

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

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

}

问题可能是什么?哦,还有着色器:

#version 120

attribute vec3 position;
attribute vec4 inColor;

uniform vec2 offset;
uniform float zNear;
uniform float zFar;
uniform float frustumScale;

varying vec4 outColor;

void main()
{
    vec4 cameraPos = vec4(position, 1.0) + vec4(offset.x, offset.y, 0.0, 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;
}

#version 120

varying vec4 outColor;

void main()
{
    gl_FragColor = outColor;
}

好的,将我的代码的一部分更改为如下所示:

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

glUniform2fv(offsetUnif, 1, offset);
glUniform1f(frustumScaleUnif, 1.0);
glUniform1f(zNearUnif, 1.0);
glUniform1f(zFarUnif, 25.0);

我明白了:

节目结果

后端(绿色)不应该在远处看起来更小而不是更大,就像梯形底座一样吗?

4

2 回答 2

0

您似乎只使用缓冲区和着色器。在这种情况下,您应该使用 OpenGL3.2 和 GLSL 1.5。您必须通过在 NSOpenGLView(或控制器)中手动更改上下文来手动更改为核心 opengl。

NSOpenGLPixelFormatAttribute attribute[] = 
{
    NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
    0
};

NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribute];
NSOpenGLContext *context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
[self setPixelFormat:pixelFormat];
[self setOpenGLContext:context];

但是您必须更改一些着色器代码,例如将属性/变量更改为输入/输出。

然后,关于您的问题,需要您创建并绑定一个 VAO。只需添加

int vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

在绑定缓冲区和顶点属性指针之前。就是这样。而已。

另外,我认为您的顶点可能有一些问题。由于您正在查看 -Z 位置,因此您的立方体在您身后。因此,您必须将偏移更改为 vec3,并将 Z 偏移设置为 -100,看看您的立方体有多大。

于 2012-08-26T02:40:43.133 回答
0

好的,我想我修好了。将我的顶点着色器更改为如下所示:

#version 120

attribute vec3 position;
attribute vec4 inColor;

uniform vec2 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, -10.0, 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;
}

我明白了:

程序结果

也许也许?

于 2012-08-26T03:54:47.160 回答