opengl绘制教室大作业源代码wp

合集下载

OpenGL 教程 —— 一个在线教育工具

OpenGL 教程 —— 一个在线教育工具

本科毕业论文外文翻译外OpenGL教程——一个在线教育工具学院: 计算机科学与技术专业: 计算机科学与技术(电)学号: 201312237035学生姓名:指导教师:日期: 二〇一七年六月OpenGL Tutorial - An Online Education ToolHarish MuseboyinaOpenGL Tutorial - An Online Education tool, Harish Museboyina.Montana State University, 2012OpenGL 教程——一个在线教育工具哈里斯·穆塞博伊纳OpenGL 教程——一个在线教育工具,哈里斯·穆塞博伊纳.蒙大拿州立大学,20121 简单光源(无材质效果)1.1 代码举例下面的代码是用来产生一束简单的光照,它又被投射到一个球体上。

由于这是第一个光照实例,因此我们现在并不关注材质效果,这将在下一节再进行描述。

OpenGL的实现需要至少8个主要灯光从GL_LIGHT0到GL_LIGHT7。

为了能在代码中使用光照,我们必须首先通过调用程序glEnable(GL_LIGHTING)来开启光照。

glLookAt()程序用来设置相机位置,我们可以使用glLightfv()来设置光照的位置。

这个程序实际是用来设置光照的方向,而并不是光照的实际位置。

虽然我们好像能够将光源设置在一个位置,但它实际表现出来反而像在无限远处。

#include"stdafx.h"#include<GL/glut.h>static double yVal = 50.0;void drawSphere(){GLUquadricObj* cyl;GLfloat light_position[] = { 0.0, 20.0, yVal, 10.0 };glLightfv(GL_LIGHT0, GL_POSITION, light_position); // Usinglighting (effects all the objects drawn below)glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(35.0, 1.0, 1.0, 100.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(20.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);cyl = gluNewQuadric();gluQuadricDrawStyle(cyl, GLU_FILL);gluSphere(cyl, 2.0, 50, 100);glFlush();}void display(void){/* clear all pixels */glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);drawSphere();glFlush();}void init(void){glClearColor(0.5, 0.5, 0.5, 0.0);glEnable(GL_DEPTH_TEST);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluLookAt(30.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);}void keyboard(unsigned char key, int x, int y){switch (key){case27: // "esc" on keyboardexit(0);break;case 97: // "a" on keyboardyVal = yVal - 5.0;glutPostRedisplay();break;}}int main(int argc, char** argv){glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowSize(600, 600);glutInitWindowPosition(100, 100);glutCreateWindow("Simple Light...");init();glutDisplayFunc(display);glutKeyboardFunc(keyboard);glutMainLoop();return 0;}1.2 输出1.3 使用的关键方法void glLightfv(GLenum light, GLenum pname, const GLfloat *params)2 光照(材质效果)2.1 材质材质参数指定表面如何反射光。

openGL画直线、圆、椭圆

