学习opengl入门

当然,这些只是我7天来业余时间的学习,我觉得这个网址不错,大家如果也想学习opengl,并且具有一定的C语言C++基础,入门课程推荐大家去学习这个网址OpenGL入门学习 - CrazyXiaoM - 博客园

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

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

#include<GL/GLUT.H>  
  
void myDisplay(void){  
    glClear(GL_COLOR_BUFFER_BIT);  
    glRectf(-0.5f, -0.4f, 0.5f, 0.5f);  
    glFlush();  
}  
  
int main(int argc, char *argv[])  
{  
    glutInit(&argc,argv);  
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);  
    glutInitWindowPosition(100, 100);  
    glutInitWindowSize(400, 400);  
    glutCreateWindow("openglѧϰ1");  
    glutDisplayFunc(&myDisplay);  
    glutMainLoop();  
    return 0;  
}
​​​​​​​

2.

#include <GL/glut.h>  
  
void myDisplay(){  
    glClearColor(1.0, 1.0, 1.0, 0.0);  
    glClear(GL_COLOR_BUFFER_BIT);  
    glColor3f(1.0, 0.0, 0.0);  
    glRectf(-0.5f,-0.5f,0.5f,0.5f);  
    glFlush();  
}  
  
int main(int argc, char* argv[]){  
    glutInit(&argc, argv);  
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  
    glutInitWindowPosition(400, 400);  
    glutInitWindowSize(200, 200);  
    glutCreateWindow("独立");  
  
     glutDisplayFunc(myDisplay);  
     glutMainLoop();  
     return 0;  
  
} 


3.


4.

[cpp]  view plain  copy
  1. #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. }  

 

5.

[cpp]  view plain  copy
  1. #include <GL/glut.h>
  2. void myDisplay()  
  3. {  
  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_LINE_STRIP);  
  8.         glVertex2f(0.1f, 0.8f);  
  9.         glVertex2f(-0.1f, -0.8f);  
  10.         glVertex2f(0.1f, -0.8f);  
  11.         glVertex2f(-0.1f, 0.8f);  
  12.         glVertex2f(0.1f, 0.9f);  
  13.         glVertex2f(0.4f, 0.8f);  
  14.     glEnd();  
  15.     glFlush();  
  16. }
  17. int main(int argc, char* argv[])  
  18. {  
  19.     glutInit(&argc, argv);  
  20.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  21.     glutInitWindowPosition(400,400);  
  22.     glutInitWindowSize(400,400);  
  23.     glutCreateWindow("Study04");  
  24.     glutDisplayFunc(myDisplay);  
  25.     glutMainLoop();  
  26.     return 0;  
  27. }  

 

6.1

[cpp]  view plain  copy
  1. #include <GL/glut.h>
  2. void myDisplay()  
  3. {  
  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_LINE_LOOP);  
  8.         glVertex2f(0.2f, 0.2f);  
  9.         glVertex2f(-0.2f, 0.2f);  
  10.         glVertex2f(-0.2f, -0.2f);  
  11.         glVertex2f(0.2f, -0.2f);  
  12.         //glVertex2f(0.1f, 0.9f);  
  13.         //glVertex2f(0.4f, 0.8f);  
  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. }  

7

