首页 > 代码库 > Lane-学习OpenGL(3)
Lane-学习OpenGL(3)
运行环境:
(1)Windows 7
(2)CodeBlocks
(3)GLUT
2.三维混合
3.抗锯齿直线
(1)Windows 7
(2)CodeBlocks
(3)GLUT
(4)Author:Lane
2014-12-17
1.色彩混合
#include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include <cstdlib> #include <cstdio> #include <cmath> static int leftFirst = GL_TRUE; static void init(void){ glEnable(GL_BLEND);//开启混合 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);//混合因子 glShadeModel(GL_FLAT); glClearColor(0.0,0.0,0.0,0.0); } static void drawLeftTriangle(void){ glBegin(GL_TRIANGLES); glColor4f(1.0,1.0,0.0,0.75);//Alpha值0.75 glVertex3f(0.1,0.9,0.0); glVertex3f(0.1,0.1,0.0); glVertex3f(0.7,0.5,0.0); glEnd(); } static void drawRightTriangle(void){ glBegin(GL_TRIANGLES); glColor4f(0.0,1.0,1.0,0.75);//Alpha值0.75 glVertex3f(0.9,0.9,0.0); glVertex3f(0.3,0.5,0.0); glVertex3f(0.9,0.1,0.0); glEnd(); } void display(void){ glClear(GL_COLOR_BUFFER_BIT); if(leftFirst){ drawLeftTriangle(); drawRightTriangle(); }else{ drawRightTriangle(); drawLeftTriangle(); } glFlush(); } void reshape(int w,int h){ glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w <= h) gluOrtho2D(0.0,1.0,0.0,1.0*(GLfloat)h/(GLfloat)w); else gluOrtho2D(0.0,1.0*(GLfloat)w/(GLfloat)h,0.0,1.0); } void keyboard(unsigned char key,int x,int y){ switch(key){ case 't': case 'T': leftFirst =!leftFirst; glutPostRedisplay(); break; case 27: exit(0); break; default: break; } } int main(int argc,char** argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(200,200); glutCreateWindow(argv[0]); init(); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutMainLoop(); return 0; }
2.三维混合
#include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include <cstdlib> #include <cstdio> #include <cmath> #define MAXZ 8.0 #define MINZ -8.0 #define ZINC 0.4 static float solidZ = MAXZ; static float transparentZ = MINZ; static GLuint sphereList,cubeList; static void init(void){ GLfloat mat_specular[] = {1.0,1.0,1.0,0.15}; GLfloat mat_shininess[] = {100,0}; GLfloat position[] = {0.5,0.5,1.0,0.0}; glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular); glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess); glLightfv(GL_LIGHT0,GL_POSITION,position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); sphereList = glGenLists(1); glNewList(sphereList,GL_COMPILE); glutSolidSphere(0.4,16,16); glEndList(); cubeList = glGenLists(1); glNewList(cubeList,GL_COMPILE); glutSolidCube(0.6); glEndList(); } void display(void){ GLfloat mat_solid[] = {0.75,0.75,0.0,1.0}; GLfloat mat_zero[] = {0.0,0.0,0.0,1.0}; GLfloat mat_transparent[] = {0.0,0.8,0.8,0.6}; GLfloat mat_emission[] = {0.0,0.3,0.3,0.6}; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef(-0.15,-0.15,solidZ); glMaterialfv(GL_FRONT,GL_EMISSION,mat_zero); glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_solid); glCallList(sphereList); glPopMatrix(); glPushMatrix(); glTranslatef(0.15,0.15,transparentZ); glRotatef(15.0,1.0,1.0,0.0); glRotatef(30.0,0.0,1.0,0.0); glMaterialfv(GL_FRONT,GL_EMISSION,mat_emission); glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_transparent); glEnable(GL_BLEND); glDepthMask(GL_FALSE); glBlendFunc(GL_SRC_ALPHA,GL_ONE); glCallList(cubeList); glDepthMask(GL_TRUE); glDisable(GL_BLEND); glPopMatrix(); glutSwapBuffers(); } void reshape(int w,int h){ glViewport(0,0,(GLint)w,(GLint)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); else glOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5,-10.0,10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void animate(void){ if(solidZ <= MINZ || transparentZ >= MAXZ) glutIdleFunc(NULL); else{ solidZ -= ZINC; transparentZ += ZINC; glutPostRedisplay(); } } void keyboard(unsigned char key,int x,int y){ switch(key){ case 'a': case 'A': solidZ = MAXZ; transparentZ = MINZ; glutIdleFunc(animate); break; case 'r': case 'R': solidZ = MAXZ; transparentZ = MINZ; glutPostRedisplay(); break; case 27: exit(0); break; } } int main(int argc,char ** argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(500,500); glutCreateWindow(argv[0]); init(); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutMainLoop(); return 0; }
3.抗锯齿直线
#include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include <cstdlib> #include <cstdio> #include <cmath> static float rotAngle = 0.0; void init(void){ GLfloat values[2]; glGetFloatv(GL_LINE_WIDTH_GRANULARITY,values); printf("GL_LINE_WIDTH_GRANULARITY value is %3.1f \n ",values[0]); glGetFloatv(GL_LINE_WIDTH_RANGE,values); printf("GL_LINE_WIDTH_RANGE values are %3.1f %3.1f \n ",values[0],values[1]); glEnable(GL_LINE_SMOOTH); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glHint(GL_LINE_SMOOTH_HINT,GL_DONT_CARE); glLineWidth(1.5); glClearColor(0.0,0.0,0.0,0.0); } void display(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0,1.0,0.0); glPushMatrix(); glRotatef(-rotAngle,0.0,0.0,0.1); glBegin(GL_LINES); glVertex2f(-0.5,0.5); glVertex2f(0.5,-0.5); glEnd(); glPopMatrix(); glColor3f(0.0,0.0,1.0); glPushMatrix(); glRotatef(rotAngle,0.0,0.0,0.1); glBegin(GL_LINES); glVertex2f(0.5,0.5); glVertex2f(-0.5,-0.5); glEnd(); glPopMatrix(); glFlush(); } void reshape(int w,int h){ glViewport(0,0,(GLint)w,(GLint)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w <= h) gluOrtho2D(-1.0,1.0,-1.0*(GLfloat)h/(GLfloat)w,1.0*(GLfloat)h/(GLfloat)w); else gluOrtho2D(-1.0*(GLfloat)w/(GLfloat)h,1.0*(GLfloat)w/(GLfloat)h,-1.0,1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void keyboard(unsigned char key,int x,int y){ switch(key){ case 'r': case 'R': rotAngle += 20; if(rotAngle >= 360.0) rotAngle = 0.0; glutPostRedisplay(); break; case 27: exit(0); break; default: break; } } int main(int argc,char** argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(200,200); glutCreateWindow(argv[0]); init(); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutMainLoop(); return 0; }
4.5个雾化球体
#include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include <cstdlib> #include <cstdio> #include <cmath> static GLint fogMode; static void init(void){ GLfloat position[] = {0.5,0.5,3.0,0.0}; glEnable(GL_DEPTH_TEST); glLightfv(GL_LIGHT0,GL_POSITION,position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); { GLfloat mat[3] = {0.1745,0.01175,0.1175}; glMaterialfv(GL_FRONT,GL_AMBIENT,mat); mat[0] = 0.61424; mat[1] = 0.04136; mat[2] = 0.04136; glMaterialfv(GL_FRONT,GL_DIFFUSE,mat); mat[0] = 0.727811; mat[1] = 0.626959; mat[2] = 0.626959; glMaterialfv(GL_FRONT,GL_SPECULAR,mat); glMaterialf(GL_FRONT,GL_SHININESS,0.6*128.0); } glEnable(GL_FOG); { GLfloat fogColor[4] = {0.5,0.5,0.5,1.0}; fogMode = GL_EXP; glFogi(GL_FOG_MODE,fogMode); glFogfv(GL_FOG_COLOR,fogColor); glFogf(GL_FOG_DENSITY,0.35); glHint(GL_FOG_HINT,GL_DONT_CARE); glFogf(GL_FOG_START,1.0); glFogf(GL_FOG_END,5.0); } glClearColor(0.5,0.5,0.5,1.0); } static void renderSphere(GLfloat x,GLfloat y,GLfloat z){ glPushMatrix(); glTranslatef(x,y,z); glutSolidSphere(0.4,16,16); glPopMatrix(); } void display(void){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); renderSphere(-2,-0.5,-1.0); renderSphere(-1,-0.5,-2.0); renderSphere(0.0,-0.5,-4.0); renderSphere(1.0,-0.5,-4.0); renderSphere(2.0,-0.5,-5.0); glFlush(); } void reshape(int w,int h){ glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w <= h) glOrtho(-2.5,2.5,-2.5*(GLfloat)h/(GLfloat)w,2.5*(GLfloat)h/(GLfloat)w,-10.0,10.0); else glOrtho(-2.5*(GLfloat)w/(GLfloat)h,2.5*(GLfloat)w/(GLfloat)h,-2.5,2.5,-10.0,10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void keyboard(unsigned char key,int x,int y){ switch(key){ case 'f': case 'F': if(fogMode == GL_EXP){ fogMode = GL_EXP2; printf("Fog mode is GL_EXP2 \n "); }else if(fogMode == GL_EXP2){ fogMode = GL_LINEAR; printf("Fog mode is GL_LINEAR \n "); }else if(fogMode == GL_LINEAR){ fogMode = GL_EXP; printf("Fog mode is GL_EXP \n "); } glFogi(GL_FOG_MODE,fogMode); glutPostRedisplay(); break; case 27: exit(0); break; default: break; } } int main(int argc,char** argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(500,500); glutCreateWindow(argv[0]); init(); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutMainLoop(); return 0; }
5.创建显示列表
#include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include <cstdlib> #include <cstdio> #include <cmath> GLuint theTorus; static void torus(int numc,int numt){ int i,j,k; double s,t,x,y,z,twopi; twopi = 2*(double)M_PI; for(i = 0; i < numc; i++){ glBegin(GL_QUAD_STRIP); for(j = 0; j <= numt; j++){ for(k = 1; k >= 0; k--){ s = (i + k) % numc + 0.5; t = j % numt; x = (1 + 0.1 * cos(s * twopi/numc))*cos(t*twopi/numt); y = (1 + 0.1 * cos(s * twopi/numc))*sin(t*twopi/numt); z = 0.1 * sin(s * twopi / numc); glVertex3f(x,y,z); } } glEnd(); } } static void init(void){ theTorus = glGenLists(1); glNewList(theTorus,GL_COMPILE); torus(8,25); glEndList(); glShadeModel(GL_FLAT); glClearColor(0.0,0.0,0.0,0.0); } void display(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,1.0); glCallList(theTorus); glFlush(); } void reshape(int w,int h){ glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(30,(GLfloat)w/x(GLfloat)h,1.0,100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0,0,10,0,0,0,0,1,0); } void keyboard(unsigned char key,int x,int y){ switch(key){ case 'x': case 'X': glRotatef(30.0,1.0,0.0,0.0); glutPostRedisplay(); break; case 'y': case 'Y': glRotatef(30.0,0.0,1.0,0.0); glutPostRedisplay(); break; case 'i': case 'I': glLoadIdentity(); gluLookAt(0,0,10,0,0,0,0,1,0); glutPostRedisplay(); break; case 27: exit(0); break; } } int main(int argc,char** argv){ glutInitWindowSize(200,200); glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutCreateWindow(argv[0]); init(); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutMainLoop(); return 0; }
6.多个显示列表
#include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include <cstdlib> #include <cstdio> #include <cmath> #define PT 1 #define STROKE 2 #define END 3 typedef struct charpoint{ GLfloat x,y; int type; }CP; CP Adata[] = { {0,0,PT},{0,9,PT},{1,10,PT},{4,10,PT}, {5,9,PT},{5,0,STROKE},{0,5,PT},{5,5,END} }; CP Edata[] = { {5,0,PT},{0,0,PT},{0,10,PT},{5,10,STROKE},{0,5,PT},{4,5,END} }; CP Pdata[] = { {0,0,PT},{0,10,PT},{4,10,PT},{5,9,PT},{5,6,PT},{4,5,PT},{0,5,END} }; CP Rdata[] = { {0,0,PT},{0,10,PT},{4,10,PT},{5,9,PT}, {5,6,PT},{4,5,PT},{0,5,STROKE},{3,5,PT},{5,0,END} }; CP Sdata[] = { {0,1,PT},{1,0,PT},{4,0,PT},{5,1,PT},{5,4,PT}, {4,5,PT},{1,5,PT},{0,6,PT},{0,9,PT},{1,10,PT}, {4,10,PT},{5,9,END} }; static void drawLetter(CP *l){ glBegin(GL_LINE_STRIP); while(l){ switch(l->type){ case PT: glVertex2fv(&l->x); break; case STROKE: glVertex2fv(&l->x); glEnd(); glBegin(GL_LINE_STRIP); break; case END: glVertex2fv(&l->x); glEnd(); glTranslatef(8.0,0.0,0.0); return; } l++; } } static void init(void){ GLuint base; glShadeModel(GL_FLAT); base = glGenLists(128); glListBase(base); glNewList(base+'A',GL_COMPILE); drawLetter(Adata); glEndList(); glNewList(base+'E',GL_COMPILE); drawLetter(Edata); glEndList(); glNewList(base+'P',GL_COMPILE); drawLetter(Pdata); glEndList(); glNewList(base+'R',GL_COMPILE); drawLetter(Rdata); glEndList(); glNewList(base+'S',GL_COMPILE); drawLetter(Sdata); glEndList(); glNewList(base+' ',GL_COMPILE); glTranslatef(8.0,0.0,0.0); glEndList(); } char *test1 = "A SPARE SERAPE APPEARS AS "; char *test2 = "APES PREPARE RARE PEPPERS "; static void printStrokedString(char *s){ GLsizei len = strlen(s); glCallLists(len,GL_BYTE,(GLbyte*)s); } void display(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,1.0); glPushMatrix(); glScalef(2.0,2.0,2.0); glTranslatef(10.0,30.0,0.0); printStrokedString(test1); glPopMatrix(); glPushMatrix(); glScalef(2.0,2.0,2.0); glTranslatef(10.0,13.0,0.0); printStrokedString(test2); glPopMatrix(); glFlush(); } void reshape(int w,int h){ glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h); } void keyboard(unsigned char key,int x,int y){ switch(key){ case ' ': glutPostRedisplay(); break; case 27: exit(0); break; } } int main(int argc,char** argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(440,120); glutCreateWindow(argv[0]); init(); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutMainLoop(); return 0; }
7.自动生成纹理坐标
#include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include <cstdlib> #include <cstdio> #include <cmath> #define stripeImageWidth 32 GLubyte stripeImage [4*stripeImageWidth]; static GLuint texName; void makeStripeImage(void){ int j; for(j = 0; j < stripeImageWidth; j++){ stripeImage[4*j] = (GLubyte)((j<=4)?255:0); stripeImage[4*j+1] = (GLubyte)((j>4)?255:0); stripeImage[4*j+2] = (GLubyte)0; stripeImage[4*j+3] = (GLubyte)255; } } static GLfloat xequalzero[] = {1.0,0.0,0.0,0.0}; static GLfloat slanted[] = {1.0,1.0,1.0,0.0}; static GLfloat *currentCoeff; static GLenum currentPlane; static GLint currentGenMode; void init(void){ glClearColor(0.0,0.0,0.0,0.0); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); makeStripeImage(); glPixelStorei(GL_UNPACK_ALIGNMENT,1); glGenTextures(1,&texName); glBindTexture(GL_TEXTURE_1D,texName); glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexImage1D(GL_TEXTURE_1D,0,GL_RGBA,stripeImageWidth,0,GL_RGBA,GL_UNSIGNED_BYTE,stripeImage); glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE); currentCoeff = xequalzero; currentGenMode = GL_OBJECT_LINEAR; currentPlane = GL_OBJECT_PLANE; glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,currentGenMode); glTexGenfv(GL_S,currentPlane,currentCoeff); glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_1D); glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glFrontFace(GL_CW); glCullFace(GL_BACK); glMaterialf(GL_FRONT,GL_SHININESS,64.0); } void display(void){ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(45.0,0.0,0.0,1.0); glBindTexture(GL_TEXTURE_1D,texName); glutSolidTeapot(2.0); glPopMatrix(); glFlush(); } void reshape(int w,int h){ glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w<=h) glOrtho(-3.5,3.5,-3.5*(GLfloat)h/(GLfloat)w,3.5*(GLfloat)h/(GLfloat)w,-3.5,3.5); else glOrtho(-3.5*(GLfloat)h/(GLfloat)w,3.5*(GLfloat)h/(GLfloat)w,-3.5,3.5,-3.5,3.5); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void keyboard(unsigned char key,int x,int y){ switch(key){ case 'e': case 'E': currentGenMode = GL_EYE_LINEAR; currentPlane = GL_EYE_PLANE; glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,currentGenMode); glTexGenfv(GL_S,currentPlane,currentCoeff); glutPostRedisplay(); break; case 'o': case 'O': currentGenMode = GL_OBJECT_LINEAR; currentPlane = GL_OBJECT_PLANE; glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,currentGenMode); glTexGenfv(GL_S,currentPlane,currentCoeff); glutPostRedisplay(); break; case 's': case 'S': currentCoeff = slanted; glTexGenfv(GL_S,currentPlane,currentCoeff); glutPostRedisplay(); break; case 'x': case 'X': currentCoeff = xequalzero; glTexGenfv(GL_S,currentPlane,currentCoeff); glutPostRedisplay(); break; case 27: exit(0); break; default: break; } } int main(int argc,char** argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(256,256); glutInitWindowPosition(100,100); glutCreateWindow(argv[0]); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }
Lane-学习OpenGL(3)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。