简单三角形不会在 OpenGL 4.3 中绘制



我正在尝试opengl编程,在代码块中加载了一个默认示例,在做了大量的函数指针加载后,我在一个窗口中得到了一个opengl 4.3上下文,可以用特定的颜色清除(是的!),所以我继续尝试渲染一个三角形…好吧,它不工作,不知道我哪里出了问题,我一直在谷歌和我的调试器的黑暗角落,看看是否VBO和着色器创建出错…没有运气

这里是相关的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);
    } 

And shaders:

const char vertexShader[] =
"#version 150 coren"
"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 coren"
"smooth in vec4 inColor;n"
"out vec4 f_color;n"
"void main()n"
"{n"
"   f_color = inColor;n"
"}";

不用说,我将永远感激任何帮助,可能拯救我从OpenGL受挫!!

编辑:我只看到清除的背景,如果你想知道…

好的,弄清楚了:显然你在着色器之间传递的数据必须与两边的变量具有相同的名称,所以我的代码应该是:

const char vertexShader[] =
"#version 150 coren"
"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 coren"
"smooth in vec4 outColor;n"
"out vec4 f_color;n"
"void main()n"
"{n"
"   f_color = inColor;n"
"}";

相关内容

  • 没有找到相关文章