openGL画直线、圆、椭圆
putpixel(xc,yc,-x,-y);
putpixel(xc,yc,-x,y);
putpixel(xc,yc,x,-y);
while(b*b*(x+1)<a*a*(y-0.5))
{
if(d1<0)
{
d1+=b*b*(2*x+3);
x++;
}
else
{
d1+=b*b*(2*x+3)+a*a*(-2*y+2);
{
glBegin(GL_POINTS);
glVertex3f(xc+x,yc+y,0);
glEnd();
}
void drawEllipse(int xc,int yc,int a,int b)
{
int x,y;
float d1,d2;
x=0;
y=b;
d1=b*b+a*a*(-b+0.25);
putpixel(xc,yc,x,y);
{
glBegin(GL_POINTS);
glVertex3f(xc+x,yc+y,0);
glVertex3f(xc-x,yc+y,0);
glVertex3f(xc+x,yc-y,0);
glVertex3f(xc-x,yc-y,0);
glVertex3f(xc+y,yc+x,0);
glVertex3f(xc-y,yc+x,0);
/* so this could be omitted: */
drawEllipse(250,250,60,40);
/* and flush that buffer to the screen */

图形学opengl绘图入门代码

图形学opengl绘图入门代码

// opg1.cpp : Defines the entry point for the console application.//#include "stdafx.h"/*#include <GL/glut.h>void display(void){glClear (GL_COLOR_BUFFER_BIT);// clear all pixelsglColor3f (1.0, 1.0, 1.0);glBegin(GL_POL YGON);//draw white polygonglVertex3f (0.25, 0.25, 0.0);glVertex3f (0.75, 0.25, 0.0);glVertex3f (0.75, 0.75, 0.0);glVertex3f (0.25, 0.75, 0.0);glEnd();glFlush ();// start processing buffered OpenGL routines}void init (void){glClearColor (0.0, 0.0, 0.0, 0.0);// select clearing colorglMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);// initialize viewing values}int main(int argc, char** argv){glutInit(&argc, argv);glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);glutInitWindowSize (250, 250); //Declare initial window size.glutInitWindowPosition (100, 100);//Declare initial window position.glutCreateWindow ("hello");//Open window with "hello"in its title bar.init ();//Call initialization routines.glutDisplayFunc(display); /*Register callback function to display graphics.glutMainLoop();//Enter main loop and process events.return 0; // ANSI C requires main to return int.}*/// GL_2_17.cpp : Defines the entry point for the console application.///*#include <windows.h>#include <math.h>#include <gl/gl.h>#include <gl/glut.h>const int screenWidth=640;const int screenHeight=480;GLdouble A,B,C,D;void myInit(void){glClearColor(1.0,1.0,1.0,0.0);glColor3f(0.0f,0.0f,0.0f);glPointSize(2.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,screenWidth,0.0,screenHeight);A=screenWidth/4.0;B=0.0;C=D=screenHeight/2.0;}void myDisplay(void){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glBegin(GL_POINTS);for(GLdouble x=0;x<4.0;x+=0.001){GLdouble func=exp(-x)*cos(2*3.14159265*x);glVertex2d(A*x+B,C*func+D);}glEnd();glFlush();}void main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(640,480);glutInitWindowPosition(100,150);glutCreateWindow("Dot plot of a function");glutDisplayFunc(myDisplay);myInit();glutMainLoop();*/图形学第三课--opengl绘图入门代码#include <windows.h>#include <math.h>#include <gl/gl.h>#include <gl/glut.h>#include <GL/glaux.h>void myInit(void){glClearColor(0.0,0.0,0.0,0.0);glClear(GL_COLOR_BUFFER_BIT);}void myReshape(GLsizei w,GLsizei h){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if(w<=h)glOrtho(-1.5,1.5,-1.5*(GLfloat)h/(GLfloat)w, 1.5*(GLfloat)h/(GLfloat)w,-10.0,10.0);elseglOrtho(-1.5*(GLfloat)h/(GLfloat)w, 1.5*(GLfloat)h/(GLfloat)w,-1.5,1.5,-10.0,10.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}void myDisplay(void){glColor3f(1.0,1.0,0.0);//auxWireSphere(1.0);auxSolidTeapot(1.0);glFlush();}void main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(640,480);glutInitWindowPosition(100,150);glutCreateWindow("AUX_SAMPLE");myInit();glutReshapeFunc(myReshape);glutDisplayFunc(myDisplay);glutMainLoop();}图形学opengl入门--建模代码#include <windows.h>#include <math.h>#include <gl/gl.h>#include <gl/glut.h>#include <GL/glaux.h>void DrawMyObjects(void){/* draw some points */glBegin(GL_POINTS);glColor3f(1.0,0.0,0.0);glVertex2f(-10.0,11.0);glColor3f(1.0,1.0,0.0);glVertex2f(-9.0,10.0);glColor3f(0.0,1.0,1.0);glVertex2f(-8.0,12.0);glEnd();/* draw some line_segments */glBegin(GL_LINES);glColor3f(1.0,1.0,0.0);glVertex2f(-11.0,8.0);glVertex2f(-7.0,7.0);glColor3f(1.0,0.0,1.0);glVertex2f(-11.0,9.0);glVertex2f(-8.0,6.0);glEnd();/* draw one opened_line */glBegin(GL_LINE_STRIP);glColor3f(0.0,1.0,0.0);glVertex2f(-3.0,9.0);glVertex2f(2.0,6.0);glVertex2f(3.0,8.0);glVertex2f(-2.5,6.5);glEnd();/* draw one closed_line */glBegin(GL_LINE_LOOP);glColor3f(0.0,1.0,1.0);glVertex2f(7.0,7.0);glVertex2f(8.0,8.0);glVertex2f(10.3,7.5);glVertex2f(11.5,6.0);glVertex2f(7.5,6.0);glEnd();/* draw one filled_polygon */glBegin(GL_POL YGON);glColor3f(0.5,0.3,0.7);glVertex2f(-7.0,2.0);glVertex2f(-8.0,3.0);glVertex2f(-10.3,0.5);glVertex2f(-7.5,-2.0);glVertex2f(-6.0,-1.0);glEnd();/* draw some filled_quandrangles */ glBegin(GL_QUADS);glColor3f(0.7,0.5,0.2);glVertex2f(0.0,2.0);glVertex2f(-1.0,3.0);glVertex2f(-3.3,0.5);glVertex2f(-0.5,-1.0);glColor3f(0.5,0.7,0.2);glVertex2f(3.0,2.0);glVertex2f(2.0,3.0);glVertex2f(0.0,0.5);glVertex2f(2.5,-1.0);glEnd();/* draw some filled_strip_quandrangles */ glBegin(GL_QUAD_STRIP);glVertex2f(6.0,-2.0);glVertex2f(5.5,1.0);glVertex2f(8.0,-1.0);glColor3f(0.8,0.0,0.0);glVertex2f(9.0,2.0);glVertex2f(11.0,-2.0);glColor3f(0.0,0.0,0.8);glVertex2f(11.0,2.0);glVertex2f(13.0,-1.0);glColor3f(0.0,0.8,0.0);glVertex2f(14.0,1.0);glEnd();/* draw some filled_triangles */glBegin(GL_TRIANGLES);glColor3f(0.2,0.5,0.7);glVertex2f(-12.3,-7.5);glVertex2f(-8.5,-6.0);glColor3f(0.2,0.7,0.5);glVertex2f(-8.0,-7.0);glVertex2f(-7.0,-4.5);glVertex2f(-5.5,-9.0);glEnd();/* draw some filled_strip_triangles */glBegin(GL_TRIANGLE_STRIP);glVertex2f(-1.0,-8.0);glVertex2f(-2.5,-5.0);glColor3f(0.8,0.8,0.0);glVertex2f(1.0,-7.0);glColor3f(0.0,0.8,0.8);glVertex2f(2.0,-4.0);glColor3f(0.8,0.0,0.8);glVertex2f(4.0,-6.0);glEnd();/* draw some filled_fan_triangles */glBegin(GL_TRIANGLE_FAN);glVertex2f(8.0,-6.0);glVertex2f(10.0,-3.0);glColor3f(0.8,0.2,0.5);glVertex2f(12.5,-4.5);glColor3f(0.2,0.5,0.8);glVertex2f(13.0,-7.5);glColor3f(0.8,0.5,0.2);glVertex2f(10.5,-9.0);glEnd();}void myInit(void){glClearColor(0.0,0.0,0.0,0.0);glClear(GL_COLOR_BUFFER_BIT);glShadeModel(GL_FLAT);}void myReshape(GLsizei w,GLsizei h) {glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if(w<=h)glOrtho(-20.0,20.0,-20.0*(GLfloat)h/(GLfloat)w, 20.0*(GLfloat)h/(GLfloat)w,-50.0,50.0);elseglOrtho(-20.0*(GLfloat)h/(GLfloat)w,20.0*(GLfloat)h/(GLfloat)w,-20.0,20.0,-50.0,50.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}void myDisplay(void){glColor3f(1.0,1.0,0.0);DrawMyObjects();glFlush();}void main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(600,600);glutInitWindowPosition(100,150);glutCreateWindow("Geometric Primitive Types");myInit();glutReshapeFunc(myReshape);glutDisplayFunc(myDisplay);glutMainLoop();}。

opengl源文件_plane_作业

opengl源文件_plane_作业

#include <stdio.h>#include <windows.h>#include<math.h>#include <GL/gl.h>#include <GL/glu.h>#include <GL/glaux.h>#define pi 3.1415926#include "MilkModel.h" // Header File For Milkshape File/*实现还需几个头文件,然后建立工程*/void init(void);void CALLBACK reshape(GLsizei w,GLsizei h);void CALLBACK On0KeyDown(void);void CALLBACK On1KeyDown(void);void CALLBACK OnSpaceDown(void);void CALLBACK OnLButtonDown(AUX_EVENTREC *);void CALLBACK OnLButtonMove(AUX_EVENTREC *);void CALLBACK display(void);void pickObjects(void);void processHits (GLint hits, GLuint buffer[]);void drawCoordinates(void);void load_oro(void);void load_photo(void);void draw_photo(void);GLint mx,my; //position of mouseGLint hits,pick;// for the pickingGLint m_state=0,space=1; //mouse usage and the space key GLfloat x_angle=12.0f, y_angle=8.0f,t_angle=0.0f;//angle of eye GLdouble dist=30; //distance from the eyeGLuint i_pick=0;// for pickingGLdouble r=45000;GLuint texPhoto = 0;//photo textureint sizeX, sizeY; //photo size;extern void DrawBitmapString(LPCSTR str);extern void DrawOutlineString(LPCSTR str);extern SIZE GetStringSize(LPCSTR str);extern void ReadDEM(char *); //read DEMGLdouble nearclip;GLdouble farclip;GLdouble steps;const GLdouble stepa = 0.2;GLdouble rx,rz;float xOrg, yOrg;float angle;float dx, dy;int nx, ny;float *pHight = NULL;GLuint texture = 0;void init(void) //初始化{glClearColor(0.0f,0.0f,0.0f,0.0f);glClear(GL_COLOR_BUFFER_BIT);if (loadModelData("Aereo.ms3d") == 0) // Loads The Model And Checks For Errors return;loadTextures(); // Loads Model Texturesload_oro();load_photo();}void load_photo(void){AUX_RGBImageRec *pImage;//Make texturepImage = auxDIBImageLoad("photo.bmp");sizeX = pImage->sizeX;sizeY = pImage->sizeY;glGenTextures(1, &texPhoto);glBindTexture(GL_TEXTURE_2D, texPhoto);gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->sizeX, pImage->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pImage->data);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);free(pImage->data);free(pImage);glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);}void load_oro(void){AUX_RGBImageRec *pImage;//Load DEMReadDEM("terrain.dem");rx = (nx-1)*dx; //地形图的x长度rz = (ny-1)*dy; //地形图的z长度nearclip=0.1*(rx+rz);farclip=200*nearclip;steps =dx+dy; //每一步dist大小dist=(rx+rz)*20/12;//Make texturepImage = auxDIBImageLoad("terrain.bmp");glGenTextures(1, &texture);glBindTexture(GL_TEXTURE_2D, texture);glTexImage2D(GL_TEXTURE_2D, 0, 3, pImage->sizeX, pImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, pImage->data);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);free(pImage->data);free(pImage);glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);}void CALLBACK AnimateFunc(void) //运动的图形,按下space静止{if(space==1){t_angle=t_angle-5.0f;if(t_angle<=-360)t_angle=360+t_angle;display();}else return;}void CALLBACK reshape(GLsizei w,GLsizei h) //调整窗口{glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if(h<1) h=1;gluPerspective(30.0, 1.0*w/h,nearclip,farclip); //实景深度400glMatrixMode(GL_MODELVIEW);glLoadIdentity();}void CALLBACK On0KeyDown(void) {m_state = 0;}void CALLBACK On1KeyDown(void) {m_state = 1;}void CALLBACK OnSpaceDown(void){space=-space;}void CALLBACK OnLButtonMove(AUX_EVENTREC *event) //鼠标移动视野{GLint dx,dy;dx = event->data[AUX_MOUSEX]-mx;dy = event->data[AUX_MOUSEY]-my;mx=event->data[AUX_MOUSEX];my=event->data[AUX_MOUSEY];if(m_state == 0){y_angle += dx*0.1f;x_angle += dy*0.1f;}else if(m_state == 1)dist += (dx+dy)*steps;display();}void CALLBACK OnLButtonDown(AUX_EVENTREC *event){mx=event->data[AUX_MOUSEX];my=event->data[AUX_MOUSEY];pickObjects();}#define BUFSIZE 600void pickObjects(void) //用于实现picking,重新画出图形所处的位置{double i;GLuint selectBuf[BUFSIZE];GLint hits;GLint viewport[4];glGetIntegerv(GL_VIEWPORT, viewport);glSelectBuffer(BUFSIZE, selectBuf);glRenderMode(GL_SELECT);glInitNames();glPushName(0);glMatrixMode(GL_PROJECTION);glPushMatrix();glLoadIdentity();gluPickMatrix((GLdouble)mx, (GLdouble)(viewport[3]-my),30, 30, viewport);gluPerspective(30.0, 1.0*viewport[2]/viewport[3], nearclip, farclip);//视景大小glMatrixMode(GL_MODELVIEW);glLoadIdentity();glPushMatrix();glTranslated(-rx*2/3,0,-0.5*dist);glRotatef(x_angle, 1.0f, 0.0f, 0.0f);glRotatef(y_angle, 0.0f, 1.0f, 0.0f);glPushMatrix();glTranslated(rx*2/3, 15000.0f,rz*1/2); //画图点击的位置glRotatef(t_angle, 0.0f, 1.0f, 0.0f);for(i=0;i<8;i++) //飞机个数(8){glPushName((GLuint) i);glPushMatrix();glTranslated(r*cos(i*pi/4),0.5,-(r*sin(i*pi/4)));glRotated(i*45,0.0,1.0,0.0);glScalef(500,500,500);drawModel(0);//the place of the model !glPopMatrix();glPopName();}glPopMatrix();glPopMatrix();glFlush();hits = glRenderMode(GL_RENDER);processHits(hits, selectBuf); //纪录点击成功glMatrixMode(GL_PROJECTION);glPopMatrix();glMatrixMode(GL_MODELVIEW);}void processHits(GLint hits, GLuint buffer[]) //纪录点击是否成功{GLuint i, minz;if(hits<1) return;pick=1;for(i=0;i<(GLuint)hits;i++) //for each hit{if(i==0 || minz > buffer[i*5+1]){minz = buffer[i*5+1];i_pick = buffer[i*5+4];}}}void CALLBACK display(void) //实现图形的显现{double i;int k, j;glClearColor(0.0f,0.2941176470588235f,0.4156862745098039f,0.0f);glClearDepth(1.0);glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glEnable(GL_DEPTH_TEST);glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(30.0, 1.0*1200/688,nearclip,farclip);//实景深度400glMatrixMode(GL_MODELVIEW);glLoadIdentity();glPushMatrix();glTranslated(-rx*2/3,0,-0.5*dist);glRotatef(x_angle, 1.0f, 0.0f, 0.0f);glRotatef(y_angle, 0.0f, 1.0f, 0.0f);drawCoordinates();glPushMatrix();glTranslated(rx*2/3, 15000.0f,rz*1/2);//画图的位置glRotatef(t_angle, 0.0f, 1.0f, 0.0f);for(i=0;i<8;i++) //画出选择图形{glPushMatrix();glTranslated(r*cos(i*pi/4),0.5,-(r*sin(i*pi/4))); //飞机的个数glRotated(i*45,0.0,1.0,0.0);if(i==i_pick&&pick==1){glRotatef(-45,0.0,0.0,1.0); //旋转飞机}glPushMatrix();glScalef(500,500,500);//放大飞机drawModel(1);glPopMatrix();glPopMatrix();}glPopMatrix();glPushMatrix();glDisable(GL_LIGHTING);glBindTexture(GL_TEXTURE_2D, texture);glEnable(GL_TEXTURE_2D);for(j=0; j<ny-1; j++){glBegin(GL_QUAD_STRIP);for(k=0; k<nx; k++){glTexCoord2f(k*1.0f/(nx-1), j*1.0f/(ny-1));glVertex3f(k*dx,pHight[j*nx+k],j*dy);glTexCoord2f(k*1.0f/(nx-1), (j+1)*1.0f/(ny-1));glVertex3f(k*dx,pHight[(j+1)*nx+k],(j+1)*dy);}glEnd();}glPopMatrix();glPopMatrix();glFlush();draw_photo();auxSwapBuffers();}void draw_photo(){GLint rect[4];LPSTR pText = "******"; //your nameSIZE size;glGetIntegerv(GL_VIEWPORT, rect);glEnable(GL_TEXTURE_2D);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0, rect[2], 0, rect[3]);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glBindTexture(GL_TEXTURE_2D, texPhoto);glBegin(GL_QUADS);glTexCoord2f(0.0f, 0.0f); glVertex2f(0.8f*rect[2], 0.0f);glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f*rect[2], 0.0f);glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f*rect[2], 0.2f*rect[2]*sizeY/sizeX);glTexCoord2f(0.0f, 1.0f); glVertex2f(0.8f*rect[2], 0.2f*rect[2]*sizeY/sizeX);glEnd();glDisable(GL_TEXTURE_2D);size = GetStringSize(pText);glColor3f(1.0f, 1.0f, 0.0f);glRasterPos2f(0.9f*rect[2]-size.cx/2, 0.2f*rect[2]*sizeY/sizeX+5);DrawBitmapString(pText);glFlush();}void drawCoordinates(void) //画坐标轴{glColor3f(1.0f,0.0f,0.0f); //画红色的x轴glBegin(GL_LINES);glVertex3f(0.0f, 0.0f, 0.0f);glVertex3f(50000.0f, 0.0f, 0.0f);glEnd();glColor3f(0.0,1.0,0.0); //画绿色的y轴glBegin(GL_LINES);glVertex3f(0.0f, 0.0f, 0.0f);glVertex3f(0.0f, 50000.0f, 0.0f);glEnd();glColor3f(0.0,0.0,1.0); //画蓝色的z轴glBegin(GL_LINES);glVertex3f(0.0f, 0.0f, 0.0f);glVertex3f(0.0f, 0.0f, 50000.0f);glEnd();}void main(void){auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA|AUX_DEPTH24);auxInitPosition(0,0,1266,668);auxInitWindow("my_program");init();printf("按0可以转换视角\n");printf("按1可以拉近距离\n");printf("按space可以暂停\n");auxIdleFunc(AnimateFunc);auxReshapeFunc(reshape);auxKeyFunc(AUX_0,On0KeyDown);auxKeyFunc(AUX_1,On1KeyDown);auxKeyFunc(AUX_SPACE ,OnSpaceDown);auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSEDOWN, OnLButtonDown);auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSELOC, OnLButtonMove);auxMainLoop(display);freeModel();free(pHight);if(glIsTexture(texture)) glDeleteTextures(1, &texture);if(glIsTexture(texPhoto)) glDeleteTextures(1, &texPhoto); }。

opengl使用手册 简书

opengl使用手册 简书

opengl使用手册简书(原创实用版)目录一、OpenGL 简介二、OpenGL 函数库1.核心函数库2.矩阵操作、几何变换和投影变换函数3.交互式输入设备函数三、OpenGL 扩展库 GLEW正文一、OpenGL 简介OpenGL(Open Graphics Library)是一个跨平台的图形编程接口,用于渲染 2D 和 3D 图形。

OpenGL 提供了一套完整的图形渲染 API,可以实现各种视觉效果,如颜色、光照、阴影、纹理贴图等。

它广泛应用于游戏开发、计算机辅助设计、虚拟现实、科学可视化等领域。

二、OpenGL 函数库OpenGL 函数库包含许多可以用于绘制图形的函数。

这些函数可以根据其功能分为不同的类别,主要包括:1.核心函数库:这个库包含了 OpenGL 的基本功能,如绘制基本的几何图元(glBegin)、设置颜色(glColor3f)等。

2.矩阵操作、几何变换和投影变换函数:这个库包含了用于操作矩阵、实现几何变换和投影变换的函数。

例如,矩阵入栈(glPushMatrix)、矩阵出栈(glPopMatrix)、矩阵乘法(glMultMatrix)等。

3.交互式输入设备函数:这个库包含了用于处理交互式输入设备的函数,例如鼠标和键盘。

这些函数可以让用户在程序中进行操作,如点击、拖动、滚动等。

三、OpenGL 扩展库 GLEWGLEW(GL Extension Wrangler Library)是一个 OpenGL 扩展库,用于简化 OpenGL 扩展的加载和使用过程。

GLEW 提供了一系列的函数,用于查询、启用和禁用 OpenGL 扩展。

使用 GLEW,开发者无需关心扩展的加载和启用,只需关注功能的实现。

总之,OpenGL 是一套功能强大的图形编程接口,包含了丰富的函数库,可以实现各种复杂的图形渲染效果。

opengl绘制教室大作业源代码wp

opengl绘制教室大作业源代码wp

wp.h#include <windows.h>// Windows的头文件#include <glut.h>// 包含OpenGL实用库#include<stdio.h>// 标准输入/输出库的头文件#include<glaux.h>// GLaux库的头文件#include<math.h>#include <stdlib.h>#include <time.h>#include <iostream.h>//..................................定义变量区............................................................////.............................用于场景漫游有关变量........................................//GLfloat xrot=0.0f;// X 旋转量GLfloat yrot=0.0f;// Y 旋转量GLfloat x=0.0f;// X 平移量GLfloat y=0.0f;// Y 平移量GLfloat z=-40.0f;// 深入屏幕的距离//.............................用于键盘操作有关变量.......................................//BOOL light=TRUE;// 光源的开/关BOOL lp;// L键按下了么?BOOL fp;// F键按下了么?BOOL TurnOn=FALSE; // 开灯GLUquadricObj *qobj;int fantheta=0; // 风扇转过角度HGLRC hRC=NULL;// 窗口着色描述表句柄HDC hDC=NULL;// OpenGL渲染描述表句柄HWND hWnd=NULL;// 保存我们的窗口句柄HINSTANCE hInstance;// 保存程序的实例bool keys[256];// 保存键盘按键的数组bool active=TRUE;// 窗口的活动标志,缺省为TRUEbool fullscreen=TRUE;// 全屏标志缺省,缺省设定成全屏模式//.................................光照材质有关变量......................................//GLfloat light_Ambient[]= { 0.1f, 0.1f, 0.1f, 1.0f }; // 环境光参数GLfloat light_Diffuse[]= { 1.0f, 1.0f, 0.45f, 1.0f };// 漫射光参数GLfloat light_position[]={0.0,29.9,0.0,1.0}; // 光源位置GLfloat light_position1[]={20.0,29.9,0.0,1.0};GLfloat mat_ambient[]={0.05f,0.05f,0.05f,1.0f}; // 材质环境光参数GLfloat mat_specular[]={0.8,1.0,1.0,1.0}; // 材质镜面光参数GLfloat mat_shininess[]={5.0};GLfloat white_light[]={1.0,1.0,1.0,1.0}; // 光GLfloat no_mat[]= {0.0f, 0.0f, 0.0f, 1.0f};// 无材质色GLfloat no_shininess[]= {0.0f};// 无反光GLfloat mat_diffuse[]= {1.0f, 1.0f, 1.0f, 1.0f};// 漫反射GLfloat diffusion[]={0.521f,0.121f,0.0547f,1.0};//......................................纹理...........................................................//GLuint texture[6];// 7种纹理的储存空间//.....................................时钟有关变量...................................................//GLfloat cx=35.0,cy=15.0,cz=-79.9,radius=5.0;GLfloat hradius=2.0,mradius=3.0,sradius=4.0;GLfloat sangle,mangle,hangle;GLuint second,miniter,hour;char timebuffer[9];LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);// WndProc的定义wp.cpp#include"wp.h"//......................................创建纹理..........................................................//AUX_RGBImageRec *LoadBMP(char *)// 载入位图图象{FILE *;// 文件句柄if (!)// 确保文件名已提供{return NULL;// 如果没提供,返回NULL}(,"r");// 尝试打开文件if (File)// 文件存在么?{fclose(File);// 关闭句柄return auxDIBImageLoad();// 载入位图并返回指针}return NULL;}// 如果载入失败,返回NULLint LoadGLTextures()// 载入位图并转换成纹理{int Status=FALSE;// 状态指示器AUX_RGBImageRec *TextureImage[6];// 创建纹理的存储空间memset(TextureImage,0,sizeof(void *)*1);// 将指针设为NULL// 载入位图,检查有无错误,如果位图没找到则退出Status=TRUE;// 将Status 设为TRUE//...............................创建第一个纹理,地板.......................................//if (TextureImage[0]=LoadBMP("floor.bmp")){glGenTextures(1, &texture[0]);// 创建纹理glBindTexture(GL_TEXTURE_2D, texture[0]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);//绑定纹理}if (TextureImage[0])//纹理是否存在{if (TextureImage[0]->data)//纹理图像是否存在{free(TextureImage[0]->data);//释放纹理图像占用的内存}free(TextureImage[0]);//释放图像结构}//...............................创建第二个纹理,墙.......................................//if (TextureImage[1]=LoadBMP("wall.bmp"))glGenTextures(1, &texture[1]); //创建纹理glBindTexture(GL_TEXTURE_2D, texture[1]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);//绑定纹理}if (TextureImage[1])//纹理是否存在{if (TextureImage[1]->data)//纹理图像是否存在{free(TextureImage[1]->data);//释放纹理图像占用的内存}free(TextureImage[1]);//释放图像结构}//...............................创建第三个纹理,窗户.......................................//if (TextureImage[2]=LoadBMP("glass.bmp")){glGenTextures(1, &texture[2]);//创建纹理glBindTexture(GL_TEXTURE_2D, texture[2]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[2]->sizeX, TextureImage[2]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[2]->data);//绑定纹理}if (TextureImage[2])//纹理是否存在{if (TextureImage[2]->data)//纹理图像是否存在{free(TextureImage[2]->data);//释放纹理图像占用的内存}free(TextureImage[2]);//释放图像结构}//...............................创建第四个纹理,门.......................................//if (TextureImage[3]=LoadBMP("door.bmp"))glGenTextures(1, &texture[3]);//创建纹理glBindTexture(GL_TEXTURE_2D, texture[3]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[3]->sizeX, TextureImage[3]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[3]->data);//绑定纹理}if (TextureImage[3])//纹理是否存在{if (TextureImage[3]->data)//纹理图像是否存在{free(TextureImage[3]->data);//释放纹理图像占用的内存}free(TextureImage[3]);//释放图像结构}//...............................创建第五个纹理,灯.......................................//if (TextureImage[4]=LoadBMP("lamp.bmp")){Status=TRUE;glGenTextures(1, &texture[4]);//创建纹理glBindTexture(GL_TEXTURE_2D, texture[4]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[4]->sizeX, TextureImage[4]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[4]->data);//绑定纹理}if (TextureImage[4])//纹理是否存在{if (TextureImage[4]->data)//纹理图像是否存在{free(TextureImage[4]->data);//释放纹理图像占用的内存}free(TextureImage[4]);//释放图像结构}//...............................创建第六个纹理,黑板.......................................//if (TextureImage[5]=LoadBMP("blackwall.bmp")){glGenTextures(1, &texture[5]);//创建纹理glBindTexture(GL_TEXTURE_2D, texture[5]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[5]->sizeX, TextureImage[5]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[5]->data);//绑定纹理}if (TextureImage[5])//纹理是否存在{if (TextureImage[5]->data)//纹理图像是否存在{free(TextureImage[5]->data);//释放纹理图像占用的内存}free(TextureImage[5]);//释放图像结构}return Status;}//...................................重置OpenGL窗口大小..............................................//GLvoid ReSizeGLScene(GLsizei width, GLsizei height)// 重置OpenGL窗口大小{if (height==0)// 防止被零除{height=1;// 将Height设为1 }glViewport(0, 0, width, height);// 重置当前的视口glMatrixMode(GL_PROJECTION);// 选择投影矩阵glLoadIdentity();// 重置投影矩阵// 设置视口的大小gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,1000.0f);glMatrixMode(GL_MODELVIEW);// 选择模型观察矩阵glLoadIdentity();// 重置模型观察矩阵}//...................................初始化操作................................................//int InitGL(GLvoid)// 此处开始对OpenGL进行所有设置{if (!LoadGLTextures())// 调用纹理载入子例程{return FALSE;// 如果未能载入,返回FALSE}glEnable(GL_TEXTURE_2D);// 启用纹理映射glShadeModel(GL_SMOOTH);// 启用阴影平滑glClearColor(0.0f, 0.0f, 0.0f, 0.0f);// 黑色背景glClearDepth(1.0f);// 设置深度缓存glEnable(GL_DEPTH_TEST);// 启用深度测试glDepthFunc(GL_LEQUAL);// 所作深度测试的类型glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// 告诉系统对透视进行修正glLightModelfv(GL_LIGHT_MODEL_AMBIENT,mat_ambient);glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE);glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);glLightfv(GL_LIGHT1,GL_POSITION,light_position); // 设置光源位置glLightfv(GL_LIGHT1,GL_AMBIENT,light_Ambient); // 设置环境光glLightfv(GL_LIGHT1,GL_DIFFUSE,white_light); // 设置漫射光glLightfv(GL_LIGHT1,GL_SPECULAR,white_light);glEnable(GL_LIGHTING);glEnable(GL_LIGHT1);// 启用一号光源glEnable(GL_COLOR_MATERIAL);glPixelStorei(GL_UNPACK_ALIGNMENT,1); //对齐像素字节函数return TRUE;// 初始化OK}//..............................................开始绘制..........................................//int DrawGLScene(GLvoid)// 从这里开始进行所有的绘制{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// 清除屏幕和深度缓存glLoadIdentity();// 重置当前的模型观察矩阵glTranslatef(x,y,z); //沿X,Y,Z轴移动glRotatef(xrot,1.0f,0.0f,0.0f);// 绕X轴旋转glRotatef(yrot,0.0f,1.0f,0.0f);// 绕Y轴旋转glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat); //选择材质光照glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);//.............................................房间........................................................////.............................................地板........................................................//glBindTexture(GL_TEXTURE_2D, texture[0]);// 选择纹理glBegin(GL_QUADS); //开始绘制四边形glColor3f(1.0f, 1.0f, 1.0f);glNormal3f(0.0,1.0,0.0); //glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f(-50.0f, -21.0f, -80.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f(-50.0f, -21.0f, 40.0f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( 50.0f, -21.0f, 40.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f( 50.0f, -21.0f, -80.0f);//左上glEnd();//.............................................天花板......................................................//glBindTexture(GL_TEXTURE_2D, texture[1]);// 选择纹理glBegin(GL_QUADS);//开始绘制四边形glColor3f(1.0f, 1.0f, 1.0f);glNormal3f(0.0f, -1.0f, 0.0f);//glNormal3f用于定义点的法向量glTexCoord2f(0.0f, 0.0f);glVertex3f(-50.0f, 30.0f, 40.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f(-50.0f, 30.0f, -80.0f);//左上glTexCoord2f(1.0f, 1.0f);glVertex3f( 50.0f, 30.0f, -80.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( 50.0f, 30.0f, 40.0f);//右下glEnd();//............................................后墙面.......................................................//glBegin(GL_QUADS);//开始绘制四边形glColor3f(1.0f, 1.0f, 1.0f);glNormal3f(0.0f, 0.0f, 1.0f);//glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f(-50.0f, 30.0f, -80.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f(-50.0f, -21.0f, -80.0f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( 50.0f, -21.0f, -80.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f( 50.0f, 30.0f, -80.0f);//左上//............................................前墙面.......................................................//glNormal3f(0.0f, 0.0f, -1.0f);//glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f( 50.0f, 30.0f, 40.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( 50.0f, -21.0f, 40.0f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f(-50.0f, -21.0f, 40.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f(-50.0f, 30.0f, 40.0f);//左上//............................................左墙面.......................................................//glNormal3f( 1.0f, 0.0f, 0.0f);//glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f(-50.0f, 30.0f, 40.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f(-50.0f, -21.0f, 40.0f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f(-50.0f, -21.0f, -80.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f(-50.0f, 30.0f, -80.0f);//左上//............................................右墙面.......................................................//glNormal3f(-1.0f, 0.0f, 0.0f);//glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f( 50.0f, 30.0f, -80.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( 50.0f, -21.0f, -80.0f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( 50.0f, -21.0f, 40.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f( 50.0f, 30.0f, 40.0f);//左上glEnd();//.............................................桌椅..........................................................//GLuint i=0;for(;i<5;i++)//一共五排桌椅{//............................................第一张桌椅......................................................////..........................桌子的上面.......................//glPushMatrix();//因为下面要对坐标进行变换,所以先将信息入栈保护,使整个工程的坐标不发生改变glColor3f(1.0,1.0,0.45);glTranslatef(30.0f,-10.0f,-30.0+i*9);//平移变换glScalef(13.0f,0.3f,5.0f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//..........................桌子的下面......................//glPushMatrix();//压入堆栈glColor3f(1.0,1.0,0.45);glTranslatef(30.0f,-13.0f,-30.0+i*9);//平移变换glScalef(11.0f,0.3f,5.0f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//...........................桌子的后面......................//glPushMatrix();//压入堆栈glColor3f(0.2,0.8,0.8);glTranslatef(30.0f,-12.0f,-32.5f+i*9);//平移变换glScalef(11.0f,3.6f,0.3f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//...........................桌子的左边桌腿...................//glPushMatrix();//压入堆栈glColor3f(0.0,0.0,0.0);glTranslatef(25.0f,-17.0f,-32.5f+i*9);//平移变换glScalef(0.5f,8.0f,0.3f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//...........................桌子的右边桌腿...................//glPushMatrix();//压入堆栈glColor3f(0.0,0.0,0.0);glTranslatef(35.0f,-17.0f,-32.5f+i*9);//平移变换glScalef(0.5f,8.0f,0.3f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//............................椅子............................//glPushMatrix();//压入堆栈glColor3f(0.2,0.8,0.8);glTranslatef(30.0f,-15.0f,-25.0+i*9);//平移变换glScalef(11.3f,0.3f,3.5f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//.....................................................第二张桌椅....................................................// //..........................桌子的上面.......................//glPushMatrix();glColor3f(1.0,1.0,0.45);glTranslatef(0.0f,-10.0f,-30.0+i*9);glScalef(26.0f,0.3f,5.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//..........................桌子的下面......................//glPushMatrix();glColor3f(1.0,1.0,0.45);glTranslatef(0.0f,-13.0f,-30.0+i*9);glScalef(24.0f,0.3f,5.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的后面......................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(0.0f,-12.0f,-32.5f+i*9);glScalef(24.0f,3.6f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的左边桌腿...................//glPushMatrix();glColor3f(0.0,0.0,0.0);glTranslatef(-11.4f,-17.0f,-32.5f+i*9);glScalef(0.5f,8.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的右边桌腿...................//glPushMatrix();glColor3f(0.0,0.0,0.0);glTranslatef(11.4f,-17.0f,-32.5f+i*9);glScalef(0.5f,8.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//............................椅子............................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(0.0f,-15.0f,-25.0+i*9);glScalef(24.3f,0.3f,3.5f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//........................................第三张桌椅..................................................////..........................桌子的上面.......................//glPushMatrix();glColor3f(1.0,1.0,0.45);glTranslatef(-30.0f,-10.0f,-30.0+i*9);glScalef(13.0f,0.3f,5.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//..........................桌子的下面......................//glPushMatrix();glColor3f(1.0,1.0,0.45);glTranslatef(-30.0f,-13.0f,-30.0+i*9);glScalef(11.0f,0.3f,5.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的后面......................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(-30.0f,-12.0f,-32.5f+i*9);glScalef(11.0f,3.6f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的左边桌腿...................//glPushMatrix();glColor3f(0.0,0.0,0.0);glTranslatef(-25.0f,-17.0f,-32.5f+i*9);glScalef(0.5f,8.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的右边桌腿...................//glPushMatrix();glColor3f(0.0,0.0,0.0);glTranslatef(-35.0f,-17.0f,-32.5f+i*9);glScalef(0.5f,8.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//............................椅子............................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(-30.0f,-15.0f,-25.0+i*9);glScalef(11.3f,0.3f,3.5f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();}//..........................................最后一排的椅背..........................................////....................最右边的椅背..................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(30.0f,-15.0f,12.6f);glScalef(11.3f,12.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//.....................中间的椅背...................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(0.0f,-15.0f,12.6f);glScalef(24.3f,12.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//.....................最左边的椅背.................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(-30.0f,-15.0f,12.6f);glScalef(11.3f,12.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//................................风扇................................................//GLuint m=0;for(;m<2;m++)//一共两排风扇{qobj = gluNewQuadric();//初始化二次曲面并创建一个指向二次曲面的指针//...............................右边的风扇.............................//glPushMatrix();//压入堆栈,保证此处坐标变换不影响外部大环境glTranslatef(20.0,-3.0,-10.0-40*m); //风扇的位置if(TurnOn==TRUE) //如果TurnOn为1{fantheta+=10; //风扇转过的角度fantheta不断增加if(fantheta==360) //如果旋转角度超过360度fantheta=0; //则fantheta重新回0}glRotatef(fantheta,0,1,0); //风扇旋转//..........风扇中间的扁圆形....................//glPushMatrix();//压入堆栈glColor3f(0.7,0.7,0.7);glTranslatef(0.0,20.0,0.0);//平移变换glScalef(1,0.3,1);//缩放变换gluSphere(qobj,2,20,20);//绘制一个半径为2的球体glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glPopMatrix();//弹出堆栈//............风扇旋转120度的扇叶...............//glPushMatrix();//压入堆栈glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);//平移变换glScalef(1,0.05,1);//缩放变换glRotatef(120,0,1,0);//绕y周旋转120度gluCylinder(qobj,1,3,10,20,20);//绘制一个底面圆半径为1、顶面圆半径为2、高为10的圆锥体glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glPopMatrix();//弹出堆栈//............风扇不做旋转的扇叶.................//glPushMatrix();//压入堆栈glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);//平移变换glScalef(1,0.05,1);//缩放变换gluCylinder(qobj,1,3,10,20,20);//绘制一个底面圆半径为1、顶面圆半径为2、高为10的圆锥体glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glPopMatrix();//弹出堆栈//.............风扇旋转240度的扇叶...............//glPushMatrix();//压入堆栈glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);//平移变换glScalef(1,0.05,1);//缩放变换glRotatef(240,0,1,0);//绕y周旋转240度gluCylinder(qobj,1,3,10,20,20);//绘制一个底面圆半径为1、顶面圆半径为2、高为10的圆锥体glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glPopMatrix();//弹出堆栈//.............风扇的杆...........................//glPushMatrix();//压入堆栈glTranslatef(0,33,0.0);//平移变换glRotatef(90,1,0,0);//缩放变换gluCylinder(qobj,0.5,0.5,13,10,10);//绘制一个半径为0.5、高为13的圆锥体glPopMatrix();//弹出堆栈glPopMatrix();//弹出堆栈//................左边的风扇...........................................//glPushMatrix();glTranslatef(-20.0,-3.0,-10.0-40*m);if(TurnOn==TRUE){fantheta+=10;if(fantheta==360)fantheta=0;}glRotatef(fantheta,0,1,0);//..........风扇中间的扁圆形....................//glPushMatrix();//风扇中间的扁圆形glColor3f(0.7,0.7,0.7);glTranslatef(0.0,20.0,0.0);glScalef(1,0.3,1);gluSphere(qobj,2,20,20);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glPopMatrix();//............风扇旋转120度的扇叶...............//glPushMatrix();//风扇旋转120度的扇叶glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);glScalef(1,0.05,1);glRotatef(120,0,1,0);gluCylinder(qobj,1,3,10,20,20);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glPopMatrix();//............风扇不做旋转的扇叶.................//glPushMatrix();//风扇不做旋转的扇叶glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);glScalef(1,0.05,1);gluCylinder(qobj,1,3,10,20,20);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glPopMatrix();//.............风扇旋转240度的扇叶...............//glPushMatrix();//风扇旋转240度的扇叶glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);glScalef(1,0.05,1);glRotatef(240,0,1,0);gluCylinder(qobj,1,3,10,20,20);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glPopMatrix();//.............风扇的杆...........................//glPushMatrix();glTranslatef(0,33,0.0);glRotatef(90,1,0,0);gluCylinder(qobj,0.5,0.5,13,10,10);glPopMatrix();glPopMatrix();}//...........................................门.....................................................................//glColor3f(0.521f,0.121f,0.0547f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,diffusion);//定义材质光照glBindTexture(GL_TEXTURE_2D,texture[3]);//绑定纹理glBegin(GL_QUADS);//开始绘制四边形glColor3f(1.0f, 1.0f, 1.0f);glNormal3f(-1.0f, 0.0f, 0.0f);//glNormal3f用于定义点的法向量//............................前门.........................................//glTexCoord2f(0.0,1.0);glVertex3f( 49.9f, 20.0f, -70.0f);//左上glTexCoord2f(1.0,1.0);glVertex3f( 49.9f, 20.0f, -45.0f);//右上glTexCoord2f(1.0,0.0);glVertex3f( 49.9f, -21.0f, -45.0f);//右下glTexCoord2f(0.0,0.0);glVertex3f( 49.9f, -21.0f, -69.9f);//左下//............................后门.....................................//glTexCoord2f(0.0,1.0);glVertex3f( 49.9f, 20.0f, 5.0f);//左上glTexCoord2f(1.0,1.0);glVertex3f( 49.9f, 20.0f, 30.0f);//右上glTexCoord2f(1.0,0.0);glVertex3f( 49.9f, -21.0f, 30.0f);//右下glTexCoord2f(0.0,0.0);glVertex3f( 49.9f, -21.0f, 5.0f);//左下glEnd();//..............................................窗户.............................................................////.....................右面墙上的窗户......................................//glBindTexture(GL_TEXTURE_2D,texture[2]);//绑定纹理glBegin(GL_QUADS);//开始绘制四边形glColor4f(1.0f,1.0f,1.0f,0.5f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glNormal3f(-1.0f, 0.0f, 0.0f);//glNormal3f用于定义点的法向量glBlendFunc(GL_SRC_ALPHA,GL_ONE);// 基于源象素alpha通道值的半透明混合函数glTexCoord2f(0.0,1.0);glVertex3f( 49.9f,29.0f,-35.0f);//左上glTexCoord2f(1.0,1.0);glVertex3f( 49.9f,29.0f,0.0f);//右上glTexCoord2f(1.0,0.0);glVertex3f( 49.9f,10.0f,0.0f);//右下glTexCoord2f(0.0,0.0);glVertex3f( 49.9f,10.0f,-35.0f);//左下glEnd();//......................左面墙上的窗户(两扇)..............................//glBegin(GL_QUADS);//开始绘制四边形glNormal3f(1.0f, 0.0f, 0.0f);//glNormal3f用于定义点的法向量glColor4f(1.0f,1.0f,1.0f,0.5f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glTexCoord2f(0.0,1.0);glVertex3f( -49.9f, 15.0f,-60.0f);//左上glTexCoord2f(1.0,1.0);glVertex3f( -49.9f, 15.0f,-20.0f);//右上glTexCoord2f(1.0,0.0);glVertex3f( -49.9f, -10.0f,-20.0f);//右下glTexCoord2f(0.0,0.0);glVertex3f( -49.9f, -10.0f,-60.0f);//左下glTexCoord2f(0.0,1.0);glVertex3f( -49.9f, 15.0f,-10.0f);//左上glTexCoord2f(1.0,1.0);glVertex3f( -49.9f, 15.0f,30.0f);//右上glTexCoord2f(1.0,0.0);glVertex3f( -49.9f, -10.0f,30.0f);//右下glTexCoord2f(0.0,0.0);glVertex3f( -49.9f, -10.0f,-10.0f);//左下glEnable(GL_BLEND);// 打开混合glDisable(GL_DEPTH_TEST);// 关闭深度测试glEnd();//............................................灯......................................................//GLuint j=0;for(;j<2;j++) //一共两排灯{glBindTexture(GL_TEXTURE_2D,texture[4]);//绑定纹理glColor3f(1.0f,1.0f,1.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glBegin(GL_QUADS);//开始绘制四边形glNormal3f(0.0f, -1.0f, 0.0f);//glNormal3f用于定义点的法向量glTexCoord2f(0.0f, 1.0f);glVertex3f( 25.0f, 29.9f, -70.0f+j*40);//左上glTexCoord2f(1.0f, 1.0f);glVertex3f( 45.0f, 29.9f, -70.0f+j*40);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( 45.0f, 29.9f, -50.0f+j*40);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( 25.0f, 29.9f, -50.0f+j*40);//左下glColor3f(1.0f,1.0f,1.0f);glNormal3f(0.0f, -1.0f, 0.0f);glTexCoord2f(0.0f, 1.0f);glVertex3f( -10.0f, 29.9f, -70.0f+j*40);//左上glTexCoord2f(1.0f, 1.0f);glVertex3f( 10.0f, 29.9f, -70.0f+j*40);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( 10.0f, 29.9f, -50.0f+j*40);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( -10.0f, 29.9f, -50.0f+j*40);//左下glColor3f(1.0f,1.0f,1.0f);glNormal3f(0.0f, -1.0f, 0.0f);glTexCoord2f(0.0f, 1.0f);glVertex3f( -45.0f, 29.9f, -70.0f+j*40);//左上glTexCoord2f(1.0f, 1.0f);glVertex3f( -25.0f, 29.9f, -70.0f+j*40);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( -25.0f, 29.9f, -50.0f+j*40);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( -45.0f, 29.9f, -50.0f+j*40);//左下glEnd();}//....................................讲台.............................................................////.....................讲台的桌子........................................//glPushMatrix();//压入堆栈glColor3f(0.5f,0.9f,0.5f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义光照材质glTranslatef(-0.0f,-10.0f,-60.5f);//平移变换glScalef(20.0f,20.0f,10.0f);//缩放变换glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//......................地面上高起的平台.................................//glPushMatrix();//压入堆栈glColor3f(0.8f,0.8f,0.8f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义光照材质glTranslatef(0.0f,-20.0f,-70.0f);//平移变换glScalef(65.0f,2.9f,30.0f);//缩放变换glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//........................................黑板............................................................//glBindTexture(GL_TEXTURE_2D, texture[5]); //绑定纹理glBegin(GL_QUADS); //绘制四边形glColor3f(1.0f, 1.0f, 1.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义光照材质glNormal3f(0.0f, 0.0f, 1.0f);//glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f(-25.0f, 20.0f, -79.9f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f(-25.0f, -5.0f, -79.9f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( 25.0f, -5.0f, -79.9f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f( 25.0f, 20.0f, -79.9f);//左上glEnd();//........................................时钟..........................................................//_strtime(timebuffer);//取系统时间到timebuffer数组中hour=((int)timebuffer[0]-48)*10+((int)timebuffer[1]-48);//从timebuffer数组中得到小时的值miniter=((int)timebuffer[3]-48)*10+((int)timebuffer[4]-48);//从timebuffer数组中得到分钟。

opengl鼠标绘制直线及圆图元算法源代码

opengl鼠标绘制直线及圆图元算法源代码

#include <assert.h>#include <cmath>#include <iostream>#include <vector>#include <glut.h>using namespace std;typedef vector<int> list;vector<list> ls, cs;int old_x, old_y, new_x, new_y, cx, cy, cr; int mouse_type;/* breseman画线算法*/void line(int x1, int y1, int x2, int y2) { glBegin(GL_POINTS);if (x1==x2 && y1==y2) {glVertex2i(x1, y1);return;}if (x1>x2) {swap(x1, x2);swap(y1, y2);}glVertex2d(x1, y1);int dy=abs(y2-y1);int dx=abs(x2-x1);if (dx>=dy) {int incy=(dy!=0?dy/(y2-y1):0);int p=2*dy-dx;int dy2=2*dy;int dd2=2*(dy-dx);int x, y=y1;for (x=x1+1; x<=x2; ++x) {if (p<0) {glVertex2i(x, y);p+=dy2;}else {y+=incy;glVertex2i(x, y);p+=dd2;}}}else {if (y1>y2) {swap(x1, x2);swap(y1, y2);}int incx=(dx!=0?dx/(x2-x1):0);int p=2*dx-dy;int dx2=2*dx;int dd2=2*(dx-dy);int x=x1, y;for (y=y1+1; y<=y2; ++y) {if (p<0) {glVertex2i(x, y);p+=dx2;}else {x+=incx;glVertex2i(x, y);p+=dd2;}}}glEnd();}/* 中点圆算法*/void circle(int cx, int cy, int r) { int x=0, y=r;float p=1.25-r;while (x<=y) {glBegin(GL_POINTS);glVertex2i(cx+x, cy+y);glVertex2i(cx-x, cy+y);glVertex2i(cx-x, cy-y);glVertex2i(cx+x, cy-y);glVertex2i(cx+y, cy+x);glVertex2i(cx-y, cy+x);glVertex2i(cx-y, cy-x);glVertex2i(cx+y, cy-x);glEnd();if (p<0) {++x;p+=2*x+1;}else {p+=2*x-2*y+5;++x;--y;}}}void draw(){int i;for (i=0; i<ls.size(); ++i)line(ls[i][0], ls[i][1], ls[i][2], ls[i][3]);for (i=0; i<cs.size(); ++i)circle(cs[i][0], cs[i][1], cs[i][2]);if(mouse_type == 1)line(old_x, old_y, new_x, new_y);if(mouse_type == 3)circle(cx, cy, cr); //圆上的点和半径}void display() {glClear(GL_COLOR_BUFFER_BIT);glColor3i(0, 0, 0);draw();glutSwapBuffers();}void keyboard( unsigned char key, int x, int y) {switch (key) {case 27:exit (0);break;case 'a':case 'A':break;default:if (key<='z' && key>='a' || key<='Z' && key>='A' || key<='9' && key>='0'){ cout<<char(key);}else{cout<<"\n字符码为"<<static_cast<int>(key);display();}}}void mouseclick( int button, int state, int x, int y ){cout<<"("<<x<<","<<y<<")";switch ( button ) {case GLUT_LEFT_BUTTON:if ( state == GLUT_DOWN ) {cout<<"左键被按下";old_x = x;old_y = 600-y;mouse_type = 1;}else if ( state == GLUT_UP ) {cout<<"左键被松开";list l;l.push_back(old_x);l.push_back(old_y);l.push_back(x);l.push_back(600-y);ls.push_back(l);mouse_type = 0;glutPostRedisplay();for(int i=0; i<ls.size(); ++i)cout<<endl<<"line_"<<i<<": "<<ls[i][0]<<","<<ls[i][1]<<" "<<ls[i][2]<<","<<ls[i][3];}break;case GLUT_MIDDLE_BUTTON:break;case GLUT_RIGHT_BUTTON:if ( state == GLUT_DOWN ) {cout<<"右键被按下";cx = x;cy = 600-y;mouse_type = 3;}else if ( state == GLUT_UP ) {cout<<"右键被松开";list l;l.push_back(cx);l.push_back(cy);cr=int(sqrt(double(x-cx)*(x-cx)+(600-y-cy)*(600-y-cy))+0.5);l.push_back(cr);cs.push_back(l);mouse_type = 2;glutPostRedisplay();for(int i=0; i<cs.size(); ++i)cout<<endl<<"circle_"<<i<<": "<<cs[i][0]<<","<<cs[i][1]<<" "<<cs[i][2];}break;}}void screenmotion(int x, int y){if(mouse_type == 1){new_x = x;new_y = 600-y;cout<<endl<<"new-line:"<<old_x<<","<<old_y<<" "<<new_x<<","<<new_y<<endl; }else if(mouse_type == 3){cr=int(sqrt(double(x-cx)*(x-cx)+(600-y-cy)*(600-y-cy))+0.5);cout<<endl<<"new-circle: "<<cx<<","<<cy<<" "<<cr<<endl;}glutPostRedisplay();}void init() {glClearColor(0.2, 0.5, 0.8, 0.0); //背景颜色glMatrixMode(GL_PROJECTION);gluOrtho2D(-1.0, 600, 0.0, 601);}int main(int argc, char** argv) {glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);glutInitWindowPosition(100, 50); //图框在屏幕的位置glutInitWindowSize(600, 600); //边框大小glutCreateWindow("图元算法"); //图框边显示的字init();glutDisplayFunc(display);glutMotionFunc(screenmotion);glutKeyboardFunc( keyboard );glutMouseFunc( mouseclick );glutMainLoop();return 0;}。

计算机图形学opengl动画的实现源代码

计算机图形学opengl动画的实现源代码

实验目的:
在VC6.0的opengl的环境下,实现固定物体的连续变化,从而使简单动画得以实现。

实验思想:
由于是简单图形的变化过程,所以问题关键在于:
1、物体的位置变化如何实现,而位置变化在opengl中体现在坐标的变化中,因而设置坐标的每一步物体的位置变化情况是该问题的关键,此关键代码如下:
wAngleX += 1.0f;
wAngleY += 10.0f;
wAngleZ += 5.0f;
2、再有就是如何使得连续的变化得以实现,这就实现了手动变化相自动变化的转变,为了实现这一问题,我们想到设置一个变化时间,使得物体在每个时间段变换一次位置,当这个时间适当小时,就实现了物体的连续变化;这就需要引进一个计时器,通过设置间隔时间实现物体的连续变化,此关键代码如下:
SetTimer(1, 150, NULL);
实验步骤:
1、初始化OpenGL。

2、设置菜单与关联函数初始化,设置视口与窗口匹配,重新设置坐标系统。

3、建立正交变换下的剪切体(即变换物体)。

4、编写变换幅度函数,设置没不变换情况。

5、编写连续变换计时器,使得在一定间隔下连续变换。

实验关键代码:
实验结果:。

OpenGL编程实例

OpenGL编程实例

OpenGL程序实例2——绘制奥运五环
int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(400,400); glutInitWindowPosition(100,100); glutCreateWindow("OpenGL模型绘制函数示例"); glutDisplayFunc(Display); glutReshapeFunc(ChangeSize); Initial(); glutMainLoop(); return 0; }
OpenGL程序实例2——绘制奥运五环
glColor3f(0.0, 1.0, 0.0); glTranslatef(44.0, 0.0, 0.0); glutSolidTorus(0.5, 20.0, 15, 50); glColor3f(0.0, 0.0, 0.0); glTranslatef(-22.0, 30.0, 0.0); glutSolidTorus(0.5, 20.0, 15, 50); glColor3f(0.0, 0.0, 1.0); glTranslatef(-42.0, 0.0, 0.0); glutSolidTorus(0.5, 20.0, 15, 50); glColor3f(1.0, 0.0, 0.0); glTranslatef(84.0, 0.0, 0.0); glutSolidTorus(0.5, 20.0, 15, 50); glEndList(); //绘制绿色环
16
OpenGL程序实例1——绘制矩形
#include <gl/glut.h> void Initial(void) { glClearColor(1.0f, 1.0f, 1.0f, 1.0f); //设置窗口背景颜色为白色 glMatrixMode(GL_PROJECTION); //设置投影参数 gluOrtho2D(0.0,200.0,0.0,150.0); } void Display(void) { glClear(GL_COLOR_BUFFER_BIT); //用当前背景色填充窗口 glColor3f(1.0f, 0.0f, 0.0f); //设置当前的绘图颜色为红色 glRectf(50.0f, 100.0f, 150.0f, 50.0f); //绘制一个矩形 glFlush(); //处理所有的OpenGL程序 }

openGL构造教室

openGL构造教室

基于openGL的虚拟教室——虚拟现实与数据可视化课程作业董元22011207指导老师:孙立博老师仪器科学与工程学院2013年12月16日一、作业要求用openGL制作一个虚拟教室。

要求视角可变化,教室里的物品不少于五件。

二、完成情况我的openGL虚拟教室完成情况如下:1、绘制出的教室里的基本物体:黑板、讲台、投影仪、空调、门窗、音响、九张桌子、九张凳子等。

虚拟教室整体看上去接近真实生活中的教室。

2、添加了灯光和纹理效果:门窗以及空调均是用纹理贴出来的。

使教室看起来更加生动形象。

3、为教室加上了可用键盘控制的动态效果:可控制灯光亮灭、投影仪收起放下、六块黑板的上下滑动以及音响的开关。

4、引用FMOD音乐引擎,为教室加上可控背景音效(即3中的音响开关)。

5、在输出窗口添加操作提示信息,使界面更加人性化。

三、成果演示1、整体效果图2、改变视角效果3、开灯关灯对比效果图未开灯时:开灯时:4、投影仪放下效果图:5、黑板移动效果图6、局部细节效果图四、内容介绍下面我将从代码的角度简单介绍一下我的整个教室程序。

代码部分包括一个头文件(MyHeader.h)和一个cpp文件(main.cpp)。

为了增加程序的可读性,我将相关的变量定义、函数声明等集中放置在头文件下面。

在我的全部代码中,除了主函数外,共定义了19个子函数。

如下图所示:主程序部分和一般的openGL程序一样,完成初始化、窗口的绘制、显示回调函数和键盘以及一些特殊事件的响应。

主函数如下:初始化部分由函数init()完成,而init中又调用了进行灯光初始化的函数initlight()以及进行纹理图像载入的LoadTexture1()、LoadTexture2()、LoadTexture3()。

这主要是因为灯光初始化和载入纹理相关代码较多,这么做是为了使代码看上去更加层次分明绘制教室的工作由回调函数display()完成。

而所有子函数中与绘制教室相关的子函数还有DrawRoom()、DrawDesk()、DrawBlackboard()以及Drawotherthings();分别完成教室墙面、黑板、桌椅及其他物体的绘制。

opengl绘制教室

opengl绘制教室

OpenGL绘制虚拟教室姓名:王璞学号:220123132014年12月12日一、程序说明1.1项目要求①设计一个虚拟教室;②要求有四种物体以上;③具有光照、材质;④可以实现场景的巡游;1.2图形项目说明①绘制出的教室包含有黑板、时钟、讲台、课桌板凳、风扇、灯、门窗等基本物体,基本做到了对现实中教室的还原;②添加了灯光和纹理效果:地面、门窗、四面墙体、天花板及灯均使用了纹理贴图,使教室看起来更加生动形象;③为教室加上了时钟走时的动态效果,以及键盘交互功能,按键L可控制灯的开关,按键F可控制电风扇的转停;④在场景漫游方面:按键W控制前进、按键S控制后退、按键A控制左平移、按键D控制右平移、按键PagUp控制上移、按键PagDown控制下移、按键上控制y轴方向逆时针转动,按键下控制y轴方向顺时针转动,按键左控制x轴方向逆时针转动,按键右控制x轴方向顺时针转动共10个方向的漫游功能。

初始视角在位于教室最后方的中心线处。

1.3参考资料①/nehe NeHe OpenGL教程中文版②/nonkicat/archive/2013/03/28/2987125.html博客园[OpenGL]二次曲面图③/s/blog_4735d8eb010002t6.html vera的博客二、应用软件的总体设计2.1 需求规定输入:键盘的“←”、“↑”、“→”和“↓”、PagUp、PagDown、W、S、A、D。

输出:显示屏。

基本图形建模:工具库:四边形、立方体,球。

实用库:二次曲面。

使用旋转、平移、放大缩小等方法变换坐标矩阵,组成相应物体。

使用纹理和光照模型,完善场景外观。

其它处理:工具库:创建子窗口,键盘响应函数。

核心库:光栅位图。

Windows相关:读BMP位图作为纹理数据2.2 运行环境开发环境:Visual C++ 6.0 Console运行环境:Windows 8头文件:windows.h, glut.h, stdio.h , glaux.h, math.h, stdlib.h, time.h, iostream.h动态链接库:glu.dll, glu32.dll, glut32.dll和opengl32.dll静态链接库:glu32.lib, glut32.lib, opengl32.lib2.3 基本设计概念∙系统的初始化①窗口初始化②材质、光照∙对象模型的建立①房间墙体、门、窗、灯、黑板的绘制房间六面墙体、门、窗、灯和黑板的绘制均是通过画四边形的方式实现的,函数为glBegin(GL_QUADS),而其纹理则是按照由glBindTexture( )函数选择纹理、glTexCoord2f(1.0f, 1.0f);glVertex3f(-50.0f, -21.0f, -80.0f)函数将纹理坐标赋给顶点的方式完成纹理映射来实现的,其材质光照则是通过glMaterialfv( )函数完成。

opengl教程

opengl教程

opengl教程
OpenGL是一种用于图形处理的开放式图形库。

它提供了一套
函数接口,可以在不同的平台上进行图形编程,并且可以利用图形硬件加速来实现高性能的图形渲染。

OpenGL教程是一种指导用户学习和使用OpenGL的资料。


些教程通常会包含一些基础知识,例如如何初始化OpenGL上下文,以及如何创建和绘制基本的几何图形。

此外,教程还会介绍一些高级技术,例如纹理映射、光照和阴影等。

初学者可以通过阅读和完成这些教程来快速上手OpenGL编程。

教程通常会提供一些示例代码,让学习者可以直接运行和尝试,并且每个步骤都会有详细的说明和解释。

通过实际操作,学习者可以更好地理解OpenGL的概念和原理。

一般来说,OpenGL教程可以按照难度和内容的不同分为多个
部分。

初级教程主要介绍OpenGL的基本概念和操作,例如如何绘制简单的几何图形和设置基本的材质和光照。

中级教程则会介绍一些常用的高级技术,例如纹理映射、着色器编程和渲染优化等。

高级教程则会涉及一些更复杂和专业的主题,例如体积渲染、GPU计算和图形学算法等。

OpenGL教程是学习和掌握OpenGL编程的重要资源。

通过学
习这些教程,不仅可以获得对OpenGL的全面理解,还可以提高图形编程的能力和技巧。

无论是想要从事图形相关的工作,还是对图形编程感兴趣的爱好者,都可以通过学习OpenGL教程来提升自己的技术水平。

OpenGL编程 GLUT

OpenGL编程 GLUT

第一章OpenGL的基本框架1.1OpenGL简介在计算机发展初期,人们就开始从事计算机图形的开发,但直到20世纪80年代末90年代初,三维图形才开始迅速发展。

于是各种三维图形工具软件包相继推出,如GL,RenderMan等,但没有一种软件包能够在三维图形建模能力和编程方便程度上与OpenGL 相比拟。

OpenGL(Open Graphics Library,开放图形库),是一个三维的计算机图形和模型库,它源于SGI公司为其图形工作站开发的IRIS GL,在跨平台移植过程中发展成为OpenGL。

SGI公司在1992年6月发布1.0版,后成为工业标准。

目前,OpenGL标准由1992年成立的独立财团OpenGL Architecture Review Board(ARB)以投票方式产生,并制成规范文档公布,各软硬件厂商据此开发自己系统上的实现。

目前最新版规范是1999年5月通过的1.2.1。

OpenGL作为一个性能优越的图形应用程序设计界面(API),它独立于硬件和窗口系统,在运行各种操作系统的各种计算机上都可用,并能在网络环境下以客户/服务器模式工作,是专业图形处理、科学计算等高端应用领域的标准图形库。

OpenGL在军事、广播电视、CAD/CAM/CAE、娱乐、艺术造型、医疗影像、虚拟世界等领域都有着广泛的应用。

它具有以下功能。

1. 模型绘制OpenGL能够绘制点、线和多边形。

应用这些基本的形体,可以构造出几乎所有的三维模型。

OpenGL通常用模型的多边形的顶点来描述三维模型。

2. 模型观察在建立了三维景物模型后,就需要用OpenGL描述如何观察所建立的三维模型。

观察三维模型是通过一系列的坐标变换进行的。

模型的坐标变换在使观察者能够在视点位置观察与视点相适应的三维模型景观。

在整个三维模型的观察过程中,投影变换的类型决定观察三维模型的观察方式,不同的投影变换得到的三维模型的景象也是不同的。

最后的视窗变换则对模型的景象进行裁剪缩放,即决定整个三维模型在屏幕上的图象。

OpenGl大作业

OpenGl大作业

八大行星与太阳代码:#include<glut.h>#include<math.h>// Lighting valuesGLfloat whiteLight[] = { 0.2f, 0.2f, 0.2f, 1.0f }; GLfloat sourceLight[] = { 0.8f, 0.8f, 0.8f, 1.0f }; GLfloat lightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };// Earth and Moon angle of revolutionGLfloat fMoonRot = 10.0f;GLfloat fEarthRot = 29.786f;GLfloat fMercuryRot = 47.874f;GLfloat fVenusRot = 35.024f;GLfloat fMarsRot = 24.131f;GLfloat fJupiterRot = 13.059f;GLfloat fSaturnRot = 9.644f;GLfloat fUranusRot = 6.799f;GLfloat fNeptuneRot = 5.433f;void Neptune(){glColor3ub(162,56,90);glutSolidSphere(22.0f,30,30);}void Uranus(){glColor3ub(146,48,47);glutSolidSphere(22.5f,30,30);}void Saturn(){glColor3ub(100,190,59);glutSolidSphere(29.0f,30,30);}void Jupiter(){glColor3ub(49,100,112);glutSolidSphere(30.0f,30,30);}void Mars(){glColor3ub(40,90,10);glutSolidSphere(7.50f,30,30);}void Venus(){glColor3ub(46,112,12);glutSolidSphere(7.0f,30,30);}void Mercury(){glColor3ub(18,46,19);glutSolidSphere(5.0f,30,30);}void Moon(){glColor3ub(0,200,0);glutSolidSphere(5.0f,30,30);}void Earth(){glColor3ub(46,10,0);glutSolidSphere(15.0f,30,30);glPushMatrix();glRotatef(fMoonRot,0.0f,1.0f,0.0f);glTranslatef(25.0f,0.0f,0.0f);Moon();glPopMatrix();}void Solar(){glColor3ub(255,63,0);glutSolidSphere(40.0f,30,30);glPushMatrix();glRotatef(fMercuryRot,0.0f,1.0f,0.09f); glTranslatef(48.50f,0.0f,0.0f); Mercury();glPopMatrix();glPushMatrix();glRotatef(fVenusRot,0.0f,1.0f,0.07f); glTranslatef(54.0f,0.0f,0.0f);Venus();glPopMatrix();glPushMatrix();glRotatef(fEarthRot,0.0f,1.0f,0.05f); glTranslatef(69.50f,0.0f,0.0f);Earth();glPopMatrix();glPushMatrix();glRotatef(fMarsRot,0.0f,1.0f,0.09f); glTranslatef(118.50f,0.0f,0.0f);Mars();glPopMatrix();glPushMatrix();glRotatef(fJupiterRot,0.0f,1.0f,.06f); glTranslatef(778.0f/2,0.0f,0.0f); Jupiter();glPopMatrix();glPushMatrix();glRotatef(fSaturnRot,0.0f,1.0f,0.12f); glTranslatef(1426.0f/6,0.0f,0.0f); Saturn();glPopMatrix();glPushMatrix();glRotatef(fUranusRot,0.0f,1.0f,0.08f); glTranslatef(2870.0f/6,0.0f,0.0f); Uranus();glPopMatrix();glPushMatrix();glRotatef(fNeptuneRot,0.0f,1.0f,0.14f);glTranslatef(4497.0f/6,0.0f,0.0f);Neptune();glPopMatrix();}// Called to draw scenevoid display(void){glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glPushMatrix();glTranslatef(0.0f,0.0f,-400.0f);Solar();glPopMatrix();glutSwapBuffers();}// This function does any needed initialization on the rendering// context.void init(){// Light values and coordinatesglEnable(GL_DEPTH_TEST); // Hidden surface removalglFrontFace(GL_CCW); // Counter clock-wise polygons face out glEnable(GL_CULL_FACE); // Do not calculate inside of jet// Enable lightingglEnable(GL_LIGHTING);// Setup and enable light 0glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);glLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);glLightfv(GL_LIGHT0,GL_POSITION,lightPos);glEnable(GL_LIGHT0);// Enable color trackingglEnable(GL_COLOR_MATERIAL);// Set Material properties to follow glColor values glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);// Black blue backgroundglClearColor(0.0f, 0.0f, 0.0f, 1.0f );}void move(void){fMoonRot+= 0.50f;if(fMoonRot > 360.0f)fMoonRot = 0.0f;// Step earth orbit .25 degreesfEarthRot += .29786f;if(fEarthRot > 360.0f)fEarthRot = 0.0f;fMercuryRot += .47874f;if(fMercuryRot > 360.0f)fMercuryRot = 0.0f;fVenusRot += .35024f;if(fVenusRot > 360.0f)fVenusRot = 0.0f;fMarsRot += .24131f;if(fMarsRot > 360.0f)fMarsRot = 0.0f;fJupiterRot += .13059f;if(fJupiterRot > 360.0f)fJupiterRot = 0.0f;fSaturnRot += .09644f;if(fSaturnRot > 360.0f)fSaturnRot = 0.0f;fUranusRot += .06799f;if(fUranusRot > 360.0f)fUranusRot = 0.0f;fNeptuneRot += .05433f;if(fNeptuneRot > 360.0f)fNeptuneRot = 0.0f;glutPostRedisplay();}void reshape(int w, int h){GLfloat fAspect;// Prevent a divide by zeroif(h == 0)h = 1;// Set Viewport to window dimensionsglViewport(0, 0, w, h);// Calculate aspect ratio of the windowfAspect = (GLfloat)w/(GLfloat)h;// Set the perspective coordinate systemglMatrixMode(GL_PROJECTION);glLoadIdentity();// field of view of 90 degrees, near and far planes 1.0 and 425 gluPerspective(90.0f, fAspect, 1.0, 1000.0);// Modelview matrix resetglMatrixMode(GL_MODELVIEW);glLoadIdentity();}void InitAWindow(int argc, char **argv){glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);glutInitWindowSize(800,800);glutCreateWindow("Sun System:the eight and the one");return;}int main(int argc, char* argv[]){InitAWindow(argc, argv);glutReshapeFunc(reshape);glutDisplayFunc(display); glutIdleFunc(move);init();glutMainLoop();return 0;}。

opengl教程

opengl教程

opengl教程OpenGL是一种图形编程接口,常用于计算机图形学和游戏开发。

本文将介绍OpenGL的基本概念、功能和使用方法。

首先,OpenGL是一种跨平台的图形编程接口,可以在不同操作系统和硬件上运行。

它是一个开放标准,由Khronos Group维护和发展,因此可以在各种平台上使用,如Windows、MacOS、Linux等。

OpenGL的核心是图形渲染管线。

图形渲染管线是指一系列的图形处理阶段,用于将3D图形数据转化为2D图像。

这些阶段包括几何处理、光栅化、片元处理等。

每个阶段都包含了一些特定的操作和功能,通过这些操作和功能,我们可以实现各种不同的图形效果和渲染技术。

在使用OpenGL之前,需要初始化OpenGL的上下文,并创建一个OpenGL窗口。

通过OpenGL的API(Application Programming Interface),我们可以控制各个渲染阶段的操作和参数。

例如,我们可以设置物体的位置、颜色、纹理等属性,还可以控制光照、深度测试等渲染参数。

OpenGL还提供了一些基本的几何图形绘制函数,如绘制点、线段、三角形等。

通过这些函数,我们可以绘制各种基本的几何图形。

此外,OpenGL还支持纹理映射、着色器编程等高级渲染技术,可以实现更加复杂的视觉效果。

在OpenGL中,最常用的是顶点数组和顶点缓冲对象。

顶点数组用于存储顶点的位置、颜色、纹理坐标等属性,而顶点缓冲对象用于管理顶点数组的内存。

通过顶点数组和顶点缓冲对象,我们可以高效地传输大量的顶点数据到显存,并在图形渲染管线中使用。

除了基本的图形绘制,OpenGL还支持一些高级的渲染技术,如光照、阴影、深度测试等。

这些技术可以让我们实现更加逼真和真实感的图形效果。

例如,通过光照技术,我们可以模拟不同光源的光照效果,使物体看起来更加立体和有质感。

而深度测试可以确保正确的渲染顺序,使得物体之间的遮挡关系得到正确的呈现。

总结一下,OpenGL是一种功能丰富且强大的图形编程接口。

实验1__OpenGL基本图形绘制[整理版]

实验1__OpenGL基本图形绘制[整理版]

实验一OpenGL基本图形绘制一、实验目的和要求:1. 了解VC++环境下,OpenGL图形绘制的程序结构;2. 通过编程实现简单二维图形的绘制;3. 掌握理解简单的OpenGL程序结构;掌握OpenGL提供的基本图形函数,尤其是生成点、线、面的函数4. 掌握图形显示原理。

二、实验内容:1、OpenGL在VC++下的安装和配置2、读懂给定的示例程序,使用Visual C++ 6.0和OpenGL编写几个简单图形的绘制程序。

3、建议有能力的学生进一步学习OpenGL的复杂功能创建效果更好的三维图形;三、实验步骤:1、初步了解OpenGL的功能以及语法特点;2、下载glut包,复制 glut32.dll到winnt/system32文件夹下,复制glut32.lib到vc98/lib文件夹下,复制glut.h到vc98/include/gl文件夹下;3、创建VC工程:选择菜单File中的New选项,弹出一个分页的对话框,选中页Projects中的Win32 Console Application项,然后填入你自己的Project name,如Test,回车即可。

VC为你创建一个工作区(WorkSpace),你的项目Test就放在这个工作区里;4、为项目添加文件:为了使用OpenGL,我们需要在项目中加入三个相关的Lib文件:glu32.lib、glut32.lib、opengl32.lib,这三个文件位于VC安装目录下的lib目录(如c:\program files\ devstudio\vc\lib)中。

选中菜单Project->Add ToProject->Files项(或用鼠标右键),把这三个文件加入项目,在FileView中会有显示。

这三个文件请务必加入,否则编译时会出错。

或者将这三个文件名添加到Project->Setting->Link-> Object /library Modules 即可。

opengl源代码

opengl源代码

GLubyte fire[128] = { 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xc0,
0x00, 0x00, 0x01, 0xf0,
0x00, 0x00, 0x07, 0xf0,
0x0f, 0x00, 0x1f, 0xe0,
glPushMatrix();
glRotatef(xRot,1.0f,0.0f,0.0f);
glRotatef(yRot,0.0f,1.0f,0.0f);
glPointSize(5);
glLineWidth(5);
if(bWinding)
glFrontFace(GL_CW);
if(yRot>356.0f) yRot=0.0f;
if(yRot<-1.0f) yRot=355.0f;
glutPostRedisplay();
}
void ProcessMenu(int value)
{
switch(value)
{
case 1: bDepth=!bDepth;break;
#include <gl/glut.h>
static GLfloat xRot=0.0f;
static GLfloat yRot=0.0f;
GLboolean bWinding=1;

c语言opengl代码

c语言opengl代码

c语言opengl代码使用C语言编写OpenGL代码OpenGL是一种跨平台的图形库,可以用于创建2D和3D图形。

它是一种开放源代码的库,可以在多种操作系统上使用,包括Windows、Linux和Mac OS X等。

在本文中,我们将介绍如何使用C语言编写OpenGL代码。

我们需要安装OpenGL库。

在Windows上,我们可以使用Mingw-w64或Visual Studio等编译器来安装OpenGL库。

在Linux上,我们可以使用GCC编译器来安装OpenGL库。

在Mac OS X上,我们可以使用Xcode编译器来安装OpenGL库。

一旦我们安装了OpenGL库,我们就可以开始编写OpenGL代码了。

下面是一个简单的OpenGL程序,它创建了一个窗口,并在窗口中绘制了一个三角形。

```c#include <GL/glut.h>void display(){glClear(GL_COLOR_BUFFER_BIT);glBegin(GL_TRIANGLES);glColor3f(1.0, 0.0, 0.0);glVertex2f(-0.5, -0.5);glColor3f(0.0, 1.0, 0.0);glVertex2f(0.5, -0.5);glColor3f(0.0, 0.0, 1.0);glVertex2f(0.0, 0.5);glEnd();glFlush();}int main(int argc, char** argv){glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500);glutCreateWindow("OpenGL Window");glutDisplayFunc(display);glutMainLoop();return 0;}```在这个程序中,我们使用了GLUT库来创建窗口和处理事件。

计算机图形学实验2------OPengl绘制3D图形

计算机图形学实验2------OPengl绘制3D图形

实验二 OPengl绘制3D图形一、实验目的➢了解和学习OpengL的编程➢通过学习掌握OpenGL 基本图元的绘制,进行3D图形的绘制➢掌握基于 Win32、Visual C++环境绘制3D 图形配置过程、绘制原理二、环境配置1)创建一个工程。

2)链接OpenGL libraries。

在Visual C++中先单击Project,再单击Settings,再找到Link单击,最后在Object/library modules 的最前面加上opengl32.lib glu32.lib glut.lib g laux.lib gdi32.lib user32.lib advapi32.lib或者在project -> add to project ->files引入需要的头文件3)单击Project Settings中的C/C++标签,将Preprocessor definitions 中的_CONSOLE 改为__WINDOWS。

最后单击OK。

三、实验关键代码void CCMyOpenGLView::RenderScene(){glLoadIdentity();glTranslatef(m_xPos,m_yPos,-5.0f);glRotatef(m_xAngle,1.0f,0.0f,0.0f);glRotatef(m_yAngle,0.0f,1.0f,0.0f);glutWireCone(1.0f,2.0,20,20);}bool CCMyOpenGLView::InitializeOpenGL(){m_pDC = new CClientDC(this);if(m_pDC == NULL){MessageBox("Error Obtaining DC");return FALSE;}if(!SetupPixelFormat()){return FALSE;}m_hRC = ::wglCreateContext (m_pDC->GetSafeHdc ());if(m_hRC == 0){MessageBox("RC创建失败!");return FALSE;}if(::wglMakeCurrent (m_pDC->GetSafeHdc (), m_hRC)==FALSE) {MessageBox("设置当前RC失败!");return FALSE;}::glClearColor(0.0f,0.0f,0.0f,0.0f);::glClearDepth(1.0f);::glEnable(GL_DEPTH_TEST);return TRUE;}bool CCMyOpenGLView::SetupPixelFormat(){static PIXELFORMATDESCRIPTOR pfd ={sizeof(PIXELFORMATDESCRIPTOR),1, PFD_DRAW_TO_WINDOW |PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,PFD_TYPE_RGBA, 24, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 16, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0};int m_nPixelFormat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);if ( m_nPixelFormat == 0 ){return FALSE;}if(::SetPixelFormat(m_pDC->GetSafeHdc(),m_nPixelFormat,&pfd)==FALSE){return FALSE;}return TRUE;}int CCMyOpenGLView::OnCreate(LPCREATESTRUCT lpCreateStruct) {if (CView::OnCreate(lpCreateStruct) == -1)return -1;InitializeOpenGL();return 0;}void CCMyOpenGLView::OnDestroy(){CView::OnDestroy();if(::wglMakeCurrent (0,0) == FALSE)MessageBox("Could not make RC non-current");if(::wglDeleteContext (m_hRC)==FALSE)MessageBox("Could not delete RC");if(m_pDC)delete m_pDC;m_pDC = NULL;}BOOL CCMyOpenGLView::OnEraseBkgnd(CDC* pDC){return TRUE;}void CCMyOpenGLView::OnSize(UINT nType, int cx, int cy){CView::OnSize(nType, cx, cy);GLdouble aspect_ratio;if ( 0 >= cx || 0 >= cy )return;::glViewport(0, 0, cx, cy);aspect_ratio = (GLdouble)cx/(GLdouble)cy;::glMatrixMode(GL_PROJECTION);::glLoadIdentity();::gluPerspective(45.0f, aspect_ratio, .01f, 200.0f);::glMatrixMode(GL_MODELVIEW);::glLoadIdentity();}void CCMyOpenGLView::OnLButtonDown(UINT nFlags, CPoint point) {m_MouseDownPoint=point;SetCapture();CView::OnLButtonDown(nFlags, point);}void CCMyOpenGLView::OnLButtonUp(UINT nFlags, CPoint point) {m_MouseDownPoint=CPoint(0,0);ReleaseCapture();CView::OnLButtonUp(nFlags, point);}void CCMyOpenGLView::OnMouseMove(UINT nFlags, CPoint point) {if (GetCapture()==this){m_xAngle+=(point.y-m_MouseDownPoint.y)/3.6;m_yAngle+=(point.x-m_MouseDownPoint.x)/3.6;InvalidateRect(NULL,FALSE);m_MouseDownPoint=point;};CView::OnMouseMove(nFlags, point);}void CCMyOpenGLView::OnObjectsCone(){m_bTeapot=FALSE;m_bSphere=FALSE;m_bCone=TRUE;InvalidateRect(NULL,FALSE);}四、实验结果五、心得体会通过学习Opengl编程,实现三维立体图形的绘制,了解了设置像素格式,即OpenGL怎样操作像素、创建着色描述表并当前化着色表、删除着色表,响应WM_DESTROY消息响应WM_CREATE消息等等,函数实现了解了Opengl实用工具集,它体现了几乎所有现代窗口系统所共有的功能的函数库。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

wp.h#include <windows.h>// Windows的头文件#include <glut.h>// 包含OpenGL实用库#include<stdio.h>// 标准输入/输出库的头文件#include<glaux.h>// GLaux库的头文件#include<math.h>#include <stdlib.h>#include <time.h>#include <iostream.h>//..................................定义变量区............................................................////.............................用于场景漫游有关变量........................................//GLfloat xrot=0.0f;// X 旋转量GLfloat yrot=0.0f;// Y 旋转量GLfloat x=0.0f;// X 平移量GLfloat y=0.0f;// Y 平移量GLfloat z=-40.0f;// 深入屏幕的距离//.............................用于键盘操作有关变量.......................................//BOOL light=TRUE;// 光源的开/关BOOL lp;// L键按下了么?BOOL fp;// F键按下了么?BOOL TurnOn=FALSE; // 开灯GLUquadricObj *qobj;int fantheta=0; // 风扇转过角度HGLRC hRC=NULL;// 窗口着色描述表句柄HDC hDC=NULL;// OpenGL渲染描述表句柄HWND hWnd=NULL;// 保存我们的窗口句柄HINSTANCE hInstance;// 保存程序的实例bool keys[256];// 保存键盘按键的数组bool active=TRUE;// 窗口的活动标志,缺省为TRUEbool fullscreen=TRUE;// 全屏标志缺省,缺省设定成全屏模式//.................................光照材质有关变量......................................//GLfloat light_Ambient[]= { 0.1f, 0.1f, 0.1f, 1.0f }; // 环境光参数GLfloat light_Diffuse[]= { 1.0f, 1.0f, 0.45f, 1.0f };// 漫射光参数GLfloat light_position[]={0.0,29.9,0.0,1.0}; // 光源位置GLfloat light_position1[]={20.0,29.9,0.0,1.0};GLfloat mat_ambient[]={0.05f,0.05f,0.05f,1.0f}; // 材质环境光参数GLfloat mat_specular[]={0.8,1.0,1.0,1.0}; // 材质镜面光参数GLfloat mat_shininess[]={5.0};GLfloat white_light[]={1.0,1.0,1.0,1.0}; // 光GLfloat no_mat[]= {0.0f, 0.0f, 0.0f, 1.0f};// 无材质色GLfloat no_shininess[]= {0.0f};// 无反光GLfloat mat_diffuse[]= {1.0f, 1.0f, 1.0f, 1.0f};// 漫反射GLfloat diffusion[]={0.521f,0.121f,0.0547f,1.0};//......................................纹理...........................................................//GLuint texture[6];// 7种纹理的储存空间//.....................................时钟有关变量...................................................//GLfloat cx=35.0,cy=15.0,cz=-79.9,radius=5.0;GLfloat hradius=2.0,mradius=3.0,sradius=4.0;GLfloat sangle,mangle,hangle;GLuint second,miniter,hour;char timebuffer[9];LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);// WndProc的定义wp.cpp#include"wp.h"//......................................创建纹理..........................................................//AUX_RGBImageRec *LoadBMP(char *Filename)// 载入位图图象{FILE *File=NULL;// 文件句柄if (!Filename)// 确保文件名已提供{return NULL;// 如果没提供,返回NULL}File=fopen(Filename,"r");// 尝试打开文件if (File)// 文件存在{fclose(File);// 关闭句柄return auxDIBImageLoad(Filename);// 载入位图并返回指针}return NULL;}// 如果载入失败,返回NULLint LoadGLTextures()// 载入位图并转换成纹理{int Status=FALSE;// 状态指示器AUX_RGBImageRec *TextureImage[6];// 创建纹理的存储空间memset(TextureImage,0,sizeof(void *)*1);// 将指针设为NULL// 载入位图,检查有无错误,如果位图没找到则退出Status=TRUE;// 将Status 设为TRUE//...............................创建第一个纹理,地板.......................................//if (TextureImage[0]=LoadBMP("floor.bmp")){glGenTextures(1, &texture[0]);// 创建纹理glBindTexture(GL_TEXTURE_2D, texture[0]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);//绑定纹理}if (TextureImage[0])//纹理是否存在{if (TextureImage[0]->data)//纹理图像是否存在{free(TextureImage[0]->data);//释放纹理图像占用的内存}free(TextureImage[0]);//释放图像结构//...............................创建第二个纹理,墙.......................................//if (TextureImage[1]=LoadBMP("wall.bmp")){glGenTextures(1, &texture[1]); //创建纹理glBindTexture(GL_TEXTURE_2D, texture[1]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);//绑定纹理}if (TextureImage[1])//纹理是否存在{if (TextureImage[1]->data)//纹理图像是否存在{free(TextureImage[1]->data);//释放纹理图像占用的内存}free(TextureImage[1]);//释放图像结构}//...............................创建第三个纹理,窗户.......................................//if (TextureImage[2]=LoadBMP("glass.bmp")){glGenTextures(1, &texture[2]);//创建纹理glBindTexture(GL_TEXTURE_2D, texture[2]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[2]->sizeX, TextureImage[2]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[2]->data);//绑定纹理}if (TextureImage[2])//纹理是否存在{if (TextureImage[2]->data)//纹理图像是否存在{free(TextureImage[2]->data);//释放纹理图像占用的内存}free(TextureImage[2]);//释放图像结构//...............................创建第四个纹理,门.......................................//if (TextureImage[3]=LoadBMP("door.bmp")){glGenTextures(1, &texture[3]);//创建纹理glBindTexture(GL_TEXTURE_2D, texture[3]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[3]->sizeX, TextureImage[3]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[3]->data);//绑定纹理}if (TextureImage[3])//纹理是否存在{if (TextureImage[3]->data)//纹理图像是否存在{free(TextureImage[3]->data);//释放纹理图像占用的内存}free(TextureImage[3]);//释放图像结构}//...............................创建第五个纹理,灯.......................................//if (TextureImage[4]=LoadBMP("lamp.bmp")){Status=TRUE;glGenTextures(1, &texture[4]);//创建纹理glBindTexture(GL_TEXTURE_2D, texture[4]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[4]->sizeX, TextureImage[4]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[4]->data);//绑定纹理}if (TextureImage[4])//纹理是否存在{if (TextureImage[4]->data)//纹理图像是否存在{free(TextureImage[4]->data);//释放纹理图像占用的内存}free(TextureImage[4]);//释放图像结构}//...............................创建第六个纹理,黑板.......................................//if (TextureImage[5]=LoadBMP("blackwall.bmp")){glGenTextures(1, &texture[5]);//创建纹理glBindTexture(GL_TEXTURE_2D, texture[5]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[5]->sizeX, TextureImage[5]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[5]->data);//绑定纹理}if (TextureImage[5])//纹理是否存在{if (TextureImage[5]->data)//纹理图像是否存在{free(TextureImage[5]->data);//释放纹理图像占用的内存}free(TextureImage[5]);//释放图像结构}return Status;}//...................................重置OpenGL窗口大小..............................................//GLvoid ReSizeGLScene(GLsizei width, GLsizei height)// 重置OpenGL窗口大小{if (height==0)// 防止被零除{height=1;// 将Height设为1 }glViewport(0, 0, width, height);// 重置当前的视口glMatrixMode(GL_PROJECTION);// 选择投影矩阵glLoadIdentity();// 重置投影矩阵// 设置视口的大小gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,1000.0f);glMatrixMode(GL_MODELVIEW);// 选择模型观察矩阵glLoadIdentity();// 重置模型观察矩阵}//...................................初始化操作................................................//int InitGL(GLvoid)// 此处开始对OpenGL进行所有设置{if (!LoadGLTextures())// 调用纹理载入子例程{return FALSE;// 如果未能载入,返回FALSE}glEnable(GL_TEXTURE_2D);// 启用纹理映射glShadeModel(GL_SMOOTH);// 启用阴影平滑glClearColor(0.0f, 0.0f, 0.0f, 0.0f);// 黑色背景glClearDepth(1.0f);// 设置深度缓存glEnable(GL_DEPTH_TEST);// 启用深度测试glDepthFunc(GL_LEQUAL);// 所作深度测试的类型glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// 告诉系统对透视进行修正glLightModelfv(GL_LIGHT_MODEL_AMBIENT,mat_ambient);glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE);glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);glLightfv(GL_LIGHT1,GL_POSITION,light_position); // 设置光源位置glLightfv(GL_LIGHT1,GL_AMBIENT,light_Ambient); // 设置环境光glLightfv(GL_LIGHT1,GL_DIFFUSE,white_light); // 设置漫射光glLightfv(GL_LIGHT1,GL_SPECULAR,white_light);glEnable(GL_LIGHTING);glEnable(GL_LIGHT1);// 启用一号光源glEnable(GL_COLOR_MATERIAL);glPixelStorei(GL_UNPACK_ALIGNMENT,1); //对齐像素字节函数return TRUE;// 初始化OK}//..............................................开始绘制..........................................//int DrawGLScene(GLvoid)// 从这里开始进行所有的绘制{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// 清除屏幕和深度缓存glLoadIdentity();// 重置当前的模型观察矩阵glTranslatef(x,y,z); //沿X,Y,Z轴移动glRotatef(xrot,1.0f,0.0f,0.0f);// 绕X轴旋转glRotatef(yrot,0.0f,1.0f,0.0f);// 绕Y轴旋转glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat); //选择材质光照glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);//.............................................房间........................................................////.............................................地板........................................................//glBindTexture(GL_TEXTURE_2D, texture[0]);// 选择纹理glBegin(GL_QUADS); //开始绘制四边形glColor3f(1.0f, 1.0f, 1.0f);glNormal3f(0.0,1.0,0.0); //glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f(-50.0f, -21.0f, -80.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f(-50.0f, -21.0f, 40.0f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( 50.0f, -21.0f, 40.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f( 50.0f, -21.0f, -80.0f);//左上glEnd();//.............................................天花板......................................................//glBindTexture(GL_TEXTURE_2D, texture[1]);// 选择纹理glBegin(GL_QUADS);//开始绘制四边形glColor3f(1.0f, 1.0f, 1.0f);glNormal3f(0.0f, -1.0f, 0.0f);//glNormal3f用于定义点的法向量glTexCoord2f(0.0f, 0.0f);glVertex3f(-50.0f, 30.0f, 40.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f(-50.0f, 30.0f, -80.0f);//左上glTexCoord2f(1.0f, 1.0f);glVertex3f( 50.0f, 30.0f, -80.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( 50.0f, 30.0f, 40.0f);//右下glEnd();//............................................后墙面.......................................................//glBegin(GL_QUADS);//开始绘制四边形glColor3f(1.0f, 1.0f, 1.0f);glNormal3f(0.0f, 0.0f, 1.0f);//glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f(-50.0f, 30.0f, -80.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f(-50.0f, -21.0f, -80.0f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( 50.0f, -21.0f, -80.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f( 50.0f, 30.0f, -80.0f);//左上//............................................前墙面.......................................................//glNormal3f(0.0f, 0.0f, -1.0f);//glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f( 50.0f, 30.0f, 40.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( 50.0f, -21.0f, 40.0f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f(-50.0f, -21.0f, 40.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f(-50.0f, 30.0f, 40.0f);//左上//............................................左墙面.......................................................//glNormal3f( 1.0f, 0.0f, 0.0f);//glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f(-50.0f, 30.0f, 40.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f(-50.0f, -21.0f, 40.0f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f(-50.0f, -21.0f, -80.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f(-50.0f, 30.0f, -80.0f);//左上//............................................右墙面.......................................................//glNormal3f(-1.0f, 0.0f, 0.0f);//glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f( 50.0f, 30.0f, -80.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( 50.0f, -21.0f, -80.0f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( 50.0f, -21.0f, 40.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f( 50.0f, 30.0f, 40.0f);//左上glEnd();//.............................................桌椅..........................................................//GLuint i=0;for(;i<5;i++)//一共五排桌椅{//............................................第一张桌椅......................................................////..........................桌子的上面.......................//glPushMatrix();//因为下面要对坐标进行变换,所以先将信息入栈保护,使整个工程的坐标不发生改变glColor3f(1.0,1.0,0.45);glTranslatef(30.0f,-10.0f,-30.0+i*9);//平移变换glScalef(13.0f,0.3f,5.0f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//..........................桌子的下面......................//glPushMatrix();//压入堆栈glColor3f(1.0,1.0,0.45);glTranslatef(30.0f,-13.0f,-30.0+i*9);//平移变换glScalef(11.0f,0.3f,5.0f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//...........................桌子的后面......................//glPushMatrix();//压入堆栈glColor3f(0.2,0.8,0.8);glTranslatef(30.0f,-12.0f,-32.5f+i*9);//平移变换glScalef(11.0f,3.6f,0.3f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//...........................桌子的左边桌腿...................//glPushMatrix();//压入堆栈glColor3f(0.0,0.0,0.0);glTranslatef(25.0f,-17.0f,-32.5f+i*9);//平移变换glScalef(0.5f,8.0f,0.3f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//...........................桌子的右边桌腿...................//glPushMatrix();//压入堆栈glColor3f(0.0,0.0,0.0);glTranslatef(35.0f,-17.0f,-32.5f+i*9);//平移变换glScalef(0.5f,8.0f,0.3f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//............................椅子............................//glPushMatrix();//压入堆栈glColor3f(0.2,0.8,0.8);glTranslatef(30.0f,-15.0f,-25.0+i*9);//平移变换glScalef(11.3f,0.3f,3.5f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//.....................................................第二张桌椅....................................................// //..........................桌子的上面.......................//glPushMatrix();glColor3f(1.0,1.0,0.45);glTranslatef(0.0f,-10.0f,-30.0+i*9);glScalef(26.0f,0.3f,5.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//..........................桌子的下面......................//glPushMatrix();glColor3f(1.0,1.0,0.45);glTranslatef(0.0f,-13.0f,-30.0+i*9);glScalef(24.0f,0.3f,5.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的后面......................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(0.0f,-12.0f,-32.5f+i*9);glScalef(24.0f,3.6f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的左边桌腿...................//glPushMatrix();glColor3f(0.0,0.0,0.0);glTranslatef(-11.4f,-17.0f,-32.5f+i*9);glScalef(0.5f,8.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的右边桌腿...................//glPushMatrix();glColor3f(0.0,0.0,0.0);glTranslatef(11.4f,-17.0f,-32.5f+i*9);glScalef(0.5f,8.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//............................椅子............................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(0.0f,-15.0f,-25.0+i*9);glScalef(24.3f,0.3f,3.5f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//........................................第三张桌椅..................................................////..........................桌子的上面.......................//glPushMatrix();glColor3f(1.0,1.0,0.45);glTranslatef(-30.0f,-10.0f,-30.0+i*9);glScalef(13.0f,0.3f,5.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//..........................桌子的下面......................//glPushMatrix();glColor3f(1.0,1.0,0.45);glTranslatef(-30.0f,-13.0f,-30.0+i*9);glScalef(11.0f,0.3f,5.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的后面......................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(-30.0f,-12.0f,-32.5f+i*9);glScalef(11.0f,3.6f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的左边桌腿...................//glPushMatrix();glColor3f(0.0,0.0,0.0);glTranslatef(-25.0f,-17.0f,-32.5f+i*9);glScalef(0.5f,8.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的右边桌腿...................//glPushMatrix();glColor3f(0.0,0.0,0.0);glTranslatef(-35.0f,-17.0f,-32.5f+i*9);glScalef(0.5f,8.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//............................椅子............................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(-30.0f,-15.0f,-25.0+i*9);glScalef(11.3f,0.3f,3.5f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();}//..........................................最后一排的椅背..........................................////....................最右边的椅背..................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(30.0f,-15.0f,12.6f);glScalef(11.3f,12.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//.....................中间的椅背...................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(0.0f,-15.0f,12.6f);glScalef(24.3f,12.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//.....................最左边的椅背.................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(-30.0f,-15.0f,12.6f);glScalef(11.3f,12.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//................................风扇................................................//GLuint m=0;for(;m<2;m++)//一共两排风扇{qobj = gluNewQuadric();//初始化二次曲面并创建一个指向二次曲面的指针//...............................右边的风扇.............................//glPushMatrix();//压入堆栈,保证此处坐标变换不影响外部大环境glTranslatef(20.0,-3.0,-10.0-40*m); //风扇的位置if(TurnOn==TRUE) //如果TurnOn为1{fantheta+=10; //风扇转过的角度fantheta不断增加if(fantheta==360) //如果旋转角度超过360度fantheta=0; //则fantheta重新回0}glRotatef(fantheta,0,1,0); //风扇旋转//..........风扇中间的扁圆形....................//glPushMatrix();//压入堆栈glColor3f(0.7,0.7,0.7);glTranslatef(0.0,20.0,0.0);//平移变换glScalef(1,0.3,1);//缩放变换gluSphere(qobj,2,20,20);//绘制一个半径为2的球体glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glPopMatrix();//弹出堆栈//............风扇旋转120度的扇叶...............//glPushMatrix();//压入堆栈glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);//平移变换glScalef(1,0.05,1);//缩放变换glRotatef(120,0,1,0);//绕y周旋转120度gluCylinder(qobj,1,3,10,20,20);//绘制一个底面圆半径为1、顶面圆半径为2、高为10的圆锥体glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glPopMatrix();//弹出堆栈//............风扇不做旋转的扇叶.................//glPushMatrix();//压入堆栈glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);//平移变换glScalef(1,0.05,1);//缩放变换gluCylinder(qobj,1,3,10,20,20);//绘制一个底面圆半径为1、顶面圆半径为2、高为10的圆锥体glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glPopMatrix();//弹出堆栈//.............风扇旋转240度的扇叶...............//glPushMatrix();//压入堆栈glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);//平移变换glScalef(1,0.05,1);//缩放变换glRotatef(240,0,1,0);//绕y周旋转240度gluCylinder(qobj,1,3,10,20,20);//绘制一个底面圆半径为1、顶面圆半径为2、高为10的圆锥体glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glPopMatrix();//弹出堆栈//.............风扇的杆...........................//glPushMatrix();//压入堆栈glTranslatef(0,33,0.0);//平移变换glRotatef(90,1,0,0);//缩放变换gluCylinder(qobj,0.5,0.5,13,10,10);//绘制一个半径为0.5、高为13的圆锥体glPopMatrix();//弹出堆栈glPopMatrix();//弹出堆栈//................左边的风扇...........................................//glPushMatrix();glTranslatef(-20.0,-3.0,-10.0-40*m);if(TurnOn==TRUE){fantheta+=10;if(fantheta==360)fantheta=0;}glRotatef(fantheta,0,1,0);//..........风扇中间的扁圆形....................//glPushMatrix();//风扇中间的扁圆形glColor3f(0.7,0.7,0.7);glTranslatef(0.0,20.0,0.0);glScalef(1,0.3,1);gluSphere(qobj,2,20,20);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glPopMatrix();//............风扇旋转120度的扇叶...............//glPushMatrix();//风扇旋转120度的扇叶glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);glScalef(1,0.05,1);glRotatef(120,0,1,0);gluCylinder(qobj,1,3,10,20,20);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glPopMatrix();//............风扇不做旋转的扇叶.................//glPushMatrix();//风扇不做旋转的扇叶glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);glScalef(1,0.05,1);gluCylinder(qobj,1,3,10,20,20);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glPopMatrix();//.............风扇旋转240度的扇叶...............//glPushMatrix();//风扇旋转240度的扇叶glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);glScalef(1,0.05,1);glRotatef(240,0,1,0);gluCylinder(qobj,1,3,10,20,20);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glPopMatrix();//.............风扇的杆...........................//glPushMatrix();glTranslatef(0,33,0.0);glRotatef(90,1,0,0);gluCylinder(qobj,0.5,0.5,13,10,10);glPopMatrix();glPopMatrix();}//...........................................门.....................................................................//glColor3f(0.521f,0.121f,0.0547f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,diffusion);//定义材质光照glBindTexture(GL_TEXTURE_2D,texture[3]);//绑定纹理glBegin(GL_QUADS);//开始绘制四边形glColor3f(1.0f, 1.0f, 1.0f);glNormal3f(-1.0f, 0.0f, 0.0f);//glNormal3f用于定义点的法向量//............................前门.........................................//glTexCoord2f(0.0,1.0);glVertex3f( 49.9f, 20.0f, -70.0f);//左上glTexCoord2f(1.0,1.0);glVertex3f( 49.9f, 20.0f, -45.0f);//右上glTexCoord2f(1.0,0.0);glVertex3f( 49.9f, -21.0f, -45.0f);//右下glTexCoord2f(0.0,0.0);glVertex3f( 49.9f, -21.0f, -69.9f);//左下//............................后门.....................................//glTexCoord2f(0.0,1.0);glVertex3f( 49.9f, 20.0f, 5.0f);//左上glTexCoord2f(1.0,1.0);glVertex3f( 49.9f, 20.0f, 30.0f);//右上glTexCoord2f(1.0,0.0);glVertex3f( 49.9f, -21.0f, 30.0f);//右下glTexCoord2f(0.0,0.0);glVertex3f( 49.9f, -21.0f, 5.0f);//左下glEnd();//..............................................窗户.............................................................////.....................右面墙上的窗户......................................//glBindTexture(GL_TEXTURE_2D,texture[2]);//绑定纹理glBegin(GL_QUADS);//开始绘制四边形glColor4f(1.0f,1.0f,1.0f,0.5f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glNormal3f(-1.0f, 0.0f, 0.0f);//glNormal3f用于定义点的法向量glBlendFunc(GL_SRC_ALPHA,GL_ONE);// 基于源象素alpha通道值的半透明混合函数glTexCoord2f(0.0,1.0);glVertex3f( 49.9f,29.0f,-35.0f);//左上glTexCoord2f(1.0,1.0);glVertex3f( 49.9f,29.0f,0.0f);//右上glTexCoord2f(1.0,0.0);glVertex3f( 49.9f,10.0f,0.0f);//右下glTexCoord2f(0.0,0.0);glVertex3f( 49.9f,10.0f,-35.0f);//左下glEnd();//......................左面墙上的窗户(两扇)..............................//glBegin(GL_QUADS);//开始绘制四边形glNormal3f(1.0f, 0.0f, 0.0f);//glNormal3f用于定义点的法向量glColor4f(1.0f,1.0f,1.0f,0.5f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glTexCoord2f(0.0,1.0);glVertex3f( -49.9f, 15.0f,-60.0f);//左上glTexCoord2f(1.0,1.0);glVertex3f( -49.9f, 15.0f,-20.0f);//右上glTexCoord2f(1.0,0.0);glVertex3f( -49.9f, -10.0f,-20.0f);//右下glTexCoord2f(0.0,0.0);glVertex3f( -49.9f, -10.0f,-60.0f);//左下glTexCoord2f(0.0,1.0);glVertex3f( -49.9f, 15.0f,-10.0f);//左上glTexCoord2f(1.0,1.0);glVertex3f( -49.9f, 15.0f,30.0f);//右上glTexCoord2f(1.0,0.0);glVertex3f( -49.9f, -10.0f,30.0f);//右下glTexCoord2f(0.0,0.0);glVertex3f( -49.9f, -10.0f,-10.0f);//左下glEnable(GL_BLEND);// 打开混合glDisable(GL_DEPTH_TEST);// 关闭深度测试glEnd();//............................................灯......................................................//GLuint j=0;for(;j<2;j++) //一共两排灯{glBindTexture(GL_TEXTURE_2D,texture[4]);//绑定纹理glColor3f(1.0f,1.0f,1.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glBegin(GL_QUADS);//开始绘制四边形glNormal3f(0.0f, -1.0f, 0.0f);//glNormal3f用于定义点的法向量glTexCoord2f(0.0f, 1.0f);glVertex3f( 25.0f, 29.9f, -70.0f+j*40);//左上glTexCoord2f(1.0f, 1.0f);glVertex3f( 45.0f, 29.9f, -70.0f+j*40);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( 45.0f, 29.9f, -50.0f+j*40);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( 25.0f, 29.9f, -50.0f+j*40);//左下glColor3f(1.0f,1.0f,1.0f);glNormal3f(0.0f, -1.0f, 0.0f);glTexCoord2f(0.0f, 1.0f);glVertex3f( -10.0f, 29.9f, -70.0f+j*40);//左上glTexCoord2f(1.0f, 1.0f);glVertex3f( 10.0f, 29.9f, -70.0f+j*40);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( 10.0f, 29.9f, -50.0f+j*40);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( -10.0f, 29.9f, -50.0f+j*40);//左下glColor3f(1.0f,1.0f,1.0f);glNormal3f(0.0f, -1.0f, 0.0f);glTexCoord2f(0.0f, 1.0f);glVertex3f( -45.0f, 29.9f, -70.0f+j*40);//左上glTexCoord2f(1.0f, 1.0f);glVertex3f( -25.0f, 29.9f, -70.0f+j*40);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( -25.0f, 29.9f, -50.0f+j*40);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( -45.0f, 29.9f, -50.0f+j*40);//左下glEnd();}//....................................讲台.............................................................////.....................讲台的桌子........................................//glPushMatrix();//压入堆栈glColor3f(0.5f,0.9f,0.5f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义光照材质glTranslatef(-0.0f,-10.0f,-60.5f);//平移变换glScalef(20.0f,20.0f,10.0f);//缩放变换glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//......................地面上高起的平台.................................//glPushMatrix();//压入堆栈glColor3f(0.8f,0.8f,0.8f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义光照材质glTranslatef(0.0f,-20.0f,-70.0f);//平移变换glScalef(65.0f,2.9f,30.0f);//缩放变换glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//........................................黑板............................................................//glBindTexture(GL_TEXTURE_2D, texture[5]); //绑定纹理glBegin(GL_QUADS); //绘制四边形glColor3f(1.0f, 1.0f, 1.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义光照材质glNormal3f(0.0f, 0.0f, 1.0f);//glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f(-25.0f, 20.0f, -79.9f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f(-25.0f, -5.0f, -79.9f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( 25.0f, -5.0f, -79.9f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f( 25.0f, 20.0f, -79.9f);//左上glEnd();//........................................时钟..........................................................//_strtime(timebuffer);//取系统时间到timebuffer数组中。

相关文档
最新文档