码迷,mamicode.com
首页 > 其他好文 > 详细

7天学习opengl入门

时间:2015-03-20 12:37:22      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

http://blog.csdn.net/slience_perseverance/article/details/8096233

10月13号下午3:00队长给我开了一个会,10.14号开始学习opengl

今天10月21号,期间,虽然有时候课程很满,但每天都至少写一个程序。

当然,这些只是我7天来业余时间的学习,我觉得这个网址不错,大家如果也想学习opengl,并且具有一定的C语言C++基础,入门课程推荐大家去学习这个网址http://www.cnblogs.com/crazyxiaom/articles/2073586.html

我的这些代码等都是从这个网址学习的,推荐你还是去这个网址学习,那更全更准确。

PS:“今天”(发表文章的今天)把我入门学习的资料送给一块儿学习的同学了,看着那厚厚的一叠折折状状的资料,我穿越了,我很清晰的看到了我接下来的学习生活--将会更加投入,将会有更厚更厚的资料进入我的大脑!

 

10.14

今天写了15个程序。这些是画二维图形的基础。

开始的时候犯了一个错误,以为坐标范围是像素点呢,后来才知道坐标范围是-1~1;(0,0)在中心。

第三个程序到第15个程序都是在练习glBegin()的使用,最后可以画一个近似圆的多边形。

GL_POINTS,GL_LINES,GL_LINE_STRIP,GL_LINE_LOOP,GL_TRIANGLES,GL_TRIANGLE_STRIP,GL_TRIANGLE_FAN

GL_POLYGON

1.

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/GLUT.H>  
  2.   
  3. void myDisplay(void){  
  4.     glClear(GL_COLOR_BUFFER_BIT);  
  5.     glRectf(-0.5f, -0.4f, 0.5f, 0.5f);  
  6.     glFlush();  
  7. }  
  8.   
  9. int main(int argc, char *argv[])  
  10. {  
  11.     glutInit(&argc,argv);  
  12.     glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);  
  13.     glutInitWindowPosition(100, 100);  
  14.     glutInitWindowSize(400, 400);  
  15.     glutCreateWindow("opengl??1");  
  16.     glutDisplayFunc(&myDisplay);  
  17.     glutMainLoop();  
  18.     return 0;  
  19. }  
  20. </strong></span>  

技术分享

 

2.

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2.   
  3. void myDisplay(){  
  4.     glClearColor(1.0, 1.0, 1.0, 0.0);  
  5.     glClear(GL_COLOR_BUFFER_BIT);  
  6.     glColor3f(1.0, 0.0, 0.0);  
  7.     glRectf(-0.5f,-0.5f,0.5f,0.5f);  
  8.     glFlush();  
  9. }  
  10.   
  11. int main(int argc, char* argv[]){  
  12.     glutInit(&argc, argv);  
  13.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  14.   
  15.     glutInitWindowPosition(400, 400);  
  16.     glutInitWindowSize(200, 200);  
  17.     glutCreateWindow("独立");  
  18.   
  19.      glutDisplayFunc(myDisplay);  
  20.      glutMainLoop();  
  21.      return 0;  
  22.   
  23. }</strong></span>  

技术分享
3.

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2.   
  3. void myDisplay(){  
  4.     glClearColor(1.0,1.0,1.0,0.0);  
  5.     glClear(GL_COLOR_BUFFER_BIT);  
  6.     glBegin(GL_POINTS);  
  7.       glVertex2i(100, 100);  
  8.       glVertex2i(50, 50);  
  9.       glVertex2i(70, 70);  
  10.         
  11.       glVertex2f(0.3f, 0.3f);  
  12.       glVertex2f(-0.3f, -0.3f);  
  13.       glVertex2f(0.5f, 0.5f);  
  14.   
  15.       glVertex2d(0.2,0.5);  
  16.       glVertex2d(-0.4, -0.6);  
  17.       glVertex2d(0.3,0.6);  
  18.     glEnd();  
  19.   
  20.     glFlush();        
  21. }  
  22.   
  23. int main(int argc, char* argv[])  
  24. {  
  25.     glutInit(&argc, argv);  
  26.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  27.   
  28.     glutInitWindowPosition(400,400);  
  29.     glutInitWindowSize(400,400);  
  30.     glutCreateWindow("study02");  
  31.     glutDisplayFunc(&myDisplay);  
  32.     glutMainLoop();  
  33.     return 0;  
  34. }  
  35. </strong></span>  

技术分享
4.

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2.   
  3. void myDisplay(){  
  4.     glClearColor(1.0,1.0,1.0,0.0);  
  5.     glClear(GL_COLOR_BUFFER_BIT);  
  6.     glColor3f(1.0,0.0,0.0);  
  7.     glBegin(GL_LINES);  
  8.     //glVertex2i(100, 100);  
  9.     //glVertex2i(50, 50);  
  10.     //glVertex2i(70, 70);  
  11.       
  12.     glVertex2f(0.3f, 0.3f);  
  13.     glVertex2f(-0.3f, -0.3f);  
  14.     glVertex2f(0.5f, 0.5f);  
  15.       
  16.     glVertex2d(0.2,0.5);  
  17.     glVertex2d(-0.4, -0.6);  
  18.     glVertex2d(0.3,0.6);  
  19.     glEnd();  
  20.       
  21.     glFlush();        
  22. }  
  23.   
  24. int main(int argc, char* argv[])  
  25. {  
  26.     glutInit(&argc, argv);  
  27.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  28.       
  29.     glutInitWindowPosition(400,400);  
  30.     glutInitWindowSize(400,400);  
  31.     glutCreateWindow("study03");  
  32.     glutDisplayFunc(&myDisplay);  
  33.     glutMainLoop();  
  34.     return 0;  
  35. }  
  36. </strong></span>  

技术分享

 

5.

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClearColor(1.0,1.0,1.0,0.0);  
  6.     glClear(GL_COLOR_BUFFER_BIT);  
  7.     glColor3f(1.0,0.0,0.0);  
  8.     glBegin(GL_LINE_STRIP);  
  9.         glVertex2f(0.1f, 0.8f);  
  10.         glVertex2f(-0.1f, -0.8f);  
  11.         glVertex2f(0.1f, -0.8f);  
  12.         glVertex2f(-0.1f, 0.8f);  
  13.         glVertex2f(0.1f, 0.9f);  
  14.         glVertex2f(0.4f, 0.8f);  
  15.     glEnd();  
  16.     glFlush();  
  17. }  
  18.   
  19. int main(int argc, char* argv[])  
  20. {  
  21.     glutInit(&argc, argv);  
  22.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  23.     glutInitWindowPosition(400,400);  
  24.     glutInitWindowSize(400,400);  
  25.     glutCreateWindow("Study04");  
  26.     glutDisplayFunc(myDisplay);  
  27.     glutMainLoop();  
  28.     return 0;  
  29. }</strong></span>  

技术分享

 

6.1

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClearColor(1.0,1.0,1.0,0.0);  
  6.     glClear(GL_COLOR_BUFFER_BIT);  
  7.     glColor3f(1.0,0.0,0.0);  
  8.     glBegin(GL_LINE_LOOP);  
  9.         glVertex2f(0.2f, 0.2f);  
  10.         glVertex2f(-0.2f, 0.2f);  
  11.         glVertex2f(-0.2f, -0.2f);  
  12.         glVertex2f(0.2f, -0.2f);  
  13.         //glVertex2f(0.1f, 0.9f);  
  14.         //glVertex2f(0.4f, 0.8f);  
  15.     glEnd();  
  16.     glFlush();  
  17. }  
  18.   
  19. int main(int argc, char* argv[])  
  20. {  
  21.     glutInit(&argc, argv);  
  22.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  23.     glutInitWindowPosition(400,400);  
  24.     glutInitWindowSize(400,400);  
  25.     glutCreateWindow("Study04");  
  26.     glutDisplayFunc(myDisplay);  
  27.     glutMainLoop();  
  28.     return 0;  
  29. }</strong></span>  

技术分享

 

7

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClearColor(1.0,1.0,1.0,0.0);  
  6.     glClear(GL_COLOR_BUFFER_BIT);  
  7.     glColor3f(1.0,0.0,0.0);  
  8.     glBegin(GL_TRIANGLES);  
  9.         //glVertex2f(0.2f, 0.2f);  
  10.         //glVertex2f(-0.2f, 0.2f);  
  11.         //glVertex2f(-0.2f, -0.2f);  
  12.         glVertex2f(0.8f, 0.4f);  
  13.         glVertex2f(0.4f, 0.8f);  
  14.         glVertex2f(0.0f, 0.0f);  
  15.         //glVertex2f(0.1f, 0.9f);  
  16.         //glVertex2f(0.4f, 0.8f);  
  17.     glEnd();  
  18.     glFlush();  
  19. }  
  20.   
  21. int main(int argc, char* argv[])  
  22. {  
  23.     glutInit(&argc, argv);  
  24.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  25.     glutInitWindowPosition(400,400);  
  26.     glutInitWindowSize(400,400);  
  27.     glutCreateWindow("Study04");  
  28.     glutDisplayFunc(myDisplay);  
  29.     glutMainLoop();  
  30.     return 0;  
  31. }</strong></span>  

技术分享

 

7.1

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClearColor(1.0,1.0,1.0,0.0);  
  6.     glClear(GL_COLOR_BUFFER_BIT);  
  7.     glColor3f(1.0,0.0,0.0);  
  8.     glBegin(GL_TRIANGLES);  
  9.         glVertex2f(0.2f, 0.2f);  
  10.         glVertex2f(-0.2f, 0.2f);  
  11.         glVertex2f(-0.2f, -0.2f);  
  12.     glEnd();  
  13.   
  14.     glBegin(GL_TRIANGLES);  
  15.         glVertex2f(0.8f, 0.4f);  
  16.         glVertex2f(0.4f, 0.8f);  
  17.         glVertex2f(0.0f, 0.0f);  
  18.         //glVertex2f(0.1f, 0.9f);  
  19.         //glVertex2f(0.4f, 0.8f);  
  20.     glEnd();  
  21.     glFlush();  
  22. }  
  23.   
  24. int main(int argc, char* argv[])  
  25. {  
  26.     glutInit(&argc, argv);  
  27.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  28.     glutInitWindowPosition(400,400);  
  29.     glutInitWindowSize(400,400);  
  30.     glutCreateWindow("Study04");  
  31.     glutDisplayFunc(myDisplay);  
  32.     glutMainLoop();  
  33.     return 0;  
  34. }</strong></span>  

 

技术分享

7.2

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClearColor(1.0,1.0,1.0,0.0);  
  6.     glClear(GL_COLOR_BUFFER_BIT);  
  7.     glColor3f(1.0,0.0,0.0);  
  8.     glBegin(GL_TRIANGLES);  
  9.         glVertex2f(0.2f, 0.2f);  
  10.         glVertex2f(-0.2f, 0.2f);  
  11.         glVertex2f(-0.2f, -0.2f);  
  12.       
  13.         glVertex2f(0.8f, 0.4f);  
  14.         glVertex2f(0.4f, 0.8f);  
  15.         glVertex2f(0.0f, 0.0f);  
  16.         //glVertex2f(0.1f, 0.9f);  
  17.         //glVertex2f(0.4f, 0.8f);  
  18.     glEnd();  
  19.     glFlush();  
  20. }  
  21.   
  22. int main(int argc, char* argv[])  
  23. {  
  24.     glutInit(&argc, argv);  
  25.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  26.     glutInitWindowPosition(400,400);  
  27.     glutInitWindowSize(400,400);  
  28.     glutCreateWindow("Study04");  
  29.     glutDisplayFunc(myDisplay);  
  30.     glutMainLoop();  
  31.     return 0;  
  32. }</strong></span>  

技术分享

 

7.3

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClearColor(1.0,1.0,1.0,0.0);  
  6.     glClear(GL_COLOR_BUFFER_BIT);  
  7.     glColor3f(1.0,0.0,0.0);  
  8.     glBegin(GL_TRIANGLES);  
  9.         glVertex2f(0.2f, 0.2f);  
  10.         glVertex2f(-0.2f, 0.2f);  
  11.         glVertex2f(-0.2f, -0.2f);  
  12.       
  13.         glVertex2f(0.8f, 0.4f);  
  14.         glVertex2f(0.4f, 0.8f);  
  15.         glVertex2f(0.0f, 0.0f);  
  16.         glVertex2f(0.1f, 0.9f);  
  17.         glVertex2f(0.4f, 0.8f);  
  18.         glVertex2f(0.9f,0.3f);  
  19.     glEnd();  
  20.     glFlush();  
  21. }  
  22.   
  23. int main(int argc, char* argv[])  
  24. {  
  25.     glutInit(&argc, argv);  
  26.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  27.     glutInitWindowPosition(400,400);  
  28.     glutInitWindowSize(400,400);  
  29.     glutCreateWindow("Study04");  
  30.     glutDisplayFunc(myDisplay);  
  31.     glutMainLoop();  
  32.     return 0;  
  33. }</strong></span>  

 

技术分享

8.

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClearColor(1.0,1.0,1.0,0.0);  
  6.     glClear(GL_COLOR_BUFFER_BIT);  
  7.     glColor3f(1.0,0.0,0.0);  
  8.     glBegin(GL_TRIANGLE_STRIP);  
  9.         glVertex2f(0.0f,0.0f);  
  10.         glVertex2f(0.4f,0.8f);  
  11.         glVertex2f(0.8f,0.4f);  
  12.         glVertex2f(0.8f,0.5f);  
  13.         glVertex2f(-0.1f,0.9f);  
  14.     glEnd();  
  15.     glFlush();  
  16. }  
  17.   
  18. int main(int argc, char* argv[])  
  19. {  
  20.     glutInit(&argc, argv);  
  21.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  22.     glutInitWindowPosition(400,400);  
  23.     glutInitWindowSize(400,400);  
  24.     glutCreateWindow("Study04");  
  25.     glutDisplayFunc(myDisplay);  
  26.     glutMainLoop();  
  27.     return 0;  
  28. }</strong></span>  

技术分享
9.

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClearColor(1.0,1.0,1.0,0.0);  
  6.     glClear(GL_COLOR_BUFFER_BIT);  
  7.     glColor3f(1.0,0.0,0.0);  
  8.     glBegin(GL_TRIANGLE_FAN);  
  9.         glVertex2f(0.0f,0.0f);  
  10.         glVertex2f(0.4f,0.8f);  
  11.         glVertex2f(0.8f,0.4f);  
  12.         glVertex2f(0.8f,-0.5f);  
  13.         glVertex2f(0.1f,-0.9f);  
  14.     glEnd();  
  15.     glFlush();  
  16. }  
  17.   
  18. int main(int argc, char* argv[])  
  19. {  
  20.     glutInit(&argc, argv);  
  21.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  22.     glutInitWindowPosition(400,400);  
  23.     glutInitWindowSize(400,400);  
  24.     glutCreateWindow("Study04");  
  25.     glutDisplayFunc(myDisplay);  
  26.     glutMainLoop();  
  27.     return 0;  
  28. }</strong></span>  

技术分享
10.

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClearColor(1.0,1.0,1.0,0.0);  
  6.     glClear(GL_COLOR_BUFFER_BIT);  
  7.     glColor3f(1.0,0.0,0.0);  
  8.     glBegin(GL_POLYGON);  
  9.         glVertex2f(0.0f,0.0f);  
  10.           
  11.         glVertex2f(0.8f,0.4f);  
  12.         glVertex2f(0.4f,0.8f);  
  13.         glVertex2f(0.8f,-0.5f);  
  14.         glVertex2f(0.1f,-0.9f);  
  15.     glEnd();  
  16.     glFlush();  
  17. }  
  18.   
  19. int main(int argc, char* argv[])  
  20. {  
  21.     glutInit(&argc, argv);  
  22.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  23.     glutInitWindowPosition(400,400);  
  24.     glutInitWindowSize(400,400);  
  25.     glutCreateWindow("Study04");  
  26.     glutDisplayFunc(myDisplay);  
  27.     glutMainLoop();  
  28.     return 0;  
  29. }</strong></span>  

技术分享
11.

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClearColor(1.0,1.0,1.0,0.0);  
  6.     glClear(GL_COLOR_BUFFER_BIT);  
  7.     glColor3f(1.0,0.0,0.0);  
  8.     glBegin(GL_QUADS);  
  9.           
  10.         glVertex2f(0.2f, 0.2f);  
  11.         glVertex2f(0.2f, -0.2f);  
  12.         glVertex2f(-0.2f,-0.2f);  
  13.         glVertex2f(-0.2f,0.2f);  
  14.   
  15.         glVertex2f(0.3f, 0.3f);  
  16.         glVertex2f(0.3f, 0.7f);  
  17.         glVertex2f(0.7f,1.0f);  
  18.         glVertex2f(0.7f,0.3f);  
  19.     glEnd();  
  20.     glFlush();  
  21. }  
  22.   
  23. int main(int argc, char* argv[])  
  24. {  
  25.     glutInit(&argc, argv);  
  26.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  27.     glutInitWindowPosition(400,400);  
  28.     glutInitWindowSize(400,400);  
  29.     glutCreateWindow("Study04");  
  30.     glutDisplayFunc(myDisplay);  
  31.     glutMainLoop();  
  32.     return 0;  
  33. }</strong></span>  

