7天学习opengl入门

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. void myDisplay(void){
  3. glClear(GL_COLOR_BUFFER_BIT);
  4. glRectf(-0.5f, -0.4f, 0.5f, 0.5f);
  5. glFlush();
  6. }
  7. int main(int argc, char *argv[])
  8. {
  9. glutInit(&argc,argv);
  10. glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
  11. glutInitWindowPosition(100, 100);
  12. glutInitWindowSize(400, 400);
  13. glutCreateWindow("opengl??1");
  14. glutDisplayFunc(&myDisplay);
  15. glutMainLoop();
  16. return 0;
  17. }
  18. </strong></span>

2.

[cpp] view plaincopy

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


3.

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>
  2. void myDisplay(){
  3. glClearColor(1.0,1.0,1.0,0.0);
  4. glClear(GL_COLOR_BUFFER_BIT);
  5. glBegin(GL_POINTS);
  6. glVertex2i(100, 100);
  7. glVertex2i(50, 50);
  8. glVertex2i(70, 70);
  9. glVertex2f(0.3f, 0.3f);
  10. glVertex2f(-0.3f, -0.3f);
  11. glVertex2f(0.5f, 0.5f);
  12. glVertex2d(0.2,0.5);
  13. glVertex2d(-0.4, -0.6);
  14. glVertex2d(0.3,0.6);
  15. glEnd();
  16. glFlush();
  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("study02");
  25. glutDisplayFunc(&myDisplay);
  26. glutMainLoop();
  27. return 0;
  28. }
  29. </strong></span>


4.

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#include<GL/glut.h>
  2. void myDisplay(){
  3. glClearColor(1.0,1.0,1.0,0.0);
  4. glClear(GL_COLOR_BUFFER_BIT);
  5. glColor3f(1.0,0.0,0.0);
  6. glBegin(GL_LINES);
  7. //glVertex2i(100, 100);
  8. //glVertex2i(50, 50);
  9. //glVertex2i(70, 70);
  10. glVertex2f(0.3f, 0.3f);
  11. glVertex2f(-0.3f, -0.3f);
  12. glVertex2f(0.5f, 0.5f);
  13. glVertex2d(0.2,0.5);
  14. glVertex2d(-0.4, -0.6);
  15. glVertex2d(0.3,0.6);
  16. glEnd();
  17. glFlush();
  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("study03");
  26. glutDisplayFunc(&myDisplay);
  27. glutMainLoop();
  28. return 0;
  29. }
  30. </strong></span>


5.

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#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. }</strong></span>


6.1

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#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. 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. }</strong></span>

7

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#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. 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.1

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#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. glBegin(GL_TRIANGLES);
  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. 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.2

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#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. 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.3

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#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. glVertex2f(0.9f,0.3f);
  17. glEnd();
  18. glFlush();
  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;
  30. }</strong></span>


8.

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#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_TRIANGLE_STRIP);
  8. glVertex2f(0.0f,0.0f);
  9. glVertex2f(0.4f,0.8f);
  10. glVertex2f(0.8f,0.4f);
  11. glVertex2f(0.8f,0.5f);
  12. glVertex2f(-0.1f,0.9f);
  13. glEnd();
  14. glFlush();
  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("Study04");
  23. glutDisplayFunc(myDisplay);
  24. glutMainLoop();
  25. return 0;
  26. }</strong></span>


9.

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#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_TRIANGLE_FAN);
  8. glVertex2f(0.0f,0.0f);
  9. glVertex2f(0.4f,0.8f);
  10. glVertex2f(0.8f,0.4f);
  11. glVertex2f(0.8f,-0.5f);
  12. glVertex2f(0.1f,-0.9f);
  13. glEnd();
  14. glFlush();
  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("Study04");
  23. glutDisplayFunc(myDisplay);
  24. glutMainLoop();
  25. return 0;
  26. }</strong></span>


10.

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#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_POLYGON);
  8. glVertex2f(0.0f,0.0f);
  9. glVertex2f(0.8f,0.4f);
  10. glVertex2f(0.4f,0.8f);
  11. glVertex2f(0.8f,-0.5f);
  12. glVertex2f(0.1f,-0.9f);
  13. glEnd();
  14. glFlush();
  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("Study04");
  23. glutDisplayFunc(myDisplay);
  24. glutMainLoop();
  25. return 0;
  26. }</strong></span>


