0

我正在尝试opengl编程,在代码块中加载了一个默认示例,在加载了大量函数指针之后,我在一个窗口中获得了一个opengl 4.3上下文,可以用某种颜色清除(是的! ),所以我继续尝试渲染一个三角形......好吧,它不工作,不知道我哪里出错了。没运气

这是相关的gl代码:

GLuint IndiceArray[3] = {
0, 1, 2
};

const float vertexPositions[] = {
    0.75f, 0.75f,
    0.75f, -0.75f,
    -0.75f, -0.75f,
};

const float vertexColors[] = {
    1.0f, 0.0f, 0.0f, 1.0f,
    0.0f, 1.0f, 0.0f, 1.0f,
    0.0f, 0.0f, 1.0f, 1.0f,
};

//Prepare GPU program
vertexID = glCreateShader( GL_VERTEX_SHADER );
fragmentID = glCreateShader( GL_FRAGMENT_SHADER );

glShaderSource(vertexID, 1, &vsSource, NULL) ;
glShaderSource(fragmentID, 1, &fsSource, NULL) ;

glCompileShader( vertexID );
glCompileShader( fragmentID );

programID = glCreateProgram();
glAttachShader( programID, vertexID );
glAttachShader( programID, fragmentID );
glLinkProgram( programID );
glUseProgram( programID );

//Send to OpenGL
glGenBuffers(3, vertexAttribBufferID);

glBindBuffer( GL_ARRAY_BUFFER, vertexAttribBufferID[0] );
glBufferData( GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW );

glBindBuffer( GL_ARRAY_BUFFER, vertexAttribBufferID[1] );
glBufferData( GL_ARRAY_BUFFER, sizeof(vertexColors), vertexColors, GL_STATIC_DRAW );

glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vertexAttribBufferID[2] );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(IndiceArray), IndiceArray, GL_STATIC_DRAW );

colorAttribID = glGetAttribLocation( programID, "colorIn" );
positionAttribID = glGetAttribLocation( programID, "vertexPos" );

/* program main loop */
while (!bQuit)
{
    /* check for messages */
    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        /* handle or dispatch messages */
        if (msg.message == WM_QUIT)
        {
            bQuit = TRUE;
        }
        else
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    else
    {
        /* OpenGL animation code goes here */

        glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        //Render!!
        glBindBuffer( GL_ARRAY_BUFFER, vertexAttribBufferID[0] );
        glEnableVertexAttribArray( positionAttribID );
        glVertexAttribPointer( positionAttribID, 2, GL_FLOAT, GL_FALSE, 0, 0 );

        glBindBuffer( GL_ARRAY_BUFFER, vertexAttribBufferID[1] );
        glEnableVertexAttribArray( colorAttribID );
        glVertexAttribPointer( colorAttribID, 4, GL_FLOAT, GL_FALSE, 0, 0 );

        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vertexAttribBufferID[2] );

        glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0 );
        glDisableVertexAttribArray( colorAttribID );
        glDisableVertexAttribArray( positionAttribID );

        SwapBuffers(hDC);
        Sleep (1);
    } 

和着色器:

const char vertexShader[] =
"#version 150 core\n"

"in vec2 vertexPos;\n"
"in vec4 colorIn;\n"
"smooth out vec4 outColor;\n"

"void main()\n"
"{\n"
"   gl_Position = vec4(vertexPos, 0.0f, 1.0f);\n"
"   outColor = colorIn;\n"
"}";

const char fragShader[] =
"#version 150 core\n"

"smooth in vec4 inColor;\n"
"out vec4 f_color;\n"

"void main()\n"
"{\n"
"   f_color = inColor;\n"
"}";

不用说,我将永远感谢任何帮助,可能会让我免于对 OpenGL 感到沮丧!!!

编辑:我只看到清除的背景,以防你想知道......

4

1 回答 1

0

好的,想通了:显然,您在着色器之间传递的数据必须与两边的变量同名,所以我的代码应该是:

const char vertexShader[] =
"#version 150 core\n"

"in vec2 vertexPos;\n"
"in vec4 colorIn;\n"
"smooth out vec4 outColor;\n"

"void main()\n"
"{\n"
"   gl_Position = vec4(vertexPos, 0.0f, 1.0f);\n"
"   outColor = colorIn;\n"
"}";

const char fragShader[] =
"#version 150 core\n"

"smooth in vec4 outColor;\n"
"out vec4 f_color;\n"

"void main()\n"
"{\n"
"   f_color = inColor;\n"
"}";
于 2013-01-16T13:01:28.247 回答