技术分享

 

12

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2. #include<math.h>  
  3. const int n = 20;  
  4. const GLfloat R = 0.5f;  
  5. const GLfloat Pi = 3.14159265358979f;  
  6.   
  7. void myDisplay()  
  8. {  
  9.     glClearColor(1.0,1.0,1.0,0.0);  
  10.     glClear(GL_COLOR_BUFFER_BIT);  
  11.     glColor3f(1.0,0.0,0.0);  
  12.     glBegin(GL_POLYGON);  
  13.         for(int i=0;i<n;++i)  
  14.         {  
  15.             glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));  
  16.         }  
  17.           
  18.     glEnd();  
  19.     glFlush();  
  20. }  
  21.   
  22. int main(int argc, char* argv[])  
  23. {  
  24.     glutInit(&argc, argv);  
  25.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  26.     glutInitWindowPosition(400,400);  
  27.     glutInitWindowSize(400,400);  
  28.     glutCreateWindow("Study04");  
  29.     glutDisplayFunc(myDisplay);  
  30.     glutMainLoop();  
  31.     return 0;  
  32. }</strong></span>  

技术分享
12.1

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2. #include<math.h>  
  3. const int n = 30;  
  4. const GLfloat R = 0.5f;  
  5. const GLfloat Pi = 3.14159265358979f;  
  6.   
  7. void myDisplay()  
  8. {  
  9.     glClearColor(1.0,1.0,1.0,0.0);  
  10.     glClear(GL_COLOR_BUFFER_BIT);  
  11.     glColor3f(1.0,0.0,0.0);  
  12.     glBegin(GL_POLYGON);  
  13.         for(int i=0;i<n;++i)  
  14.         {  
  15.             glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));  
  16.         }  
  17.           
  18.     glEnd();  
  19.     glFlush();  
  20. }  
  21.   
  22. int main(int argc, char* argv[])  
  23. {  
  24.     glutInit(&argc, argv);  
  25.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  26.     glutInitWindowPosition(400,400);  
  27.     glutInitWindowSize(400,400);  
  28.     glutCreateWindow("Study04");  
  29.     glutDisplayFunc(myDisplay);  
  30.     glutMainLoop();  
  31.     return 0;  
  32. }</strong></span>  

技术分享
12.2

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2. #include<math.h>  
  3. const int n = 50;  
  4. const GLfloat R = 0.5f;  
  5. const GLfloat Pi = 3.14159265358979f;  
  6.   
  7. void myDisplay()  
  8. {  
  9.     glClearColor(1.0,1.0,1.0,0.0);  
  10.     glClear(GL_COLOR_BUFFER_BIT);  
  11.     glColor3f(1.0,0.0,0.0);  
  12.     glBegin(GL_POLYGON);  
  13.         for(int i=0;i<n;++i)  
  14.         {  
  15.             glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));  
  16.         }  
  17.           
  18.     glEnd();  
  19.     glFlush();  
  20. }  
  21.   
  22. int main(int argc, char* argv[])  
  23. {  
  24.     glutInit(&argc, argv);  
  25.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  26.     glutInitWindowPosition(400,400);  
  27.     glutInitWindowSize(400,400);  
  28.     glutCreateWindow("Study04");  
  29.     glutDisplayFunc(myDisplay);  
  30.     glutMainLoop();  
  31.     return 0;  
  32. }</strong></span>  

技术分享
13

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2. #include<math.h>  
  3.   
  4. const GLfloat R = 0.5f;  
  5. const GLfloat Pi = 3.14159265358979f;  
  6.   
  7. void myDisplay()  
  8. {  
  9.     GLfloat PointA[2] = {0,R};  
  10.     GLfloat PointB[2] = {R*cos(162*Pi/180),R*sin(162*Pi/180)};  
  11.     GLfloat PointC[2] = {R*cos(234*Pi/180),R*sin(234*Pi/180)};  
  12.     GLfloat PointD[2] = {R*cos(306*Pi/180),R*sin(306*Pi/180)};  
  13.     GLfloat PointE[2] = {R*cos(18*Pi/180),R*sin(18*Pi/180)};  
  14.     glClearColor(1.0,1.0,1.0,0.0);  
  15.     glClear(GL_COLOR_BUFFER_BIT);  
  16.     glColor3f(1.0,0.0,0.0);  
  17.     glBegin(GL_LINE_LOOP);  
  18.         glVertex2fv(PointA);  
  19.         glVertex2fv(PointC);  
  20.         glVertex2fv(PointE);  
  21.         glVertex2fv(PointB);  
  22.         glVertex2fv(PointD);  
  23.     glEnd();  
  24.     glFlush();  
  25. }  
  26.   
  27. int main(int argc, char* argv[])  
  28. {  
  29.     glutInit(&argc, argv);  
  30.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  31.     glutInitWindowPosition(400,400);  
  32.     glutInitWindowSize(400,400);  
  33.     glutCreateWindow("Study04");  
  34.     glutDisplayFunc(myDisplay);  
  35.     glutMainLoop();  
  36.     return 0;  
  37. }</strong></span>  

技术分享

 

10.15

以下程序诸个描述

14.用很多线段的连线去模拟一个正玄函数线。用多条短的线段去模拟复杂的曲线或弧线似乎是一个很好的思维,在这思维下可以画出很多复杂的函数曲线

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2. #include<math.h>  
  3.   
  4. const GLfloat factor = 0.1f;  
  5.   
  6. void myDisplay()  
  7. {  
  8.     GLfloat x;  
  9.     glClearColor(1.0,1.0,1.0,0.0);  
  10.     glClear(GL_COLOR_BUFFER_BIT);  
  11.     glColor3f(0.0,0.0,1.0);  
  12.     glBegin(GL_LINES);  
  13.         glVertex2f(-1.0f, 0.0f);  
  14.         glVertex2f(1.0f, 0.0f);  
  15.         glVertex2f(0.0f, -1.0f);  
  16.         glVertex2f(0.0f, 1.0f);  
  17.     glEnd();  
  18.     glBegin(GL_LINE_STRIP);  
  19.         for(x=-1.0f/factor;x<1.0f/factor;x+=0.01)  
  20.         {  
  21.             glVertex2f(x*factor, sin(x)*factor);  
  22.         }  
  23.     glEnd();  
  24.     glFlush();  
  25. }  
  26.   
  27. int main(int argc, char* argv[])  
  28. {  
  29.     glutInit(&argc, argv);  
  30.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  31.     glutInitWindowPosition(400,400);  
  32.     glutInitWindowSize(400,400);  
  33.     glutCreateWindow("Study04");  
  34.     glutDisplayFunc(myDisplay);  
  35.     glutMainLoop();  
  36.     return 0;  
  37. }</strong></span>  

技术分享
15.上周上机实验时设置点的大小总是不通过,今天发现设置点的大小不是那么难。

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2. #include<math.h>  
  3.   
  4. const GLfloat factor = 0.1f;  
  5.   
  6. void myDisplay()  
  7. {  
  8.     GLfloat x;  
  9.     glClearColor(1.0,1.0,1.0,0.0);  
  10.     glClear(GL_COLOR_BUFFER_BIT);  
  11.     glColor3f(0.0,0.0,1.0);  
  12.     glPointSize(5.0f);  
  13.     glBegin(GL_POINTS);  
  14.         glVertex2f(0.0f, 0.0f);  
  15.         glVertex2f(0.4f, 0.4f);  
  16.         glVertex2f(-0.2f,0.3f);  
  17.     glEnd();  
  18.     glFlush();  
  19. }  
  20.   
  21. int main(int argc, char* argv[])  
  22. {  
  23.     glutInit(&argc, argv);  
  24.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  25.     glutInitWindowPosition(400,400);  
  26.     glutInitWindowSize(400,400);  
  27.     glutCreateWindow("Study04");  
  28.     glutDisplayFunc(myDisplay);  
  29.     glutMainLoop();  
  30.     return 0;  
  31. }</strong></span>  

 

技术分享

15.1 这是一个点的大小设置的有点夸张的程序

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2. #include<math.h>  
  3.   
  4. const GLfloat factor = 0.1f;  
  5.   
  6. void myDisplay()  
  7. {  
  8.     GLfloat x;  
  9.     glClearColor(1.0,1.0,1.0,0.0);  
  10.     glClear(GL_COLOR_BUFFER_BIT);  
  11.     glColor3f(0.0,0.0,1.0);  
  12.     glPointSize(50.0f);  
  13.     glBegin(GL_POINTS);  
  14.         glVertex2f(0.0f, 0.0f);  
  15.         glVertex2f(0.4f, 0.4f);  
  16.         glVertex2f(-0.2f,0.3f);  
  17.     glEnd();  
  18.     glFlush();  
  19. }  
  20.   
  21. int main(int argc, char* argv[])  
  22. {  
  23.     glutInit(&argc, argv);  
  24.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  25.     glutInitWindowPosition(400,400);  
  26.     glutInitWindowSize(400,400);  
  27.     glutCreateWindow("Study04");  
  28.     glutDisplayFunc(myDisplay);  
  29.     glutMainLoop();  
  30.     return 0;  
  31. }</strong></span>  

 

技术分享

16.设置了线宽

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2. #include<math.h>  
  3.   
  4. const GLfloat factor = 0.1f;  
  5.   
  6. void myDisplay()  
  7. {  
  8.     GLfloat x;  
  9.     glClearColor(1.0,1.0,1.0,0.0);  
  10.     glClear(GL_COLOR_BUFFER_BIT);  
  11.     glColor3f(0.0,0.0,1.0);  
  12.     glLineWidth(5.0f);  
  13.     glBegin(GL_LINE_STRIP);  
  14.         glVertex2f(0.0f, 0.0f);  
  15.         glVertex2f(0.4f, 0.4f);  
  16.         glVertex2f(-0.2f,0.3f);  
  17.     glEnd();  
  18.     glFlush();  
  19. }  
  20.   
  21. int main(int argc, char* argv[])  
  22. {  
  23.     glutInit(&argc, argv);  
  24.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  25.     glutInitWindowPosition(400,400);  
  26.     glutInitWindowSize(400,400);  
  27.     glutCreateWindow("Study04");  
  28.     glutDisplayFunc(myDisplay);  
  29.     glutMainLoop();  
  30.     return 0;  
  31. }</strong></span>  

技术分享
16.1 设置了夸张的线宽

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2. #include<math.h>  
  3.   
  4. const GLfloat factor = 0.1f;  
  5.   
  6. void myDisplay()  
  7. {  
  8.     GLfloat x;  
  9.     glClearColor(1.0,1.0,1.0,0.0);  
  10.     glClear(GL_COLOR_BUFFER_BIT);  
  11.     glColor3f(0.0,0.0,1.0);  
  12.     glLineWidth(50.0f);  
  13.     glBegin(GL_LINE_STRIP);  
  14.         glVertex2f(0.0f, 0.0f);  
  15.         glVertex2f(0.4f, 0.4f);  
  16.         glVertex2f(-0.2f,0.3f);  
  17.     glEnd();  
  18.     glFlush();  
  19. }  
  20.   
  21. int main(int argc, char* argv[])  
  22. {  
  23.     glutInit(&argc, argv);  
  24.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  25.     glutInitWindowPosition(400,400);  
  26.     glutInitWindowSize(400,400);  
  27.     glutCreateWindow("Study04");  
  28.     glutDisplayFunc(myDisplay);  
  29.     glutMainLoop();  
  30.     return 0;  
  31. }</strong></span>  

技术分享
17. 画圆。圆心和周长上的点的连线,线加粗。

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2. #include<math.h>  
  3.   
  4. const GLfloat factor = 0.1f;  
  5.   
  6. class screenPt  
  7. {  
  8.   private :  
  9.     GLint x,y;  
  10.   
  11.   public:  
  12.       screenPt()   
  13.       {  
  14.        x=y=0;  
  15.       }  
  16.   void setCoords(GLint xCoordValue,GLint yCoordValue)  
  17.   {  
  18.       x=xCoordValue;  
  19.       y=yCoordValue;  
  20.   }  
  21.   GLint getx() const  
  22.   {  
  23.       return x;  
  24.   }  
  25.  GLint gety() const  
  26.   {  
  27.       return y;  
  28.   }  
  29.   void incrementx()  
  30.   {  
  31.       x++;  
  32.   
  33.   }  
  34.   void decrementy()  
  35.   {  
  36.       y--;  
  37.   }  
  38. };  
  39.   
  40.  void line(GLint x1, GLint y1, GLint x2, GLint y2){  
  41.     glClearColor(1.0,1.0,1.0,0.0);  
  42.     glClear(GL_COLOR_BUFFER_BIT);  
  43.     glColor3f(1.0, 0.0, 0.0);  
  44.     glLineWidth(5.0f);  
  45.     glBegin(GL_LINES);  
  46.         glVertex2f(x1/200.0f, y1/200.0f);  
  47.         glVertex2f(x2/200.0f, y2/200.0f);  
  48.     //glEnd();  
  49.     glFlush();  
  50. }  
  51.   
  52. void circleMidpoint(GLint xc,GLint yc,GLint radius)  
  53. {  
  54.   
  55.     screenPt circPt;  
  56.   
  57.     GLint p=1-radius;  
  58.   
  59.     circPt.setCoords(0,radius);  
  60.   
  61.     void circlePlotPoints (GLint,GLint,screenPt);  
  62.     circlePlotPoints(xc, yc, circPt);  
  63.   
  64.   while(circPt.getx()<circPt.gety())  
  65.   {  
  66.       circPt.incrementx();  
  67.       if(p<0)  
  68.            p+=2*circPt.getx() +1;  
  69.       else   
  70.       {  
  71.           circPt.decrementy();  
  72.           p+=2*(circPt.getx()-circPt.gety())+1;  
  73.       }  
  74.   
  75.           circlePlotPoints(xc,yc,circPt);  
  76.        
  77.   }  
  78. }  
  79.   
  80. void circlePlotPoints(GLint xc,GLint yc,screenPt circPt)  
  81. {  
  82.   
  83.     line(xc, yc, xc+circPt.getx(),yc+circPt.gety());  
  84.   
  85.     line(xc, yc, xc-circPt.getx(),yc+circPt.gety());  
  86.   
  87.     line(xc, yc, xc+circPt.getx(),yc-circPt.gety());  
  88.   
  89.     line(xc, yc, xc-circPt.getx(),yc-circPt.gety());  
  90.   
  91.     line(xc, yc, xc+circPt.gety(),yc+circPt.getx());  
  92.           
  93.     line(xc, yc, xc-circPt.gety(),yc+circPt.getx());  
  94.   
  95.     line(xc, yc, xc+circPt.gety(),yc-circPt.getx());  
  96.   
  97.     line(xc, yc, xc-circPt.gety(),yc-circPt.getx());  
  98.   
  99. }  
  100.   
  101.   
  102.   
  103. void  Mycircle(void)  
  104. {  
  105.       circleMidpoint(100,80,50);  
  106.       glEnd();  
  107.       glFlush();  
  108.   
  109. }  
  110.   
  111.   
  112. int main(int argc, char* argv[])  
  113. {  
  114.     glutInit(&argc, argv);  
  115.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  116.     glutInitWindowPosition(400,400);  
  117.     glutInitWindowSize(400,400);  
  118.     glutCreateWindow("Study04");  
  119.     glutDisplayFunc(&Mycircle);  
  120.     glutMainLoop();  
  121.     return 0;  
  122. }  
  123. </strong></span>  

