高效学习OpenGL之显示列表实例glCallList(),glGenLists(),glNewList(),glEndList()

来源:互联网 发布:湖南机电职院网络 编辑:程序博客网 时间:2024/06/06 17:11


#include <GL/glut.h>
002.#include <stdio.h>
003.#include <math.h>
004.#include <stdlib.h>
005. 
006.#define PI_ 3.14159265358979323846
007. 
008.GLuint theTorus;
009. 
010./* Draw a torus */
011.static void torus(int numc, int numt)//显示列表只能包含<a href="http://www.it165.net/Pro/pkgame/" target="_blank" class="keylink">OpenGL</a>函数
012.{
013.int i, j, k;
014.double s, t, x, y, z, twopi;
015. 
016.twopi = 2 * PI_;
017.for (i = 0; i < numc; i++) {
018.glBegin(GL_QUAD_STRIP);
019.for (j = 0; j <= numt; j++) {
020.for (k = 1; k >= 0; k--) {
021.s = (i + k) % numc + 0.5;
022.t = j % numt;
023. 
024.x = (1+.1*cos(s*twopi/numc))*cos(t*twopi/numt);
025.y = (1+.1*cos(s*twopi/numc))*sin(t*twopi/numt);
026.z = .1 * sin(s * twopi / numc);
027.glVertex3f(x, y, z);
028.}
029.}
030.glEnd();
031.}
032.}
033. 
034./* Create display list with Torus and initialize state */
035.static void init(void)//创建了一个绘制圆环面的显示列表
036.{
037.theTorus = glGenLists (1);//glGenLists()唯一的标识一个显示列表
038.glNewList(theTorus, GL_COMPILE);//用于对显示列表进行定界。第一个参数是一个整形索引值,由glGenLists()指定
039.torus(825);
040.glEndList();
041. 
042.glShadeModel(GL_FLAT);
043.glClearColor(0.00.00.00.0);
044.}
045. 
046./* Clear window and draw torus */
047.void display(void)
048.{
049.glClear(GL_COLOR_BUFFER_BIT);
050.glColor3f (1.01.01.0);
051.glCallList(theTorus);//执行显示列表所存储的函数
052.glFlush();
053.}
054. 
055./* Handle window resize */
056.void reshape(int w, int h)
057.{
058.glViewport(00, (GLsizei) w, (GLsizei) h);
059.glMatrixMode(GL_PROJECTION);
060.glLoadIdentity();
061.gluPerspective(30, (GLfloat) w/(GLfloat) h, 1.0100.0);
062.glMatrixMode(GL_MODELVIEW);
063.glLoadIdentity();
064.gluLookAt(0010000010);
065.}
066. 
067./* Rotate about x-axis when "x" typed; rotate about y-axis
068.when "y" typed; "i" returns torus to original view */
069.void keyboard(unsigned char key, int x, int y)
070.{
071.switch (key) {
072.case 'x':
073.case 'X':
074.glRotatef(30.,1.0,0.0,0.0);
075.glutPostRedisplay();//使glutMainLoop()函数调用display()函数
076.break;
077.case 'y':
078.case 'Y':
079.glRotatef(30.,0.0,1.0,0.0);
080.glutPostRedisplay();
081.break;
082.case 'i':
083.case 'I':
084.glLoadIdentity();//恢复初始模型矩阵
085.gluLookAt(0010000010);
086.glutPostRedisplay();
087.break;
088.case 27:
089.exit(0);
090.break;
091.}
092.}
093. 
094.int main(int argc, char **argv)
095.{
096.glutInitWindowSize(200200);
097.glutInit(&argc, argv);
098.glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
099.glutCreateWindow(argv[0]);
100.init();
101.glutReshapeFunc(reshape);
102.glutKeyboardFunc(keyboard);
103.glutDisplayFunc(display);
104.glutMainLoop();
105.return 0;
106.}
#include <GL/glut.h>
002.#include <stdio.h>
003.#include <math.h>
004.#include <stdlib.h>
005. 
006.#define PI_ 3.14159265358979323846
007. 
008.GLuint theTorus;
009. 
010./* Draw a torus */
011.static void torus(int numc, int numt)//显示列表只能包含<a href="http://www.it165.net/Pro/pkgame/" target="_blank" class="keylink">OpenGL</a>函数
012.{
013.int i, j, k;
014.double s, t, x, y, z, twopi;
015. 
016.twopi = 2 * PI_;
017.for (i = 0; i < numc; i++) {
018.glBegin(GL_QUAD_STRIP);
019.for (j = 0; j <= numt; j++) {
020.for (k = 1; k >= 0; k--) {
021.s = (i + k) % numc + 0.5;
022.t = j % numt;
023. 
024.x = (1+.1*cos(s*twopi/numc))*cos(t*twopi/numt);
025.y = (1+.1*cos(s*twopi/numc))*sin(t*twopi/numt);
026.z = .1 * sin(s * twopi / numc);
027.glVertex3f(x, y, z);
028.}
029.}
030.glEnd();
031.}
032.}
033. 
034./* Create display list with Torus and initialize state */
035.static void init(void)//创建了一个绘制圆环面的显示列表
036.{
037.theTorus = glGenLists (1);//glGenLists()唯一的标识一个显示列表
038.glNewList(theTorus, GL_COMPILE);//用于对显示列表进行定界。第一个参数是一个整形索引值,由glGenLists()指定
039.torus(825);
040.glEndList();
041. 
042.glShadeModel(GL_FLAT);
043.glClearColor(0.00.00.00.0);
044.}
045. 
046./* Clear window and draw torus */
047.void display(void)
048.{
049.glClear(GL_COLOR_BUFFER_BIT);
050.glColor3f (1.01.01.0);
051.glCallList(theTorus);//执行显示列表所存储的函数
052.glFlush();
053.}
054. 
055./* Handle window resize */
056.void reshape(int w, int h)
057.{
058.glViewport(00, (GLsizei) w, (GLsizei) h);
059.glMatrixMode(GL_PROJECTION);
060.glLoadIdentity();
061.gluPerspective(30, (GLfloat) w/(GLfloat) h, 1.0100.0);
062.glMatrixMode(GL_MODELVIEW);
063.glLoadIdentity();
064.gluLookAt(0010000010);
065.}
066. 
067./* Rotate about x-axis when "x" typed; rotate about y-axis
068.when "y" typed; "i" returns torus to original view */
069.void keyboard(unsigned char key, int x, int y)
070.{
071.switch (key) {
072.case 'x':
073.case 'X':
074.glRotatef(30.,1.0,0.0,0.0);
075.glutPostRedisplay();//使glutMainLoop()函数调用display()函数
076.break;
077.case 'y':
078.case 'Y':
079.glRotatef(30.,0.0,1.0,0.0);
080.glutPostRedisplay();
081.break;
082.case 'i':
083.case 'I':
084.glLoadIdentity();//恢复初始模型矩阵
085.gluLookAt(0010000010);
086.glutPostRedisplay();
087.break;
088.case 27:
089.exit(0);
090.break;
091.}
092.}
093. 
094.int main(int argc, char **argv)
095.{
096.glutInitWindowSize(200200);
097.glutInit(&argc, argv);
098.glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
099.glutCreateWindow(argv[0]);
100.init();
101.glutReshapeFunc(reshape);
102.glutKeyboardFunc(keyboard);
103.glutDisplayFunc(display);
104.glutMainLoop();
105.return 0;
106.}
0 0
原创粉丝点击