???????
?????????GLFW是现在较流行、使用广泛的OpenGL的界面库,而glut库已经比较老了。GLEW是和管理OpenGL函数指针有关的库,因为OpenGL只是一个标准/规范,具体的实现是由驱动开发商针对特定显卡实现的。由于OpenGL驱动版本众多,它大多数函数的位置都无法在编译时确定下来,需要在运行时查询,而GLEW可以解决这些问题。下面就来看下OpenGL是如何基于glfw库实现画点、画线、画三角的。
1、glVertex函数
2、坐标系
代码如下:
#include <GLFW/glfw3.h>
void drawPoint()
{
/* Draw a point */
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(2.0f);
glBegin(GL_POINTS);
glColor3f(1.0, 0.0, 0.0); // Red
glVertex2f(0.0f,0.0f);
glVertex2f(0.5f,0.8f);
glEnd();
}
void drawLint()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
glLineWidth(2);//设置线段宽度
glBegin(GL_LINES);
glColor3f(1.0,0.0,0.0);
glVertex2f(0.8,1); //定点坐标范围
glVertex2f(0,-1);
glEnd();
}
void drawTriangle()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0); // Red
glVertex3f(0.0, 1.0, 0.0);
glColor3f(0.0, 1.0, 0.0); // Green
glVertex3f(-1.0, -1.0, 0.0);
glColor3f(0.0, 0.0, 1.0); // Blue
glVertex3f(1.0, -1.0, 0.0);
glEnd();
}
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(480, 320, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/*your draw*/
// drawPoint();
// drawLint();
drawTriangle();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
运行效果
????????