技术分享
17.1 画圆。圆心和周长上的点的连线,线稍细。

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2. #include<math.h>  
  3.   
  4. const GLfloat factor = 0.1f;  
  5.   
  6. class screenPt  
  7. {  
  8.   private :  
  9.     GLint x,y;  
  10.   
  11.   public:  
  12.       screenPt()   
  13.       {  
  14.        x=y=0;  
  15.       }  
  16.   void setCoords(GLint xCoordValue,GLint yCoordValue)  
  17.   {  
  18.       x=xCoordValue;  
  19.       y=yCoordValue;  
  20.   }  
  21.   GLint getx() const  
  22.   {  
  23.       return x;  
  24.   }  
  25.  GLint gety() const  
  26.   {  
  27.       return y;  
  28.   }  
  29.   void incrementx()  
  30.   {  
  31.       x++;  
  32.   
  33.   }  
  34.   void decrementy()  
  35.   {  
  36.       y--;  
  37.   }  
  38. };  
  39.   
  40.  void line(GLint x1, GLint y1, GLint x2, GLint y2){  
  41.     glClearColor(1.0,1.0,1.0,0.0);  
  42.     glClear(GL_COLOR_BUFFER_BIT);  
  43.     glColor3f(1.0, 0.0, 0.0);  
  44.     glLineWidth(1.0f);  
  45.     glBegin(GL_LINES);  
  46.         glVertex2f(x1/200.0f, y1/200.0f);  
  47.         glVertex2f(x2/200.0f, y2/200.0f);  
  48.     //glEnd();  
  49.     glFlush();  
  50. }  
  51.   
  52. void circleMidpoint(GLint xc,GLint yc,GLint radius)  
  53. {  
  54.   
  55.     screenPt circPt;  
  56.   
  57.     GLint p=1-radius;  
  58.   
  59.     circPt.setCoords(0,radius);  
  60.   
  61.     void circlePlotPoints (GLint,GLint,screenPt);  
  62.     circlePlotPoints(xc, yc, circPt);  
  63.   
  64.   while(circPt.getx()<circPt.gety())  
  65.   {  
  66.       circPt.incrementx();  
  67.       if(p<0)  
  68.            p+=2*circPt.getx() +1;  
  69.       else   
  70.       {  
  71.           circPt.decrementy();  
  72.           p+=2*(circPt.getx()-circPt.gety())+1;  
  73.       }  
  74.   
  75.           circlePlotPoints(xc,yc,circPt);  
  76.        
  77.   }  
  78. }  
  79.   
  80. void circlePlotPoints(GLint xc,GLint yc,screenPt circPt)  
  81. {  
  82.   
  83.     line(xc, yc, xc+circPt.getx(),yc+circPt.gety());  
  84.   
  85.     line(xc, yc, xc-circPt.getx(),yc+circPt.gety());  
  86.   
  87.     line(xc, yc, xc+circPt.getx(),yc-circPt.gety());  
  88.   
  89.     line(xc, yc, xc-circPt.getx(),yc-circPt.gety());  
  90.   
  91.     line(xc, yc, xc+circPt.gety(),yc+circPt.getx());  
  92.           
  93.     line(xc, yc, xc-circPt.gety(),yc+circPt.getx());  
  94.   
  95.     line(xc, yc, xc+circPt.gety(),yc-circPt.getx());  
  96.   
  97.     line(xc, yc, xc-circPt.gety(),yc-circPt.getx());  
  98.   
  99. }  
  100.   
  101.   
  102.   
  103. void  Mycircle(void)  
  104. {  
  105.       circleMidpoint(100,80,50);  
  106.       glEnd();  
  107.       glFlush();  
  108.   
  109. }  
  110.   
  111.   
  112. int main(int argc, char* argv[])  
  113. {  
  114.     glutInit(&argc, argv);  
  115.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  116.     glutInitWindowPosition(400,400);  
  117.     glutInitWindowSize(400,400);  
  118.     glutCreateWindow("Study04");  
  119.     glutDisplayFunc(&Mycircle);  
  120.     glutMainLoop();  
  121.     return 0;  
  122. }  
  123. </strong></span>  

技术分享
18. 设置线的模式

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong><span style="white-space:pre">    </span>glEnable(GL_LINE_STIPPLE);//激活模式选择  
  2.     glLineStipple(2,0x3333);//单位线,虚实匹配</strong></span>  

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>  
  2. #include<math.h>  
  3.   
  4. void myDisplay()  
  5. {  
  6.   
  7.     glClearColor(1.0,1.0,1.0,0.0);  
  8.     glClear(GL_COLOR_BUFFER_BIT);  
  9.     glColor3f(0.0,0.0,1.0);  
  10.     glEnable(GL_LINE_STIPPLE);  
  11.     glLineStipple(2,0x3333);  
  12.     glLineWidth(3.0f);  
  13.     glBegin(GL_LINES);  
  14.         glVertex2f(-1.0f, 0.0f);  
  15.         glVertex2f(1.0f, 0.0f);  
  16.         glVertex2f(0.0f, -1.0f);  
  17.         glVertex2f(0.0f, 1.0f);  
  18.     glEnd();  
  19.     glFlush();  
  20. }  
  21.   
  22. int main(int argc, char* argv[])  
  23. {  
  24.     glutInit(&argc, argv);  
  25.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  26.     glutInitWindowPosition(400,400);  
  27.     glutInitWindowSize(400,400);  
  28.     glutCreateWindow("Study04");  
  29.     glutDisplayFunc(myDisplay);  
  30.     glutMainLoop();  
  31.     return 0;  
  32. }</strong></span>  

技术分享
19.练习一下程序的基本流程。不能看书,自己敲完。

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClearColor(1.0,1.0,1.0,0.0);  
  6.     glClear(GL_COLOR_BUFFER_BIT);  
  7.     glColor3f(0.0,0.0,1.0);  
  8.     glLineWidth(5.0f);  
  9.     glBegin(GL_LINES);  
  10.         glVertex2f(0.0f, 0.0f);  
  11.         glVertex2f(0.6f,0.8f);  
  12.     glEnd();  
  13.     glFlush();  
  14. }  
  15.   
  16. int main(int argc, char* argv[])  
  17. {  
  18.     glutInit(&argc, argv);  
  19.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  20.     glutInitWindowPosition(400,400);  
  21.     glutInitWindowSize(400,400);  
  22.     glutCreateWindow("study");  
  23.   
  24.     glutDisplayFunc(myDisplay);  
  25.     glutMainLoop();  
  26.     return 0;  
  27. }</strong></span>  

 

技术分享

20.opengl中面是具有两面的,opengl画点的顺序不变,但从面的两个面来看这些点的相连顺序相反。设置不同的目标方向,出现的面就不同。

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClearColor(1.0,1.0,1.0,0.0);  
  6.     glClear(GL_COLOR_BUFFER_BIT);  
  7.     glColor3f(0.0,0.0,1.0);  
  8.   
  9.     glPolygonMode(GL_FRONT,GL_FILL); //设置正面为填充模式  
  10.     glPolygonMode(GL_BACK,GL_LINE);  //设置反面为线性模式  
  11.     glFrontFace(GL_CCW);  
  12.   
  13.     glBegin(GL_POLYGON);  
  14.         glVertex2f(-0.5f, -0.5f);  
  15.         glVertex2f(0.0f,-0.5f);  
  16.         glVertex2f(0.0f,0.0f);  
  17.         glVertex2f(-0.5f,0.0f);  
  18.     glEnd();  
  19.     glFlush();  
  20. }  
  21.   
  22. int main(int argc, char* argv[])  
  23. {  
  24.     glutInit(&argc, argv);  
  25.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  26.     glutInitWindowPosition(400,400);  
  27.     glutInitWindowSize(400,400);  
  28.     glutCreateWindow("study");  
  29.   
  30.     glutDisplayFunc(myDisplay);  
  31.     glutMainLoop();  
  32.     return 0;  
  33. }</strong></span>  

技术分享
20.

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>//glEnable(GL_CULL_FACE);  
  2.     glCullFace(GL_FRONT);</strong></span>  

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClearColor(1.0,1.0,1.0,0.0);  
  6.     glClear(GL_COLOR_BUFFER_BIT);  
  7.     glColor3f(0.0,0.0,1.0);  
  8.   
  9.     glPolygonMode(GL_FRONT,GL_FILL); //设置正面为填充模式  
  10.     glPolygonMode(GL_BACK,GL_LINE);  //设置反面为线性模式  
  11.     glFrontFace(GL_CW);  
  12.   
  13.     glBegin(GL_POLYGON);  
  14.         glVertex2f(-0.5f, -0.5f);  
  15.         glVertex2f(0.0f,-0.5f);  
  16.         glVertex2f(0.0f,0.0f);  
  17.         glVertex2f(-0.5f,0.0f);  
  18.     glEnd();  
  19.     glFlush();  
  20. }  
  21.   
  22. int main(int argc, char* argv[])  
  23. {  
  24.     glutInit(&argc, argv);  
  25.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  26.     glutInitWindowPosition(400,400);  
  27.     glutInitWindowSize(400,400);  
  28.     glutCreateWindow("study");  
  29.   
  30.     glutDisplayFunc(myDisplay);  
  31.     glutMainLoop();  
  32.     return 0;  
  33. }</strong></span>  

技术分享
21 面具有两个面,可以剔除一个面,当面被挡着时可以剔除一个面。

 

glEnable(GL_CULL_FACE);//opengl是一个状态机,需要激活才能使用一些功能

glCullFace(GL_FRONT);//剔除正面

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>//glEnable(GL_CULL_FACE);  
  2.     glCullFace(GL_FRONT);</strong></span>  

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClearColor(1.0,1.0,1.0,0.0);  
  6.     glClear(GL_COLOR_BUFFER_BIT);  
  7.     glColor3f(0.0,0.0,1.0);  
  8.   
  9.     glPolygonMode(GL_FRONT,GL_FILL); //设置正面为填充模式  
  10.     glPolygonMode(GL_BACK,GL_LINE);  //设置反面为线性模式  
  11.     glFrontFace(GL_CCW);  
  12.       
  13.     glBegin(GL_POLYGON);  
  14.         glVertex2f(-0.5f, -0.5f);  
  15.         glVertex2f(0.0f,-0.5f);  
  16.         glVertex2f(0.0f,0.0f);  
  17.         glVertex2f(-0.5f,0.0f);  
  18.     glEnd();  
  19.       
  20.     glColor3f(1.0,0.0,0.0);  
  21.     //glEnable(GL_CULL_FACE);  
  22.     glCullFace(GL_FRONT);  
  23.     glBegin(GL_POLYGON);  
  24.         glVertex2f(-0.5f, -0.5f);  
  25.         glVertex2f(0.0f,-0.5f);  
  26.         glVertex2f(0.0f,0.0f);  
  27.         glVertex2f(-0.5f,0.0f);  
  28.     glEnd();  
  29.     glDisable(GL_CULL_FACE);  
  30.     glFlush();  
  31. }  
  32.   
  33. int main(int argc, char* argv[])  
  34. {  
  35.     glutInit(&argc, argv);  
  36.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  37.     glutInitWindowPosition(400,400);  
  38.     glutInitWindowSize(400,400);  
  39.     glutCreateWindow("study");  
  40.   
  41.     glutDisplayFunc(myDisplay);  
  42.     glutMainLoop();  
  43.     return 0;  
  44. }</strong></span>  


21.1剔除了一个面后,被挡的面出现

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClearColor(1.0,1.0,1.0,0.0);  
  6.     glClear(GL_COLOR_BUFFER_BIT);  
  7.     glColor3f(0.0,0.0,1.0);  
  8.   
  9.     glPolygonMode(GL_FRONT,GL_FILL); //设置正面为填充模式  
  10.     glPolygonMode(GL_BACK,GL_LINE);  //设置反面为线性模式  
  11.     glFrontFace(GL_CCW);  
  12.       
  13.     glBegin(GL_POLYGON);  
  14.         glVertex2f(-0.5f, -0.5f);  
  15.         glVertex2f(0.0f,-0.5f);  
  16.         glVertex2f(0.0f,0.0f);  
  17.         glVertex2f(-0.5f,0.0f);  
  18.     glEnd();  
  19.       
  20.     glColor3f(1.0,0.0,0.0);  
  21.     glEnable(GL_CULL_FACE);  
  22.     glCullFace(GL_FRONT);  
  23.     glBegin(GL_POLYGON);  
  24.         glVertex2f(-0.5f, -0.5f);  
  25.         glVertex2f(0.0f,-0.5f);  
  26.         glVertex2f(0.0f,0.0f);  
  27.         glVertex2f(-0.5f,0.0f);  
  28.     glEnd();  
  29.     glDisable(GL_CULL_FACE);  
  30.     glFlush();  
  31. }  
  32.   
  33. int main(int argc, char* argv[])  
  34. {  
  35.     glutInit(&argc, argv);  
  36.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  37.     glutInitWindowPosition(400,400);  
  38.     glutInitWindowSize(400,400);  
  39.     glutCreateWindow("study");  
  40.   
  41.     glutDisplayFunc(myDisplay);  
  42.     glutMainLoop();  
  43.     return 0;  
  44. }</strong></span>  


22 画板,镂空的实现

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong><span style="white-space:pre">    </span>glEnable(GL_POLYGON_STIPPLE);激活多面体镂空模式  
  2.     glPolygonStipple(Mask); 镂空数组</strong></span>  

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2. #include<stdio.h>  
  3. #include<STDLIB.H>  
  4.   
  5. void myDisplay()  
  6. {  
  7.     static GLubyte Mask[128];  
  8.     FILE *fp;  
  9.     fp = fopen("mmmm.bmp", "rb");  
  10.     if(!fp)  
  11.         exit(0);  
  12.     if(fseek(fp, -(int)sizeof(Mask),SEEK_END))  
  13.         exit(0);  
  14.     if(!fread(Mask,sizeof(Mask),1,fp))  
  15.         exit(0);  
  16.     fclose(fp);  
  17.   
  18.     glClearColor(1.0,1.0,1.0,0.0);  
  19.     glClear(GL_COLOR_BUFFER_BIT);  
  20.     glColor3f(0.0,0.0,1.0);  
  21.     glEnable(GL_POLYGON_STIPPLE);  
  22.     glPolygonStipple(Mask);  
  23.     glRectf(-0.5f,-0.5f,0.0f,0.0f);  
  24.     glDisable(GL_POLYGON_STIPPLE);  
  25.     glRectf(0.0f,0.0f,0.5f,0.5f);  
  26.     glFlush();  
  27. }  
  28.   
  29. int main(int argc, char* argv[])  
  30. {  
  31.     glutInit(&argc, argv);  
  32.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  33.     glutInitWindowPosition(400,400);  
  34.     glutInitWindowSize(400,400);  
  35.     glutCreateWindow("study");  
  36.   
  37.     glutDisplayFunc(myDisplay);  
  38.     glutMainLoop();  
  39.     return 0;  
  40. }</strong></span>  

技术分享
23 默认光滑模式

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2.   
  3. #include<math.h>  
  4. const GLdouble Pi = 3.1415926536;  
  5. void myDisplay()  
  6. {  
  7.     int i;  
  8.     glClearColor(1.0,1.0,1.0,0.0);  
  9.     glClear(GL_COLOR_BUFFER_BIT);  
  10.       
  11.     glBegin(GL_TRIANGLE_FAN);  
  12.     glColor3f(0.0,0.0,1.0);  
  13.     glVertex2f(0.0f,0.0f);  
  14.     for(i=0;i<=8;++i)  
  15.     {  
  16.         glColor3f(i&0x04, i&0x02, i&0x01);  
  17.         glVertex2f((float)cos(i*Pi/4), (float)sin(i*Pi/4));  
  18.     }  
  19.     glEnd();  
  20.     glFlush();  
  21. }  
  22.   
  23. int main(int argv, char* argc[])  
  24. {  
  25.     glutInit(&argv, argc);  
  26.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  27.     glutInitWindowPosition(400,400);  
  28.     glutInitWindowSize(400,400);  
  29.     glutCreateWindow("study");  
  30.       
  31.     glutDisplayFunc(myDisplay);  
  32.     //Sleep(10*1000);  
  33.     glutMainLoop();  
  34.     return 0;  
  35. }</strong></span>  

技术分享
23.1 设置了清除色

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2.   
  3. #include<math.h>  
  4. const GLdouble Pi = 3.1415926536;  
  5. void myDisplay()  
  6. {  
  7.     int i;  
  8.     glClearColor(1.0,1.0,1.0,0.0);  
  9.     glClear(GL_COLOR_BUFFER_BIT);  
  10.       
  11.     glBegin(GL_TRIANGLE_FAN);  
  12.     //glColor3f(0.0,0.0,1.0);  
  13.     glVertex2f(0.0f,0.0f);  
  14.     for(i=0;i<=8;++i)  
  15.     {  
  16.         glColor3f(i&0x04, i&0x02, i&0x01);  
  17.         glVertex2f((float)cos(i*Pi/4), (float)sin(i*Pi/4));  
  18.     }  
  19.     glEnd();  
  20.     glFlush();  
  21. }  
  22.   
  23. int main(int argv, char* argc[])  
  24. {  
  25.     glutInit(&argv, argc);  
  26.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  27.     glutInitWindowPosition(400,400);  
  28.     glutInitWindowSize(400,400);  
  29.     glutCreateWindow("study");  
  30.       
  31.     glutDisplayFunc(myDisplay);  
  32.     //Sleep(10*1000);  
  33.     glutMainLoop();  
  34.     return 0;  
  35. }</strong></span>  

