OpenGL游戏编程第三章 glut实现

来源:互联网 发布:软件测试师考试 编辑:程序博客网 时间:2024/06/13 00:21

注意:

1、如果使用单缓冲 (GLUT_SINGLE),则每次glEnd()之后要添加 glFlush(); 如果使用双缓冲(GLUT_DOUBLE) 则在最后输出图像的时候,要调用glutSwapBuffers().

2、如果要显示对应的图像只需要更换这两个函数中的参数glutDisplayFunc(render2); glutIdleFunc(render2).

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include "stdafx.h"#include <Windows.h>#include <GL/glew.h>#include <GL/glut.h>void ds(){float ps = 0.5;glClear(GL_COLOR_BUFFER_BIT);for (float point = -4; point < 5; point += 0.5){glPointSize(ps);glBegin(GL_POINTS);glVertex3f(point, 0, 0);glEnd();ps += 1;}glFlush();}void lines(){glEnable(GL_LINE_STIPPLE);GLushort sp = 0xFAFA;glLineStipple(1, sp);glClear(GL_COLOR_BUFFER_BIT);glLineWidth(1);glBegin(GL_LINES);glVertex3f(0, 0, 0);glVertex3f(1, 1, 0);glEnd();GLushort sp2 = 0xFAFA;glLineStipple(2, sp2);glBegin(GL_LINES);glVertex3f(1, 0, 0);glVertex3f(2, 1, 0);glEnd();glFlush();}void triangles(){glClear(GL_COLOR_BUFFER_BIT);glBegin(GL_TRIANGLES);glVertex3f(-2, -1.0, 0);glVertex3f(3, 1, 0);glVertex3f(0, 3, 0);glEnd();glFlush();}void DrawPlane(){// draw a red 4x4 unit flat square plane along the x/y axesglBegin(GL_TRIANGLE_STRIP);glColor3f(1.0, 0.0, 0.0);glVertex3f(-2.0, -2.0, -2.0);glColor3f(0.0, 1.0, 0.0);glVertex3f(2.0, -2.0, -2.0);glColor3f(0.0, 0.0, 1.0);glVertex3f(-2.0, -2.0, 2.0);glColor3f(1.0, 1.0, 0.0);glVertex3f(2.0, -2.0, 2.0);glEnd();glFlush();}bool direction = false;GLfloat zPos = 0.0;void prepare(){if (direction)zPos = zPos - 0.01f;else{zPos += 0.01f;}if (zPos >= 0.0){direction = true;}else if(zPos <= -20.0){direction = false;}}void render(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);prepare();//printf_s("%f ", zPos);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glTranslatef(0, 0, zPos);glRotatef(90, 1, 1, 0);DrawPlane();}float xAxisAngle = 0.0;float yAxisAngle = 0.0;void prepare2(){xAxisAngle += 0.05;yAxisAngle += 0.25;// if rotation angles hit 360 degrees (complete rotation)// then reset the values to 0if (xAxisAngle >= 360.0)xAxisAngle = 0.0;if (yAxisAngle >= 360.0)yAxisAngle = 0.0;}void DrawPlane2(){// draw a red 4x4 unit flat square plane along the x/y axesglBegin(GL_TRIANGLE_STRIP);glColor3f(1.0, 0.0, 0.0);glVertex3f(-2.0, 0.0, -2.0);glColor3f(0.0, 1.0, 0.0);glVertex3f(2.0, 0.0, -2.0);glColor3f(0.0, 0.0, 1.0);glVertex3f(-2.0, 0.0, 2.0);glColor3f(1.0, 1.0, 0.0);glVertex3f(2.0, 0.0, 2.0);glEnd();// draw plane's local axes// y axis from (0,0,0) to (0,3,0)glBegin(GL_LINES);glColor3f(1.0, 1.0, 0.0);glVertex3f(0.0, 0.0, 0.0);glVertex3f(0.0, 3.0, 0.0);glEnd();// x axis from (0,0,0) to (3,0,0)glBegin(GL_LINES);glColor3f(1.0, 1.0, 0.0);glVertex3f(0.0, 0.0, 0.0);glVertex3f(3.0, 0.0, 0.0);glEnd();}void DrawAxes(){// draw x axis line from (0,0,0) to (3,0,0)glBegin(GL_LINES);glColor3f(1.0, 1.0, 1.0);glVertex3f(0.0, 0.0, 0.0);glVertex3f(3.0, 0.0, 0.0);glEnd();// draw y axis line from (0,0,0) to (0,3,0)glBegin(GL_LINES);glColor3f(1.0, 1.0, 1.0);glVertex3f(0.0, 0.0, 0.0);glVertex3f(0.0, 3.0, 0.0);glEnd();}void render2(){// clear screen and depth bufferglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);prepare2();// load the identity matrix (clear to default position and orientation)glLoadIdentity();// move eye back 10 units and draw world coordinate x-y axesglTranslatef(0.0, 0.0, -10.0);DrawAxes();// rotate about x axis then y axis at prescribed angles and draw planeglRotatef(xAxisAngle, 1.0, 0.0, 0.0);glRotatef(yAxisAngle, 0.0, 1.0, 0.0);DrawPlane2();glutSwapBuffers();}void changeSize(int w, int h){float ratio = 1.0 * w / h;glMatrixMode(GL_PROJECTION);glLoadIdentity();glViewport(0, 0, w, h);gluPerspective(45, ratio, 1, 1000);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(0, 0, 5, 0, 0, -1, 0, 1, 0);}int _tmain(int argc, char* argv[]){glutInit(&argc, argv);glPolygonMode(GL_FRONT, GL_LINE);glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);glutInitWindowSize(1000,800);glutInitWindowPosition(100, 100);glutCreateWindow("hello");glutDisplayFunc(render2);glutIdleFunc(render2);glutReshapeFunc(changeSize);glEnable(GL_DEPTH_TEST);glutMainLoop();return 0;}



0 0
原创粉丝点击