opengl编程指南程序_第1页
opengl编程指南程序_第2页
opengl编程指南程序_第3页
opengl编程指南程序_第4页
opengl编程指南程序_第5页
已阅读5页,还剩44页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include "stdafx.h"#include <glut.h>#include <gl/gl.h>void display() glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glColor4f(1.0,1.0,0.0,1.0); / set the point color glPointSize(10); glBegin(GL_POLYGON); glVertex2f(0.0,0.0); glVertex2f(0.0,3.0); glVertex2f(4.0,3.0

2、); glVertex2f(6.0,1.5); glVertex2f(4.0,0.0); glEnd(); glFlush();int main(int argc, char* argv) glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100, 100); glutInitWindowSize(400, 400); glutCreateWindow("第一个OpenGL程序"); glutDisplayFunc(display);

3、glutMainLoop(); return 0;#include "stdafx.h"#include <glut.h>#include <gl/gl.h>void display() glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glColor4f(1.0,0.0,0.0,1.0); / set the point color glPointSize(10); glBegin(GL_POINTS); glVertex3f(-0.5,-0.5,0.0); glVertex3

4、f(0.5,-0.5,0.0); glVertex3f(0.5,0.5,0.0); glVertex3f(-0.5,0.5,0.0); glEnd(); glFlush();int main(int argc, char* argv) glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100, 100); glutInitWindowSize(400, 400); glutCreateWindow("第一个OpenGL程序"); glu

5、tDisplayFunc(display); glutMainLoop(); return 0;#include "stdafx.h"#include <glut.h>#include <gl/gl.h>void display() glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glColor4f(1.0,1.0,0.0,1.0); glPointSize(10); glBegin(GL_TRIANGLES); glVertex3f(0.25,0.25,0.0); glVer

6、tex3f(0.75,0.25,0.0); glVertex3f(0.75,0.75,0.0); glEnd(); glFlush();int main(int argc, char* argv) glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100, 100); glutInitWindowSize(400, 400); glutCreateWindow("第台?一?个?OpenGL程ì序ò"); glutDi

7、splayFunc(display); glutMainLoop(); return 0;#include <glut.h>#include <gl/gl.h>void display()/ 绘?制?函数簓 glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glColor4f(1.0,1.0,0.0,1.0); glPointSize(10);glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);glBegin(GL_POLYGON);glEdgeFlag(GL_TRUE

8、);glVertex3f (0.25,0.25,0.0);glEdgeFlag(GL_FALSE);glVertex3f(0.75,0.25,0.0);glEdgeFlag(GL_TRUE);glVertex3f(0.75,0.75,0.0); glEnd(); glFlush();int main(int argc, char* argv) glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100, 100); glutInitWindowSize(40

9、0, 400); glutCreateWindow("标括?记?多à变?形?边?界?边?"); glutDisplayFunc(display); glutMainLoop(); return 0;#include "stdafx.h"#pragma comment( lib, "opengl32.lib" )#pragma comment( lib, "glu32.lib" )#pragma comment( lib, "glut32.lib")#include <stdlib

10、.h>#include <glut.h>void background(void)glClearColor(0.0,0.0,0.0,0.0);/设?置?背?景°颜?色?为a黑ú色?void myDisplay(void)glClear(GL_COLOR_BUFFER_BIT);/buffer设?置?为a颜?色?可é写glBegin(GL_TRIANGLES);/开a始?画-三角?形?glShadeModel(GL_SMOOTH);/设?置?为a光a滑?明÷暗悝?模式?glColor3f(1.0,0.0,0.0);/设?置?第台?一?个?

11、顶¥点?为a红ì色?glVertex2f(-1.0,-1.0);/设?置?第台?一?个?顶¥点?的?坐?标括?为a(辍?1.0,?-1.0)?glColor3f(0.0,1.0,0.0);/设?置?第台?二t个?顶¥点?为a绿ì色?glVertex2f(0.0,-1.0);/设?置?第台?二t个?顶¥点?的?坐?标括?为a(辍?.0,?-1.0)?glColor3f(0.0,0.0,1.0);/设?置?第台?三个?顶¥点?为a蓝?色?glVertex2f(-0.5,1.0);/设?置?第台?三个?顶¥点?的?坐?标括?为a(辍?0.5,?1.0)?glEnd();/三角?形