技术分享
23.2 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>glShadeModel(GL_FLAT);采用平板展现模式---其对应光滑渐变模式</strong></span>  

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2.   
  3. #include<math.h>  
  4. const GLdouble Pi = 3.1415926536;  
  5. void myDisplay()  
  6. {  
  7.     int i;  
  8.     glShadeModel(GL_FLAT);  
  9.     glClearColor(1.0,1.0,1.0,0.0);  
  10.     glClear(GL_COLOR_BUFFER_BIT);  
  11.       
  12.     glBegin(GL_TRIANGLE_FAN);  
  13.     //glColor3f(0.0,0.0,1.0);  
  14.     glVertex2f(0.0f,0.0f);  
  15.     for(i=0;i<=8;++i)  
  16.     {  
  17.         glColor3f(i&0x04, i&0x02, i&0x01);  
  18.         glVertex2f((float)cos(i*Pi/4), (float)sin(i*Pi/4));  
  19.     }  
  20.     glEnd();  
  21.     glFlush();  
  22. }  
  23.   
  24. int main(int argv, char* argc[])  
  25. {  
  26.     glutInit(&argv, argc);  
  27.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  28.     glutInitWindowPosition(400,400);  
  29.     glutInitWindowSize(400,400);  
  30.     glutCreateWindow("study");  
  31.       
  32.     glutDisplayFunc(myDisplay);  
  33.     //Sleep(10*1000);  
  34.     glutMainLoop();  
  35.     return 0;  
  36. }</strong></span>  

技术分享
24 今天是周一,明天周二,计算机图形学上机实验,不能太给老是丢人,就勉强自己写了个三维的,借用隔壁同学的方法使它旋转起来了,发现这方法竟然是下一天的课程,呵呵

 

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2. #include<windows.h>  
  3. #include<math.h>  
  4. static int day = 200;  
  5.   
  6. void myDisplay()  
  7. {  
  8.   
  9.     glEnable(GL_DEPTH_TEST); //启动深度测试  
  10.   
  11.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清空深度缓冲和颜色缓冲  
  12.     glMatrixMode(GL_PROJECTION); //操作投影矩阵  
  13.     glLoadIdentity(); //进行变换前通常把当前矩阵设置为单位矩阵  
  14.     gluPerspective(75,1,1,400000000); //设置可视空间,得到透视效果(可视角,高宽比,最近可视距离,最远可视距离)  
  15.     glMatrixMode(GL_MODELVIEW); //设置当前操作的矩阵为“模型视图矩阵”  
  16.     glLoadIdentity(); //把当前矩阵设置为单位矩阵  
  17.     gluLookAt(0,-200000000,200000000,0,0,0,0,0,1); //设定观察点位置(观察点位置,目标位置,观察者上方向)  
  18.   
  19.   
  20.     glColor3f(1.0f,0.0f,0.0f);  
  21.     //glRotatef(day/360.0*360.0, 0.0f,0.0f,-1.0f);  
  22.     glutSolidSphere(69600000,50,50);  
  23.   
  24.     glColor3f(0.0f,0.0f,1.0f);  
  25.     glRotatef(day, 0.0f,0.0f,-1.0f);  
  26.     glTranslatef(150000000,0.0f,0.0f);  
  27.     glutSolidSphere(15945000,50,50);  
  28.       
  29.     glColor3f(1.0f,1.0f,0.0f);  
  30.     glRotatef(day/30.0*360.0-day,0.0f,0.0f,-1.0f);  
  31.     glTranslatef(38000000,0.0f,0.0f);  
  32.     glutSolidSphere(4345000,50,50);  
  33.   
  34.     glutSwapBuffers();  
  35. }  
  36.   
  37. void play()  
  38. {  
  39.     day++;  
  40.     if(day >= 360)  
  41.         day = 0;  
  42.     myDisplay();  
  43.     Sleep(100);  
  44.     glutPostRedisplay();  
  45. }  
  46.   
  47.   
  48. int main(int argv, char* argc[])  
  49. {  
  50.     glutInit(&argv, argc);  
  51.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  52.     glutInitWindowPosition(400,400);  
  53.     glutInitWindowSize(400,400);  
  54.     glutCreateWindow("study");  
  55.       
  56.     glutDisplayFunc(play);  
  57.       
  58.     glutMainLoop();  
  59.     return 0;  
  60. }</strong></span>  

技术分享

 

10.16

今天只有晚上有时间了,白天都满课

25 光照,材质等,不是很懂,光照必须要会用!

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2.   
  3. #define WIDTH 400  
  4. #define HEIGHT 400  
  5.   
  6. static GLfloat angle = 0.0f;  
  7.   
  8. void myDisplay()  
  9. {  
  10.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
  11.       
  12.     //创建透视效果视图  
  13.     glMatrixMode(GL_PROJECTION); //操作投影矩阵  
  14.     glLoadIdentity();  //当前矩阵设置为单位矩阵  
  15.     gluPerspective(90.0f, 1.0f,1.0f,20.0f);  //得到透视效果  
  16.     glMatrixMode(GL_MODELVIEW); //操作“模型视图”矩阵  
  17.     glLoadIdentity();  
  18.     gluLookAt(0.0,0.0,-10.0,0.0,0.0,0.0,0.0,1.0,0.0);  
  19.       
  20.     //定义太阳光源,它是一种白色光源  
  21.     {  
  22.         GLfloat sun_light_position[] = {0.0f,0.0f,0.0f,1.0f};  
  23.         GLfloat sun_light_ambient[] = {0.0f,0.0f,0.0f,1.0f};  
  24.         GLfloat sun_light_diffuse[] = {1.0f,1.0f,1.0f,1.0f};  
  25.         GLfloat sun_light_specular[] = {1.0f,1.0f,1.0f,1.0f};  
  26.           
  27.         glLightfv(GL_LIGHT0, GL_POSITION, sun_light_position);  
  28.         glLightfv(GL_LIGHT0, GL_AMBIENT, sun_light_ambient);  
  29.         glLightfv(GL_LIGHT0, GL_DIFFUSE, sun_light_diffuse);  
  30.         glLightfv(GL_LIGHT0, GL_SPECULAR, sun_light_specular);  
  31.           
  32.         glEnable(GL_LIGHT0);  
  33.         glEnable(GL_LIGHTING);  
  34.         glEnable(GL_DEPTH_TEST);  
  35.     }  
  36.       
  37.     //定义太阳的材质并绘制太阳  
  38.     {  
  39.         GLfloat sun_mat_ambient[] = {0.0f,0.0f,0.0f,1.0f};  
  40.         GLfloat sun_mat_diffuse[] = {0.0f,0.0f,0.0f,1.0f};  
  41.         GLfloat sun_mat_specular[] = {0.0f,0.0f,0.0f,1.0f};  
  42.         GLfloat sun_mat_emission[] = {0.5f,0.0f,0.0f,1.0f};  
  43.         GLfloat sun_mat_shininess = 0.0f;  
  44.           
  45.         glMaterialfv(GL_FRONT, GL_AMBIENT, sun_mat_ambient); //环境变量  
  46.         glMaterialfv(GL_FRONT, GL_DIFFUSE, sun_mat_diffuse); //散射模式  
  47.         glMaterialfv(GL_FRONT, GL_SPECULAR, sun_mat_specular); //镜面反射  
  48.         glMaterialfv(GL_FRONT, GL_EMISSION, sun_mat_emission); //发射,散发喷射  
  49.         glMaterialf(GL_FRONT, GL_SHININESS, sun_mat_shininess);   
  50.           
  51.         glutSolidSphere(2.0,40,32);  
  52.     }  
  53.       
  54.     //定义地球材质并绘制地球  
  55.     {  
  56.         GLfloat earth_mat_ambient[] = {0.0f,0.0f,0.5f,1.0f};  
  57.         GLfloat earth_mat_diffuse[] = {0.0f,0.0f,0.5f,1.0f};  
  58.         GLfloat earth_mat_specular[] = {0.0f,0.0f,1.0f,1.0f};  
  59.         GLfloat earth_mat_emission[] = {0.0f,0.0f,0.0f,1.0f};  
  60.         GLfloat earth_mat_shininess = 30.0f;  
  61.           
  62.         glMaterialfv(GL_FRONT, GL_AMBIENT, earth_mat_ambient); //环境变量  
  63.         glMaterialfv(GL_FRONT, GL_DIFFUSE, earth_mat_diffuse); //散射模式  
  64.         glMaterialfv(GL_FRONT, GL_SPECULAR, earth_mat_specular); //镜面反射  
  65.         glMaterialfv(GL_FRONT, GL_EMISSION, earth_mat_emission); //发射,散发喷射  
  66.         glMaterialf(GL_FRONT, GL_SHININESS, earth_mat_shininess);   
  67.           
  68.         glRotatef(angle,0.0f,-1.0f,0.0f);  
  69.         glTranslatef(5.0f,0.0f,0.0f);  
  70.         glutSolidSphere(1.5,40,32);  
  71.     }  
  72.     glutSwapBuffers();  
  73. }  
  74.   
  75. void myIdle()  
  76. {  
  77.     angle += 1.0f;  
  78.     if(angle >= 360.0f)  
  79.         angle = 0.0f;  
  80.     myDisplay();  
  81. }  
  82.   
  83. int main(int argc, char* argv[])  
  84. {  
  85.     glutInit(&argc, argv);  
  86.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  
  87.     glutInitWindowPosition(200,200);  
  88.     glutInitWindowSize(WIDTH, HEIGHT);  
  89.     glutCreateWindow("opengl光照演示");  
  90.     glutDisplayFunc(&myDisplay);  
  91.     glutIdleFunc(&myIdle); //回调  
  92.     glutMainLoop();  
  93.     return 0;  
  94. }</strong></span>  

技术分享技术分享
10.17

 

今天周三,满课,且晚上还有数据库上机实验,自己电脑不能用,中午看过这课后借同学的手机敲了代码练习

26 列表的使用(一次编译,多次使用,节省效率)、glutIdleFunc(&myIdle)调用cpu空闲资源且控制旋转角度,注意矩阵的push和pop

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2. #include<math.h>  
  3. #include<windows.h>  
  4.   
  5. #define WIDTH 400  
  6. #define HEIGHT 400  
  7.   
  8. #define ColoredVertex(c,v) do{glColor3fv(c);glVertex3fv(v);}while(0)  
  9.   
  10. GLfloat angle=0.0f;  
  11.   
  12. void myDisplay()  
  13. {  
  14.     static int list = 0;  
  15.     if(list == 0)  
  16.     {  
  17.         GLfloat  
  18.             PointA[] = {0.5f,-sqrt(6.0f)/12,-sqrt(3.0f)/6},  
  19.             PointB[] = {-0.5f,-sqrt(6.0f)/12,-sqrt(3.0f)/6},  
  20.             PointC[] = {0.0f,-sqrt(6.0f)/12,sqrt(3.0f)/3},  
  21.             PointD[] = {0.0f,sqrt(6.0f)/4,0};  
  22.         GLfloat  
  23.             ColorR[] = {1,0,0},  
  24.             ColorG[] = {0,1,0},  
  25.             ColorB[] = {0,0,1},  
  26.             ColorY[] = {1,1,0};  
  27.   
  28.         list = glGenLists(1);  
  29.         glNewList(list,GL_COMPILE);  
  30.         glBegin(GL_TRIANGLES);  
  31.             ColoredVertex(ColorR,PointA);  
  32.             ColoredVertex(ColorG,PointB);  
  33.             ColoredVertex(ColorB,PointC);  
  34.   
  35.             ColoredVertex(ColorR,PointA);  
  36.             ColoredVertex(ColorB,PointC);  
  37.             ColoredVertex(ColorY,PointD);  
  38.   
  39.             ColoredVertex(ColorB,PointC);  
  40.             ColoredVertex(ColorG,PointB);  
  41.             ColoredVertex(ColorY,PointD);  
  42.   
  43.             ColoredVertex(ColorG,PointB);  
  44.             ColoredVertex(ColorR,PointA);  
  45.             ColoredVertex(ColorY,PointD);  
  46.         glEnd();  
  47.         glEndList();  
  48.         glEnable(GL_DEPTH_TEST);  
  49.     }  
  50.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
  51.     glPushMatrix();  
  52.     glRotatef(angle,1,0.5,0);  
  53.     glCallList(list);  
  54.     glPopMatrix();  
  55.     glutSwapBuffers();  
  56. }  
  57.   
  58. void myIdle()  
  59. {  
  60.     ++angle;  
  61.     if(angle >= 360.0f)  
  62.         angle = 0.0f;  
  63.     Sleep(1000/10);  
  64.     myDisplay();  
  65. }  
  66.   
  67. int main(int argc, char* argv[])  
  68. {  
  69.     glutInit(&argc,argv);  
  70.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  
  71.     glutInitWindowPosition(200,200);  
  72.     glutInitWindowSize(400,400);  
  73.     glutCreateWindow("study");  
  74.     glutDisplayFunc(&myDisplay);  
  75.     glutIdleFunc(&myIdle);  
  76.     glutMainLoop();  
  77.     return 0;  
  78. }</strong></span>  

技术分享技术分享技术分享
10.18

 

今天学颜色的混合,会有半透明的效果

27. glBlendFunc(GL_ONE,GL_ZERO);完全使用源色

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClear(GL_COLOR_BUFFER_BIT);  
  6.     glEnable(GL_BLEND);  
  7.     glBlendFunc(GL_ONE,GL_ZERO);  
  8.   
  9.     glColor4f(1,0,0,0.5);  
  10.     glRectf(-1,-1,0.5,0.5);  
  11.     glColor4f(0,1,0,0.5);  
  12.     glRectf(-0.5,-0.5,1,1);  
  13.   
  14.     glutSwapBuffers();  
  15. }  
  16.   
  17. void myIdle()  
  18. {  
  19.     myDisplay();  
  20. }  
  21.   
  22. int main(int argc, char* argv[])  
  23. {  
  24.     glutInit(&argc,argv);  
  25.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  
  26.     glutInitWindowPosition(200,200);  
  27.     glutInitWindowSize(400,400);  
  28.     glutCreateWindow("study");  
  29.   
  30.     myDisplay();  
  31.     glutDisplayFunc(&myDisplay);  
  32.     glutIdleFunc(&myIdle);  
  33.     glutMainLoop();  
  34.     return 0;  
  35.   
  36. }</strong></span>  

技术分享
27.1两种颜色混合

 

glBlendFunc(GL_ONE, GL_ONE);,则表示完全使用源颜色和目标颜色,最终的颜色实际上就是两种颜色的简单相加。例如红色(1, 0, 0)和绿色(0, 1, 0)相加得到(1, 1, 0),结果为黄色

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClear(GL_COLOR_BUFFER_BIT);  
  6.     glEnable(GL_BLEND);  
  7.     glBlendFunc(GL_ONE,GL_ONE);  //改动  
  8.   
  9.     glColor4f(1,0,0,0.5);  
  10.     glRectf(-1,-1,0.5,0.5);  
  11.     glColor4f(0,1,0,0.5);  
  12.     glRectf(-0.5,-0.5,1,1);  
  13.   
  14.     glutSwapBuffers();  
  15. }  
  16.   
  17. void myIdle()  
  18. {  
  19.     myDisplay();  
  20. }  
  21.   
  22. int main(int argc, char* argv[])  
  23. {  
  24.     glutInit(&argc,argv);  
  25.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  
  26.     glutInitWindowPosition(200,200);  
  27.     glutInitWindowSize(400,400);  
  28.     glutCreateWindow("study");  
  29.   
  30.     myDisplay();  
  31.     glutDisplayFunc(&myDisplay);  
  32.     glutIdleFunc(&myIdle);  
  33.     glutMainLoop();  
  34.     return 0;  
  35.   
  36. }</strong></span>  

 

技术分享

27.2

GL_ONE_MINUS_SRC_ALPHA:表示用1.0减去源颜色的alpha值来作为因子。
GL_ONE_MINUS_DST_ALPHA:表示用1.0减去目标颜色的alpha值来作为因子。

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2.   
  3. void myDisplay()  
  4. {  
  5.     glClear(GL_COLOR_BUFFER_BIT);  
  6.     glEnable(GL_BLEND);  
  7.     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);  //改动  
  8.   
  9.     glColor4f(1,0,0,0.5);  
  10.     glRectf(-1,-1,0.5,0.5);  
  11.     glColor4f(0,1,0,0.5);  
  12.     glRectf(-0.5,-0.5,1,1);  
  13.   
  14.     glutSwapBuffers();  
  15. }  
  16.   
  17. void myIdle()  
  18. {  
  19.     myDisplay();  
  20. }  
  21.   
  22. int main(int argc, char* argv[])  
  23. {  
  24.     glutInit(&argc,argv);  
  25.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  
  26.     glutInitWindowPosition(200,200);  
  27.     glutInitWindowSize(400,400);  
  28.     glutCreateWindow("study");  
  29.   
  30.     myDisplay();  
  31.     glutDisplayFunc(&myDisplay);  
  32.     glutIdleFunc(&myIdle);  
  33.     glutMainLoop();  
  34.     return 0;  
  35.   
  36. }  
  37. </strong></span>  

