opengl画图完整代码
OpenGL编程学习(2)
OpenGL编程学习(2)•OpenGL画点函数OpenGL图元默认大小为一个像素单位,默认颜色为白色。
画点函数为:glVertex*();星号(*)说明该函数要有后缀码。
一般来说,会有2到3个后缀码。
第一个后缀码代表维数,二维、三维、四维坐标分别用2、3、4等数字表示。
第二个后缀码代表坐标的数值类型,如i(整数)、s (短整数)、f(浮点)、d(双精度浮点)等。
glVertex中可以使用显式的坐标值或引入矩阵形式坐标位置的单个变量,此时需要第三个后缀:v(向量)。
下面例子在斜率为2的直线上绘出了三个等距离的点。
glBegin(GL_POINTS);glVertex2i (50,100);glVertex2i (75,150);glVertex2i (100,200);glEnd();换一种方法,以矩阵形式表示上述坐标值:int point1 []={50,100};int point2 []={75,150};int point3 []={100,200};glBegin(GL_POINTS);glVertex2iv (point1);glVertex2iv (point2);glVertex2iv (point3);glEnd();•OpenGL画线函数与OpenGL画点函数十分类似,不同在于glBegin()中的符号常量。
使用图元常量GL_LINES可连接每一对相邻定点而得到一组直线段。
glBegin(GL_LINES);glVertex2iv (p1);glVertex2iv (p2);glVertex2iv (p3);glVertex2iv (p4);glVertex2iv (p5);glEnd();上述代码可以画出线段p1-p2和p3-p4。
使用GL_LINE_STRIP则可以获得折线,如果需要封闭的折线则需要重复某些点坐标。
glBegin(GL_LINE_STRIP);glVertex2iv (p1);glVertex2iv (p2);glVertex2iv (p3);glVertex2iv (p4);glVertex2iv (p5);glEnd();上述代码画出折线p1-p2-p3-p4-p5。
图形学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编程轻松入门之一个简单的例子
OpenGL编程轻松入门之一个简单的例子先编译运行一个简单的例子,这样我们可以有一个直观的印象。
从这个例子我们可以看到OpenGL可以做什么,当然这个例子只做了很简单的一件事--绘制一个彩色的三角形。
除此以外,我们还可以看到典型的OpenGL程序结构及openGL 的运行顺序。
例1:本例在黑色的背景下绘制一个彩色的三角形,如图一所示。
#include <stdlib.h>#include <GL/glut.h>void background(void){glClearColor(0.0,0.0,0.0,0.0);//设置背景颜色为黑色}void myDisplay(void){glClear(GL_COLOR_BUFFER_BIT);//buffer设置为颜色可写glBegin(GL_TRIANGLES);//开始画三角形glShadeModel(GL_SMOOTH);//设置为光滑明暗模式glColor3f(1.0,0.0,0.0);//设置第一个顶点为红色glV ertex2f(-1.0,-1.0);//设置第一个顶点的坐标为(-1.0,-1.0)glColor3f(0.0,1.0,0.0);//设置第二个顶点为绿色glV ertex2f(0.0,-1.0);//设置第二个顶点的坐标为(0.0,-1.0)glColor3f(0.0,0.0,1.0);//设置第三个顶点为蓝色glV ertex2f(-0.5,1.0);//设置第三个顶点的坐标为(-0.5,1.0)glEnd();//三角形结束glFlush();//强制OpenGL函数在有限时间内运行}void myReshape(GLsizei w,GLsizei h){glViewport(0,0,w,h);//设置视口glMatrixMode(GL_PROJECTION);//指明当前矩阵为GL_PROJECTION glLoadIdentity();//将当前矩阵置换为单位阵if(w <= h)gluOrtho2D(-1.0,1.5,-1.5,1.5*(GLfloat)h/(GLfloat)w);//定义二维正视投影矩阵elsegluOrtho2D(-1.0,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5);glMatrixMode(GL_MODELVIEW);//指明当前矩阵为GL_MODELVIEW }int main(int argc,char ** argv){/*初始化*/glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(400,400);glutInitWindowPosition(200,200);/*创建窗口*/glutCreateWindow("Triangle");/*绘制与显示*/background();glutReshapeFunc(myReshape);glutDisplayFunc(myDisplay);glutMainLoop();return(0);}图一:一个彩色的三角形首先创建工程,其步骤如下:1)创建一个Win32 Console Application。
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编程基础
显示点、直线和多边形: 三、多边形的细节 2. 设置点画多边形的函数: glPolygonStipple() 句法:void glPolygonStipple(const GLubyte *mask); 描述:定义填充多边形的当前点画模式。Mask参数是一 个指向32x32位图的指针,后者被解释为0和1的掩码。如果 模式中出现的是1,则多边形对应的像素就被绘制,如果模
OpenGL编程基础
显示点、直线和多边形: 二、直线的细节 直线的细节包括直线的宽度和不同点画模式的直线。 1. 设置直线的宽度函数: glLineWidth() 句法:void glLineWidth(GL float Width); 描述:设置被渲染直线的粗细,以像素为单位。Width必 须大于0.0,默认值为1.0 。在未使用反走样功能时,直线的
OpenGL编程基础
OpenGL基础函数
OpenGL图元绘制
OpenGL编程基础
显示点、直线和多边形: 一、点的细节 点的基本大小对应屏幕上的1个像素。使用glPointSize()函 数可设置被渲染点的大小。 句法:void glPointSize(GL float size); 描述:设置被渲染点的大小,以像素为单位。Size必须 大于0.0,默认值为1.0 。被渲染的点呈正方形方块。如果 启用了反走样或多重采样,被渲染的点呈圆形。调用函数 glEnable(),并将参数设置为GL_POINT_SMOOTH,启 用反走样。例:point
x[i ] xc r (cos(t ) t sin( t )) y[i ] yc r (sin( t ) t cos(t ))
绘制渐开线,如图所示。
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画图完整代码#define NULL 0#define LINE 1#define RECTANGLE 2#define TRIANGLE 3#define POINTS 4#define TEXT 5#define CIRCLE 6const int cl = 360;const float Pi = 3.1415926536f;#include#include#includevoid mouse(int, int, int, int);void key(unsigned char, int, int);void display();void drawSquare(int, int);void myReshape(GLsizei, GLsizei); void myinit();void screen_box(int, int, int);void right_menu(int);void middle_menu(int);void color_menu(int);void pixel_menu(int);void fill_menu(int);int pick(int, int);/* globals */GLsizei wh = 500, ww = 500;GLfloat size = 3.0; /* half side length of square */int draw_mode = 0;int rx, ry; /*raster position*/GLfloat r = 1.0, g = 1.0, b = 1.0;int fill = 0;void drawSquare(int x, int y){y=wh-y;glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256);glBegin(GL_POLYGON);glVertex2f(x+size, y+size);glVertex2f(x-size, y+size);glVertex2f(x-size, y-size);glVertex2f(x+size, y-size);glEnd();}void myReshape(GLsizei w, GLsizei h){ww=w;wh=h;glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW);glLoadIdentity();glViewport(0,0,w,h);glClearColor (0.8, 0.8, 0.8, 1.0);glClear(GL_COLOR_BUFFER_BIT);display();glFlush();ww = w;wh = h;}void myinit(){glViewport(0,0,ww,wh);glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(0.0, (GLdouble) ww , 0.0, (GLdouble) wh , -1.0, 1.0);glClearColor (0.8, 0.8, 0.8, 1.0);glClear(GL_COLOR_BUFFER_BIT);glFlush();}void mouse(int btn, int state, int x, int y){static int count;int where;static int xp[2],yp[2];if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) {glPushAttrib(GL_ALL_ATTRIB_BITS);where = pick(x,y);glColor3f(r, g, b);if(where != 0){count = 0;draw_mode = where;}else switch(draw_mode){case(LINE):if(count==0){count++;xp[0] = x;yp[0] = y;}else{glBegin(GL_LINES);glVertex2i(x,wh-y);glVertex2i(xp[0],wh-yp[0]); glEnd();draw_mode=0;count=0;}break;case(RECTANGLE):if(count == 0){count++;xp[0] = x;yp[0] = y;}else{if(fill) glBegin(GL_POLYGON); else glBegin(GL_LINE_LOOP); glVertex2i(x,wh-y); glVertex2i(x,wh-yp[0]); glVertex2i(xp[0],wh-yp[0]);glVertex2i(xp[0],wh-y); glEnd();draw_mode=0;count=0;}break;case (TRIANGLE):switch(count){case(0):count++;xp[0] = x;yp[0] = y;break;case(1):count++;xp[1] = x;yp[1] = y;break;case(2):if(fill) glBegin(GL_POLYGON); else glBegin(GL_LINE_LOOP); glVertex2i(xp[0],wh-yp[0]); glVertex2i(xp[1],wh-yp[1]); glVertex2i(x,wh-y);glEnd();draw_mode=0;count=0;}break;case(POINTS):{drawSquare(x,y);count++;}break;case(TEXT):{rx=x;ry=wh-y;glRasterPos2i(rx,ry);count=0;}case(CIRCLE):if(count == 0){count++;xp[0] = x;yp[0] = y;}else{int i=0;if(fill) glBegin(GL_POLYGON);else glBegin(GL_LINE_LOOP);for(i=0; iglVertex2f(0.5*xp[0]+0.5*x+0.5*pow(pow(x-xp[0],2)+pow(y-yp[0],2),0.5)*cos(2*Pi/cl*i),wh-(0.5*yp[0]+0.5*y+0.5*pow(pow(x-xp[0],2)+pow(y-yp[0],2),0.5)*sin(2*Pi/cl*i)));glEnd();draw_mode=0;count=0;}}glPopAttrib();glFlush();}}int pick(int x, int y){y = wh - y;if(y < wh-ww/10) return 0;else if(x < ww/10) return LINE;else if(x < ww/5) return RECTANGLE; else if(x < 3*ww/10) return TRIANGLE; else if(x < 2*ww/5) return POINTS; else if(x < ww/2) return TEXT;else if(x < 3*ww/5) return CIRCLE; else return 0;}void screen_box(int x, int y, int s ) {glBegin(GL_QUADS);glVertex2i(x, y);glVertex2i(x+s, y);glVertex2i(x+s, y+s);glVertex2i(x, y+s);glEnd();}void right_menu(int id){if(id == 1) exit(0);else display();}void middle_menu(int id){}void color_menu(int id){if(id == 1) {r = 1.0; g = 0.0; b = 0.0;} else if(id == 2) {r = 0.0; g = 1.0; b = 0.0;} else if(id == 3) {r = 0.0; g = 0.0; b = 1.0;} else if(id == 4) {r = 0.0; g = 1.0; b = 1.0;} else if(id == 5) {r = 1.0; g = 0.0; b = 1.0;} else if(id == 6) {r = 1.0; g = 1.0; b = 0.0;} else if(id == 7) {r = 1.0; g = 1.0; b = 1.0;} else if(id == 8) {r = 0.0; g = 0.0; b = 0.0;} }void pixel_menu(int id){if (id == 1) size = 2 * size;else if (size > 1) size = size/2;}void fill_menu(int id){if (id == 1) fill = 1;else fill = 0;}void key(unsigned char k, int xx, int yy){if(draw_mode!=TEXT) return;glColor3f(0.0,0.0,0.0);glRasterPos2i(rx,ry);glutBitmapCharacter(GLUT_BITMAP_9_BY_15, k); rx+=glutBitmapWidth(GLUT_BITMAP_9_BY_15,k);}void display(){int shift=0;glPushAttrib(GL_ALL_ATTRIB_BITS); glClearColor (0.8, 0.8, 0.8, 1.0);glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0, 1.0, 1.0);screen_box(0,wh-ww/10,ww/10);glColor3f(1.0, 0.0, 0.0);screen_box(ww/10,wh-ww/10,ww/10);glColor3f(0.0, 1.0, 0.0);screen_box(ww/5,wh-ww/10,ww/10);glColor3f(0.0, 0.0, 1.0);screen_box(3*ww/10,wh-ww/10,ww/10);glColor3f(1.0, 1.0, 0.0);screen_box(2*ww/5,wh-ww/10,ww/10);glColor3f(0.0, 1.0, 1.0);screen_box(1*ww/2,wh-ww/10,ww/10);glColor3f(0.0, 0.0, 0.0);glBegin(GL_LINES);glVertex2i(wh/40,wh-ww/20);glVertex2i(wh/40+ww/20,wh-ww/20);glEnd();glBegin(GL_POLYGON);glVertex2i(ww/9+ww/60,wh-ww/10+ww/40);glVertex2i(ww/9+ww/60+ww/20,wh-ww/10+ww/40);glVertex2i(ww/9+ww/60+ww/20,wh-ww/10+3*ww/40);glVertex2i(ww/9+ww/60,wh-ww/10+3*ww/40);glEnd();int i;glBegin(GL_POLYGON);// glVertex2i(5*ww/9,wh-ww/10+ww/40);for(i=0; iglVertex2f(5*ww/9-ww/150+ww/30*cos(2*Pi/cl*i), wh-ww/13+ww/40+ww/30*sin(2*Pi/cl*i));glEnd();glBegin(GL_TRIANGLES);glVertex2i(ww/5+ww/40,wh-ww/10+ww/40); glVertex2i(ww/5+ww/20,wh-ww/40);glVertex2i(ww/5+3*ww/40,wh-ww/10+ww/40); glEnd();glPointSize(3.0);glBegin(GL_POINTS);glVertex2i(3*ww/10+ww/20, wh-ww/20);glEnd();glRasterPos2i(2*ww/5,wh-ww/20); glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'A'); shift=glutBitmapWidth(GLUT_BITMAP_9_BY_15, 'A'); glRasterPos2i(2*ww/5+shift,wh-ww/20); glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'B'); shift+=glutBitmapWidth(GLUT_BITMAP_9_BY_15, 'B'); glRasterPos2i(2*ww/5+shift,wh-ww/20); glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'C'); glFlush();glPopAttrib();}int main(int argc, char** argv){int c_menu, p_menu, f_menu;glutInit(&argc,argv);glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);glutCreateWindow("work"); glutDisplayFunc(display);c_menu = glutCreateMenu(color_menu); glutAddMenuEntry("Red",1); glutAddMenuEntry("Green",2); glutAddMenuEntry("Blue",3); glutAddMenuEntry("Cyan",4); glutAddMenuEntry("Magenta",5); glutAddMenuEntry("Yellow",6); glutAddMenuEntry("White",7); glutAddMenuEntry("Black",8);p_menu = glutCreateMenu(pixel_menu); glutAddMenuEntry("increase pixel size", 1); glutAddMenuEntry("decrease pixel size", 2); f_menu = glutCreateMenu(fill_menu); glutAddMenuEntry("fill on", 1); glutAddMenuEntry("fill off", 2); glutCreateMenu(right_menu); glutAddMenuEntry("quit",1); glutAddMenuEntry("clear",2);glutAttachMenu(GLUT_RIGHT_BUTTON); glutCreateMenu(middle_menu); glutAddSubMenu("Colors", c_menu); glutAddSubMenu("Pixel Size", p_menu); glutAddSubMenu("Fill", f_menu); glutAttachMenu(GLUT_MIDDLE_BUTTON); myinit ();glutReshapeFunc (myReshape);glutKeyboardFunc(key);glutMouseFunc (mouse); glutMainLoop();}。
OpenGL图形编程2基本图形绘制(陈永强)只是分享
这个函数采用一个参数来指定画点时以象素为单位的近似 直径。
7
2.2点的绘制
点的属性(大小)
通常使用下面的代码来获取点大小的范围和它们之间最小的中间值:
GLfloat sizes[2]; //保存绘制点的尺寸范围
GLfloat step;
将指定的各个顶点用于创建单个的点
GL_LINES GL_LINE_STRIP GL_LINE_LOOP
GL_TRIANGLES
将指定的顶点用于创建线段。每两个顶点指定一条单独的线 段。如果顶点个数是奇数,则忽略后一个
将指定的顶点用于创建线条。第一个顶点之后的每个顶点指 定的是线条延伸到的下一个点
特性和GL_LINE_STRIP 相似,只不过最后一条线段是在 指定的后一个和第一个顶点之间绘制。典型情况下,这用于 绘制那些可能违反了 GL_POLYGON 用法规则的封闭区域
glBegin(GL_LINE_STRIP); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(10.0f,10.0f,0.0f); glVertex3f(20.0f,5.0f,0.0f);
glEnd();
在xy平面内绘制了两条直线(0,0,0)到(10,0,0)和(0,10,0)到(20,5,0)。
OpenGL图形编程2基本图形绘 制(陈永强)
2.1glBegin/glEnd
OpenGL的图元绘制放在函数glBegin和 glEnd之间,由函数glBegin的参数指定绘制 图元的类型。
2.1glBegin/glEnd
模式
GL_POINTS
表 glBegin可支持的OpenGL图元
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图形编程2基本图形绘制(陈永强)只是分享
31
2.4多边形面的绘制
多边形面的属性 颜色
glShadeModel(GL_FLAT) 指定多边形最后一个顶点 时的当前颜色作为填充多边形的纯色,唯一例外 是GL_POLYGON图元,它采用的是第一个顶点的 颜色。
glGetFloatv(GL_LINE_WIDTH_GRANULARITY,&step);
数组 sizes中保存了glLineWidth的最小有效值和最大有效值,而变量step将 保存线宽之间允许的最小增量。OpenGL规范只要求支持一种线宽:1.0。
Microsoft的OpenGL实现允许线宽从0.5到10.0,最小增量为 0.125。
点的坐标(x,y,z,w)实际上构成了一个齐次坐标。在OpenGL中 ,仍然使用规范化齐次坐标以保证点的齐次坐标与三维坐标的一一对应关 系,最后指定的空间点的坐标为(x/w,y/w,z/w,1),w成了坐
标值的一个缩放因子。
5
2.2点的绘制
点的绘制 glBegin(GL_POINTS); glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(10.0f, 0.0f, 0.0f); glEnd();
与点的大小类似,glLineWidth函数采用一个参数 来指定要画的线以象素计的近似宽度 。
15
2.3直线的绘制
直线的属性
线宽
可以用下面的代码来获取线宽范围和它们之间的最小间隔:
GLfloat sizes[2]; //保存线宽的尺寸范围
GLfloat step;
//保存线宽尺寸的最小间隔
Байду номын сангаас
glGetFloarv(GL_LINE_WIDTH_RANGE,sizes);
openGL画直线、圆、椭圆
drawEllipse(250,250,60,40);
/* and flush that buffer to the screen */
glFlush();
}
int main(int argc, char** argv)
{
/* Standard GLUT initialization */
}
}
void dda_line(int xa,int ya,int xb,int yb)//画直线
{
GLfloat delta_x,delta_y,x,y;
int dx,dy,steps;
dx=xb-xa;
dy=yb-ya;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
cout<<"************************************************"<<endl;
cout<<"****请选择如下操作: *****"<<endl;
cout<<"**** 1.用DDA画线*****"<<endl;
cout<<"**** 2.用Bresenham算法画圆*****"<<endl;
使用openGl画直线(DDA算法)、画圆、椭圆(Bresenham算法)
#include<stdlib.h>
#include<iostream.h>
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库来创建窗口和处理事件。
QT使用OpenGL进行3D绘图
QT使用OpenGL进行3D绘图目录一、成果展示:二、代码展示:1、main.cpp2、MyGLWidget.h3、MyGLWidget.cpp三、编译错误解析:1、gluPerspective 和 gluLookAt 找不到标识符2、error: LNK2019: 无法解析的外部符号 __imp_glBegin等一、成果展示:二、代码展示:1、main.cpp1.#include <QtWidgets/QApplication>2.3.#include "myglwidget.h"4.5.int main(int argc, char *argv[])6.{7.QApplication a(argc, argv);8.9.MyGLWidget w;10.w.resize(400, 300);11.w.show();12.13.return a.exec();14.}2、MyGLWidget.h1.#ifndef MYGLWIDGET_H2.#define MYGLWIDGET_H3.4.#include <QGLWidget>5.#include <GL/GLU.h>6.7.class MyGLWidget : public QGLWidget8.{9.Q_OBJECT10.public:11.explicit MyGLWidget(QWidget *parent = 0);12.13.protected:14.void initializeGL();15.void resizeGL(int w, int h);16.void paintGL();17.void keyPressEvent(QKeyEvent *);18.void paintEvent(QPaintEvent *);19.void setupViewport(int w, int h);20.21.private:22.GLfloat translate, xRot, yRot, zRot;23.GLuint textures[3];24.25.signals:26.27.public slots:28.29.};30.31.#endif // MYGLWIDGET_H3、MyGLWidget.cpp1.#include "myglwidget.h"2.#include <QKeyEvent>3.4.MyGLWidget::MyGLWidget(QWidget *parent) :5.QGLWidget(parent)6.{7.translate = -6.0;8.xRot = yRot = zRot = 0.0;9.10.// 关闭自动填充背景11.setAutoFillBackground(false);12.}13.14.// 初始化环境15.void MyGLWidget::initializeGL()16.{17.}18.19.// 调整视口大小20.void MyGLWidget::resizeGL(int w, int h)21.{22.setupViewport(w, h);23.}24.25.// 渲染场景26.void MyGLWidget::paintGL()27.{28.}29.30.void MyGLWidget::keyPressEvent(QKeyEvent *event)31.{32.switch (event->key())33.{34.case Qt::Key_Up :35.xRot += 10;36.break;37.case Qt::Key_Left :38.yRot += 10;39.break;40.case Qt::Key_Right :41.zRot += 10;42.break;43.case Qt::Key_Down :44.translate -= 1;45.if (translate <= -20)46.translate = -1;47.break;48.}49.//updateGL();50.update();51.QGLWidget::keyPressEvent(event);52.}53.54.void MyGLWidget::paintEvent(QPaintEvent *)55.{56.// 在当前窗口中进行OpenGL的绘制57.makeCurrent();58.// 将模型视图矩阵压入堆栈59.glMatrixMode(GL_MODELVIEW);60.glPushMatrix();61.62.// 以下是以前initializeGL()函数中的全部内容63.glClearColor(0.0, 0.0, 0.0, 0.0);64.glShadeModel(GL_SMOOTH);65.glClearDepth(1.0);66.glEnable(GL_DEPTH_TEST);67.68.textures[0] = bindTexture(QPixmap(":/LHCGraphicsView/Resources/side1.png "));69.textures[1] = bindTexture(QPixmap(":/LHCGraphicsView/Resources/side2.png "));70.textures[2] = bindTexture(QPixmap(":/LHCGraphicsView/Resources/side3.png "));71.glEnable(GL_TEXTURE_2D);72.73.// 该函数中是以前resizeGL()函数中的全部内容74.setupViewport(width(), height());75.76.// 以下是以前paintGL()函数中的全部内容77.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);78.glLoadIdentity();79.80.glTranslatef(0.0, 0.0, translate);81.glRotatef(xRot, 1.0, 0.0, 0.0);82.glRotatef(yRot, 0.0, 1.0, 0.0);83.glRotatef(zRot, 0.0, 0.0, 1.0);84.85.glBindTexture(GL_TEXTURE_2D, textures[2]);86.glBegin(GL_QUADS);87.glTexCoord2f(1.0, 0.0);88.glVertex3f(1.0, 1.0, 1.0);89.glTexCoord2f(1.0, 1.0);90.glVertex3f(1.0, 1.0, -1.0);91.glTexCoord2f(0.0, 1.0);92.glVertex3f(-1.0, 1.0, -1.0);93.glTexCoord2f(0.0, 0.0);94.glVertex3f(-1.0, 1.0, 1.0);95.glEnd();96.glBindTexture(GL_TEXTURE_2D, textures[1]);97.glBegin(GL_QUADS);98.glTexCoord2f(1.0, 0.0);99.glVertex3f(1.0, -1.0, 1.0);100.glTexCoord2f(1.0, 1.0);101.glVertex3f(1.0, -1.0, -1.0);102.glTexCoord2f(0.0, 1.0);103.glVertex3f(-1.0, -1.0, -1.0);104.glTexCoord2f(0.0, 0.0);105.glVertex3f(-1.0, -1.0, 1.0);106.glEnd();107.glBindTexture(GL_TEXTURE_2D, textures[0]); 108.glBegin(GL_QUADS);109.glTexCoord2f(1.0, 1.0);110.glVertex3f(1.0, 1.0, 1.0);111.glTexCoord2f(0.0, 1.0);112.glVertex3f(-1.0, 1.0, 1.0);113.glTexCoord2f(0.0, 0.0);114.glVertex3f(-1.0, -1.0, 1.0);115.glTexCoord2f(1.0, 0.0);116.glVertex3f(1.0, -1.0, 1.0);117.glEnd();118.119.// 关闭启用的功能并弹出模型视图矩阵120.glDisable(GL_DEPTH_TEST);121.glDisable(GL_TEXTURE_2D);122.glMatrixMode(GL_MODELVIEW);123.glPopMatrix();124.125.// 下面是2D绘图的内容126.QPainter painter(this);127.painter.setRenderHint(QPainter::Antialiasing);128.painter.setBrush(Qt::red);129.painter.drawRect(0, 0, 100, 100);130.painter.end();131.}132.133.void MyGLWidget::setupViewport(int w, int h)134.{135.glViewport(0, 0, (GLint)w, (GLint)h);136.glMatrixMode(GL_PROJECTION);137.glLoadIdentity();138.gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1, 100.0);139.glMatrixMode(GL_MODELVIEW);140.glLoadIdentity();141.}4、图片资源三、编译错误解析:1、gluPerspective 和 gluLookAt 找不到标识符请添加头文件 #include <GL/glu.h>且,必须要加在 #include <QGLWidget> 的后面2、error: LNK2019: 无法解析的外部符号 __imp_glBegin等我用的 vs+qt 开发链接器-- 常规-- 附加库目录--添加D:\Windows Kits\10\Lib\10.0.17763.0\um 64 (vs其实已经继承了该路径。
OpenGL位图绘制和笔画绘制
#inclu de<GL/glut.h>#inclu de <m ath.h>#pr agmacomme nt(li b, "o pengl32.li b")#pragm a com ment(lib,"glu32.lib")in t Cr eatGL Windo w(voi d);v oid I nitGL Scene(void);vo id Di splay(void);vo id Re shape(intwidth,intheigh t);v oid S etWin dow();voi d Set Viewp ort(i nt wi dth,i nt he ight); vo id Bm pFont(void);in t mai n(int argc,char **ar gv){Cre atGLW indow();InitG LScen e();/*初始化绘制状态*/glutD ispla yFunc(Disp lay);/*显示回调函数*/glut Resha peFun c(Res hape); /*形状大小改变*/gl utMai nLoop();retu rn 0;}i nt Cr eatGL Windo w(){int wind owID=0;glutI nitDi splay Mode(GLUT_DOUBL E | G LUT_R GBA |GLUT_DEPTH);/*窗口显示模式:缓冲和RGBA颜色模式*/gl utIni tWind owSiz e(500,500); /*设置窗口属性*/gl utIni tWind owPos ition(0,0);w indow ID=gl utCre ateWi ndow("OPEN GL DE MO");/*创建窗口并设置窗口名称*/r eturn(wind owID);}voidInitG LScen e(){glC learC olor(0.0f, 0.0f, 0.0f, 1.0f);glE nable(GL_D EPTH_TEST);g lEnab le(GL_POIN T_SMO OTH);//启用点平滑// gl Enabl e(GL_LINE_SMOOT H); //启用线平滑}void Disp lay(){g lClea r(GL_COLOR_BUFF ER_BI T|GL_DEPTH_BUFF ER_BI T);glMa trixM ode(G L_MOD ELVIE W);glLoa dIden tity();glCol or3f(1.0f,1.0f,1.0f);gl PushM atrix();BmpFo nt(); //绘制位图字符Z ZCg lTran slate f(0,-25,0);gl Scale f( 0.1f, .1f, 1.0);char ch[]="ZHA OZHIC HENG";in t i=0;gl Raste rPos2f(0,0);for(; ch[i]!='\0'; i++){glut Strok eChar acter( GLU T_STR OKE_R OMAN,ch[i]); //绘制笔画字符ZHAO ZHICH ENG}gl PopMa trix();glRa sterP os2f(0,25);f or(i=0 ; c h[i]!= '\0' ; i++){ gl utBit mapCh aract er(GL UT_BI TMAP_HELVE TICA_18,ch[i]); //绘制位图字符ZHAOZ HICHE NG}g lutSw apBuf fers();}void Resh ape(i nt wi dth,i nt he ight){G Lfloa t rat io;if(heigh t ==0) hei ght=1;glVie wport(0, 0, wid th, h eight);glMat rixMo de(GL_PROJ ECTIO N);glLoa dIden tity();rati o = (GLflo at)wi dth / (GLf loat)heigh t; /*建立裁剪区域(左,右,底,顶,近平面,远平面)*/if (wid th <= heig ht )glO rtho(-100.0, 100.0, -100/rati o, 100.0/r atio,1.0,-1.0);el seglOrt ho (-100.0 * ra tio,100.0 * ra tio,-100.0, 100.0, 1.0, -1.0);gl Matri xMode(GL_M ODELV IEW);glL oadId entit y();}voi d Bmp Font(void){G Lubyt e ras ters[32]={0x7f,0xf c,0x3f,0xfc,0x18,0x00,0x08,0x00,0x04,0x00,0x04,0x00,0x02,0x00,0x01,0x00,0x00,0x80,0x00,0x80,0x00,0x40,0x00,0x20,0x00,0x10,0x00,0x10,0x3f,0xf8,0x3f,0xf8};GLu byteraste rs1[32]={0x03,0xe0,0x0f,0x f8,0x1e,0x18,0x38,0x00,0x38,0x00,0x70,0x00,0x70,0x00,0x70,0x00,0x70,0x00,0x70,0x00,0x70,0x00,0x38,0x00,0x38,0x00,0x1e,0x18,0x0f,0xf8,0x03,0xe0};glP ixelS torei(GL_U NPACK_ALIG NMENT, 1);gl Raste rPos2f (-1.0, 1.0);g lBitm ap(16, 16, 0.0, 0.0, 18.0, 0.0, ras ters); glBi tmap(16, 16, 0.0, 0.0, 18.0, 0.0, r aster s);glBit map(16, 16, 0.0, 0.0, 18.0, 0.0, ra sters1); }。
几个opengl立方体绘制案例
⼏个opengl⽴⽅体绘制案例⼀ OpenGL 绘制⼀个⽴⽅体代码#include<GL/glut.h>// 绘制⽴⽅体// 将⽴⽅体的⼋个顶点保存到⼀个数组⾥⾯static const float vertex_list[][3] ={-0.5f, -0.5f, -0.5f,0.5f, -0.5f, -0.5f,-0.5f, 0.5f, -0.5f,0.5f, 0.5f, -0.5f,-0.5f, -0.5f, 0.5f,0.5f, -0.5f, 0.5f,-0.5f, 0.5f, 0.5f,0.5f, 0.5f, 0.5f,};// 将要使⽤的顶点的序号保存到⼀个数组⾥⾯static const GLint index_list[][2] ={{0, 1},{2, 3},{4, 5},{6, 7},{0, 2},{1, 3},{4, 6},{5, 7},{0, 4},{1, 5},{7, 3},{2, 6}};// 绘制⽴⽅体void DrawCube(void){int i,j;glBegin(GL_LINES);for(i=0; i<12; ++i) // 12 条线段{for(j=0; j<2; ++j) // 每条线段 2个顶点{glVertex3fv(vertex_list[index_list[i][j]]);}}glEnd();}static float rotate = 0;static int times = 0;void renderScene(void){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glPushMatrix();//glTranslatef(-0.2, 0, 0); // 平移//glScalef(2, 1, 1); // 缩放times++;if(times > 100){times = 0;}if(times % 100 == 0){rotate += 0.3;}glRotatef(rotate, 0, 1, 0);glRotatef(rotate, 1, 0, 0);glColor3f(0, 0, 1);DrawCube();glPopMatrix();glutSwapBuffers();}void main(int argc, char **argv){glutInit(&argc, argv);glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);glutInitWindowPosition(100,100);glutInitWindowSize(500, 500);glutCreateWindow("GLDemo");glutDisplayFunc(renderScene);glutIdleFunc(renderScene);glutMainLoop();}效果。
OpenGL绘制简单的参数曲线(完)三次B样条曲线
OpenGL绘制简单的参数曲线(完)三次B样条曲线001.#include <math.h>002.#include <gl/glut.h>003.#include <iostream>ing namespace std;005.006.#define NUM_POINTS 6007.#define NUM_SEGMENTS (NUM_POINTS-3) 008.009.struct Point2010.{011. double x;012. double y;013.014. Point2() { ; }015. Point2(int px, int py) { x = px; y = py; }016. void SetPoint2(int px, int py) { x = px; y = py; } 017.};018.019./*全局变量*/020.Point2 vec[NUM_POINTS];021.bool mouseLeftDown = false;022.023./*绘制B样条曲线*/024.void Bspline(int n)025.{026. float f1, f2, f3, f4;027. float deltaT = 1.0 / n;028. float T;029.030. glBegin(GL_LINE_STRIP);031. for (int num = 0; num < NUM_SEGMENTS; num++) 032. {033. for (int i = 0; i <= n; i++) {034.035. T = i * deltaT;036.037. f1 = (-T*T*T + 3*T*T - 3*T + 1) / 6.0;038. f2 =(3*T*T*T - 6*T*T + 4) / 6.0;039. f3 = (-3*T*T*T +3*T*T + 3*T + 1) / 6.0;040. f4 = (T*T*T) / 6.0;041.042. glVertex2f( f1*vec[num].x + f2*vec[num+1].x + f3*vec[num+2].x + f4*vec[num+3].x,043. f1*vec[num].y + f2*vec[num+1].y + f3*vec[num+2].y + f4*vec[num+3].y);044. }045. }046.047. glEnd();048.}049.050.051.void display()052.{053. glClear(GL_COLOR_BUFFER_BIT);054. glLoadIdentity();055.056. glLineWidth(1.5f);057. glColor3f(1.0,0.0,0.0);058. glBegin(GL_LINE_STRIP);059. for(int i = 0;i < NUM_POINTS; i++) 060. {061. glVertex2f(vec[i].x, vec[i].y); 062. }063. glEnd();064.065. glPointSize(10.0f);066. glColor3f(0.0, 0.0, 1.0);067. glBegin(GL_POINTS);068. for(int i = 0;i < NUM_POINTS; i++) 069. {070. glVertex2f(vec[i].x, vec[i].y); 071. }072. glEnd();073.074. Bspline(20);075.076. glFlush();077. glutSwapBuffers();078.}079.080.void init()081.{082. glClearColor(1.0, 1.0, 1.0, 0.0); 083. glShadeModel(GL_FLAT);084.085. vec[0].SetPoint2(200, 400);086. vec[1].SetPoint2(100, 300);087. vec[2].SetPoint2(200, 200);088. vec[3].SetPoint2(250, 300);089. vec[4].SetPoint2(400, 200);090. vec[5].SetPoint2(400, 400);091.}092.093.void reshape(int w, int h)094.{095. glViewport(0, 0, (GLsizei)w, (GLsizei)h);096. glMatrixMode(GL_PROJECTION);097. glLoadIdentity();098. gluOrtho2D(0.0, (GLsizei)w, (GLsizei)h, 0.0);099. glMatrixMode(GL_MODELVIEW);100. glLoadIdentity();101.}102.103.void mouse(int button, int state, int x, int y)104.{105. if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)106. {107. mouseLeftDown = true;108. }109.110. if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)111. {112. mouseLeftDown = false;113. }114.}116.double distance(int x1, int y1, int x2, int y2)117.{118. return sqrt((x1-x2) * (x1 -x2) + (y1-y2) * (y1-y2)); 119.}120.121.void motion(int x, int y)122.{123. if (mouseLeftDown)124. {125. for (int i = 0; i < NUM_POINTS; i++)126. {127. if (distance(vec[i].x, vec[i].y, x, y) < 20)128. {129. vec[i].SetPoint2(x, y);130. }131. }132. }133.134. glutPostRedisplay();135.}136.137.int main(int argc,char** argv)138.{139. glutInit(&argc,argv);140. glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE); 141. glutInitWindowSize(500, 500);142. glutInitWindowPosition (200, 200);143. glutCreateWindow('B-Spline Curve');144. init();146. glutDisplayFunc(display); 147. glutReshapeFunc(reshape); 148. glutMouseFunc(mouse); 149. glutMotionFunc(motion); 150. glutMainLoop();151.152. return 0;153.154.}。
OpenGL中绘制太阳,地球,月亮的代码
glTranslatef(38000000, 0.0f, 0.0f); glutSolidSphere(4345000, 100, 100); glFlush(); glutSwapBuffers(); } void myIdle(void) { ++day; if( day >= 360 ) day = 0; myDisplay(); } int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); char *argv[] = {"hello ", " "}; int argc = 2; // must/should match the number of strings in argv glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutInitWindowPosition(100, 100); glutInitWindowSize(450, 450); glutCreateWindow("system"); glutDisplayFunc(&myDisplay); glutIdleFunc(&myIdle); glutMainLoongl绘制三维图形opengl绘制球体opengl绘制图形太阳地球月亮运行图地球月亮太阳的关系opengl绘制机器人太阳月亮地球太阳月亮地球关系图太阳月亮地球那个大太阳月亮地球一条线
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
glVertex2f(x+size, y+size);
glVertex2f(x-size, y+size);
glVertex2f(x-size, y-size);
glVertex2f(x+size, y-size);
glVertex2i(x,wh-y);
glEnd();
draw_mode=0;
count=0;
}
break;
case(POINTS):
if(count == 0)
{
count++;
xp[0] = x;
yp[0] = y;
}
else
{
void pixel_menu(int);
void fill_menu(int);
int pick(int, int);
/* globals */
GLsizei wh = 500, ww = 500;
GLfloat size = 3.0; /* half side length of square */
{
glBegin(GL_QUADS);
glVertex2i(x, y);
glVertex2i(x+s, y);
glVertex2i(x+s, y+s);
glVertex2i(x, y+s);
yp[0] = y;
break;
case(1):
count++;
xp[1] = x;
yp[1] = y;
break;
if(fill) glBegin(GL_POLYGON);
else glBegin(GL_LINE_LOOP);
glVertex2i(x,wh-y);
glVertex2i(x,wh-yp[0]);
glEnd();
draw_mode=0;
count=0;
}
}
glPopAttrib();
glFlush();
}
}
#define NULL 0
#define LINE 1
#define RECTANGLE 2
#define TRIANGLE 3
#define POINTS 4
#define TEXT 5
#define CIRCLE 6
const int cl = 360;
const float Pi = 3.1415926536f;
else if(x < 2*ww/5) return POINTS;
else if(x < ww/2) return TEXT;
else if(x < 3*ww/5) return CIRCLE;
else return 0;
}
void screen_box(int x, int y, int s )
glVertex2i(xp[0],wh-yp[0]);
glVertex2i(xp[0],wh-y);
glEnd();
draw_mode=0;
count=0;
void myReshape(GLsizei, GLsizei);
void myinit();
void screen_box(int, int, int);
void right_menu(int);
void middle_menu(int);
void color_menu(int);
else glBegin(GL_LINE_LOOP);
for(i=0; i<cl; ++i)
glVertex2f(0.5*xp[0]+0.5*x+0.5*pow(pow(x-xp[0],2)+pow(y-yp[0],2),0.5)*cos(2*Pi/cl*i),wh-(0.5*yp[0]+0.5*y+0.5*pow(pow(x-xp[0],2)+pow(y-yp[0],2),0.5)*sin(2*Pi/cl*i)));
int draw_mode = 0;
int rx, ry; /*raster position*/
GLfloat r = 1.0, g = 1.0, b = 1.0;
int fill = 0;
void drawSquare(int x, int y)
{
y=wh-y;
glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256);
glVertex2i(xp[0],wh-yp[0]);
glEnd();
draw_mode=0;
count=0;
}
break;
case(RECTANGLE):
display();
glFlush();
ww = w;
wh = h;
}
void myinit()
{
glViewport(0,0,ww,wh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,w,h);
glClearColor (0.8, 0.8, 0.8, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}
break;
case (TRIANGLE):
switch(count)
{
case(0):
count++;
xp[0] = x;
{
drawSquare(x,y);
count++;
}
break;
case(TEXT):
{
rx=x;
glOrtho(0.0, (GLdouble) ww , 0.0, (GLdouble) wh , -1.0, 1.0);
glClearColor (0.8, 0.8, 0.8, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
ry=wh-y;
glRasterPos2i(rx,ry);
count=0;
}
case(CIRCLE):
if(count == 0)
{
count++;
xp[0] = x;
yp[0] = y;
}
else
{
int i=0;
if(fill) glBegin(GL_POLYGON);
int pick(int x, int y)
{
y = wh - y;
if(y < wh-ww/10) return 0;
else if(x < ww/10) return LINE;
else if(x < ww/5) return RECTANGLE;
else if(x < 3*ww/10) return TRIANGLE;
case(2):
if(fill) glBegin(GL_POLYGON);
else glBegin(GL_LINE_LOOP);
glVertex2i(xp[0],wh-yp[0]);
glVertex2i(xp[1],wh-yp[1]);
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
void mouse(int, int, int, int);
void key(unsigned char, int, int);
void display();
void drawSquare(int, int);
xp[0] = x;
yp[0] = y;
}
else
{
glBegin(GL_LINES);
glVertex2i(x,wh-y);
glEnd();
}
void myReshape(GLsizei w, GLsizei h)
{
ww=w;
wh=h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0);
}
void mouse(int btn, int state, ount;
int where;
static int xp[2],yp[2];
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
draw_mode = where;
}
else switch(draw_mode)
{
case(LINE):
if(count==0)
{
count++;
{
glPushAttrib(GL_ALL_ATTRIB_BITS);