2013-01-14 46 views
0

我想在opengl編程我的手,在codeblocks中加載默認示例,並在做了大量的函數指針加載後,我得到了一個opengl 4.3上下文在一個窗口,可以用某種顏色(YES!),所以我繼續嘗試呈現一個三角形被清除......以及它不工作,不知道我哪裏錯了,我是見過世面的谷歌和陰暗角落我調試器,看看是否VBO及Shader創建了錯誤...沒有運氣簡單的三角形不會繪製在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失望!

編輯:我只看到萬一清除背景你知道...

回答

0

OK,想通了:顯然,你傳遞着色器之間的數據必須具有相同的名稱,兩邊的變量,因此我的代碼應該是:

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" 
"}";