技术分享

 

28 光源,绘制半透明物体,注意深度测试的控制

在进行三维混合时,不仅要考虑源因子和目标因子,还应该考虑深度缓冲区。必须先绘制所有不透明的物体,再绘制半透明的物体。在绘制半透明物体时前,还需要将深度缓冲区设置为只读形式,否则可能出现画面错误。

 

[cpp] view plaincopy
 
  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>  
  2.   
  3. //在1,1,-1处设置白色的光源  
  4. void setLight()  
  5. {  
  6.     static const GLfloat light_position[] = {1.0f,1.0f,-1.0f,1.0f};  
  7.     static const GLfloat light_ambient[] = {0.2f,0.2f,0.2f,1.0f};  
  8.     static const GLfloat light_diffuse[] = {1.0f,1.0f,1.0f,1.0f};  
  9.     static const GLfloat light_specular[] = {1.0f,1.0f,1.0f,1.0f};  
  10.       
  11.     glLightfv(GL_LIGHT0,GL_POSITION, light_position);  
  12.     glLightfv(GL_LIGHT0,GL_AMBIENT, light_ambient);  
  13.     glLightfv(GL_LIGHT0,GL_DIFFUSE, light_diffuse);  
  14.     glLightfv(GL_LIGHT0,GL_SPECULAR, light_specular);  
  15.       
  16.     glEnable(GL_LIGHT0);  
  17.     glEnable(GL_LIGHTING);  
  18.     glEnable(GL_DEPTH_TEST);  
  19. }  
  20.   
  21. //设置材质  
  22. void setMatirial(const GLfloat mat_diffuse[4], GLfloat mat_shininess)  
  23. {  
  24.     static const GLfloat mat_specular[] = {0.0f,0.0f,0.0f,1.0f};  
  25.     static const GLfloat mat_emission[] = {0.0f,0.0f,0.0f,1.0f};  
  26.       
  27.     glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mat_diffuse);  
  28.     glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);  
  29.     glMaterialfv(GL_FRONT,GL_EMISSION,mat_emission);  
  30.     glMaterialf(GL_FRONT,GL_SHININESS,mat_shininess);  
  31. }  
  32.   
  33.   
  34. void myDisplay()  
  35. {  
  36.     //定义一些材质颜色  
  37.     const static GLfloat red_color[] = {1.0f,0.0f,0.0f,1.0f};  
  38.     const static GLfloat green_color[] = {0.0f,1.0f,0.0f,0.3333f};  
  39.     const static GLfloat blue_color[] = {0.0f,0.0f,1.0f,0.5f};  
  40.       
  41.     //清除屏幕  
  42.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
  43.       
  44.       
  45.       
  46.     //设置光源  
  47.     setLight();  
  48.   
  49.     //启动混合并设置混合因子  
  50.     glEnable(GL_BLEND);  
  51.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);  
  52.       
  53.     //以(0,0,0.5)为中心,绘制一个半径为0.3的不透明红色球体(离观察者最远)  
  54.     setMatirial(red_color, 30.0);  
  55.     glPushMatrix();  
  56.     glTranslatef(0.0f,0.0f,0.5f);  
  57.     glutSolidSphere(0.3,30,30);   
  58.     glPopMatrix();  
  59.       
  60.     //绘制半透明物体  
  61.     glDepthMask(GL_FALSE);  
  62.       
  63.     //以(0.2,0,-0.5)为中心,绘制一个半径为0.2的半透明蓝色球体(离观察者最近)  
  64.     setMatirial(blue_color, 30.0);  
  65.     glPushMatrix();  
  66.     glTranslatef(0.2f,0.0f,-0.5f);  
  67.     glutSolidSphere(0.2,30,30);  
  68.     glPopMatrix();    
  69.   
  70.   
  71.   
  72.   
  73.     //以(0.1,0,0)为中心,绘制一个半径为0.15的半透明绿色球体(在两球体之间)  
  74.     setMatirial(green_color, 30.0);  
  75.     glPushMatrix();  
  76.     glTranslatef(0.1,0,0);  
  77.     glutSolidSphere(0.15,30,30);  
  78.     glPopMatrix();    
  79.       
  80.       
  81.     //深度缓冲区恢复为可读可写模式  
  82.     glDepthMask(GL_TRUE);  
  83.       
  84.     glutSwapBuffers();  
  85.       
  86. }  
  87.   
  88. void myIdle()  
  89. {  
  90.     myDisplay();  
  91. }  
  92.   
  93. int main(int argc, char* argv[])  
  94. {  
  95.     glutInit(&argc,argv);  
  96.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  
  97.     glutInitWindowPosition(200,200);  
  98.     glutInitWindowSize(400,400);  
  99.     glutCreateWindow("study");  
  100.       
  101.     myDisplay();  
  102.     glutDisplayFunc(&myDisplay);  
  103.     //glutIdleFunc(&myIdle);  
  104.     glutMainLoop();  
  105.     return 0;  
  106.       
  107. }</strong></span>  

技术分享

10.19

29 读取bmp图片的宽度和高度值,代码跟下一个程序开头类似

主代码:

 

