【发布时间】:2019-03-24 14:38:42
【问题描述】:
我正在编写正确的代码,但编译器会抛出错误。错误说错误在glGenBuffers,但我已经从官方网站上复制了它。我的错在哪里?
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
int main(void)
{
GLFWwindow* window;
glewExperimental = GL_TRUE;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
float pos[6] = {
-0.5f, -0.5f,
0.0f, 0.5f,
0.5f, -0.5f
};
GLuint buf;
glGenBuffers(1, &buf);
glBindBuffer(GL_ARRAY_BUFFER, buf);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), pos, GL_STATIC_DRAW);
if (glewInit() != GLEW_OK)
printf("Error\n");
printf("%s", glGetString(GL_VERSION));
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
【问题讨论】:
-
“我正在编写正确的代码,但编译器抛出错误” - 嗯,no。如果编译器产生错误,那么您很可能没有编写“正确的代码”。
-
int main(void)是正确的 C,但在 C++ 中void是多余的。 -
return -1;- 从main返回负值通常是一个错误。在大多数情况下,它不会产生您所期望的结果。最好酌情返回EXIT_FAILURE或EXIT_SUCCESS。 -
谢谢!真的很有帮助。