1

I am currently working my way through the current OpenGL Programming Guide, Version 4.3. I implemented the code of the first example, that should display two triangles. Basically pretty simple.

But nothing is displayed when I run that code (just the black window).

This is the init function where the triangles are created:

GLuint vertexArrayObjects[1];
GLuint buffers[1];
const GLuint vertexCount = 6;

void init() {
//  glClearColor(0,0,0,0);

    glGenVertexArrays(1, vertexArrayObjects);
    glBindVertexArray(vertexArrayObjects[0]);

    GLfloat vertices[vertexCount][1] {
        {-90.0, -90.0},
        { 85.0, -90.0},
        {-90.0,  85.0},
        { 90.0, -85.0},
        { 90.0,  90.0},
        {-85.0,  90.0}
    };

    glGenBuffers(1, buffers);
    glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    ShaderInfo shaders[] {
        {GL_VERTEX_SHADER, "one.vsh"},
        {GL_FRAGMENT_SHADER, "one.fsh"},
        {GL_NONE, nullptr}
    };

    GLuint program = LoadShaders(shaders);
    glUseProgram(program);

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, ((char *)NULL + (0)));
    glEnableVertexAttribArray(0);
}

And the very simple draw:

void display() {
    glClear(GL_COLOR_BUFFER_BIT);

    glBindVertexArray(vertexArrayObjects[0]);
    glDrawArrays(GL_TRIANGLES, 0, vertexCount);

    glFlush();
}

But as said, nothing is drawn. Is there an error in my implementation?

The full code is here and LoadShaders (straight from the books website, so this should be out of interest) is here. The shaders are here.

4

2 回答 2

0

好吧,我发现了问题所在。应用程序在构建时运行在与源完全不同的位置,并且着色器文件不会复制到目标目录中。因此,程序显然无法读取它们并且没有任何显示。用绝对路径替换相对着色器one.vsh路径one.fsh解决了这个问题。

于 2013-06-05T15:01:06.413 回答
0

对于书中的第一个示例,没有应用任何转换(即,在顶点着色器中,顶点的位置vPosition直接映射到输出位置gl_Position)。因此,窗口的坐标系是标准化的设备坐标只有xy坐标在 [-1,1] 之间的对象才会被传递给光栅化器(而不是被裁剪)。鉴于您在程序中指定的几何图形,您渲染的三角形完全覆盖了视口,因此您应该看到您在片段着色器中指定的颜色(蓝色)。

否则,您的代码看起来是正确的。(尽管您不需要将 . In 传递nullptr到着色器列表中LoadShaders。仅查找GL_NONE)。

于 2013-06-05T03:36:58.477 回答