[cpp] view plaincopy
 
    1. <pre name="code" class="cpp"><span style="font-size:18px;"><strong>static GLint ImageWidth;  
    2. static GLint ImageHeight;</strong></span></pre>  
    3. <pre></pre>  
    4. <p></p>  
    5. <pre></pre>  
    6. <pre name="code" class="cpp"><span style="font-size:18px;"><strong>//打开文件  
    7.     FILE* pFile = fopen("1234.bmp", "rb");  
    8.     if(pFile == 0)  
    9.         exit(0);  
    10.   
    11.     //读取图象的大小信息  
    12.     fseek(pFile, 0x0012, SEEK_SET);  
    13.     fread(&ImageWidth,sizeof(ImageWidth),1,pFile);  
    14.     fread(&ImageHeight,sizeof(ImageHeight),1,pFile);</strong></span></pre><br>  
    15. <p></p>  
    16. <p><span style="font-size:18px"><strong><img src="http://img.my.csdn.net/uploads/201210/27/1351343996_9120.png" alt=""><br>  
    17. </strong></span></p>  
    18. <p><span style="font-size:18px"><strong><br>  
    19. </strong></span></p>  
    20. <p><span style="font-size:18px"><strong>30 读取bmp图片文件--存像素数值,画出来</strong></span></p>  
    21. <p><span style="font-size:18px"><strong></strong></span></p>  
    22. <pre name="code" class="cpp"><strong><span style="font-size:18px;">#include<GL/glut.h>  
    23. #include<stdio.h>  
    24. #include<stdlib.h>  
    25.   
    26. #define FileName "1234.bmp"  
    27.   
    28. static GLint ImageWidth;  
    29. static GLint ImageHeight;  
    30. static GLint PixelLength;  
    31. static GLubyte* PixelData;  
    32.   
    33. void display()  
    34. {  
    35.     /*清除屏幕并不必要 
    36.     * 每次绘制时,画面都覆盖整个屏幕 
    37.     * 因此无论是否清除屏幕,结果都一样 
    38.     */  
    39.     //glClear(GL_COLOR_BUFFER_BIT);  
    40.   
    41.     //绘制像素  
    42.     glDrawPixels(ImageWidth,ImageHeight,  
    43.         GL_BGR_EXT,GL_UNSIGNED_BYTE,PixelData);  
    44.     //完成绘制  
    45.     glutSwapBuffers();  
    46. }  
    47.   
    48. int main(int argc, char* argv[])  
    49. {  
    50.     //打开文件  
    51.     FILE* pFile = fopen("1234.bmp", "rb");  
    52.     if(pFile == 0)  
    53.         exit(0);  
    54.   
    55.     //读取图象的大小信息  
    56.     fseek(pFile, 0x0012, SEEK_SET);  
    57.     fread(&ImageWidth,sizeof(ImageWidth),1,pFile);  
    58.     fread(&ImageHeight,sizeof(ImageHeight),1,pFile);  
    59.   
    60.     //计算像素数据长度  
    61.     PixelLength = ImageWidth*3;  
    62.     while(PixelLength%4 != 0)  
    63.         ++PixelLength;  
    64.     PixelLength *= ImageHeight;  
    65.   
    66.     //读取像素数据  
    67.     PixelData = (GLubyte*)malloc(PixelLength);  
    68.     if(PixelData == 0)  
    69.         exit(0);  
    70.   
    71.     fseek(pFile,54,SEEK_SET);  
    72.     fread(PixelData,PixelLength,1,pFile);  
    73.   
    74.     //关闭文件  
    75.     fclose(pFile);  
    76.   
    77.     //初始化GLUT并运行  
    78.     glutInit(&argc,argv);  
    79.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);  
    80.     glutInitWindowPosition(200,200);  
    81.     glutInitWindowSize(ImageWidth,ImageHeight);  
    82.     glutCreateWindow(FileName);  
    83.     glutDisplayFunc(&display);  
    84.     glutMainLoop();  
    85.     free(PixelData);  
    86.     return 0;  
    87. }</span></strong></pre>  
    88. <p></p>  
    89. <p><img src="http://img.my.csdn.net/uploads/201210/27/1351343956_5014.png" alt=""><br>  
    90. </p>  
    91. <p><strong><span style="font-size:18px"><br>  
    92. </span></strong></p>  
    93. <p><strong><span style="font-size:18px">31 像素的拷贝<span style="color:rgb(0,0,255); font-family:georgia,Verdana,Helvetica,Arial; line-height:19px; text-indent:26px">glCopyPixels()--坐标点坐标,宽度值、高度值、GL_COLOR</span></span></strong></p>  
    94. <p></p>  
    95. <pre name="code" class="cpp"><span style="font-size:18px;"><strong>#include<GL/glut.h>  
    96. #include<iostream>  
    97. using namespace std;  
    98.   
    99. #define WindowWidth 400  
    100. #define WindowHeight 400  
    101.   
    102. /*函数grab 
    103. * 抓取窗口中的像素 
    104. * 假设窗口宽度为WindowWidth,高度为WindowHeight 
    105. */  
    106.   
    107. #define BMP_Header_Length 54  
    108.   
    109. void grap()  
    110. {  
    111.     FILE* pDummyFile;  
    112.     FILE* pWritingFile;  
    113.     GLubyte* pPixelData;  
    114.     GLubyte BMP_Header[BMP_Header_Length];  
    115.     GLint i, j;  
    116.     GLint PixelDataLength;  
    117.       
    118.     //计算像素数据的实际长度  
    119.     i = WindowWidth * 3;  //得到每一行的像素数据长度  
    120.     while(i%4 != 0)   //补充数据知道i是4的倍数  
    121.         ++i;          //本来还有更快的算法,但这里追求直观,对速度没有太高要求  
    122.     PixelDataLength = i*WindowHeight; //内存字节大小  
    123.       
    124.     //分配内存和打开文件  
    125.     pPixelData = (GLubyte*)malloc(PixelDataLength);  
    126.     if(pPixelData == 0)  
    127.         exit(0);  
    128.       
    129.     pDummyFile = fopen("dummy.bmp", "rb");  
    130.     if(pDummyFile == 0)  
    131.         exit(0);  
    132.       
    133.     pWritingFile = fopen("grab.bmp", "wb");  
    134.     if(pWritingFile == 0)  
    135.         exit(0);  
    136.       
    137.     //读取像素  
    138.     glPixelStorei(GL_UNPACK_ALIGNMENT, 4);  
    139.     glReadPixels(0,0,WindowWidth, WindowHeight,  
    140.         GL_BGR_EXT,GL_UNSIGNED_BYTE,pPixelData);  
    141.       
    142.     //把dummy.bmp的头文件复制为新文件的文件头  
    143.     fread(BMP_Header,sizeof(BMP_Header),1,pDummyFile);  
    144.     fwrite(BMP_Header,sizeof(BMP_Header),1,pWritingFile);  
    145.     fseek(pWritingFile,0x0012,SEEK_SET);  
    146.     i = WindowWidth;  
    147.     j = WindowHeight;  
    148.     fwrite(&i,sizeof(i),1,pWritingFile);  
    149.     fwrite(&j,sizeof(j),1,pWritingFile);  
    150.       
    151.     //写入像素数据  
    152.     fseek(pWritingFile,0,SEEK_END);  
    153.     fwrite(pPixelData,PixelDataLength,1,pWritingFile);  
    154.       
    155.     //释放内存和关闭文件  
    156.     fclose(pDummyFile);  
    157.     fclose(pWritingFile);  
    158.     free(pPixelData);  
    159. }  
    160.   
    161.   
    162. void myDisplay()  
    163. {  
    164.   
    165.     glClear(GL_COLOR_BUFFER_BIT);  
    166.     glBegin(GL_TRIANGLES);  
    167.             glColor3f(1.0,0.0,0.0); glVertex2f(0.0f,0.0f);  
    168.             glColor3f(0.0,1.0,0.0); glVertex2f(1.0f,0.0f);  
    169.             glColor3f(0.0,0.0,1.0); glVertex2f(0.5f,1.0f);    
    170.     glEnd();  
    171.     glPixelZoom(-0.5f,-0.5f);  
    172.     glRasterPos2i(1,1);  
    173.     glCopyPixels(WindowWidth/2,WindowHeight/2,  
    174.         WindowWidth/2,WindowHeight/2,GL_COLOR);  
    175.     glutSwapBuffers();  
    176.     grap();  
    177. }  
    178.   
    179. void myIdle()  
    180. {  
    181.     cout<<"doing"<<endl;  
    182.     grap();  
    183.     return;  
    184. }  
    185.   
    186. int main(int argc, char* argv[])  
    187. {  
    188.     glutInit(&argc, argv);  
    189.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
    190.     glutInitWindowPosition(400,400);  
    191.     glutInitWindowSize(400,400);  
    192.     glutCreateWindow("Study04");  
    193.     glutDisplayFunc(&myDisplay);  
    194.   
    195.     glutMainLoop();  
    196.       
    197.     cout<<"ok"<<endl;  
    198.     return 0;  
    199. }</strong></span></pre>  
    200. <p></p>  
    201. <p><span style="font-size:18px"><strong><img src="http://img.my.csdn.net/uploads/201210/27/1351343928_8023.png" alt=""></strong></span></p>  
    202. <p><span style="font-size:18px"><strong>10.20 纹理测试</strong></span></p>  
    203. <p><span style="font-size:18px"><strong>32.1<span style="font-family:georgia,Verdana,Helvetica,Arial; line-height:19px; text-indent:26px; background-color:rgb(255,255,255)">纹理的使用方法,只要指定每一个顶点在纹理图象中所对应的像素位置,OpenGL就会自动计算顶点以外的其它点在纹理图象中所对应的像素位置</span></strong></span></p>  
    204. <p><span style="font-size:18px"><strong></strong></span></p>  
    205. <pre name="code" class="cpp"><strong><span style="font-size:18px;">#define WindowWidth 400  
    206. #define WindowHeight 400  
    207. #define WindowTitle "OpenGL纹理测试"  
    208.   
    209. #include<GL/glut.h>  
    210. #include<stdio.h>  
    211. #include<stdlib.h>  
    212.   
    213. /*函数grap 
    214.  *抓取窗口中的像素 
    215.  *假设窗口宽度为WindowWidth,高度为WindowHeight 
    216.  */  
    217.   
    218. #define BMP_Header_Length 54  
    219. void grap()  
    220. {  
    221.     FILE* pDummyFile;  
    222.     FILE* pWritingFile;  
    223.     GLubyte* pPixelData;  
    224.     GLubyte BMP_Header[BMP_Header_Length];  
    225.     GLint i,j;  
    226.     GLint PixelDataLength;  
    227.   
    228.     //计算像素数据的实际长度  
    229.     i = WindowWidth * 3;  //得到每一行的像素数据长度  
    230.     while(i%4 == 0)  
    231.         ++i;  
    232.     PixelDataLength = i * WindowHeight;  
    233.   
    234.     //分配内存和打开文件  
    235.     pPixelData = (GLubyte*)malloc(PixelDataLength);  
    236.     if(pPixelData == 0)  
    237.         exit(0);  
    238.   
    239.     pDummyFile = fopen("dummy.bmp","rb");  
    240.     if(pDummyFile == 0)  
    241.         exit(0);  
    242.   
    243.     pWritingFile = fopen("grap.bmp","wb");  
    244.     if(pWritingFile == 0)  
    245.         exit(0);  
    246.   
    247.     //读取像素  
    248.     glPixelStorei(GL_UNPACK_ALIGNMENT,4);  
    249.   
    250.     glReadPixels(0,0,WindowWidth,WindowHeight,  
    251.         GL_BGR_EXT,GL_UNSIGNED_BYTE,pPixelData);  
    252.   
    253.     //把dummy.bmp的文件头复制为新文件的文件头  
    254.     fread(BMP_Header,sizeof(BMP_Header),1,pDummyFile);  
    255.     fwrite(BMP_Header,sizeof(BMP_Header),1,pWritingFile);  
    256.     fseek(pWritingFile,0x0012,SEEK_SET);  
    257.     i = WindowWidth;  
    258.     j = WindowHeight;  
    259.     fwrite(&i,sizeof(i),1,pWritingFile);  
    260.     fwrite(&j,sizeof(j),1,pWritingFile);  
    261.   
    262.     //写入像素数据  
    263.     fseek(pWritingFile,54,SEEK_SET);  
    264.     fwrite(pPixelData,PixelDataLength,1,pWritingFile);  
    265.   
    266.     //释放内存并关闭文件  
    267.     fclose(pDummyFile);  
    268.     fclose(pWritingFile);  
    269.     free(pPixelData);  
    270. }  
    271.   
    272. /* 函数power_of_two 
    273.  * 检查一个整数是否为2的整数次方,如果是,返回1,否则返回0 
    274.  * 实际上只要查看其二进制位中有多少个1,如果正好有1个,返回1,否则返回0 
    275.  * 在“查看其二进制位中有多少个”时使用了一个小技巧 
    276.  * 使用n &= (n-1)可以使得n中的减少一个 
    277. */  
    278. int power_of_two(int n)  
    279. {  
    280.     if(n <= 0)  
    281.         return 0;  
    282.     return (n&(n-1)) == 0;  
    283. }  
    284.   
    285. /* 函数load_texture 
    286.  * 读取一个BMP文件作为纹理 
    287.  * 如果失败,返回0,如果成功,返回纹理编号 
    288. */  
    289. GLuint load_texture(const char* file_name)  
    290. {  
    291.     GLint width,height,total_bytes;  
    292.     GLubyte* pixels=0;  
    293.     GLuint last_texture_ID,texture_ID=0;  
    294.   
    295.     //打开文件,如果失败,返回  
    296.     FILE* pFile=fopen(file_name,"rb");  
    297.     if(pFile == 0)  
    298.         return 0;  
    299.   
    300.     //读取文件中图像的宽度和高度  
    301.     fseek(pFile,0x0012,SEEK_SET);  
    302.     fread(&width,4,1,pFile);  
    303.     fread(&height,4,1,pFile);  
    304.     fseek(pFile,BMP_Header_Length,SEEK_SET);  
    305.   
    306.     //计算每行像素所占的字节数,并根据此数据计算总像素字节数  
    307.     {  
    308.         GLint line_bytes = width*3;  
    309.         while(line_bytes % 4  !=  0)  
    310.             ++line_bytes;  
    311.         total_bytes = line_bytes*height;  
    312.     }  
    313.   
    314.     //根据总像素字节数分配内存  
    315.     pixels = (GLubyte*)malloc(total_bytes);  
    316.     if(pixels == 0)  
    317.     {  
    318.         fclose(pFile);  
    319.         return 0;  
    320.     }  
    321.   
    322.     //读取像素数据  
    323.     if(fread(pixels,total_bytes,1,pFile) <= 0)  
    324.     {  
    325.         free(pixels);  
    326.         fclose(pFile);  
    327.         return 0;  
    328.     }  
    329.   
    330.     //在旧版本的OpenGL中  
    331.     //如果图像宽度和高度不是2的整数次方,则需要进行缩放  
    332.     //这里并没有检查openGL版本,出于对版本兼容性的考虑,按旧版本处理  
    333.     //另外,无论是旧版本还是新版本  
    334.     //当图像的宽度和高度超过当前openGL实现所支持的最大值时,也要进行缩放  
    335.     {  
    336.         GLint max;  
    337.         glGetIntegerv(GL_MAX_TEXTURE_SIZE,&max);  
    338.         if(!power_of_two(width) || !power_of_two(height) || width>max || height>max)  
    339.         {  
    340.             const GLint new_width = 256;  
    341.             const GLint new_height = 256; //规定缩放后新的大小为256的正方形  
    342.             GLint new_line_bytes,new_total_bytes;  
    343.             GLubyte* new_pixels=0;  
    344.   
    345.             //计算每行所需要的字节数和总字节数  
    346.             new_line_bytes = new_width * 3;  
    347.             while(new_line_bytes % 4  != 0)  
    348.                 ++new_line_bytes;  
    349.             new_total_bytes = new_line_bytes * new_height;  
    350.   
    351.             //分配内存  
    352.             new_pixels = (GLubyte*)malloc(new_total_bytes);  
    353.             if(new_pixels == 0)  
    354.             {  
    355.                 free(pixels);  
    356.                 fclose(pFile);  
    357.                 return 0;  
    358.             }  
    359.   
    360.             //进行像素缩放  
    361.             gluScaleImage(GL_RGB,  
    362.                  width,height,GL_UNSIGNED_BYTE,pixels,  
    363.                  new_width,new_height,GL_UNSIGNED_BYTE,new_pixels);  
    364.   
    365.             //释放原来的像素数据,把pixels指向新的像素数据,并重新设置width和height  
    366.             free(pixels);  
    367.             pixels = new_pixels;  
    368.             width = new_width;  
    369.             height = new_height;  
    370.         }  
    371.     }  
    372.   
    373.     //分配一个新的纹理编号  
    374.     glGenTextures(1,&texture_ID);  
    375.     if(texture_ID == 0)  
    376.     {  
    377.         free(pixels);  
    378.         fclose(pFile);  
    379.         return 0;  
    380.     }  
    381.   
    382.     //绑定新的纹理,载入纹理并设置纹理参数  
    383.     //在绑定前,先获得原来绑定的纹理编号,以便在最后进行恢复  
    384.     glGetIntegerv(GL_TEXTURE_BINDING_2D,(int*)&last_texture_ID);  
    385.     glBindTexture(GL_TEXTURE_2D,texture_ID);  
    386.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);  
    387.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);  
    388.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT);  
    389.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);  
    390.     glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,  
    391.         GL_BGR_EXT,GL_UNSIGNED_BYTE,pixels);  
    392.     glBindTexture(GL_TEXTURE_2D,last_texture_ID);  
    393.   
    394.   
    395.     //之前为pixels分配的内存可在使用glTexImage2D后释放  
    396.     //因为此时像素数据已经被openGL另行保存了一份(可能被保存在专门的图形硬件中)  
    397.     free(pixels);  
    398.     return texture_ID;  
    399. }  
    400.   
    401.   
    402. /* 两个纹理对象的编号 
    403. */  
    404. GLuint texGround;  
    405. GLuint texWall;  
    406.   
    407. void display()  
    408. {  
    409.     //清除屏幕  
    410.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
    411.       
    412.     //设置视角  
    413.     glMatrixMode(GL_PROJECTION);  
    414.     glLoadIdentity();  
    415.     gluPerspective(75,1,1,21);  
    416.     glMatrixMode(GL_MODELVIEW);  
    417.     glLoadIdentity();  
    418.     gluLookAt(1,5,5,0,0,0,0,0,1);  
    419.   
    420.     //使用“地”纹理绘制土地  
    421.     glBindTexture(GL_TEXTURE_2D,texWall);  
    422.     glBegin(GL_QUADS);  
    423.         glTexCoord2f(0.0f,0.0f); glVertex3f(-8.0f,-8.0f,0.0f);  
    424.         glTexCoord2f(0.0f,5.0f); glVertex3f(-8.0f,8.0f,0.0f);  
    425.         glTexCoord2f(5.0f,5.0f); glVertex3f(8.0f,8.0f,0.0f);  
    426.         glTexCoord2f(5.0f,0.0f); glVertex3f(8.0f,-8.0f,0.0f);  
    427.     glEnd();  
    428.   
    429.     //使用“墙”纹理绘制栅栏  
    430.     glBindTexture(GL_TEXTURE_2D,texWall);  
    431.     glBegin(GL_QUADS);  
    432.         glTexCoord2f(0.0f,0.0f); glVertex3f(-6.0f,-3.0f,0.0f);  
    433.         glTexCoord2f(0.0f,1.0f); glVertex3f(-6.0f,-3.0f,1.5f);  
    434.         glTexCoord2f(5.0f,1.0f); glVertex3f(6.0f,-3.0f,1.5f);  
    435.         glTexCoord2f(5.0f,0.0f); glVertex3f(6.0f,-3.0f,0.0f);  
    436.     glEnd();  
    437.   
    438.     //旋转后再绘制一个  
    439.     glRotatef(-90,0,0,1);  
    440.     glBegin(GL_QUADS);  
    441.         glTexCoord2f(0.0f,0.0f); glVertex3f(-6.0f,-3.0f,0.0f);  
    442.         glTexCoord2f(0.0f,1.0f); glVertex3f(-6.0f,-3.0f,1.5f);  
    443.         glTexCoord2f(5.0f,1.0f); glVertex3f(6.0f,-3.0f,1.5f);  
    444.         glTexCoord2f(5.0f,0.0f); glVertex3f(6.0f,-3.0f,0.0f);         
    445.     glEnd();  
    446.   
    447.     //交换缓冲区,并保存像素数据到文件  
    448.     glutSwapBuffers();  
    449.     grap();  
    450. }  
    451.   
    452. int main(int argc, char* argv[])  
    453. {  
    454.     glutInit(&argc,argv);  
    455.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);  
    456.     glutInitWindowPosition(200,200);  
    457.     glutInitWindowSize(WindowWidth,WindowHeight);  
    458.     glutCreateWindow(WindowTitle);  
    459.     glutDisplayFunc(&display);  
    460.   
    461.     //在这里做一些初始化  
    462.     glEnable(GL_DEPTH_TEST);  
    463.     glEnable(GL_TEXTURE_2D);  
    464.     texGround = load_texture("ground.bmp");  
    465.     texWall = load_texture("wall.bmp");  
    466.   
    467.     //开始显示  
    468.     glutMainLoop();  
    469.     return 0;  
    470. }</span></strong></pre><img src="http://img.my.csdn.net/uploads/201210/27/1351344610_2938.png" alt=""><br>  
    471. <p><br>  
    472. </p>  
    473. <p><strong><span style="font-size:18px">32 跟上个程序原理一样,上一个载入的都是墙的坐标,所以图像也不是很正确</span></strong></p>  
    474. <p></p>  
    475. <p><span style="font-size:18px"><strong></strong></span></p>  
    476. <pre name="code" class="cpp"><strong><span style="font-size:18px;">#define WindowWidth 400  
    477. #define WindowHeight 400  
    478. #define WindowTitle "OpenGL纹理测试"  
    479.   
    480. #include<GL/glut.h>  
    481. #include<stdio.h>  
    482. #include<stdlib.h>  
    483.   
    484. /*函数grap 
    485.  *抓取窗口中的像素 
    486.  *假设窗口宽度为WindowWidth,高度为WindowHeight 
    487.  */  
    488.   
    489. #define BMP_Header_Length 54  
    490. void grap()  
    491. {  
    492.     FILE* pDummyFile;  
    493.     FILE* pWritingFile;  
    494.     GLubyte* pPixelData;  
    495.     GLubyte BMP_Header[BMP_Header_Length];  
    496.     GLint i,j;  
    497.     GLint PixelDataLength;  
    498.   
    499.     //计算像素数据的实际长度  
    500.     i = WindowWidth * 3;  //得到每一行的像素数据长度  
    501.     while(i%4 == 0)  
    502.         ++i;  
    503.     PixelDataLength = i * WindowHeight;  
    504.   
    505.     //分配内存和打开文件  
    506.     pPixelData = (GLubyte*)malloc(PixelDataLength);  
    507.     if(pPixelData == 0)  
    508.         exit(0);  
    509.   
    510.     pDummyFile = fopen("dummy.bmp","rb");  
    511.     if(pDummyFile == 0)  
    512.         exit(0);  
    513.   
    514.     pWritingFile = fopen("grap.bmp","wb");  
    515.     if(pWritingFile == 0)  
    516.         exit(0);  
    517.   
    518.     //读取像素  
    519.     glPixelStorei(GL_UNPACK_ALIGNMENT,4);  
    520.   
    521.     glReadPixels(0,0,WindowWidth,WindowHeight,  
    522.         GL_BGR_EXT,GL_UNSIGNED_BYTE,pPixelData);  
    523.   
    524.     //把dummy.bmp的文件头复制为新文件的文件头  
    525.     fread(BMP_Header,sizeof(BMP_Header),1,pDummyFile);  
    526.     fwrite(BMP_Header,sizeof(BMP_Header),1,pWritingFile);  
    527.     fseek(pWritingFile,0x0012,SEEK_SET);  
    528.     i = WindowWidth;  
    529.     j = WindowHeight;  
    530.     fwrite(&i,sizeof(i),1,pWritingFile);  
    531.     fwrite(&j,sizeof(j),1,pWritingFile);  
    532.   
    533.     //写入像素数据  
    534.     fseek(pWritingFile,54,SEEK_SET);  
    535.     fwrite(pPixelData,PixelDataLength,1,pWritingFile);  
    536.   
    537.     //释放内存并关闭文件  
    538.     fclose(pDummyFile);  
    539.     fclose(pWritingFile);  
    540.     free(pPixelData);  
    541. }  
    542.   
    543. /* 函数power_of_two 
    544.  * 检查一个整数是否为2的整数次方,如果是,返回1,否则返回0 
    545.  * 实际上只要查看其二进制位中有多少个1,如果正好有1个,返回1,否则返回0 
    546.  * 在“查看其二进制位中有多少个”时使用了一个小技巧 
    547.  * 使用n &= (n-1)可以使得n中的减少一个 
    548. */  
    549. int power_of_two(int n)  
    550. {  
    551.     if(n <= 0)  
    552.         return 0;  
    553.     return (n&(n-1)) == 0;  
    554. }  
    555.   
    556. /* 函数load_texture 
    557.  * 读取一个BMP文件作为纹理 
    558.  * 如果失败,返回0,如果成功,返回纹理编号 
    559. */  
    560. GLuint load_texture(const char* file_name)  
    561. {  
    562.     GLint width,height,total_bytes;  
    563.     GLubyte* pixels=0;  
    564.     GLuint last_texture_ID,texture_ID=0;  
    565.   
    566.     //打开文件,如果失败,返回  
    567.     FILE* pFile=fopen(file_name,"rb");  
    568.     if(pFile == 0)  
    569.         return 0;  
    570.   
    571.     //读取文件中图像的宽度和高度  
    572.     fseek(pFile,0x0012,SEEK_SET);  
    573.     fread(&width,4,1,pFile);  
    574.     fread(&height,4,1,pFile);  
    575.     fseek(pFile,BMP_Header_Length,SEEK_SET);  
    576.   
    577.     //计算每行像素所占的字节数,并根据此数据计算总像素字节数  
    578.     {  
    579.         GLint line_bytes = width*3;  
    580.         while(line_bytes % 4  !=  0)  
    581.             ++line_bytes;  
    582.         total_bytes = line_bytes*height;  
    583.     }  
    584.   
    585.     //根据总像素字节数分配内存  
    586.     pixels = (GLubyte*)malloc(total_bytes);  
    587.     if(pixels == 0)  
    588.     {  
    589.         fclose(pFile);  
    590.         return 0;  
    591.     }  
    592.   
    593.     //读取像素数据  
    594.     if(fread(pixels,total_bytes,1,pFile) <= 0)  
    595.     {  
    596.         free(pixels);  
    597.         fclose(pFile);  
    598.         return 0;  
    599.     }  
    600.   
    601.     //在旧版本的OpenGL中  
    602.     //如果图像宽度和高度不是2的整数次方,则需要进行缩放  
    603.     //这里并没有检查openGL版本,出于对版本兼容性的考虑,按旧版本处理  
    604.     //另外,无论是旧版本还是新版本  
    605.     //当图像的宽度和高度超过当前openGL实现所支持的最大值时,也要进行缩放  
    606.     {  
    607.         GLint max;  
    608.         glGetIntegerv(GL_MAX_TEXTURE_SIZE,&max);  
    609.         if(!power_of_two(width) || !power_of_two(height) || width>max || height>max)  
    610.         {  
    611.             const GLint new_width = 256;  
    612.             const GLint new_height = 256; //规定缩放后新的大小为256的正方形  
    613.             GLint new_line_bytes,new_total_bytes;  
    614.             GLubyte* new_pixels=0;  
    615.   
    616.             //计算每行所需要的字节数和总字节数  
    617.             new_line_bytes = new_width * 3;  
    618.             while(new_line_bytes % 4  != 0)  
    619.                 ++new_line_bytes;  
    620.             new_total_bytes = new_line_bytes * new_height;  
    621.   
    622.             //分配内存  
    623.             new_pixels = (GLubyte*)malloc(new_total_bytes);  
    624.             if(new_pixels == 0)  
    625.             {  
    626.                 free(pixels);  
    627.                 fclose(pFile);  
    628.                 return 0;  
    629.             }  
    630.   
    631.             //进行像素缩放  
    632.             gluScaleImage(GL_RGB,  
    633.                  width,height,GL_UNSIGNED_BYTE,pixels,  
    634.                  new_width,new_height,GL_UNSIGNED_BYTE,new_pixels);  
    635.   
    636.             //释放原来的像素数据,把pixels指向新的像素数据,并重新设置width和height  
    637.             free(pixels);  
    638.             pixels = new_pixels;  
    639.             width = new_width;  
    640.             height = new_height;  
    641.         }  
    642.     }  
    643.   
    644.     //分配一个新的纹理编号  
    645.     glGenTextures(1,&texture_ID);  
    646.     if(texture_ID == 0)  
    647.     {  
    648.         free(pixels);  
    649.         fclose(pFile);  
    650.         return 0;  
    651.     }  
    652.   
    653.     //绑定新的纹理,载入纹理并设置纹理参数  
    654.     //在绑定前,先获得原来绑定的纹理编号,以便在最后进行恢复  
    655.     glGetIntegerv(GL_TEXTURE_BINDING_2D,(int*)&last_texture_ID);  
    656.     glBindTexture(GL_TEXTURE_2D,texture_ID);  
    657.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);  
    658.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);  
    659.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT);  
    660.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);  
    661.     glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,  
    662.         GL_BGR_EXT,GL_UNSIGNED_BYTE,pixels);  
    663.     glBindTexture(GL_TEXTURE_2D,last_texture_ID);  
    664.   
    665.   
    666.     //之前为pixels分配的内存可在使用glTexImage2D后释放  
    667.     //因为此时像素数据已经被openGL另行保存了一份(可能被保存在专门的图形硬件中)  
    668.     free(pixels);  
    669.     return texture_ID;  
    670. }  
    671.   
    672.   
    673. /* 两个纹理对象的编号 
    674. */  
    675. GLuint texGround;  
    676. GLuint texWall;  
    677.   
    678. void display()  
    679. {  
    680.     //清除屏幕  
    681.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
    682.       
    683.     //设置视角  
    684.     glMatrixMode(GL_PROJECTION);  
    685.     glLoadIdentity();  
    686.     gluPerspective(75,1,1,21);  
    687.     glMatrixMode(GL_MODELVIEW);  
    688.     glLoadIdentity();  
    689.     gluLookAt(1,5,5,0,0,0,0,0,1);  
    690.   
    691.     //使用“地”纹理绘制土地  
    692.     glBindTexture(GL_TEXTURE_2D,texGround);  
    693.     glBegin(GL_QUADS);  
    694.         glTexCoord2f(0.0f,0.0f); glVertex3f(-8.0f,-8.0f,0.0f);  
    695.         glTexCoord2f(0.0f,5.0f); glVertex3f(-8.0f,8.0f,0.0f);  
    696.         glTexCoord2f(5.0f,5.0f); glVertex3f(8.0f,8.0f,0.0f);  
    697.         glTexCoord2f(5.0f,0.0f); glVertex3f(8.0f,-8.0f,0.0f);  
    698.     glEnd();  
    699.   
    700.     //使用“墙”纹理绘制栅栏  
    701.     glBindTexture(GL_TEXTURE_2D,texWall);  
    702.     glBegin(GL_QUADS);  
    703.         glTexCoord2f(0.0f,0.0f); glVertex3f(-6.0f,-3.0f,0.0f);  
    704.         glTexCoord2f(0.0f,1.0f); glVertex3f(-6.0f,-3.0f,1.5f);  
    705.         glTexCoord2f(5.0f,1.0f); glVertex3f(6.0f,-3.0f,1.5f);  
    706.         glTexCoord2f(5.0f,0.0f); glVertex3f(6.0f,-3.0f,0.0f);  
    707.     glEnd();  
    708.   
    709.     //旋转后再绘制一个  
    710.     glRotatef(-90,0,0,1);  
    711.     glBegin(GL_QUADS);  
    712.         glTexCoord2f(0.0f,0.0f); glVertex3f(-6.0f,-3.0f,0.0f);  
    713.         glTexCoord2f(0.0f,1.0f); glVertex3f(-6.0f,-3.0f,1.5f);  
    714.         glTexCoord2f(5.0f,1.0f); glVertex3f(6.0f,-3.0f,1.5f);  
    715.         glTexCoord2f(5.0f,0.0f); glVertex3f(6.0f,-3.0f,0.0f);         
    716.     glEnd();  
    717.   
    718.     //交换缓冲区,并保存像素数据到文件  
    719.     glutSwapBuffers();  
    720.     grap();  
    721. }  
    722.   
    723. int main(int argc, char* argv[])  
    724. {  
    725.     glutInit(&argc,argv);  
    726.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);  
    727.     glutInitWindowPosition(200,200);  
    728.     glutInitWindowSize(WindowWidth,WindowHeight);  
    729.     glutCreateWindow(WindowTitle);  
    730.     glutDisplayFunc(&display);  
    731.   
    732.     //在这里做一些初始化  
    733.     glEnable(GL_DEPTH_TEST);  
    734.     glEnable(GL_TEXTURE_2D);  
    735.     texGround = load_texture("ground.bmp");  
    736.     texWall = load_texture("wall.bmp");  
    737.   
    738.     //开始显示  
    739.     glutMainLoop();  
    740.     return 0;  
    741. }</span></strong></pre><img src="http://img.my.csdn.net/uploads/201210/27/1351344918_9096.png" alt=""><br>  
    742. <p><br>  
    743. </p>  
    744. <p><strong><span style="font-size:18px">33.alpha测试</span></strong></p>  
    745. <p></p>  
    746. <p></p>  
    747. <pre name="code" class="cpp"><strong><span style="font-size:18px;">#define WindowWidth 400  
    748. #define WindowHeight 400  
    749. #define WindowTitle "OpenGL纹理测试"  
    750.   
    751. #include<GL/glut.h>  
    752. #include<stdio.h>  
    753. #include<stdlib.h>  
    754.   
    755. /*函数grap 
    756.  *抓取窗口中的像素 
    757.  *假设窗口宽度为WindowWidth,高度为WindowHeight 
    758.  */  
    759.   
    760. #define BMP_Header_Length 54  
    761.   
    762.   
    763. /* 函数power_of_two 
    764.  * 检查一个整数是否为2的整数次方,如果是,返回1,否则返回0 
    765.  * 实际上只要查看其二进制位中有多少个1,如果正好有1个,返回1,否则返回0 
    766.  * 在“查看其二进制位中有多少个”时使用了一个小技巧 
    767.  * 使用n &= (n-1)可以使得n中的减少一个 
    768. */  
    769. int power_of_two(int n)  
    770. {  
    771.     if(n <= 0)  
    772.         return 0;  
    773.     return (n&(n-1)) == 0;  
    774. }  
    775.   
    776. /* 函数load_texture 
    777.  * 读取一个BMP文件作为纹理 
    778.  * 如果失败,返回0,如果成功,返回纹理编号 
    779. */  
    780. GLuint load_texture(const char* file_name)  
    781. {  
    782.     GLint width,height,total_bytes;  
    783.     GLubyte* pixels=0;  
    784.     GLuint last_texture_ID,texture_ID=0;  
    785.   
    786.     //打开文件,如果失败,返回  
    787.     FILE* pFile=fopen(file_name,"rb");  
    788.     if(pFile == 0)  
    789.         return 0;  
    790.   
    791.     //读取文件中图像的宽度和高度  
    792.     fseek(pFile,0x0012,SEEK_SET);  
    793.     fread(&width,4,1,pFile);  
    794.     fread(&height,4,1,pFile);  
    795.     fseek(pFile,BMP_Header_Length,SEEK_SET);  
    796.   
    797.     //计算每行像素所占的字节数,并根据此数据计算总像素字节数  
    798.     {  
    799.         GLint line_bytes = width*3;  
    800.         while(line_bytes % 4  !=  0)  
    801.             ++line_bytes;  
    802.         total_bytes = line_bytes*height;  
    803.     }  
    804.   
    805.     //根据总像素字节数分配内存  
    806.     pixels = (GLubyte*)malloc(total_bytes);  
    807.     if(pixels == 0)  
    808.     {  
    809.         fclose(pFile);  
    810.         return 0;  
    811.     }  
    812.   
    813.     //读取像素数据  
    814.     if(fread(pixels,total_bytes,1,pFile) <= 0)  
    815.     {  
    816.         free(pixels);  
    817.         fclose(pFile);  
    818.         return 0;  
    819.     }  
    820.   
    821.     //在旧版本的OpenGL中  
    822.     //如果图像宽度和高度不是2的整数次方,则需要进行缩放  
    823.     //这里并没有检查openGL版本,出于对版本兼容性的考虑,按旧版本处理  
    824.     //另外,无论是旧版本还是新版本  
    825.     //当图像的宽度和高度超过当前openGL实现所支持的最大值时,也要进行缩放  
    826.     {  
    827.         GLint max;  
    828.         glGetIntegerv(GL_MAX_TEXTURE_SIZE,&max);  
    829.         if(!power_of_two(width) || !power_of_two(height) || width>max || height>max)  
    830.         {  
    831.             const GLint new_width = 256;  
    832.             const GLint new_height = 256; //规定缩放后新的大小为256的正方形  
    833.             GLint new_line_bytes,new_total_bytes;  
    834.             GLubyte* new_pixels=0;  
    835.   
    836.             //计算每行所需要的字节数和总字节数  
    837.             new_line_bytes = new_width * 3;  
    838.             while(new_line_bytes % 4  != 0)  
    839.                 ++new_line_bytes;  
    840.             new_total_bytes = new_line_bytes * new_height;  
    841.   
    842.             //分配内存  
    843.             new_pixels = (GLubyte*)malloc(new_total_bytes);  
    844.             if(new_pixels == 0)  
    845.             {  
    846.                 free(pixels);  
    847.                 fclose(pFile);  
    848.                 return 0;  
    849.             }  
    850.   
    851.             //进行像素缩放  
    852.             gluScaleImage(GL_RGB,  
    853.                  width,height,GL_UNSIGNED_BYTE,pixels,  
    854.                  new_width,new_height,GL_UNSIGNED_BYTE,new_pixels);  
    855.   
    856.             //释放原来的像素数据,把pixels指向新的像素数据,并重新设置width和height  
    857.             free(pixels);  
    858.             pixels = new_pixels;  
    859.             width = new_width;  
    860.             height = new_height;  
    861.         }  
    862.     }  
    863.   
    864.     //分配一个新的纹理编号  
    865.     glGenTextures(1,&texture_ID);  
    866.     if(texture_ID == 0)  
    867.     {  
    868.         free(pixels);  
    869.         fclose(pFile);  
    870.         return 0;  
    871.     }  
    872.   
    873.     //绑定新的纹理,载入纹理并设置纹理参数  
    874.     //在绑定前,先获得原来绑定的纹理编号,以便在最后进行恢复  
    875.     glGetIntegerv(GL_TEXTURE_BINDING_2D,(int*)&last_texture_ID);  
    876.     glBindTexture(GL_TEXTURE_2D,texture_ID);  
    877.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);  
    878.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);  
    879.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT);  
    880.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);  
    881.     glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,  
    882.         GL_BGR_EXT,GL_UNSIGNED_BYTE,pixels);  
    883.     glBindTexture(GL_TEXTURE_2D,last_texture_ID);  
    884.   
    885.   
    886.     //之前为pixels分配的内存可在使用glTexImage2D后释放  
    887.     //因为此时像素数据已经被openGL另行保存了一份(可能被保存在专门的图形硬件中)  
    888.     free(pixels);  
    889.     return texture_ID;  
    890. }  
    891.   
    892. /* 将当前纹理BGR格式转换为BGRA格式 
    893.  * 纹理中像素的RGB值如果与指定rgb相差不超过absolute,则将Alpha设置为0.0,否则设置为1.0 
    894. */  
    895. void texture_colorKey(GLubyte r,GLubyte g,GLubyte b,GLubyte absolute)  
    896. {  
    897.     GLint width,height;  
    898.     GLubyte* pixels=0;  
    899.   
    900.     //获得纹理的大小信息  
    901.     glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&width);  
    902.     glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&height);  
    903.   
    904.     //分配空间并获得纹理像素  
    905.     pixels = (GLubyte*)malloc(width*height*4);  
    906.     if(pixels == 0)  
    907.         return;  
    908.     glGetTexImage(GL_TEXTURE_2D,0,GL_BGRA_EXT,GL_UNSIGNED_BYTE,pixels);  
    909.   
    910.     //修改像素中的Alpha值  
    911.     //其中pixels[i*4],pixels[i*4+1],pixels[i*4+2],pixels[i*4+3]  
    912.     //分别表示第i个像素的蓝、绿、红、Alpha四种分量,0表示最小,255表示最大  
    913.     {  
    914.         GLint i;  
    915.         GLint count = width*height;  
    916.         for(i=0;i<count;++i)  
    917.         {  
    918.             if(abs(pixels[i*4]-b) <= absolute  
    919.                 && abs(pixels[i*4+1]-g) <= absolute  
    920.                 && abs(pixels[i*4+2]-r) <= absolute)  
    921.                 pixels[i*4+3] = 0;  
    922.             else  
    923.                 pixels[i*4+3] = 255;  
    924.         }  
    925.     }  
    926.       
    927.     //将修改后的像素重新设置到纹理中,释放内存  
    928.     glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,width,height,0,  
    929.         GL_BGRA_EXT,GL_UNSIGNED_BYTE,pixels);  
    930.     free(pixels);  
    931. }  
    932.   
    933. void display()  
    934. {  
    935.     static int initialized=0;  
    936.     static GLuint texWindow=0;  
    937.     static GLuint texPicture=0;  
    938.   
    939.     //执行初始化操作,包括:读取相片,读取相框,将相框由BGR颜色转换为BGRA,  
    940.     //启用二维纹理  
    941.     if(!initialized)  
    942.     {  
    943.         texPicture = load_texture("picture.bmp");  
    944.         texWindow = load_texture("window.bmp");  
    945.         glBindTexture(GL_TEXTURE_2D,texWindow);  
    946.         texture_colorKey(255,255,255,10);  
    947.   
    948.         glEnable(GL_TEXTURE_2D);  
    949.           
    950.         initialized = 1;  
    951.     }  
    952.   
    953.     glClear(GL_COLOR_BUFFER_BIT);  
    954.   
    955.     //绘制相片,此时不需要进行Alpha测试,所有的像素都进行绘制  
    956.     glBindTexture(GL_TEXTURE_2D,texPicture);  
    957.     glDisable(GL_ALPHA_TEST);  
    958.     glBegin(GL_QUADS);  
    959.         glTexCoord2f(0,0);  glVertex2f(-1.0f,-1.0f);  
    960.         glTexCoord2f(0,1);  glVertex2f(-1.0f,1.0f);  
    961.         glTexCoord2f(1,1);  glVertex2f(1.0f,1.0f);  
    962.         glTexCoord2f(1,0);  glVertex2f(1.0f,-1.0f);  
    963.     glEnd();  
    964.   
    965.     //绘制相框,此时进行Alpha测试,只绘制不透明部分的像素  
    966.     glBindTexture(GL_TEXTURE_2D,texWindow);  
    967.     glEnable(GL_ALPHA_TEST);  
    968.     glAlphaFunc(GL_GREATER,0.5f);  
    969.       
    970.     glBegin(GL_QUADS);  
    971.         glTexCoord2f(0,0);  glVertex2f(-1.0f,-1.0f);  
    972.         glTexCoord2f(0,1);  glVertex2f(-1.0f,1.0f);  
    973.         glTexCoord2f(1,1);  glVertex2f(1.0f,1.0f);  
    974.         glTexCoord2f(1,0);  glVertex2f(1.0f,-1.0f);  
    975.     glEnd();  
    976.   
    977.   
    978.     //交换缓冲  
    979.     glutSwapBuffers();  
    980. }  
    981.   
    982.   
    983. int main(int argc, char* argv[])  
    984. {  
    985.     glutInit(&argc,argv);  
    986.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);  
    987.     glutInitWindowPosition(200,200);  
    988.     glutInitWindowSize(WindowWidth,WindowHeight);  
    989.     glutCreateWindow("alpha-test");  
    990.   
    991.   
    992.   
    993.   
    994.     glutDisplayFunc(&display);  
    995.   
    996.   
    997.     //开始显示  
    998.     glutMainLoop();  
    999.     return 0;  
    1000. }</span></strong></pre><span style="font-size:18px"><strong><img src="http://img.my.csdn.net/uploads/201210/27/1351345099_4328.png" alt=""><br>  
    1001. </strong></span>  
    1002. <p></p>  
    1003. <p></p>  
    1004. <p><span style="font-size:18px"><strong>10.21</strong></span></p>  
    1005. <p><span style="font-size:18px"><strong>34. 模版测试。我的测试失败,具体的写法可以参考开头给的网址。</strong></span></p>  
    1006. <p><span style="font-size:18px"><strong></strong></span></p>  
    1007. <pre name="code" class="cpp"><strong><span style="font-size:18px;">#include<GL/glut.h>  
    1008.   
    1009. void draw_sphere()  
    1010. {  
    1011.     //设置光源  
    1012.     glEnable(GL_LIGHTING);  
    1013.     glEnable(GL_LIGHT0);  
    1014.     {  
    1015.         GLfloat  
    1016.             pos[] = {5.0f,5.0f,0.0f,1.0f},  
    1017.             ambient[] = {0.0f,0.0f,1.0f,1.0f};  
    1018.         glLightfv(GL_LIGHT0,GL_POSITION,pos);  
    1019.         glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);  
    1020.     }  
    1021.       
    1022.     //绘制一个球体  
    1023.     glColor3f(1,0,0);  
    1024.     glPushMatrix();  
    1025.     glTranslatef(0,0,2);  
    1026.     glutSolidSphere(0.5,20,20);  
    1027.     glPopMatrix();  
    1028. }  
    1029.   
    1030. void display()  
    1031. {  
    1032.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
    1033.       
    1034.     //设置观察点  
    1035.     glMatrixMode(GL_PROJECTION);  //采用投影矩阵  
    1036.     glLoadIdentity();  
    1037.     gluPerspective(60,1,5,25);  //投影区域,角度,宽高比,近距离,远距离  
    1038.     glMatrixMode(GL_MODELVIEW); //采用模型矩阵  
    1039.     glLoadIdentity();  
    1040.     gluLookAt(5,0,6.5,0,0,0,0,1,0);  
    1041.       
    1042.     glEnable(GL_DEPTH_TEST);  
    1043.       
    1044.     //绘制球体  
    1045.     //glDisable(GL_STENCIL_TEST);  
    1046.     draw_sphere();  
    1047.       
    1048.     //绘制一个平面镜。在绘制的同时注意设置模版缓冲  
    1049.     //另外,为了保证平面镜之后的镜像能够正确绘制,在绘制平面  
    1050.     //镜像时需要将深度缓冲区设置为只读的。  
    1051.     //在绘制时暂时关闭光照效果  
    1052.     glClearStencil(0);  
    1053.     glClear(GL_STENCIL_BUFFER_BIT);  
    1054.     glStencilFunc(GL_ALWAYS,1,0xFF);  
    1055.     glStencilOp(GL_KEEP,GL_KEEP,GL_REPLACE);  
    1056.     glEnable(GL_STENCIL_TEST);  
    1057.       
    1058.     glDisable(GL_LIGHTING);  
    1059.     glColor3f(0.5f,0.5f,0.5f);  
    1060.     glDepthMask(GL_FALSE);  
    1061.     glRectf(-1.5f,-1.5f,1.5f,1.5f);  
    1062.     glDepthMask(GL_TRUE);  
    1063.       
    1064.     //绘制一个与先前球体关于平面镜对称的球体,注意光源的位置也要发生对称改变  
    1065.     //因为平面镜是在X轴和Y轴所确定的平面,所以只要Z坐标取反即可实现对称  
    1066.     //为了保证球体的绘制范围被限制在平面镜内部,使用模版测试  
    1067.     glEnable(GL_STENCIL_TEST);  
    1068.     glStencilFunc(GL_EQUAL,1,0xFF);  
    1069.     glStencilOp(GL_KEEP,GL_KEEP,GL_REPLACE);  
    1070.     glScalef(1.0f,1.0f,-1.0f);  
    1071.     draw_sphere();  
    1072.       
    1073.     //交换缓冲  
    1074.     glutSwapBuffers();  
    1075. }   
    1076.   
    1077. int main(int argc, char* argv[])  
    1078. {  
    1079.     glutInit(&argc,argv);  
    1080.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);  
    1081.     glutInitWindowPosition(200,200);  
    1082.     glutInitWindowSize(400,400);  
    1083.     glutCreateWindow("study");  
    1084.     glutDisplayFunc(&display);  
    1085.     glutMainLoop();  
    1086.     return 0;  
    1087. }</span></strong></pre><img src="http://img.my.csdn.net/uploads/201210/27/1351345241_6397.png" alt=""><br>  
    1088. <p></p>  
    1089. <p><span style="font-size:18px"><strong>35 这题只敲了代码,没插件就没有运行,也没有图,具体大家可以看开头的网址</strong></span></p>  
    1090. <p></p>  
    1091. <pre name="code" class="cpp"><span style="font-size:18px;"><strong>//#include"GLee.h"  
    1092. #include<GL/glut.h>  
    1093. #include<stdio.h>  
    1094.   
    1095. void display()  
    1096. {  
    1097.     glClear(GL_COLOR_BUFFER_BIT);  
    1098.   
    1099.     //if(GLEE_ARB_window_pos)  
    1100.     //{//如果支持GL_ARB_window_pos  
    1101.         //则使用glWindowPos2iARB函数,指定绘制位置  
    1102.           
    1103.     //  printf("支持GL_ARB_window_pos\n");  
    1104.     //  printf("使用glWindowPos函数\n");  
    1105.     //  glWindowPos2iARB(100,100);  
    1106. //  }else{  
    1107.         GLint viewport[4];  
    1108.         GLdouble modelview[16],projection[16];  
    1109.         GLdouble x, y, z;  
    1110.           
    1111.         printf("不支持GL_ARB_window_pos\n");  
    1112.         printf("使用glRasterPos函数\n");  
    1113.           
    1114.         glGetIntegerv(GL_VIEWPORT,viewport);  
    1115.         glGetDoublev(GL_MODELVIEW_MATRIX,modelview);  
    1116.         glGetDoublev(GL_PROJECTION_MATRIX,projection);  
    1117.         gluUnProject(100,100,0.5,modelview,projection,viewport,&x,&y,&z);  
    1118.         glRasterPos3d(x,y,z);  
    1119. //  }  
    1120.       
    1121.     {//绘制一个5*5的像素块  
    1122.         GLubyte pixels[5][4][4];  
    1123.         //把像素中的所有像素都设置为红色  
    1124.         int i,j;  
    1125.         for(i=0;i<5;++i)  
    1126.             for(j=0;j<5;++j)  
    1127.             {  
    1128.                 pixels[i][j][0] = 255;  
    1129.                 pixels[i][j][1] = 0;  
    1130.                 pixels[i][j][2] = 0;  
    1131.                 pixels[i][j][3] = 255;  
    1132.             }  
    1133.             glDrawPixels(5,5,GL_RGBA,GL_UNSIGNED_BYTE,pixels);  
    1134.     }  
    1135.     glutSwapBuffers();  
    1136. }  
    1137.   
    1138. int main(int argc,char* argv[])  
    1139. {  
    1140.     glutInit(&argc,argv);  
    1141.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  
    1142.     glutInitWindowPosition(100,100);  
    1143.     glutInitWindowSize(512,512);  
    1144.     glutCreateWindow("OpenGL");  
    1145.     glutDisplayFunc(&display);  
    1146.     glutMainLoop();  
    1147.     return 0;  
    1148. }</strong></span></pre>  
    1149. <p></p>  
    1150. <p><span style="font-size:18px"><strong>36 显示字体 用的显示列表</strong></span></p>  
    1151. <p><span style="font-size:18px"><strong></strong></span></p>  
    1152. <pre name="code" class="cpp"><strong><span style="font-size:18px;">#include<GL/glut.h>  
    1153. #include<windows.h>  
    1154.   
    1155. //ASCII字符总共只有0到127,一共128种字符  
    1156. #define MAX_CHAR 128  
    1157.   
    1158.   
    1159. void drawString(const char* str)  
    1160. {  
    1161.     static int isFirstCall = 1;  
    1162.     static GLuint lists;  
    1163.   
    1164.     if(isFirstCall){//如果是第一次调用,执行初始化  
    1165.                     //为每一个ASCII字符产生一个显示列表  
    1166.         isFirstCall = 0;  
    1167.           
    1168.         //申请MAX_CHAR个连续的显示列表编号  
    1169.         lists = glGenLists(MAX_CHAR);  
    1170.   
    1171.         //把每个字符的绘制命令都装到对应的显示列表中  
    1172.         wglUseFontBitmaps(wglGetCurrentDC(),0,MAX_CHAR,lists);  
    1173.   
    1174.         //调用每个字符对应的显示列表,绘制每个字符  
    1175.         for(;*str!=‘\0‘;++str)  
    1176.             glCallList(lists + *str);  
    1177.     }  
    1178. }  
    1179.   
    1180. void display()  
    1181. {  
    1182.     glClear(GL_COLOR_BUFFER_BIT);  
    1183.     glColor3f(1.0f,0.0f,0.0f);  
    1184.     glRasterPos2f(0.0f,0.0f);  
    1185.     drawString("hello,world");  
    1186.   
    1187.     glutSwapBuffers();  
    1188. }  
    1189.   
    1190.   
    1191. int main(int argc,char* argv[])  
    1192. {  
    1193.     glutInit(&argc,argv);  
    1194.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  
    1195.     glutInitWindowPosition(100,100);  
    1196.     glutInitWindowSize(512,512);  
    1197.     glutCreateWindow("OpenGL");  
    1198.     glutDisplayFunc(&display);  
    1199.     glutMainLoop();  
    1200.     return 0;  
    1201. }</span></strong></pre><img src="http://img.my.csdn.net/uploads/201210/27/1351345370_9989.png" alt=""><br>  
    1202. <p></p>  
    1203. <p><span style="font-size:18px"><strong>37 字体设置</strong></span></p>  
    1204. <p><span style="font-size:18px"><strong></strong></span></p>  
    1205. <pre name="code" class="cpp"><strong><span style="font-size:18px;">#include<GL/glut.h>  
    1206. #include<windows.h>  
    1207.   
    1208. //ASCII字符总共只有0到127,一共128种字符  
    1209. #define MAX_CHAR 128  
    1210.   
    1211. void drawString(const char* str)  
    1212. {  
    1213.     static int isFirstCall = 1;  
    1214.     static GLuint lists;  
    1215.   
    1216.     if(isFirstCall){//如果是第一次调用,执行初始化  
    1217.                     //为每一个ASCII字符产生一个显示列表  
    1218.         isFirstCall = 0;  
    1219.           
    1220.         //申请MAX_CHAR个连续的显示列表编号  
    1221.         lists = glGenLists(MAX_CHAR);  
    1222.   
    1223.         //把每个字符的绘制命令都装到对应的显示列表中  
    1224.         wglUseFontBitmaps(wglGetCurrentDC(),0,MAX_CHAR,lists);  
    1225.   
    1226.         //调用每个字符对应的显示列表,绘制每个字符  
    1227.         for(;*str!=‘\0‘;++str)  
    1228.             glCallList(lists + *str);  
    1229.     }  
    1230. }  
    1231.   
    1232. void selectFont(int size,int charset,const char* face){  
    1233.     HFONT hFont=CreateFontA(size,0,0,0,FW_MEDIUM,0,0,0,  
    1234.         charset,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,  
    1235.         DEFAULT_QUALITY,DEFAULT_PITCH | FF_SWISS,face);  
    1236.     HFONT hOldFont = (HFONT)SelectObject(wglGetCurrentDC(),hFont);  
    1237.     DeleteObject(hOldFont);  
    1238. }  
    1239.   
    1240. void display()  
    1241. {  
    1242.     selectFont(48,ANSI_CHARSET,"Comic Sans MS");  
    1243.     glClear(GL_COLOR_BUFFER_BIT);  
    1244.     glColor3f(1.0f,0.0f,0.0f);  
    1245.     glRasterPos2f(0.0f,0.0f);  
    1246.     drawString("Hello,World!");  
    1247.   
    1248.     glutSwapBuffers();  
    1249. }  
    1250.   
    1251.   
    1252. int main(int argc,char* argv[])  
    1253. {  
    1254.     glutInit(&argc,argv);  
    1255.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  
    1256.     glutInitWindowPosition(100,100);  
    1257.     glutInitWindowSize(512,512);  
    1258.     glutCreateWindow("OpenGL");  
    1259.     glutDisplayFunc(&display);  
    1260.     glutMainLoop();  
    1261.     return 0;  
    1262. }</span></strong></pre><img src="http://img.my.csdn.net/uploads/201210/27/1351345437_6278.png" alt=""><br>  
    1263. <p></p>  
    1264. <p><span style="font-size:18px"><strong>38 显示汉字</strong></span></p>  
    1265. <p><span style="font-size:18px"><strong></strong></span></p>  
    1266. <pre name="code" class="cpp"><strong><span style="font-size:18px;">#include<GL/glut.h>  
    1267. #include<windows.h>  
    1268.   
    1269. //ASCII字符总共只有0到127,一共128种字符  
    1270. #define MAX_CHAR 128  
    1271.   
    1272. void drawCNString(const char* str)  
    1273. {  
    1274.     int len,i;  
    1275.     wchar_t* wstring;  
    1276.     HDC hDC=wglGetCurrentDC();  
    1277.     GLuint list = glGenLists(1);  
    1278.   
    1279.     //计算字符个数  
    1280.     //如果是双字节字符的(比如中文字符),两个字节才算一个字符  
    1281.     //否则一个字节算一个字符  
    1282.     len=0;  
    1283.     for(i=0;str[i]!=‘\0‘;++i)  
    1284.     {  
    1285.         if(IsDBCSLeadByte(str[i]))  
    1286.             ++i;  
    1287.         ++len;  
    1288.     }  
    1289.   
    1290.     //将混合字符转化为宽字符  
    1291.     wstring = (wchar_t*)malloc((len+1)*sizeof(wchar_t));  
    1292.     MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,str,-1,wstring,len);  
    1293.     wstring[len] = ‘\0‘;  
    1294.   
    1295.     //逐个输出字符  
    1296.     for(i=0;i<len;++i)  
    1297.     {  
    1298.         wglUseFontBitmapsW(hDC,wstring[i],1,list);  
    1299.         glCallList(list);  
    1300.     }  
    1301.   
    1302.     //回收所有临时资源  
    1303.     free(wstring);  
    1304.     glDeleteLists(list,1);  
    1305. }  
    1306.   
    1307. void selectFont(int size,int charset,const char* face){  
    1308.     HFONT hFont=CreateFontA(size,0,0,0,FW_MEDIUM,0,0,0,  
    1309.         charset,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,  
    1310.         DEFAULT_QUALITY,DEFAULT_PITCH | FF_SWISS,face);  
    1311.     HFONT hOldFont = (HFONT)SelectObject(wglGetCurrentDC(),hFont);  
    1312.     DeleteObject(hOldFont);  
    1313. }  
    1314.   
    1315. void display()  
    1316. {  
    1317.   
    1318.     glClear(GL_COLOR_BUFFER_BIT);  
    1319.   
    1320.     selectFont(48,ANSI_CHARSET,"Comic Sans MS");  
    1321.     glColor3f(1.0f,0.0f,0.0f);  
    1322.     glRasterPos2f(-0.7f,0.4f);  
    1323.     drawCNString("Hello,World!");  
    1324.       
    1325.     selectFont(48,GB2312_CHARSET,"楷体_GB2312");  
    1326.     glColor3f(1.0f,1.0f,0.0f);  
    1327.     glRasterPos2f(-0.7f,-0.1f);  
    1328.     drawCNString("当代中国汉字");  
    1329.       
    1330.     selectFont(48,GB2312_CHARSET,"华文仿宋");  
    1331.     glColor3f(0.0f,1.0f,0.0f);  
    1332.     glRasterPos2f(-0.7f,-0.6f);  
    1333.     drawCNString("傳統中國漢字");  
    1334.       
    1335.     glutSwapBuffers();  
    1336. }  
    1337.   
    1338.   
    1339. int main(int argc,char* argv[])  
    1340. {  
    1341.     glutInit(&argc,argv);  
    1342.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  
    1343.     glutInitWindowPosition(100,100);  
    1344.     glutInitWindowSize(512,512);  
    1345.     glutCreateWindow("OpenGL");  
    1346.     glutDisplayFunc(&display);  
    1347.     glutMainLoop();  
    1348.     return 0;  
    1349. }</span></strong></pre><img src="http://img.my.csdn.net/uploads/201210/27/1351345485_1205.png" alt=""><br>  
    1350. <p></p>  
    1351. <p><span style="font-size:18px; color:#ff0000"><strong>OVER!</strong></span></p>  
    1352. <p><br>  
    1353. </p>  

7天学习opengl入门

标签:

原文地址:http://www.cnblogs.com/xuejinhui/p/4353032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!