我正在用GLFW3和OpenGL制作一个简单的程序。经过近10个小时的研究,我终于可以绘制一个三角形了(这花了我很长时间,因为我不想使用像glVertex3f()
这样的函数,而我的哑图形卡只支持OpenGL到2.1)。所以在做了一个小小的庆祝舞会后,我意识到当我调整大小时,三角形不仅没有占据整个屏幕,还需要一秒多的时间来调整大小!这是我的代码:
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
static const GLfloat vertices[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
static void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glVertexPointer(3, GL_FLOAT, 0, NULL);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
static void error_callback(int error, const char* description) {
fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
}
int main(void) {
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
if (!glfwInit()) {
fprintf(stderr, "Initiating GLFW failed.");
return -1;
}
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (!window) {
fprintf(stderr, "Creating a window failed.");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetKeyCallback(window, key_callback);
GLuint triangleVBO;
glGenBuffers(1, &triangleVBO);
glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);
glClearColor(0, 0, 0.2, 1);
while (!glfwWindowShouldClose(window)) {
drawScene();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
我使用的是32位Linux和OpenGL 2.1