Advertisement
Guest User

C++testapllicationOpenGL

a guest
Aug 19th, 2011
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.91 KB | None | 0 0
  1. #define GL_GLEXT_PROTOTYPES
  2.  
  3.  
  4.  
  5. #include <GL/freeglut.h>
  6.  
  7. #include <GL/gl.h>
  8.  
  9. #include <GL/glext.h>
  10.  
  11.  
  12.  
  13. #include <iostream>
  14.  
  15. #include <vector>
  16.  
  17. #include <algorithm>
  18.  
  19.  
  20.  
  21. // GLUT CALLBACK functions
  22.  
  23. void displayCB();
  24.  
  25. void reshapeCB(int w, int h);
  26.  
  27. void keyboardCB(unsigned char key, int x, int y);
  28.  
  29.  
  30.  
  31. void initGL();
  32.  
  33. void initGLUT(int argc, char **argv);
  34.  
  35. void createVBO();
  36.  
  37. void createIBO();
  38.  
  39. void cleanUp();
  40.  
  41.  
  42.  
  43. // global variables
  44.  
  45. GLuint vboId[2];                  // ID of VBO for vertex arrays (to store vertex coords and normals)
  46.  
  47. GLuint iboId;
  48.  
  49.  
  50.  
  51. std::vector<GLfloat> vertexPositions;
  52.  
  53. std::vector<GLushort> indices;
  54.  
  55.  
  56.  
  57. const unsigned int verticesSize = 4 * 3;
  58.  
  59. const unsigned int indicesSize = 6;
  60.  
  61.  
  62.  
  63. ///////////////////////////////////////////////////////////////////////////////
  64.  
  65. int main(int argc, char **argv)
  66.  
  67. {
  68.  
  69.  
  70.  
  71.     // init GLUT and GL
  72.  
  73.     initGLUT(argc, argv);
  74.  
  75.     initGL();
  76.  
  77.  
  78.  
  79.     glutMainLoop(); /* Start GLUT event-processing loop */
  80.  
  81.  
  82.  
  83.     return 0;
  84.  
  85. }
  86.  
  87.  
  88.  
  89. ///////////////////////////////////////////////////////////////////////////////
  90.  
  91. // initialize GLUT for windowing
  92.  
  93. ///////////////////////////////////////////////////////////////////////////////
  94.  
  95. void initGLUT(int argc, char **argv)
  96.  
  97. {
  98.  
  99.     // GLUT stuff for windowing
  100.  
  101.     // initialization openGL window.
  102.  
  103.     // it is called before any other GLUT routine
  104.  
  105.     glutInit(&argc, argv);
  106.  
  107.  
  108.  
  109.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);   // display mode
  110.  
  111.  
  112.  
  113.     glutInitWindowSize(800, 600);               // window size
  114.  
  115.  
  116.  
  117.     glutInitWindowPosition(0,0);           // window location
  118.  
  119.  
  120.  
  121.     // finally, create a window with openGL context
  122.  
  123.     // Window will not displayed until glutMainLoop() is called
  124.  
  125.     // it returns a unique ID
  126.  
  127.     glutCreateWindow(argv[0]);     // param is the title of window
  128.  
  129.  
  130.  
  131.     // register GLUT callback functions
  132.  
  133.     glutDisplayFunc(displayCB);
  134.  
  135.     glutReshapeFunc(reshapeCB);
  136.  
  137.     glutKeyboardFunc(keyboardCB);
  138.  
  139. }
  140.  
  141.  
  142.  
  143. ///////////////////////////////////////////////////////////////////////////////
  144.  
  145. // initialize OpenGL
  146.  
  147. // disable unused features
  148.  
  149. ///////////////////////////////////////////////////////////////////////////////
  150.  
  151. void initGL()
  152.  
  153. {
  154.  
  155.     glDepthFunc(GL_LEQUAL);
  156.  
  157.     glEnable(GL_DEPTH_TEST);
  158.  
  159.  
  160.  
  161.     glClearColor(0, 0, 0, 1);
  162.  
  163.  
  164.  
  165.     createVBO();
  166.  
  167.     createIBO();
  168.  
  169.  
  170.  
  171.     glEnableClientState(GL_VERTEX_ARRAY);
  172.  
  173.  
  174.  
  175.     glBindBuffer(GL_ARRAY_BUFFER, vboId[0]);
  176.  
  177.     glVertexPointer(3, GL_FLOAT, 0, 0);
  178.  
  179.  
  180.  
  181.     glBindBuffer(GL_ARRAY_BUFFER, vboId[1]);
  182.  
  183.     glVertexPointer(3, GL_FLOAT, 0, 0);
  184.  
  185. }
  186.  
  187.  
  188.  
  189. ///////////////////////////////////////////////////////////////////////////////
  190.  
  191. // generate vertex buffer object and bind it with its data
  192.  
  193. // You must give 2 hints about data usage; target and mode, so that OpenGL can
  194.  
  195. // decide which data should be stored and its location.
  196.  
  197. // VBO works with 2 different targets; GL_ARRAY_BUFFER_ARB for vertex arrays
  198.  
  199. // and GL_ELEMENT_ARRAY_BUFFER_ARB for index array in glDrawElements().
  200.  
  201. // The default target is GL_ARRAY_BUFFER_ARB.
  202.  
  203. // By default, usage mode is set as GL_STATIC_DRAW_ARB.
  204.  
  205. // Other usages are GL_STREAM_DRAW_ARB, GL_STREAM_READ_ARB, GL_STREAM_COPY_ARB,
  206.  
  207. // GL_STATIC_DRAW_ARB, GL_STATIC_READ_ARB, GL_STATIC_COPY_ARB,
  208.  
  209. // GL_DYNAMIC_DRAW_ARB, GL_DYNAMIC_READ_ARB, GL_DYNAMIC_COPY_ARB.
  210.  
  211. ///////////////////////////////////////////////////////////////////////////////
  212.  
  213. void createVBO()
  214.  
  215. {
  216.  
  217.     glGenBuffers(2, &vboId[0]);
  218.  
  219.  
  220.  
  221.     GLfloat verticesPlain[] = { -1.0f, -1.0f, 0.0f,
  222.  
  223.         1.0f, -1.0f, 0.0f,
  224.  
  225.         1.0f,  1.0f, 0.0f,
  226.  
  227.         -1.0f,  1.0f, 0.0f};
  228.  
  229.  
  230.  
  231.     vertexPositions.resize(verticesSize);
  232.  
  233.     std::copy(&verticesPlain[0], &verticesPlain[12], vertexPositions.begin());
  234.  
  235.  
  236.  
  237.     glBindBuffer(GL_ARRAY_BUFFER, vboId[0]);
  238.  
  239.     glBufferData(GL_ARRAY_BUFFER, verticesSize * sizeof(GLfloat), &vertexPositions[0], GL_STATIC_DRAW);
  240.  
  241.    
  242.  
  243.     glBindBuffer(GL_ARRAY_BUFFER, vboId[1]);
  244.  
  245.     glBufferData(GL_ARRAY_BUFFER, verticesSize * sizeof(GLfloat), &vertexPositions[0], GL_STATIC_DRAW);
  246.  
  247.    
  248.  
  249.     vertexPositions.clear();
  250.  
  251. }
  252.  
  253.  
  254.  
  255. void createIBO()
  256.  
  257. {
  258.  
  259.     glGenBuffers(1, &iboId);
  260.  
  261.  
  262.  
  263.     GLushort indicesPlain[] = { 0, 1, 2, 2, 3, 0};
  264.  
  265.  
  266.  
  267.     indices.resize(indicesSize);
  268.  
  269.     std::copy(&indicesPlain[0], &indicesPlain[indicesSize], indices.begin());
  270.  
  271.  
  272.  
  273.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboId);
  274.  
  275.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesSize * sizeof(GLushort), &indices[0], GL_STATIC_DRAW); // upload data to video card
  276.  
  277.  
  278.  
  279.     indices.clear();
  280.  
  281. }
  282.  
  283.  
  284.  
  285. void cleanUp()
  286.  
  287. {
  288.  
  289.     glDeleteBuffers(2, &vboId[0]);
  290.  
  291.     glDeleteBuffers(1, &iboId);
  292.  
  293. }
  294.  
  295.  
  296.  
  297. //=============================================================================
  298.  
  299. // CALLBACKS
  300.  
  301. //=============================================================================
  302.  
  303.  
  304.  
  305. void displayCB()
  306.  
  307. {
  308.  
  309.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  310.  
  311.  
  312.  
  313.     glLoadIdentity();
  314.  
  315.  
  316.  
  317.     glTranslatef(-2.0f, 0.0f, -5.0f);
  318.  
  319.     glBindBuffer(GL_ARRAY_BUFFER, vboId[0]);
  320.  
  321.     glDrawElements(GL_TRIANGLES, indicesSize, GL_UNSIGNED_SHORT, 0);
  322.  
  323.  
  324.  
  325.     glTranslatef(4.0f, 0.0f, 0.0f);
  326.  
  327.     glBindBuffer(GL_ARRAY_BUFFER, vboId[1]);
  328.  
  329.     glDrawElements(GL_TRIANGLES, indicesSize, GL_UNSIGNED_SHORT, 0);
  330.  
  331.    
  332.  
  333.     glutSwapBuffers();
  334.  
  335.    
  336.  
  337.     glutPostRedisplay();
  338.  
  339. }
  340.  
  341.  
  342.  
  343.  
  344.  
  345. void reshapeCB(int w, int h)
  346.  
  347. {
  348.  
  349.     // set viewport to be the entire window
  350.  
  351.     glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  352.  
  353.  
  354.  
  355.     // set perspective viewing frustum
  356.  
  357.     //float aspectRatio = (float)w / h;
  358.  
  359.     glMatrixMode(GL_PROJECTION);
  360.  
  361.     glLoadIdentity();
  362.  
  363.     //glFrustum(-aspectRatio, aspectRatio, -1, 1, 1, 100);
  364.  
  365.     gluPerspective(60.0f, (float)(w)/h, 1.0f, 1000.0f); // FOV, AspectRatio, NearClip, FarClip
  366.  
  367.  
  368.  
  369.     // switch to modelview matrix in order to set scene
  370.  
  371.     glMatrixMode(GL_MODELVIEW);
  372.  
  373. }
  374.  
  375.  
  376.  
  377. void keyboardCB(unsigned char key, int x, int y)
  378.  
  379. {
  380.  
  381.     switch(key)
  382.  
  383.     {
  384.  
  385.         case 'q':
  386.  
  387.         case 'Q':
  388.  
  389.             cleanUp();
  390.  
  391.             glutLeaveMainLoop();
  392.  
  393.             break;
  394.  
  395.     }
  396.  
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement