我正在嘗試顯示一個小三角形。Opengl將不會顯示任何內容
這裏是我的初始化代碼:
void PlayerInit(Player P1)
{
glewExperimental = GL_TRUE;
glewInit();
//initialize buffers needed to draw the player
GLuint vao;
GLuint buffer;
glGenVertexArrays(1, &vao);
glGenBuffers(1, &buffer);
//bind the buffer and vertex objects
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
//Set up a buffer to hold 6 floats for position, and 9 floats for color
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*18, NULL, GL_STATIC_DRAW);
//push the vertices of the player into the buffer
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*9, CalcPlayerPoints(P1.GetPosition()));
//push the color of each player vertex into the buffer
glBufferSubData(GL_ARRAY_BUFFER, sizeof(float)*9, sizeof(float)*9, CalcPlayerColor(1));
//create and compile the vertex/fragment shader objects
GLuint vs = create_shader("vshader.glsl" ,GL_VERTEX_SHADER);
GLuint fs = create_shader("fshader.glsl" ,GL_FRAGMENT_SHADER);
//create a program object and link the shaders
GLuint program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
//error checking for linking
GLint linked;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if(!linked)
{
std::cerr<< "Shader program failed to link" <<std::endl;
GLint logSize;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logSize);
char* logMsg = new char[logSize];
glGetProgramInfoLog(program, logSize, NULL, logMsg);
std::cerr<< logMsg << std::endl;
delete [] logMsg;
exit(EXIT_FAILURE);
}
glUseProgram(program);
//create attributes for color and position to pass to shaders
//enable each attribute
GLuint Pos = glGetAttribLocation(program, "vPosition");
glEnableVertexAttribArray(Pos);
GLuint Col = glGetAttribLocation(program, "vColor");
glEnableVertexAttribArray(Col);
//set a pointer at the proper offset into the buffer for each attribute
glVertexAttribPointer(Pos, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) (sizeof(float)*0));
glVertexAttribPointer(Col, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) (sizeof(float)*9));
}
我沒有太多的經驗,編寫着色器接頭,所以我認爲這是哪裏出了問題可能是。我在着色器加載器中檢查了一些錯誤,沒有出現。所以我認爲這很好。
接下來我有我的顯示和主要功能:
//display function for the game
void GameDisplay(void)
{
//set the background color
glClearColor(1.0, 0.0, 1.0, 1.0);
//clear the screen
glClear(GL_COLOR_BUFFER_BIT);
//Draw the Player
glDrawArrays(GL_TRIANGLES, 0, 3);
glutSwapBuffers();
}
//main function
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(500, 500);
glutCreateWindow("Asteroids");
Player P1 = Player(0.0, 0.0);
PlayerInit(P1);
glutDisplayFunc(GameDisplay);
glutMainLoop();
return 0;
}
頂點着色器:
attribute vec3 vPosition;
attribute vec3 vColor;
varying vec4 color;
void main()
{
color = vec4(vColor, 1.0);
gl_Position = vec4(vPosition, 1.0);
}
片段着色器
varying vec4 color;
void main()
{
gl_FragColor = color;
}
這是所有相關的代碼。 CalcPlayerPoints只是返回一個大小爲9的float數組來保存三角座標。 CalcPlayerColor做類似的事情。
可能有助於診斷問題的最後一個問題是,無論何時嘗試通過關閉應用程序的窗口退出程序,我都會在glutmainloop中得到一個斷點,但是如果關閉控制檯窗口,它會退出正常。
編輯:我添加了着色器以供參考。 編輯:我使用的OpenGL 3.1版
你的着色器在哪裏?這些有點重要:) – genpfault
你使用的是什麼GLSL版本?我沒有看到任何#version指令。 – Grimmy
你確定你的'Pos'和'Col'實際上是否包含有效位置?我不相信舊的「屬性」類型可以在這裏工作。指定您的GL和GLSL版本IS再次非常重要! – Grimmy