[cpp]  view plain  copy
  1. #include <GL/glut.h>
  2. void myDisplay()  
  3. {  
  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_TRIANGLES);  
  8.         //glVertex2f(0.2f, 0.2f);  
  9.         //glVertex2f(-0.2f, 0.2f);  
  10.         //glVertex2f(-0.2f, -0.2f);  
  11.         glVertex2f(0.8f, 0.4f);  
  12.         glVertex2f(0.4f, 0.8f);  
  13.         glVertex2f(0.0f, 0.0f);  
  14.         //glVertex2f(0.1f, 0.9f);  
  15.         //glVertex2f(0.4f, 0.8f);  
  16.     glEnd();  
  17.     glFlush();  
  18. }  
  19.   
  20. int main(int argc, char* argv[])  
  21. {  
  22.     glutInit(&argc, argv);  
  23.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
  24.     glutInitWindowPosition(400,400);  
  25.     glutInitWindowSize(400,400);  
  26.     glutCreateWindow("Study04");  
  27.     glutDisplayFunc(myDisplay);  
  28.     glutMainLoop();  
  29.     return 0;  

 

7.1

[cpp]  view plain  copy
  1. #include <GL/glut.h>
  2. void myDisplay()  
  3. {  
  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_TRIANGLES);  
  8.         glVertex2f(0.2f, 0.2f);  
  9.         glVertex2f(-0.2f, 0.2f);  
  10.         glVertex2f(-0.2f, -0.2f);  
  11.     glEnd();  
  12.   
  13.     glBegin(GL_TRIANGLES);  
  14.         glVertex2f(0.8f, 0.4f);  
  15.         glVertex2f(0.4f, 0.8f);  
  16.         glVertex2f(0.0f, 0.0f);  
  17.         //glVertex2f(0.1f, 0.9f);  
  18.         //glVertex2f(0.4f, 0.8f);  
  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;  

7.2

[cpp]  view plain  copy
  1. #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;  

 

7.3

[cpp]  view plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  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 plain  copy
  1. <span style="font-size:18px;"><strong>//glEnable(GL_CULL_FACE);  
  2.     glCullFace(GL_FRONT);</strong></span>  

[cpp]  view plain  copy
  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 plain  copy
  1. <span style="font-size:18px;"><strong>//glEnable(GL_CULL_FACE);  
  2.     glCullFace(GL_FRONT);</strong></span>  

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

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

[cpp]  view plain  copy
  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 plain  copy
  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 plain  copy
  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

#include<GL/glut.h>  
#include<math.h>  
#include<windows.h>  
  
#define WIDTH 400  
#define HEIGHT 400  
  
#define ColoredVertex(c,v) do{glColor3fv(c);glVertex3fv(v);}while(0)  
  
GLfloat angle=0.0f;  
  
void myDisplay()  
{  
    static int list = 0;  
    if(list == 0)  
    {  
        GLfloat  
            PointA[] = {0.5f,-sqrt(6.0f)/12,-sqrt(3.0f)/6},  
            PointB[] = {-0.5f,-sqrt(6.0f)/12,-sqrt(3.0f)/6},  
            PointC[] = {0.0f,-sqrt(6.0f)/12,sqrt(3.0f)/3},  
            PointD[] = {0.0f,sqrt(6.0f)/4,0};  
        GLfloat  
            ColorR[] = {1,0,0},  
            ColorG[] = {0,1,0},  
            ColorB[] = {0,0,1},  
            ColorY[] = {1,1,0};  
  
        list = glGenLists(1);  
        glNewList(list,GL_COMPILE);  
        glBegin(GL_TRIANGLES);  
            ColoredVertex(ColorR,PointA);  
            ColoredVertex(ColorG,PointB);  
            ColoredVertex(ColorB,PointC);  
  
            ColoredVertex(ColorR,PointA);  
            ColoredVertex(ColorB,PointC);  
            ColoredVertex(ColorY,PointD);  
  
            ColoredVertex(ColorB,PointC);  
            ColoredVertex(ColorG,PointB);  
            ColoredVertex(ColorY,PointD);  
  
            ColoredVertex(ColorG,PointB);  
            ColoredVertex(ColorR,PointA);  
            ColoredVertex(ColorY,PointD);  
        glEnd();  
        glEndList();  
        glEnable(GL_DEPTH_TEST);  
    }  
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
    glPushMatrix();  
    glRotatef(angle,1,0.5,0);  
    glCallList(list);  
    glPopMatrix();  
    glutSwapBuffers();  
}  
  
void myIdle()  
{  
    ++angle;  
    if(angle >= 360.0f)  
        angle = 0.0f;  
    Sleep(1000/10);  
    myDisplay();  
}  
  
int main(int argc, char* argv[])  
{  
    glutInit(&argc,argv);  
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  
    glutInitWindowPosition(200,200);  
    glutInitWindowSize(400,400);  
    glutCreateWindow("study");  
    glutDisplayFunc(&myDisplay);  
    glutIdleFunc(&myIdle);  
    glutMainLoop();  
    return 0;  
}
​​​​​​​


10.18

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

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

#include<GL/glut.h>  
  
void myDisplay()  
{  
    glClear(GL_COLOR_BUFFER_BIT);  
    glEnable(GL_BLEND);  
    glBlendFunc(GL_ONE,GL_ZERO);  
  
    glColor4f(1,0,0,0.5);  
    glRectf(-1,-1,0.5,0.5);  
    glColor4f(0,1,0,0.5);  
    glRectf(-0.5,-0.5,1,1);  
  
    glutSwapBuffers();  
}  
  
void myIdle()  
{  
    myDisplay();  
}  
  
int main(int argc, char* argv[])  
{  
    glutInit(&argc,argv);  
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  
    glutInitWindowPosition(200,200);  
    glutInitWindowSize(400,400);  
    glutCreateWindow("study");  
  
    myDisplay();  
    glutDisplayFunc(&myDisplay);  
    glutIdleFunc(&myIdle);  
    glutMainLoop();  
    return 0;  
  
}


27.1两种颜色混合

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


#include<GL/glut.h>  
  
void myDisplay()  
{  
    glClear(GL_COLOR_BUFFER_BIT);  
    glEnable(GL_BLEND);  
    glBlendFunc(GL_ONE,GL_ONE);  //改动  
  
    glColor4f(1,0,0,0.5);  
    glRectf(-1,-1,0.5,0.5);  
    glColor4f(0,1,0,0.5);  
    glRectf(-0.5,-0.5,1,1);  
  
    glutSwapBuffers();  
}  
  
void myIdle()  
{  
    myDisplay();  
}  
  
int main(int argc, char* argv[])  
{  
    glutInit(&argc,argv);  
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  
    glutInitWindowPosition(200,200);  
    glutInitWindowSize(400,400);  
    glutCreateWindow("study");  
  
    myDisplay();  
    glutDisplayFunc(&myDisplay);  
    glutIdleFunc(&myIdle);  
    glutMainLoop();  
    return 0;  
  
}

27.2

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

>#include<GL/glut.h>  
  
void myDisplay()  
{  
    glClear(GL_COLOR_BUFFER_BIT);  
    glEnable(GL_BLEND);  
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);  //改动  
  
    glColor4f(1,0,0,0.5);  
    glRectf(-1,-1,0.5,0.5);  
    glColor4f(0,1,0,0.5);  
    glRectf(-0.5,-0.5,1,1);  
  
    glutSwapBuffers();  
}  
  
