openGL实现二维图形和三维图形
openGL是一个强大的底层图形库,其命令最初的时候使用C语言实现的。openGL定义了一个图形程序接口,常用于制作处理三维图像,功能强大,调用方便,在图像处理十分受欢迎。
实现图形主要使用的是openGL的一个工具包:GLUT。
GLUT (pronounced like the glut in gluttony) is the OpenGL Utility Toolkit, a window system independent toolkit for writing OpenGL programs. It implements a simple windowing application programming interface (API) for OpenGL.
GLUT is designed for constructing small to medium sized OpenGL programs. While GLUT is well-suited to learning OpenGL and developing simple OpenGL applications, GLUT is not a full-featured toolkit so large applications requiring sophisticated user interfaces are better off using native window system toolkits like Motif. GLUT is simple, easy, and small.
GLUT对于大型的项目来说可能功能不全,它主要针对一些中小型的openGL项目而设计。
1.实现二维图像相对简单些,直接下代码:
#include <GL/glut.h> void Rectangle(void) {
//gl开头的函数为openGL的标准函数 //(使用当前缓冲区的值来)清除指定的缓冲区 glClear(GL_COLOR_BUFFER_BIT);
//画矩形 //glRectf(-0.5f, -0.5f, 0.5f, 0.5f); //画直线 glBegin(GL_LINES); glVertex2f(0.5f, 0.5f); glVertex2f(-0.5f, -0.5f); glEnd(); //刷新缓冲,保证绘图命令能被执行 glFlush(); } int main(int argc, char *argv[]) { //初始化GLUT library glutInit(&argc, argv); //对窗口的大小进行初始化 glutInitWindowSize(300, 300); // 设置窗口出现的位置 //glutInitWindowPosition(int x, int y); //初始化程序展示模式 glutInitDisplayMode(GLUT_RGBA); glutCreateWindow("project of openGL"); //win: 指定子窗口的父窗口 //glutCreateSubWindow(int win, int x, int y, int width, int height); //为当前窗口设置函数回调,用于画图 glutDisplayFunc(&Rectangle); //进行glut事件循环,否则看不到图形效果(一闪而过) glutMainLoop(); return 0; }
实现效果:
也可以在窗口中画一个圆:
#include <math.h>
GLfloat r = 0.5f; GLfloat PI = 3.141592653f; int pre = 30; glBegin(GL_POLYGON);//画多边形 for (int i = 0; i < pre; i++) { glVertex2f(r * cos(2 * PI*i / pre), r * sin(2 * PI*i / pre)); } glEnd();
实际上这个圆并不是圆,而是多边形的近似。GL_POLYGON用于绘制多边形,边数达到一定程度,显示出来的形状近似于圆。
2.接下来来实现三维图像Cube。
三维变换:
模型视图变换(GL_MODELVIEW):从“相对移动”的观点来看,改变观察点的位置与方向和改变物体本身的位置与方向具有等效性。
透视投影变换(GL_PROJECTION):定义一个可视空间,可视空间以外的物体不会被绘制到屏幕上。
视口变换(glViewPort):通常情况下,程序默认像素填充整个窗口,通过视口变换设置像素在窗口上的填充情况。
每次对物体进行变换的时候,都需要先设置当前操作的矩阵为某种变换的矩阵,在进行变换之前还需要将矩阵转换为单位矩阵才能进行操作。
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
完整代码:
#include <GL/glut.h> #include <math.h> void setCube(void) { glClearColor(1.0, 1.0, 1.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(0, 0, 0);//设置黑色 glLoadIdentity();//加载单位矩阵 gluLookAt(6, 0, 2.5, 0, 0, 0, 1, 1, 0); //前三个参数设置观察者的观察位置,中三个参数设置观察点的位置,后三个参数设置观察者的观察方向 glLineWidth(2.0f);//设置边的像素宽,默认为1.0f glutWireCube(2.0); glFlush(); } void drawCube(void) { glClearColor(1.0, 1.0, 1.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); gluLookAt(4, 0, 1.5, 0, 0, 0, 1, 1, 0); //绘制正方体的面 glColor3f(0, 1, 0); glBegin(GL_QUADS); //---1--- glNormal3f(-1, 0, 0);//设置点的法向量 glVertex3f(0.5, 0.5, 0.5); glVertex3f(0.5, -0.5, 0.5); glVertex3f(0.5, -0.5, -0.5); glVertex3f(0.5, 0.5, -0.5); //---2--- glNormal3f(-1, 0, 0); glVertex3f(-0.5, 0.5, 0.5); glVertex3f(-0.5, -0.5, 0.5); glVertex3f(-0.5, -0.5, -0.5); glVertex3f(-0.5, 0.5, -0.5); //---3--- glNormal3f(0, 1, 0); glVertex3f(0.5, 0.5, 0.5); glVertex3f(-0.5, 0.5, 0.5); glVertex3f(-0.5, 0.5, -0.5); glVertex3f(0.5, 0.5, -0.5); //---4--- glNormal3f(0, -1, 0); glVertex3f(0.5, -0.5, 0.5); glVertex3f(-0.5, -0.5, 0.5); glVertex3f(-0.5, -0.5, -0.5); glVertex3f(0.5, -0.5, -0.5); //---5--- glNormal3f(0, 0, 1); glVertex3f(0.5, 0.5, 0.5); glVertex3f(-0.5, 0.5, 0.5); glVertex3f(-0.5, -0.5, 0.5); glVertex3f(0.5, -0.5, 0.5); //---6--- glNormal3f(0, 0, -1); glVertex3f(0.5, 0.5, 0.5); glVertex3f(-0.5, 0.5, 0.5); glVertex3f(-0.5, -0.5, 0.5); glVertex3f(0.5, -0.5, 0.5); glEnd(); //draw glColor3f(0, 0, 0); glLineWidth(2.0f); //绘制正方体的边 glBegin(GL_LINES); //---1--- glVertex3f(0.5, 0.5, 0.5); glVertex3f(-0.5, 0.5, 0.5); glVertex3f(-0.5, 0.5, 0.5); glVertex3f(-0.5, -0.5, 0.5); glVertex3f(-0.5, -0.5, 0.5); glVertex3f(0.5, -0.5, 0.5); glVertex3f(0.5, -0.5, 0.5); glVertex3f(0.5, 0.5, 0.5); //---2--- glVertex3f(0.5, 0.5, -0.5); glVertex3f(-0.5, 0.5, -0.5); glVertex3f(-0.5, 0.5, -0.5); glVertex3f(-0.5, -0.5, -0.5); glVertex3f(-0.5, -0.5, -0.5); glVertex3f(0.5, -0.5, -0.5); glVertex3f(0.5, -0.5, -0.5); glVertex3f(0.5, 0.5, -0.5); //---3--- glVertex3f(0.5, 0.5, 0.5); glVertex3f(0.5, -0.5, 0.5); glVertex3f(0.5, -0.5, 0.5); glVertex3f(0.5, -0.5, -0.5); glVertex3f(0.5, -0.5, -0.5); glVertex3f(0.5, 0.5, -0.5); glVertex3f(0.5, 0.5, -0.5); glVertex3f(0.5, 0.5, 0.5); //---4--- glVertex3f(-0.5, 0.5, 0.5); glVertex3f(-0.5, -0.5, 0.5); glVertex3f(-0.5, -0.5, 0.5); glVertex3f(-0.5, -0.5, -0.5); glVertex3f(-0.5, -0.5, -0.5); glVertex3f(-0.5, 0.5, -0.5); glVertex3f(-0.5, 0.5, -0.5); glVertex3f(-0.5, 0.5, 0.5); glEnd(); glFlush(); } void threeD(int w, int h) { glViewport(0, 0, (GLsizei)w, (GLsizei)h);//调整视图窗口大小 glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1.0, 1.0, -1.0, 1.0, 2.0, 10.0); glMatrixMode(GL_MODELVIEW); } int main(int argc, char *argv[]) { //初始化GLUT library glutInit(&argc, argv); //对窗口的大小进行初始化 glutInitWindowSize(500, 500); // 设置窗口出现的位置 //glutInitWindowPosition(int x, int y); //初始化程序展示模式 glutInitDisplayMode(GLUT_RGBA); glutCreateWindow("project of openGL");
//为当前窗口设置函数回调,用于画图 glutDisplayFunc(drawCube); //窗口改变时的函数 glutReshapeFunc(threeD); glutMainLoop(); return 0; }
可以调用openGL的glutWireCube(GLFloat size)来绘制立体正方形,如setCube函数。
我想要实现带有黑色边缘的绿色正方体,必须对正方体进行绘制,如drawCube函数。
实现效果: