ITPub博客

首页 > 应用开发 > IT综合 > openGL学习

openGL学习

原创 IT综合 作者:liiinuuux 时间:2016-09-26 16:01:56 0 删除 编辑
由小到大绘制多个点,排成螺旋

点击(此处)折叠或打开

  1. void RenderScene(void)
  2. {
  3.     GLfloat x, y, z, angle;
  4.     z = -50.0f;
  5.     GLfloat zStep = 0.5f;
  6.     GLfloat sizes[2];
  7.     GLfloat sizeStep;
  8.     GLfloat curSize;
  9.    
  10.     glGetFloatv(GL_POINT_SIZE_RANGE, sizes);
  11.     glGetFloatv(GL_POINT_SIZE_GRANULARITY, &sizeStep);

  12.     curSize = sizes[0] + sizeStep;
  13.    
  14.     glClear(GL_COLOR_BUFFER_BIT);
  15.     glPushMatrix();
  16.     glRotatef(40.0f, 10.0f, -10.0f, 0.0f);
  17.    
  18.    

  19.     for (angle = 0.0f; angle <= (2.0f * GL_PI) * 3.0f; angle += 0.1f)
  20.     {
  21.         x = 50.0f * sin(angle);
  22.         y = 50.0f * cos(angle);
  23.         z += zStep;
  24.         glPointSize(curSize);
  25.         glBegin(GL_POINTS);
  26.         glVertex3f(x, y, z);
  27.         glEnd();
  28.         if (curSize < 10.0f) curSize += sizeStep;
  29.        
  30.     }
  31.    
  32.    
  33.     glPopMatrix();
  34.     glutSwapBuffers();
  35. }

  36. void ChangeSize(GLsizei w, GLsizei h)
  37. {
  38.     GLfloat nRange = 100.0f;
  39.     if (h == 0) h = 1;
  40.     glViewport(0, 0, w, h);
  41.     glMatrixMode(GL_PROJECTION);
  42.     glLoadIdentity();
  43.    
  44.    
  45.     if (w <= h)
  46.     {
  47.         glOrtho(-nRange, nRange, -nRange * h / w, nRange * h / w, -nRange, nRange);
  48.     }
  49.     else
  50.     {
  51.         glOrtho(-nRange * w / h, nRange * w / h, -nRange, nRange, -nRange, nRange);
  52.     }
  53.    
  54.     glMatrixMode(GL_MODELVIEW);
  55.     glLoadIdentity();
  56. }



绘制直线、折线

点击(此处)折叠或打开

  1. void RenderScene(void)
  2. {
  3.     glClear(GL_COLOR_BUFFER_BIT);
  4.     glPushMatrix();
  5.     glRotatef(40.0f, 10.0f, -10.0f, 0.0f);
  6.    
  7.     GLfloat x, y, z, angle;

  8.     glBegin(GL_LINE_STRIP); //如果用GL_LINE_LOOP,会自动加一条线,形成环,如果用GL_LINES,则只是画一条线
  9.     glVertex3f(x, y, z);
  10.     glVertex3f(x + 30, y + 30, z + 30);
  11.     glVertex3f(x + 30, y + 60, z + 30);
  12.     glVertex3f(x + 20, y + 40, z + 80);
  13.     glEnd();

  14.     glPopMatrix();
  15.     glutSwapBuffers();
  16. }

用折线画螺旋,并且用上下左右控制旋转