void myIdle()  
{  
    myDisplay();  
}  
  
int main(int argc, char* argv[])  
{  
    glutInit(&argc,argv);  
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  
    glutInitWindowPosition(200,200);  
    glutInitWindowSize(400,400);  
    glutCreateWindow("study");  
  
    myDisplay();  
    glutDisplayFunc(&myDisplay);  
    glutIdleFunc(&myIdle);  
    glutMainLoop();  
    return 0;  
  
}  

 

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

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

<span style="font-size:18px;"><strong>#include<GL/glut.h>  
  
//在1,1,-1处设置白色的光源  
void setLight()  
{  
    static const GLfloat light_position[] = {1.0f,1.0f,-1.0f,1.0f};  
    static const GLfloat light_ambient[] = {0.2f,0.2f,0.2f,1.0f};  
    static const GLfloat light_diffuse[] = {1.0f,1.0f,1.0f,1.0f};  
    static const GLfloat light_specular[] = {1.0f,1.0f,1.0f,1.0f};  
      
    glLightfv(GL_LIGHT0,GL_POSITION, light_position);  
    glLightfv(GL_LIGHT0,GL_AMBIENT, light_ambient);  
    glLightfv(GL_LIGHT0,GL_DIFFUSE, light_diffuse);  
    glLightfv(GL_LIGHT0,GL_SPECULAR, light_specular);  
      
    glEnable(GL_LIGHT0);  
    glEnable(GL_LIGHTING);  
    glEnable(GL_DEPTH_TEST);  
}  
  
//设置材质  
void setMatirial(const GLfloat mat_diffuse[4], GLfloat mat_shininess)  
{  
    static const GLfloat mat_specular[] = {0.0f,0.0f,0.0f,1.0f};  
    static const GLfloat mat_emission[] = {0.0f,0.0f,0.0f,1.0f};  
      
    glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mat_diffuse);  
    glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);  
    glMaterialfv(GL_FRONT,GL_EMISSION,mat_emission);  
    glMaterialf(GL_FRONT,GL_SHININESS,mat_shininess);  
}  
  
  
void myDisplay()  
{  
    //定义一些材质颜色  
    const static GLfloat red_color[] = {1.0f,0.0f,0.0f,1.0f};  
    const static GLfloat green_color[] = {0.0f,1.0f,0.0f,0.3333f};  
    const static GLfloat blue_color[] = {0.0f,0.0f,1.0f,0.5f};  
      
    //清除屏幕  
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
      
      
      
    //设置光源  
    setLight();  
  
    //启动混合并设置混合因子  
    glEnable(GL_BLEND);  
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);  
      
    //以(0,0,0.5)为中心,绘制一个半径为0.3的不透明红色球体(离观察者最远)  
    setMatirial(red_color, 30.0);  
    glPushMatrix();  
    glTranslatef(0.0f,0.0f,0.5f);  
    glutSolidSphere(0.3,30,30);   
    glPopMatrix();  
      
    //绘制半透明物体  
    glDepthMask(GL_FALSE);  
      
    //以(0.2,0,-0.5)为中心,绘制一个半径为0.2的半透明蓝色球体(离观察者最近)  
    setMatirial(blue_color, 30.0);  
    glPushMatrix();  
    glTranslatef(0.2f,0.0f,-0.5f);  
    glutSolidSphere(0.2,30,30);  
    glPopMatrix();    
  
  
  
  
    //以(0.1,0,0)为中心,绘制一个半径为0.15的半透明绿色球体(在两球体之间)  
    setMatirial(green_color, 30.0);  
    glPushMatrix();  
    glTranslatef(0.1,0,0);  
    glutSolidSphere(0.15,30,30);  
    glPopMatrix();    
      
      
    //深度缓冲区恢复为可读可写模式  
    glDepthMask(GL_TRUE);  
      
    glutSwapBuffers();  
      
}  
  
void myIdle()  
{  
    myDisplay();  
}  
  
int main(int argc, char* argv[])  
{  
    glutInit(&argc,argv);  
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  
    glutInitWindowPosition(200,200);  
    glutInitWindowSize(400,400);  
    glutCreateWindow("study");  
      
    myDisplay();  
    glutDisplayFunc(&myDisplay);  
    //glutIdleFunc(&myIdle);  
    glutMainLoop();  
    return 0;  
      
}</strong></span>  

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值