12、?结á束?glFlush();/强?制?OpenGL函数簓在ú有瓺限T时骸?间?内ú运?行Dvoid myReshape(GLsizei w,GLsizei h)glViewport(0,0,w,h);/设?置?视酣?口úglMatrixMode(GL_PROJECTION);/指?明÷当獭?前°矩?阵ó为aGL_PROJECTIONglLoadIdentity();/将?当獭?前°矩?阵ó置?换?为a单蹋?位?阵óif(w <= h)gluOrtho2D(-1.0,1.5,-1.5,1.

13、5*(GLfloat)h/(GLfloat)w);/定¨义?二t维?正y视酣?投?影?矩?阵óelsegluOrtho2D(-1.0,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5);glMatrixMode(GL_MODELVIEW);/指?明÷当獭?前°矩?阵ó为aGL_MODELVIEWint main(int argc,char * argv)/*初?始?化*/glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWin

14、dowSize(400,400);glutInitWindowPosition(200,200);/*创洹?建¨窗洹?口ú*/glutCreateWindow("Triangle");/*绘?制?与?显?示?*/background();glutReshapeFunc(myReshape);glutDisplayFunc(myDisplay);glutMainLoop();return(0);#include "stdafx.h"#pragma comment( lib, "opengl32.lib" )#prag

15、ma comment( lib, "glu32.lib" )#pragma comment( lib, "glut32.lib")#include <stdlib.h>#include <glut.h>static int shoulder = 0, elbow = 0;/shoulder:肩部角度,elbow:肘部角度void init(void) glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glShadeModel(GL_FLAT);void display(void) glClear(GL_C

16、OLOR_BUFFER_BIT); glPushMatrix();/把当前的变换矩阵压入opengl内部栈中,用以保存当前矩阵 /画机器人的上臂 glTranslatef(-1.0f, 0.0f, 0.0f);/用平移矩阵乘当前矩阵,格式为glTranslatef(x,y,z) glRotatef(GLfloat) shoulder, 0.0f, 0.0f, 1.0f);/用旋转矩阵乘当前矩阵,格式为glRotatef(角度,x轴,y轴,z轴),这里是绕z轴旋转 glTranslatef(1.0f, 0.0f, 0.0f);/再用平移矩阵乘当前矩阵,注意顺序 glPushMatrix();/变

17、换矩阵压栈 glScalef(2.0f, 0.4f, 1.0f);/用缩放矩阵乘以当前矩阵,格式为 glRotatef(缩放比例,y缩放比例,z缩放比例) glutWireCube(1.0f);/glut库函数,画一个三维Cube,参数为边长 glPopMatrix();/弹栈,现在矩阵恢复到使用缩放前的样子/画机器人的前臂,请注意平移矩阵和旋转矩阵的变化 glTranslatef(1.0f, 0.0f, 0.0f); glRotatef(GLfloat) elbow, 0.0f, 0.0f, 1.0f); glTranslatef(1.0f, 0.0f, 0.0f); glPushMatri

18、x(); glScalef(2.0f, 0.4f, 1.0f); glutWireCube(1.0f); glPopMatrix(); glPopMatrix(); glFlush();void reshape (int width, int height) glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(65.0f, (GLfloat)width/(GLfloat)height, 1.0f, 20.0f);/建立一个透视投影视图体,格式为:gluPer

19、spective(视域的角度,宽高比,视点到近裁剪面的距离(总为正) glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -5.0f);/用平移矩阵乘当前矩阵,注意,这会将所有绘制过程中绘制的物体平移void keyboard(unsigned char key, int x, int y) switch (key) case 'a':/处理四个按键,改变旋转角度,转动手臂 shoulder = (shoulder + 5) % 360; glutPostRedisplay();/重画 b

20、reak; case 'd': shoulder = (shoulder - 5) % 360; glutPostRedisplay(); break; case 'q': elbow = (elbow + 5) % 360; glutPostRedisplay(); break; case 'e': elbow = (elbow - 5) % 360; glutPostRedisplay(); break; case 'x': exit(0); break; default: break; int main(int argc,

21、char* argv) glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("Transform"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return

22、0;#include "stdafx.h"#pragma comment( lib, "opengl32.lib" )#pragma comment( lib, "glu32.lib" )#pragma comment( lib, "glut32.lib")#include <stdlib.h>#include <glut.h>static int year = 0,day = 0;void init(void) glClearColor(0.0, 0.0, 0.0, 0.0); glSha

23、deModel(GL_FLAT);void display(void) glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,1.0); glPushMatrix(); glutWireSphere(1.0,20,16); glRotatef(GLfloat) year, 0.0, 1.0, 0.0); glTranslatef(2.0, 0.0, 0.0); glRotatef(GLfloat) day, 0.0, 1.0, 0.0); glutWireSphere(0.2,10,8); glPopMatrix(); glutSwapBuffers(

24、);void reshape (int w, int h) glViewport(0, 0, (GLsizei)w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(65.0f, (GLfloat)w/(GLfloat)h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,20.0);void keyboard(unsigned char k

25、ey, int x, int y) switch (key) case 'd': day=(day+10)%360;glutPostRedisplay(); break; case 'D': day=(day-10)%360; glutPostRedisplay(); break; case 'y': year=(year+5)%360;glutPostRedisplay(); break; case 'Y': year=(year-5)%360; glutPostRedisplay(); break; default: brea

26、k; int main(int argc, char* argv) glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow(argv0); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); re

27、turn 0;#include "stdafx.h"#pragma comment( lib, "opengl32.lib" )#pragma comment( lib, "glu32.lib" )#pragma comment( lib, "glut32.lib")#include <stdlib.h>#include <glut.h>void init(void) glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_FLAT);void d

28、isplay(void)GLdouble eqn4=0.0,1.0,0.0,0.0;GLdouble eqn24=1.0,1.0,0.0,0.0;glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0,1.0,1.0);glPushMatrix();glTranslatef(0.0,0.0,-5.0);glClipPlane(GL_CLIP_PLANE0,eqn);glEnable(GL_CLIP_PLANE0);glClipPlane(GL_CLIP_PLANE1,eqn2);glEnable(GL_CLIP_PLANE1);glRotatef(90.0,1.0

29、,0.0,0.0);glutWireSphere(1.0,20,16);glPopMatrix();glFlush();void reshape (int w, int h) glViewport(0, 0, (GLsizei)w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); int main(int argc, char* argv) glutIni

30、t(&argc, argv); glutInitDisplayMode(GLUT_SINGLE| GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow(argv0); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0;Opengl相关库函数:GLU:设置视点矩阵和投影矩阵、执行多边形网格化以及渲染曲面等任务。GLX:X窗口系统WGL;win

31、dows和OPengl之间的接口。GLUT:opengl'实用工具包。包含头文件件:#include<GL/gl.h> #include<windows.h> #include<GL/glu.h> #incude”glext.h”GLX时:#include<X11/Xlib.h> #include<GL/glx.h>WGL时: #include<windows.h>使用GLUT: #include<GL/glut.h>标准C语言库系统调用,与图形无关的头文件#include<stdlib.h>

32、;#include<stdio.h>1. 窗口管理:glutInit(int*argc,char*argv):初始化glut并处理命令行参数。 glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH) glutInitWindowPosition(int x,int y):指定窗口左上角在屏幕上的位置。 glutInitWindowSize(int width,int size)指定窗口的大小单位为像素 Int glutCreateWindow(char *string):使用一个opengl场景创建一个窗口2. 显示回调函数glut

33、DisplayFunc(void(*func)(void) glutPostDisplayFunc(void)3.运行程序:glutMainLoop(void)范例1.2一个使用GLUT的简单的OPengl程序:hello.c#include "stdafx.h"#include <stdlib.h>#include<stdio.h>#include <glut.h> #include<windows.h>void display(void)glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0,

34、1.0,1.0);glBegin(GL_POLYGON);glVertex3f(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();void init (void)glClearColor(0.0,0.0,0.0,0.0);glMatrixMode(GL_PROJECTION);glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);int main (int argc ,char*argv)glutInit(&a

35、mp;argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(250,250);glutInitWindowPosition(100,100);glutCreateWindow("hello");init();glutDisplayFunc(display);glutMainLoop();return 0; 4. 处理输入事件glutReshapeFunc(void(*func)(int w,int h):指定窗口大小发生改变时应采取的措施glutKeyboardFunc(void(*f

36、unc)(unsigned char key,int x,int y)glutMouseFunc(void(*func)(int button,int state,int x,int y)指定当特定的键和鼠标按钮被按下或者松开时应调用的调用。glutMotionFunc(void(*func)(intx,inty)注册了当用户按下鼠标按钮并移动鼠标时应调用的回调函数。5. 管理后台处理glutIdleFunc(void(*func)(void)6. 绘制三维物体(后一种指定了面法线)void glutWireCube(GLdouble size);Void glutSolidCube(GLdo

37、uble size);void glutWireSphere(GLdouble radius,GLint slices,GLint stacks);void glutSolidSphere(GLdouble radius,GLint slices,GLint stacks);范例1.3使用双缓存的程序#include "stdafx.h"#pragma comment( lib, "opengl32.lib" )#pragma comment( lib, "glu32.lib" )#pragma comment( lib, "

38、;glut32.lib")#include <stdlib.h>#include <glut.h>static GLfloat spin = 0.0;void init(void)glClearColor(0.0,0.0,0.0,0.0);glShadeModel(GL_FLAT);void display(void)glClear(GL_COLOR_BUFFER_BIT);glPushMatrix();glRotatef(spin,0.0,0.0,1.0);glColor3f(1.0,1.0,1.0);glRectf(-25.0,-25.0,25.0,25.

39、0);glPopMatrix();glutSwapBuffers();void spinDisplay(void)spin = spin+2.0;if(spin>360.0)spin = spin - 360.0;glutPostRedisplay();void reshape(int w,int h)glViewport(0,0,(GLsizei)w,(GLsizei) h);glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-50.0,50.0,-50.0,50.0,-1.0,1.0);glMatrixMode(GL_MODEL

40、VIEW);glLoadIdentity();void mouse(int button,int state,int x,int y)switch(button)case GLUT_LEFT_BUTTON:if (state=GLUT_DOWN)glutIdleFunc(spinDisplay);break;case GLUT_MIDDLE_BUTTON:if (state=GLUT_DOWN)glutIdleFunc(NULL);break;default:break;int main (int argc ,char*argv)glutInit(&argc,argv);glutIni

41、tDisplayMode(GLUT_DOUBLE|GLUT_RGB);glutInitWindowSize(250,250);glutInitWindowPosition(100,100);glutCreateWindow("argv0");init();glutDisplayFunc(display);glutReshapeFunc(reshape);glutMouseFunc(mouse);glutMainLoop();return 0;1.RGBA模式窗口的背景设置为黑色glClearColor(0.0,0.0,0.0,0.0);清空颜色设置为黑色glClear(GL

42、_COLOR_BUFFER_BIT);窗口的背景设置为当前清空颜色。2.清除颜色和深度缓存glClearColor(0.0,0.0,0.0,0.0);glClearDepth(1.0);glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);3. 首次打开窗口、移动窗口、改变窗口大小时系统都将发送一个事件,调用向glutReshapeFunc()注册的函数。重新创建用作渲染画布的矩形区域;定义绘制物体时使用的坐标系。范例2.1Void reshape(int w,int h)glViewport(0,0,(GLsizei) w,(GLsizei ) h

43、);调整像素矩形glMatriMode(GL_PROJECTION);调整绘图坐标系glLoadIdentity();gluOrtho2D(0.0,(GLdouble) w,0.0,(GLdouble) h);范例2.3绘制填充多边形#include "stdafx.h"#include<glut.h>#include<stdlib.h>#include<stdio.h>#include<windows.h>void display(void)glClearColor(0.0,0.0,0.0,0.0);glClear(GL_C

44、OLOR_BUFFER_BIT);glColor3f(1.0,1.0,1.0);/*glPointSize(10);*/glBegin(GL_POLYGON); glVertex2f(0.0,0.0); glVertex2f(0.0,3.0); glVertex2f(4.0,3.0); glVertex2f(6.0,1.5); glVertex2f(4.0,0.0); glEnd(); glFlush();void init(void)glClearColor(0.0,0.0,0.0,0.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrt

45、ho(-5.0,10.0,-3.0,5.0,-1.0,1.0);int main (int argc ,char*argv)glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(250,250);glutInitWindowPosition(100,100);glutCreateWindow("hello");init();glutDisplayFunc(display);glutMainLoop();return 0;#include "stda

46、fx.h"#include<glut.h>#include<stdlib.h>#include<stdio.h>#include<windows.h>#define drawOneLine(x1,y1,x2,y2) glBegin(GL_LINES);glVertex2f(x1),(y1);glVertex2f(x2),(y2); glEnd();void init(void)glClearColor(0.0,0.0,0.0,0.0);glShadeModel(GL_FLAT);void display(void)int i;glCle

47、ar(GL_COLOR_BUFFER_BIT);glColor3f(1.0,1.0,1.0);glEnable(GL_LINE_STIPPLE);glLineStipple(1,0x0101);drawOneLine(50.0,125.0,150.0,125.0);glLineStipple(1,0x00FF);drawOneLine(150.0,125.0,250.0,125.0);glLineStipple(1,0x1c47);drawOneLine(250.0,125.0,350.0,125.0); glLineWidth(5.0);glLineStipple(1,0x0101);dra

48、wOneLine(50.0,100.0,150.0,100.0);glLineStipple(1,0x00FF);drawOneLine(150.0,100.0,250.0,100.0);glLineStipple(1,0x1C47);drawOneLine(250.0,100.0,350.0,100.0);glLineWidth(1.0);glLineStipple(1,0x1C47);glBegin(GL_LINE_STRIP);for(i=0;i<7;i+)glVertex2f(50.0+(GLfloat)i*50.0),75.0); glEnd(); for(i=0;i<6

49、;i+) drawOneLine(50.0+(GLfloat)i*50.0),50.0, 50.0+(GLfloat)(i+1)*50.0),50.0); glLineStipple(5,0x1C47); drawOneLine(50.0,25.0,350.0,25.0); glDisable(GL_LINE_STIPPLE); glFlush(); void reshape(int w,int h) glViewport(0,0,(GLsizei) w,(GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2

50、D(0.0,(GLdouble)w,0.0,(GLdouble) h); int main(int argc,char*argv) glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(400,150); glutInitWindowPosition(100,100); glutCreateWindow(argv0); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); ret

51、urn 0; #include "stdafx.h"#include<glut.h>#include<stdlib.h>#include<stdio.h>#include<windows.h>void display(void)GLubyte fly =0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x80,0x01,0xC0,0x06,0xC0,0x03,0x60,0x04,0x60,0x06,0x20,0x04,0x30,0x0C,0x20,0x04,0x18,0x18,0x20,0x04,0x0C,0x30,0x20,0x04,0x06,0x60,0x20,0x44,0x03,0xC0,0x22,0x44,0x01,0x80,0x22,0x44,0x01,0x80,0x22,0x44,0x01,0x80,0x22,0x44,0x01,0x80

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论