0

GLSL を使用して単純な立方体を印刷しようとしていますが、空の画面しか表示されません。何が間違っているのかわかりません。頂点、法線、三角形は Blender からエクスポートされます。

void InitBuffers() {

    // monkey vertices, normals
    readVerticesNormals();

    // cube vertices
    glGenVertexArraysAPPLE(1, &CubeVao);
    glBindVertexArrayAPPLE(CubeVao);

    glGenBuffers(1, &CubeVboPositions);
    glBindBuffer(GL_ARRAY_BUFFER,CubeVboPositions);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glGenBuffers(1,&CubeVboColors);
    glBindBuffer(GL_ARRAY_BUFFER, CubeVboColors);
    glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glGenBuffers(1, &CubeNormals);
    glBindBuffer(GL_ARRAY_BUFFER, CubeNormals);
    glBufferData(GL_ARRAY_BUFFER, sizeof(normals), normals, GL_STATIC_DRAW);
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glGenBuffers(1, &CubeIbo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, CubeIbo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(triangles), triangles, GL_STATIC_DRAW);
}

頂点データをシェーダーにバインドします。

glBindAttribLocation(ProgramShader, 0, "position");
glBindAttribLocation(ProgramShader, 1, "color");
glBindAttribLocation(ProgramShader, 2, "normal");

カメラは (0,0,0) に配置され、(0,0,-1) を向いています。オブジェクト (この場合は立方体) は (0,0,-4) に配置されています。レンダリング機能は次のとおりです。

void display() {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);

    // set view matrix
    ViewMatrix.setView(0,0,0,0,0,-1,0,1,0);

    // use shader program
    glUseProgram(ProgramShader);

    // send uniforms to shader
    glUniformMatrix4fv(ProjectionMatrixLocation, 1, false, ProjectionMatrix.m);
    glUniformMatrix4fv(ViewMatrixLocation, 1, false, ViewMatrix.m);
    glUniformMatrix4fv(ModelMatrixLocation, 1, false, ModelMatrix.m);

    glBindVertexArrayAPPLE(CubeVao);

    glDrawElements(GL_TRIANGLES, 3*tri_num, GL_UNSIGNED_INT, (void*)0);

    glutSwapBuffers();
}

頂点シェーダー:

attribute vec3 position;
attribute vec3 color;
attribute vec3 normal;

uniform mat4 modelMatrix,viewMatrix,projMatrix;

varying vec4 Normal;
varying vec4 Position;
varying vec4 Color;

void main() {
    // position in view space
    Position = viewMatrix * modelMatrix * vec4(position, 1.0);

    // normal in view space
    Normal = normalize(viewMatrix * modelMatrix * vec4(normal, 1.0));

    Color = vec4(color, 1.0);

    // final position
    gl_Position = projMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
}

フラグメント シェーダー:

varying vec4 Normal;
varying vec4 Position;
varying vec4 Color;

void main() {
    gl_FragColor = Color;
}
4

1 に答える 1

0

頂点、色などの定義方法によっては、 sizeof(vertices) はポインターのサイズを返すだけの場合があります。試す:

3*numVertices*sizeof(float)
于 2013-01-19T20:45:50.360 に答える