|
红宝书中显示列表的一个例子。
Bulid没有错误,
但是绘图的那个窗口出不来。只有一个窗口,不知道为什么。
我把显示列表里面的内容改成绘制一个简单的球,则效果可以出来。
============================================================================================================
////////////////////////////////////////////////////////////////////////////////////////
//一个使用显示列表的例子
//////////////////////////////////////////////////////////////////////////////////////////////////
#pragma comment(lib,"glu32.lib")
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glut32.lib")
#pragma comment(lib,"glew32.lib")
#include <GL/glew.h>
#include <GL/glut.h>
#include <cmath>
#include <iostream>
using namespace std;
#define M_PI 3.1415926
GLuint theTorus;
///////////////////////////////////////////////////////////////////////////////////////////////
static void torus(int numc , int numt){
/*glPushMatrix();
glColor3f(1.0 , 1.0 , 0.0);
glutSolidSphere(1 , 30 , 20);
glPopMatrix();
*/
int i , j , k;
double s , t , x , y , z , twopi;
twopi=2*(double)M_PI;
for(i=0 ; i<numc ; i++){
glBegin(GL_QUAD_STRIP);
for(j=0 ; j<=numc ; i++){
for(k=1 ; k>=0 ; k--){
s=(i+k)%numc+0.5;
t=j%numt;
x=(1+.1*cos(s*twopi/numc))*cos(t*twopi/numt);
y=(1+.1*cos(s*twopi/numc))*sin(t*twopi/numt);
z=.1*sin(s*twopi/numc);
glVertex3f(x,y,z);
}
}
glEnd();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static void init(void){
theTorus=glGenLists(1); //获取未使用的显示列表索引
glNewList(theTorus , GL_COMPILE);
torus(8 , 25);
glEndList();
glShadeModel(GL_FLAT);
glClearColor(0.0 , 0.0 , 0.0 , 0.0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glCallList(theTorus);
glFlush();
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void reshape(int w , int h){
glViewport(0 , 0 , (GLsizei)w , (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30 , (GLfloat)w/(GLfloat)h , 1.0 , 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0 , 0 , 10 , 0 , 0 , 0 , 0 , 1 , 0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key , int x , int y){
switch(key){
case'x':
case'X':
glRotatef(30.0 , 1.0 , 0.0 , 0.0);
glutPostRedisplay();
break;
case'y':
case'Y':
glRotatef(30.0 , 0.0 , 1.0 , 0.0);
glutPostRedisplay();
break;
case'i':
case'I':
glLoadIdentity();
gluLookAt(0 , 0 , 10 , 0 , 0 , 0 , 0 , 1 , 0);
glutPostRedisplay();
break;
case 27:
exit(0);
break;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc , char**argv){
glutInit(&argc , argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(200 , 200);
glutInitWindowPosition(150 , 150);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
return 0 ;
} |
|