我试着画两个三角形,但最终只得到了一个白色的窗口而没有任何三角形。我已正确设置库,但我认为代码中可能有错误,由于我相对较新,无法找出错误所在。该代码没有任何错误或警告,但结果并非我所期望的,窗口是白色的,窗口中没有显示任何图形。
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <cstdlib>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
int main(void)
{
GLFWwindow* window;
// initialize the library
if (!glfwInit())
{
return -1;
}
// Create a window and its context
window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "kk", NULL, NULL);
int screenWidth, screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
if (!window)
{
glfwTerminate();
return -1;
}
// make the window's context current
glfwMakeContextCurrent(window);
glViewport(0, 0, screenWidth, screenHeight); //specifies part of the window OpenGL can draw on
glMatrixMode(GL_PROJECTION); //controls the camera
glLoadIdentity(); //put us at (0, 0, 0)
glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 600); //cordinate system
glMatrixMode(GL_MODELVIEW); //defines how objects are trasformed
glLoadIdentity(); //put us at (0, 0, 0)
GLfloat first_triangle[] = {
0, 0, 0,
0,300,0,
200,300,0,
};
GLfloat second_triangle[] = {
200,300,0,
400,300,0,
400,600,0,
};
GLfloat color[] =
{
255,0,0,
0,255,0,
0,0,255
};
// Loop until the window is closed by the user
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
//OpenGL rendering
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, first_triangle); // points to the vertices to be used
glColorPointer(3, GL_FLOAT, 0, color); // color to be used
glDrawArrays(GL_TRIANGLES, 0, 3); // draw the vetices
glVertexPointer(3, GL_FLOAT, 0, second_triangle); // points to the vertices to be used
glColorPointer(3, GL_FLOAT, 0, color); // color to be used
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
};
}