我已经好几年没见过 OpenGl 了,现在我正在尝试用新的风格编写任何东西,但是我在绘制简单的三角形时遇到了问题。首先,我找不到任何具有良好示例且不使用“支持库”的教程,但这不是重点,下面的代码应该(我认为)绘制红色三角形,而不是绘制白色三角形,什么我做错了吗?
dpy = XOpenDisplay( NULL );
glxWin = generateXWindow(dpy);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glDepthFunc(GL_LESS);
float points[] = {
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};
unsigned int vbo = 0;
glGenBuffers (1, &vbo);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), points, GL_STATIC_DRAW);
unsigned int vao = 0;
glGenVertexArrays (1, &vao);
glBindVertexArray (vao);
glEnableVertexAttribArray (0);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);
const char* fragmet_shader = "out vec4 frag_colour; void main() { frag_colour = vec4 (0.7, 0.0, 0.7, 1.0); }";
const char* vertex_shader = "in vec3 vp; void main() { gl_Position = vec4 (vp, 1.0); }";
unsigned int vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource( vs, 1, &vertex_shader, NULL);
glCompileShader(vs);
unsigned int fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource( fs, 1, &fragmet_shader, NULL );
glCompileShader(fs);
unsigned int shader_program = glCreateProgram();
glAttachShader(shader_program, fs);
glAttachShader(shader_program, vs);
glLinkProgram(shader_program);
glVertexPointer(3, GL_FLOAT, 0, NULL);
glEnableClientState(GL_VERTEX_ARRAY);
glClearColor( 0.2, 0.2, 0.2, 1.0 );
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram (shader_program);
glBindVertexArray (vao);
glDrawArrays (GL_TRIANGLES, 0, 3);
glFlush();
glXSwapBuffers( dpy, glxWin );