点击(此处)折叠或打开

  1. /*
  2. * File: main.c
  3. * Author: root
  4. *
  5. * Created on August 30, 2013, 12:57 PM
  6. */

  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include <GL/glut.h>
  11. #include <GL/gl.h>
  12. #include <math.h>


  13. #define GL_PI 3.1415f
  14. #define KEY_UP 101
  15. #define KEY_DOWN 103
  16. #define KEY_LEFT 102
  17. #define KEY_RIGHT 100

  18. GLfloat rotateX = 0.0f;
  19. GLfloat rotateY = 0.0f;

  20. void RenderScene(void)
  21. {
  22.     glClear(GL_COLOR_BUFFER_BIT);
  23.     glPushMatrix();
  24.     glRotatef(rotateX, 1.0f, 0.0f, 0.0f);
  25.     glRotatef(rotateY, 0.0f, 1.0f, 0.0f);
  26.    
  27.    
  28.     GLfloat x, y, z, angle;
  29.     z = -50.0f;
  30.     glBegin(GL_LINE_STRIP);
  31.     for(angle = 0.0f; angle < (2.0f * GL_PI) * 4; angle += 0.1f)
  32.     {
  33.         x = 50.0f * sin(angle);
  34.         y = 50.0f * cos(angle);
  35.         z += 0.5f;
  36.        
  37.         glVertex3f(x, y, z);
  38.         
  39.     }
  40.     glEnd();
  41.    
  42.    
  43.    
  44.     glPopMatrix();
  45.     glutSwapBuffers();
  46. }

  47. void ChangeSize(GLsizei w, GLsizei h)
  48. {
  49.     GLfloat nRange = 100.0f;
  50.     if (h == 0) h = 1;
  51.     glViewport(0, 0, w, h);
  52.     glMatrixMode(GL_PROJECTION);
  53.     glLoadIdentity();
  54.    
  55.    
  56.     if (w <= h)
  57.     {
  58.         glOrtho(-nRange, nRange, -nRange * h / w, nRange * h / w, -nRange, nRange);
  59.     }
  60.     else
  61.     {
  62.         glOrtho(-nRange * w / h, nRange * w / h, -nRange, nRange, -nRange, nRange);
  63.     }
  64.    
  65.     glMatrixMode(GL_MODELVIEW);
  66.     glLoadIdentity();
  67. }

  68. void SetupRC(void)
  69. {
  70.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  71.     glColor3f(0.0f, 1.0f, 0.0f);
  72. }

  73. void myTimer(int i)
  74. {

  75. }

  76. void keyAction (int key, int x, int y)
  77. {
  78.     switch (key)
  79.     {
  80.         case KEY_UP:
  81.             rotateX += 1.5f;
  82.             break;
  83.         case KEY_DOWN:
  84.             rotateX -= 1.5f;
  85.             break;
  86.         case KEY_LEFT:
  87.             rotateY -= 1.5f;
  88.             break;
  89.         case KEY_RIGHT:
  90.             rotateY += 1.5f;
  91.             break;
  92.         default:
  93.             break;
  94.     }
  95.     RenderScene();
  96. }


  97. /*
  98. *
  99. */
  100. int main(int argc, char** argv) {
  101.     glutInit(&argc, argv);
  102.     glutInitWindowPosition(100, 100);
  103.     glutInitWindowSize(399, 400);
  104.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  105.     glutCreateWindow("asd");
  106.     SetupRC();
  107.     glutDisplayFunc(RenderScene);
  108.     glutReshapeFunc(ChangeSize);
  109.     glutSpecialFunc(keyAction);
  110.     glutMainLoop();
  111.     return (EXIT_SUCCESS);
  112. }


用三角形画圆锥
两个扇面组成封口的锥形

点击(此处)折叠或打开

  1. void RenderScene(void)
  2. {
  3.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  4.     glPushMatrix();
  5.    
  6.     glRotatef(rotateX, 10.0f, 0.0f, 0.0f);
  7.     glRotatef(rotateY, 0.0f, 10.0f, 0.0f);

  8.     char p0[3] = {0.0f, 50.0f, 0.0f};
  9.     char p1[3] = {20.0f, 0.0, 20.0f};
  10.     GLfloat angle;
  11.     glShadeModel(GL_SMOOTH);
  12.     glEnable(GL_CULL_FACE); //开启多边形剔除,不渲染多边形的背面,这样如果要显示一个正面朝外的封闭多边形,则背面超前的地方根本就不渲染。如果是创建不发呢关闭的集合形体,如纸,就不能打开这个选项。
  13.     glEnable(GL_DEPTH_TEST); // 开启深度测试,可以避免晚描绘的像素错误遮挡之前描绘的Z坐标靠前的像素
  14.    
  15.     glPolygonMode(GL_BACK, GL_FILL); // 显示填充好的多边形,如果选GL_LINE,则只显示每个多边形的轮廓。
  16.    
  17.     glBegin(GL_TRIANGLE_FAN);
  18.     glVertex3f(p0[0], p0[1], p0[2]);
  19.     for (angle = 0; angle <= 2*GL_PI + 0.1f; angle += 0.1f)
  20.     {
  21.         glColor3f(sin(angle), 0.0f, 0.5f);
  22.         glVertex3f(p1[0] * sin(angle), p1[1], p1[2] * cos(angle));
  23.     }
  24.     glEnd();

  25.     glBegin(GL_TRIANGLE_FAN);
  26.     glColor3f(1.0f, 1.0f, 1.0f);
  27.     glVertex3f(p0[0], p1[1], p0[2]);
  28.     for (angle = 0; angle <= 2*GL_PI + 0.1f; angle += 0.1f)
  29.     {
  30.         glVertex3f(p1[0] * cos(angle), p1[1], p1[2] * sin(angle));
  31.     }
  32.     glEnd();
  33.    
  34.     glPopMatrix();
  35.     glutSwapBuffers();
  36. }


用GL_QUAD_STRIP绘制连续的四边形。提供顶点的方向是折线形的
提供顶点的顺序逆时针,则为正面。

