实验2:代码:#include #include GLdouble angle = 10.0;void display(){ glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glBegin(GL_POLYGON); glVertex2d(-0.5,-0.5); glVertex2d(-0.5,0.5); glVertex2d(0.5,0.5); glVertex2d(0.5,-0.5); glEnd(); glFlush(); }void mouseFunc(int button,int state,int x,int y){ //旋转 if(state==GLUT_DOWN && button==GLUT_LEFT_BUTTON) { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); if(angle<360) angle+=10; else angle-=360; glRotatef(angle,1,1,2); glutPostRedisplay(); }}void keyboard(unsigned char key,int x,int y){ if(key==27) { exit(-1); }}void init(){ glClearColor(0.0,0.0,0.0,0.0); glColor3f(1.0,0.0,0.0);}void reshape(int w,int h){ glViewport(0,0,w,h);}int main(int argc,char **argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow("OpenGL中的建模与变换"); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMouseFunc(mouseFunc); glutMainLoop(); return 0;}截图:点击旋转:体会:主要是我们在进行变换的时候,要注意glMatrixMode的设置,在进行投影变换的时候要设置为GL_PROJECTION,而当图形变换的时候要设置为GL_MODELVIEW模式。
实验3:代码:#include #include void display(){ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glColor3f(1.0,1.0,1.0); glEnable(GL_LIGHT0); glDisable(GL_LIGHT1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(-0.5,0,0); glutSolidSphere(0.3,20,22); glLoadIdentity(); glDisable(GL_LIGHT0); glEnable(GL_LIGHT1); glTranslatef(0.5,0,0); glutSolidSphere(0.3,20,22); glutSwapBuffers();}void keyboard(unsigned char key,int x,int y){ if(key==27) { exit(-1); }}void init(){ glClearColor(0.0,0.0,0.0,0.0); GLfloat position0[4]={-10,10,-10,1}; GLfloat ambient0[4]={1,0,0,1}; glLightfv(GL_LIGHT0,GL_POSITION,position0); glLightfv(GL_LIGHT0,GL_AMBIENT,ambient0); GLfloat position1[4]={10,10,-10,1}; GLfloat ambient1[4]={0,1,0,1}; glLightfv(GL_LIGHT1,GL_POSITION,position1); glLightfv(GL_LIGHT1,GL_AMBIENT,ambient1); glEnable(GL_LIGHTING); glEnable(GL_DEPTH_TEST);}void reshape(int w,int h){ glViewport(0,0,w,h);}int main(int argc,char **argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow("OpenGL中的光照"); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0;}截图:体会:不知道为什么,右边的球体好像没有左边的光线效果,这点查询了很久还是不明白。
实验4:#include #include #include using namespace std;GLint select = 0;void drawObjects(GLenum mode){ if(mode == GL_SELECT)//绘制红色的小球 glPushName(1); if(select==1)//判断选中的是什么颜色的球,然后采用什么颜色红球还是白色的球 glColor3f(1,1,1); else glColor3f(1,0,0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(-0.5,0,0); glutSolidSphere(0.3,20,22); glPopName(); glLoadIdentity(); if(mode == GL_SELECT) //绘制绿色的小球 glPushName(2); if(select==2)//判断选中的是什么颜色的球,然后采用什么颜色绘制 glColor3f(1,1,1); else glColor3f(0,1,0); glTranslatef(0.5,0,0); glutSolidSphere(0.3,20,22); glPopName(); select=0;//显示完后将select设置为0}void display(){ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); drawObjects(GL_RENDER); glutSwapBuffers();}void keyboard(unsigned char key,int x,int y){ if(key==27) { exit(-1); }}void init(){ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glClearColor(0.0,0.0,0.0,0.0);}void reshape(int w,int h){ glViewport(0,0,w,h);}void draw(GLint hits,GLuint buffer[])//这个是操作堆栈,判断点击的是什么颜色的球{ unsigned int i, j; GLuint name, *ptr; if(hits==0) cout<<"未点击任何球体!"<
实验5:代码:#include #include #define ImageWidth 64#define ImageHeight 64static GLubyte Image[ImageHeight][ImageWidth][3];static GLuint texName;void makeImage(){ int i,j,c; for(i=0;i