任何人都知道为什么这段代码在 openGL 3.1 中显示一个三角形而在 3.2 中没有?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glew.h>
#include <GL/glfw.h>
GLuint myVBO;
void onDraw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, myVBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glfwSwapBuffers();
}
void loadData() {
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
glGenBuffers(1, &myVBO);
glBindBuffer(GL_ARRAY_BUFFER, myVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
}
void createWindow() {
int w=800,h=600;
if(glfwInit() != GL_TRUE){
return;
}
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); //change to 1 to see the triangle
//glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
if(!glfwOpenWindow(w,h,5,6,5,0,8,0,GLFW_WINDOW)){
glfwTerminate();
return;
}
if(glewInit() != GLEW_OK){
return;
}
printf("Version: %s\n", glGetString(GL_VERSION));
printf("Error: %d\n", glGetError());
}
int main() {
createWindow();
loadData();
while(1){onDraw();}
return 0;
}
基于http://www.opengl-tutorial.org/上的前 2 个教程 我的输出是
Version: 3.2.0 NVIDIA 295.49
Error: 1280