我用C ++和OpenGL编写了一个程序。这个shuold绘制一个黑色三角形。通常它会以正确的方式绘制三角形,但有时看起来不正确。
这是目标:
有两种罕见的错误结果:
主要功能:
int main(void)
{
// Initialise GLFW
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
// Open a window and create its OpenGL context
window = glfwCreateWindow(700, 700, "Arkanoid", NULL, NULL);
if (window == NULL){
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
// Dark blue background
glClearColor(0.0f, 0.0f, 0.8f, 0.0f);
programID = LoadShaders("TransformVertexShader.vertexshader", "ColorFragmentShader.fragmentshader");
vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace");
vertexColorID = glGetAttribLocation(programID, "vertexColor");
glfwSetCursorPosCallback(window, mouseMove);
game = new Game();
game->vertexPosition_modelspaceID = vertexPosition_modelspaceID;
game->vertexColorID = vertexColorID;
game->prepareToDrawBoardBorderElement();
posXId = glGetUniformLocation(programID, "posX");
posYId = glGetUniformLocation(programID, "posY");
do {
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(programID);
glEnableVertexAttribArray(vertexPosition_modelspaceID);
glEnableVertexAttribArray(vertexColorID);
glUniform1f(posXId, 0.0f);
glUniform1f(posYId, 0.0f);
game->drawBoardBorders();
glDisableVertexAttribArray(vertexPosition_modelspaceID);
glDisableVertexAttribArray(vertexColorID);
glfwSwapBuffers(window);
glfwPollEvents();
} while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0);
glDeleteBuffers(1, &vertexbuffer);
glDeleteProgram(programID);
glfwTerminate();
return 0;
}
Game.cpp文件如下所示:
void Game::drawBoardBorders()
{
glDrawArrays(GL_TRIANGLES, 0, 3);
}
void Game::prepareToDrawBoardBorderElement()
{
std::cout << "prepareToDrawBoardBorderElement \n";
g_vertex_buffer_data[0] = -1.0f;
g_vertex_buffer_data[1] = 1.0f;
g_vertex_buffer_data[2] = 0.0f;
g_vertex_buffer_data[3] = -0.5f;
g_vertex_buffer_data[4] = 1.0f;
g_vertex_buffer_data[5] = 0.0f;
g_vertex_buffer_data[6] = -1.0f;
g_vertex_buffer_data[7] = 0.0f;
g_vertex_buffer_data[8] = 0.0f;
for (int i = 0; i < 8; i++) {
g_color_buffer_data[i] = 0.0f;
}
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
glVertexAttribPointer(
vertexPosition_modelspaceID, // The attribute we want to configure
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glGenBuffers(1, &colorbuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);
glVertexAttribPointer(
vertexColorID, // The attribute we want to configure
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// All of the above information you only need to specify to openGL once, not every time you draw a frame!
}
为什么有时候会出现意外情况? 我该怎么做才能使它正常工作?
答案 0 :(得分:4)
您在g_color_buffer_data中将一个元素初始化为少数。
此代码:
for (int i = 0; i < 8; i++) {
g_color_buffer_data[i] = 0.0f;
}
仅初始化数组的前8个元素,但绘制三个角点将需要9个颜色浮点数,因为您按顶点使用3个浮点数(rgb)。
另一个提示:你正在使用现代的OpenGL,它真的很棒。请注意,从OpenGL 3.3核心配置文件开始,使用VertexArrayObjects是必需的。请参阅示例this link (section Vertex Data)。这不会影响此处描述的问题,但只应作为未来工作的附加信息。