11.

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#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_QUADS);
  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.3f, 0.3f);
  13. glVertex2f(0.3f, 0.7f);
  14. glVertex2f(0.7f,1.0f);
  15. glVertex2f(0.7f,0.3f);
  16. glEnd();
  17. glFlush();
  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>


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. void myDisplay()
  7. {
  8. glClearColor(1.0,1.0,1.0,0.0);
  9. glClear(GL_COLOR_BUFFER_BIT);
  10. glColor3f(1.0,0.0,0.0);
  11. glBegin(GL_POLYGON);
  12. for(int i=0;i<n;++i)
  13. {
  14. glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));
  15. }
  16. glEnd();
  17. glFlush();
  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>


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. void myDisplay()
  7. {
  8. glClearColor(1.0,1.0,1.0,0.0);
  9. glClear(GL_COLOR_BUFFER_BIT);
  10. glColor3f(1.0,0.0,0.0);
  11. glBegin(GL_POLYGON);
  12. for(int i=0;i<n;++i)
  13. {
  14. glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));
  15. }
  16. glEnd();
  17. glFlush();
  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>


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. void myDisplay()
  7. {
  8. glClearColor(1.0,1.0,1.0,0.0);
  9. glClear(GL_COLOR_BUFFER_BIT);
  10. glColor3f(1.0,0.0,0.0);
  11. glBegin(GL_POLYGON);
  12. for(int i=0;i<n;++i)
  13. {
  14. glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));
  15. }
  16. glEnd();
  17. glFlush();
  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>