点击(此处)折叠或打开

  1. void RenderScene(void)
  2. {
  3.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  4.     glRotatef(rotateX, 1.0f, 0.0f, 0.0f);
  5.     glRotatef(rotateY, 0.0f, 1.0f, 0.0f);
  6.     glEnable(GL_CULL_FACE);
  7.     glEnable(GL_DEPTH_TEST);
  8.     glPolygonMode(GL_BACK, GL_FILL);

  9.     GLfloat r = 2.0f;
  10.    
  11.     glPushMatrix();
  12.    
  13.     glBegin(GL_QUAD_STRIP);
  14.     glColor3f(1.0, 0.0f, 0.0f);
  15.    
  16.     glVertex3f(r, -r, -r);
  17.     glVertex3f(r, -r, r);
  18.     glVertex3f(-r, -r, -r);
  19.     glVertex3f(-r, -r, r);
  20.    
  21.     glColor3f(0.0, 1.0f, 0.0f);
  22.     glVertex3f(-r, r, -r);
  23.     glVertex3f(-r, r, r);

  24.     glColor3f(0.0, 0.0f, 1.0f);
  25.     glVertex3f(r, r, -r);
  26.     glVertex3f(r, r, r);

  27.     glColor3f(0.0, 1.0f, 1.0f);
  28.     glVertex3f(r, -r, -r);
  29.     glVertex3f(r, -r, r);
  30.     glEnd();
  31.    
  32.     glBegin(GL_QUADS);
  33.     glColor3f(1.0, 1.0f, 1.0f);
  34.     glVertex3f(-r, -r, r);
  35.     glVertex3f(r, -r, r);
  36.     glVertex3f(r, r, r);
  37.     glVertex3f(-r, r, r);
  38.    
  39.     glEnd();
  40.    
  41.     glBegin(GL_QUADS);
  42.     glColor3f(1.0, 0.0f, 1.0f);
  43.     glVertex3f(r, -r, -r);
  44.     glVertex3f(-r, -r, -r);
  45.     glVertex3f(-r, r, -r);
  46.     glVertex3f(r, r, -r);
  47.    
  48.    
  49.     glEnd();
  50.    
  51.     glPopMatrix();
  52.     glutSwapBuffers();
  53. }


透视效果

点击(此处)折叠或打开

  1. void ChangeSize(GLsizei w, GLsizei h)
  2. {
  3.     if (h == 0) h = 1;
  4.     glViewport(0, 0, w, h);
  5.     glMatrixMode(GL_PROJECTION);
  6.     glLoadIdentity();
  7.    
  8.     gluPerspective(80.0f, (GLfloat)w/(GLfloat)h, 11.0, 30.0);
  9.    
  10.     glMatrixMode(GL_MODELVIEW);
  11.     glLoadIdentity();
  12.     glTranslatef(0.0f, 0.0f, -15.0f); 把坐标向x轴负方向推,使得图像装到gluPerspective的后两个参数范围里
  13. }



方块回弹

点击(此处)折叠或打开

  1. /*
  2. * File: main.c
  3. * Author: root
  4. *
  5. * Created on August 30, 2013, 12:57 PM
  6. */

  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include <GL/glut.h>
  11. #include <GL/gl.h>


  12. GLfloat rsize = 25.0f;
  13. GLfloat x = 200.0f;
  14. GLfloat y = 25.0f;

  15. GLfloat xStep = 5.0f;
  16. GLfloat yStep = 5.0f;

  17. GLfloat windowWidth;
  18. GLfloat windowHight;

  19. void RenderScene(void)
  20. {
  21.    
  22.     glClear(GL_COLOR_BUFFER_BIT);
  23.     glColor3f(1.0f, 0.0f, 0.0f);
  24.     glRectf(x , y , x + rsize, y - rsize);
  25.    
  26.    
  27.     glutSwapBuffers();
  28. }

  29. void ChangeSize(GLsizei w, GLsizei h)
  30. {
  31.     windowWidth = w;
  32.     windowHight = h;
  33.     glViewport(0, 0, windowWidth, windowHight);
  34.     glMatrixMode(GL_PROJECTION);
  35.     glLoadIdentity();
  36.     glOrtho(0, windowWidth, 0, windowHight, 20.0, -20.0);
  37.     glMatrixMode(GL_MODELVIEW);
  38.     glLoadIdentity();
  39. }

  40. void SetupRC(void)
  41. {
  42.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  43. // glColor3f(0.0f, 1.0f, 0.0f);
  44. }

  45. void myTimer(int i)
  46. {
  47.     if (x > windowWidth - rsize || x < 0) xStep *= -1;
  48.     if (y > windowHight || y < rsize) yStep *= -1;
  49.     x += xStep;
  50.     y += yStep;
  51.    
  52.     glutPostRedisplay();
  53.    
  54.     glutTimerFunc(30, myTimer, i);
  55. }

  56. /*
  57. *
  58. */
  59. int main(int argc, char** argv) {
  60.     glutInit(&argc, argv);
  61.     glutInitWindowPosition(100, 100);
  62.     glutInitWindowSize(399, 400);
  63.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  64.     glutCreateWindow("asd");
  65. // glEnable(GL_LIGHTING);
  66.     glutDisplayFunc(RenderScene);
  67.     glutReshapeFunc(ChangeSize);
  68.     glutTimerFunc(30, myTimer, 1);
  69.     SetupRC();
  70.     glutMainLoop();
  71.     return (EXIT_SUCCESS);
  72. }








来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26239116/viewspace-2125609/,如需转载,请注明出处,否则将追究法律责任。

上一篇: perl网络编程
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2012-11-12

  • 博文量
    94
  • 访问量
    309725