openGL學習

liiinuuux發表於2016-09-26
由小到大繪製多個點,排成螺旋

點選(此處)摺疊或開啟

  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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章