13

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>
  2. #include<math.h>
  3. const GLfloat R = 0.5f;
  4. const GLfloat Pi = 3.14159265358979f;
  5. void myDisplay()
  6. {
  7. GLfloat PointA[2] = {0,R};
  8. GLfloat PointB[2] = {R*cos(162*Pi/180),R*sin(162*Pi/180)};
  9. GLfloat PointC[2] = {R*cos(234*Pi/180),R*sin(234*Pi/180)};
  10. GLfloat PointD[2] = {R*cos(306*Pi/180),R*sin(306*Pi/180)};
  11. GLfloat PointE[2] = {R*cos(18*Pi/180),R*sin(18*Pi/180)};
  12. glClearColor(1.0,1.0,1.0,0.0);
  13. glClear(GL_COLOR_BUFFER_BIT);
  14. glColor3f(1.0,0.0,0.0);
  15. glBegin(GL_LINE_LOOP);
  16. glVertex2fv(PointA);
  17. glVertex2fv(PointC);
  18. glVertex2fv(PointE);
  19. glVertex2fv(PointB);
  20. glVertex2fv(PointD);
  21. glEnd();
  22. glFlush();
  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>

10.15

以下程序诸个描述

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

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>
  2. #include<math.h>
  3. const GLfloat factor = 0.1f;
  4. void myDisplay()
  5. {
  6. GLfloat x;
  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. glBegin(GL_LINES);
  11. glVertex2f(-1.0f, 0.0f);
  12. glVertex2f(1.0f, 0.0f);
  13. glVertex2f(0.0f, -1.0f);
  14. glVertex2f(0.0f, 1.0f);
  15. glEnd();
  16. glBegin(GL_LINE_STRIP);
  17. for(x=-1.0f/factor;x<1.0f/factor;x+=0.01)
  18. {
  19. glVertex2f(x*factor, sin(x)*factor);
  20. }
  21. glEnd();
  22. glFlush();
  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>


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

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>
  2. #include<math.h>
  3. const GLfloat factor = 0.1f;
  4. void myDisplay()
  5. {
  6. GLfloat x;
  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. glPointSize(5.0f);
  11. glBegin(GL_POINTS);
  12. glVertex2f(0.0f, 0.0f);
  13. glVertex2f(0.4f, 0.4f);
  14. glVertex2f(-0.2f,0.3f);
  15. glEnd();
  16. glFlush();
  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>


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

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>
  2. #include<math.h>
  3. const GLfloat factor = 0.1f;
  4. void myDisplay()
  5. {
  6. GLfloat x;
  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. glPointSize(50.0f);
  11. glBegin(GL_POINTS);
  12. glVertex2f(0.0f, 0.0f);
  13. glVertex2f(0.4f, 0.4f);
  14. glVertex2f(-0.2f,0.3f);
  15. glEnd();
  16. glFlush();
  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>


16.设置了线宽

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>
  2. #include<math.h>
  3. const GLfloat factor = 0.1f;
  4. void myDisplay()
  5. {
  6. GLfloat x;
  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. glLineWidth(5.0f);
  11. glBegin(GL_LINE_STRIP);
  12. glVertex2f(0.0f, 0.0f);
  13. glVertex2f(0.4f, 0.4f);
  14. glVertex2f(-0.2f,0.3f);
  15. glEnd();
  16. glFlush();
  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>


16.1 设置了夸张的线宽

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#include <GL/glut.h>
  2. #include<math.h>
  3. const GLfloat factor = 0.1f;
  4. void myDisplay()
  5. {
  6. GLfloat x;
  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. glLineWidth(50.0f);
  11. glBegin(GL_LINE_STRIP);
  12. glVertex2f(0.0f, 0.0f);
  13. glVertex2f(0.4f, 0.4f);
  14. glVertex2f(-0.2f,0.3f);
  15. glEnd();
  16. glFlush();
  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>


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

[cpp] view plaincopy

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


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

[cpp] view plaincopy

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


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

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#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(0.0,0.0,1.0);
  7. glLineWidth(5.0f);
  8. glBegin(GL_LINES);
  9. glVertex2f(0.0f, 0.0f);
  10. glVertex2f(0.6f,0.8f);
  11. glEnd();
  12. glFlush();
  13. }
  14. int main(int argc, char* argv[])
  15. {
  16. glutInit(&argc, argv);
  17. glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  18. glutInitWindowPosition(400,400);
  19. glutInitWindowSize(400,400);
  20. glutCreateWindow("study");
  21. glutDisplayFunc(myDisplay);
  22. glutMainLoop();
  23. return 0;
  24. }</strong></span>


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

[cpp] view plaincopy

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


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

[cpp] view plaincopy

  1. <span style="font-size:18px;"><strong>#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(0.0,0.0,1.0);
  7. glPolygonMode(GL_FRONT,GL_FILL); //设置正面为填充模式
  8. glPolygonMode(GL_BACK,GL_LINE);  //设置反面为线性模式
  9. glFrontFace(GL_CCW);
  10. glBegin(GL_POLYGON);
  11. glVertex2f(-0.5f, -0.5f);
  12. glVertex2f(0.0f,-0.5f);
  13. glVertex2f(0.0f,0.0f);
  14. glVertex2f(-0.5f,0.0f);
  15. glEnd();
  16. glColor3f(1.0,0.0,0.0);
  17. glEnable(GL_CULL_FACE);
  18. glCullFace(GL_FRONT);
  19. glBegin(GL_POLYGON);
  20. glVertex2f(-0.5f, -0.5f);
  21. glVertex2f(0.0f,-0.5f);
  22. glVertex2f(0.0f,0.0f);
  23. glVertex2f(-0.5f,0.0f);
  24. glEnd();
  25. glDisable(GL_CULL_FACE);
  26. glFlush();
  27. }
  28. int main(int argc, char* argv[])
  29. {
  30. glutInit(&argc, argv);
  31. glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  32. glutInitWindowPosition(400,400);
  33. glutInitWindowSize(400,400);
  34. glutCreateWindow("study");
  35. glutDisplayFunc(myDisplay);
  36. glutMainLoop();
  37. return 0;
  38. }</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. void myDisplay()
  5. {
  6. static GLubyte Mask[128];
  7. FILE *fp;
  8. fp = fopen("mmmm.bmp", "rb");
  9. if(!fp)
  10. exit(0);
  11. if(fseek(fp, -(int)sizeof(Mask),SEEK_END))
  12. exit(0);
  13. if(!fread(Mask,sizeof(Mask),1,fp))
  14. exit(0);
  15. fclose(fp);
  16. glClearColor(1.0,1.0,1.0,0.0);
  17. glClear(GL_COLOR_BUFFER_BIT);
  18. glColor3f(0.0,0.0,1.0);
  19. glEnable(GL_POLYGON_STIPPLE);
  20. glPolygonStipple(Mask);
  21. glRectf(-0.5f,-0.5f,0.0f,0.0f);
  22. glDisable(GL_POLYGON_STIPPLE);
  23. glRectf(0.0f,0.0f,0.5f,0.5f);
  24. glFlush();
  25. }
  26. int main(int argc, char* argv[])
  27. {
  28. glutInit(&argc, argv);
  29. glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  30. glutInitWindowPosition(400,400);
  31. glutInitWindowSize(400,400);
  32. glutCreateWindow("study");
  33. glutDisplayFunc(myDisplay);
  34. glutMainLoop();
  35. return 0;
  36. }</strong></span>


23 默认光滑模式

[cpp] view plaincopy

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


23.1 设置了清除色

[cpp] view plaincopy

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

10.16

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

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

[cpp] view plaincopy

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


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

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

[cpp] view plaincopy

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

7天学习opengl入门的相关文章

OpenGL入门学习

说起编程作图,大概还有很多人想起TC的#include <graphics.h>吧? 但是各位是否想过,那些画面绚丽的PC游戏是如何编写出来的?就靠TC那可怜的640*480分辨率.16色来做吗?显然是不行的. 本帖的目的是让大家放弃TC的老旧图形接口,让大家接触一些新事物. OpenGL作为当前主流的图形API之一,它在一些场合具有比DirectX更优越的特性. 1.与C语言紧密结合. OpenGL命令最初就是用C语言函数来进行描述的,对于学习过C语言的人来讲,OpenGL是容易理解和学习的

OpenGL入门学习(一)(转)--环境搭建

http://blog.chinaunix.net/uid-20622737-id-1912797.html 说起编程作图,大概还有很多人想起TC的#include <graphics.h>吧?但是各位是否想过,那些画面绚丽的PC游戏是如何编写出来的?就靠TC那可怜的640*480分辨率.16色来做吗?显然是不行的. 本帖的目的是让大家放弃TC的老旧图形接口,让大家接触一些新事物. OpenGL作为当前主流的图形API之一,它在一些场合具有比DirectX更优越的特性.1.与C语言紧密结合.O

OpenGL入门学习1

OpenGL官方网站(英文) http://www.opengl.org 下面将对Windows下的OpenGL编程进行简单介绍. 学习OpenGL前的准备工作 第一步,选择一个编译环境 现在Windows系统的主流编译环境有Visual Studio,Broland C++ Builder,Dev-C++等,它们都是支持OpenGL的.但这里我们选择Visual Studio 2005作为学习OpenGL的环境. 第二步,安装GLUT工具包 GLUT不是OpenGL所必须的,但它会给我们的学习

转:openGL入门(1)

                    OpenGL入门教程 第一课: 说起编程作图,大概还有很多人想起TC的#include <graphics.h>吧? 但是各位是否想过,那些画面绚丽的PC游戏是如何编写出来的?就靠TC那可怜的640*480分辨率.16色来做吗?显然是不行的. 本帖的目的是让大家放弃TC的老旧图形接口,让大家接触一些新事物. OpenGL作为当前主流的图形API之一,它在一些场合具有比DirectX更优越的特性. 1.与C语言紧密结合. OpenGL命令最初就是用C语言函

OpenGl入门——视口及物体移动函数

大学的时候有个选修课,要用OpenGl,很初级的内容,同样入门的学弟学妹适用 推荐个学习的资料NeHe的OpenGl教程,很完整而且有示例,讲的很明白.比某些破书好. 可以配合那本所谓的“红宝书”看看,中国人写的书就不要看了,我借过好几本内容都有些错误. 入门足够了 英文:http://nehe.gamedev.net/ 中文:http://www.yakergong.net/nehe/ 开始正题: glTranslatef(x,y,z);平移物体 glRotatef(角度,x,y,z);围绕制

OPENGL入门教程

OpenGL入门学习1:编写第一个OpenGL程序http://www.c3dn.net/forum.php?mo ... d=20&extra=page%3D3OpenGL入门学习2:点.直线和多边形http://www.c3dn.net/forum.php?mo ... id%3D5%26typeid%3D5OpenGL入门学习3:绘制几何图形的细节http://www.c3dn.net/forum.php?mo ... id%3D5%26typeid%3D5OpenGL入门学习4:颜色的选

Vue学习笔记入门篇——组件的使用

本文为转载,原文:Vue学习笔记入门篇--组件的使用 组件定义 组件 (Component) 是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能.在有些情况下,组件也可以是原生 HTML 元素的形式,以 is 特性扩展. 组件使用 注册 注册一个全局组件,你可以使用 Vue.component(tagName, options).组件在注册之后,便可以在父实例的模块中以自定义元素 的形式使用.

Vue学习笔记入门篇——组件的内容分发(slot)

本文为转载,原文:Vue学习笔记入门篇--组件的内容分发(slot) 介绍 为了让组件可以组合,我们需要一种方式来混合父组件的内容与子组件自己的模板.这个过程被称为 内容分发 (或 "transclusion" 如果你熟悉 Angular).Vue.js 实现了一个内容分发 API,使用特殊的 'slot' 元素作为原始内容的插槽. 编译作用域 在深入内容分发 API 之前,我们先明确内容在哪个作用域里编译.假定模板为: <child-component> {{ messa

cocos2D-X源码讲解之从cocos2D-X学习OpenGL(1)----cocos2D-X渲染结构

 个人原创,欢迎转载,转载请注明原文地址http://blog.csdn.net/bill_man 从本篇文章开始,将分析cocos2D-X 3.0源代码,第一部分是从cocos2D-X学习OpenGL,也就是分析cocos2D-X 3.0的渲染代码,本篇首先介绍cocos2D-X 3.0的渲染结构,使用的是3.0正式版. void DisplayLinkDirector::mainLoop() { if (_purgeDirectorInNextLoop) { //只有一种情况会调用到这里来,