Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define GL_GLEXT_PROTOTYPES
- #include <GL/freeglut.h>
- #include <GL/gl.h>
- #include <GL/glext.h>
- #include <iostream>
- #include <vector>
- #include <algorithm>
- // GLUT CALLBACK functions
- void displayCB();
- void reshapeCB(int w, int h);
- void keyboardCB(unsigned char key, int x, int y);
- void initGL();
- void initGLUT(int argc, char **argv);
- void createVBO();
- void createIBO();
- void cleanUp();
- // global variables
- GLuint vboId[2]; // ID of VBO for vertex arrays (to store vertex coords and normals)
- GLuint iboId;
- std::vector<GLfloat> vertexPositions;
- std::vector<GLushort> indices;
- const unsigned int verticesSize = 4 * 3;
- const unsigned int indicesSize = 6;
- ///////////////////////////////////////////////////////////////////////////////
- int main(int argc, char **argv)
- {
- // init GLUT and GL
- initGLUT(argc, argv);
- initGL();
- glutMainLoop(); /* Start GLUT event-processing loop */
- return 0;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // initialize GLUT for windowing
- ///////////////////////////////////////////////////////////////////////////////
- void initGLUT(int argc, char **argv)
- {
- // GLUT stuff for windowing
- // initialization openGL window.
- // it is called before any other GLUT routine
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); // display mode
- glutInitWindowSize(800, 600); // window size
- glutInitWindowPosition(0,0); // window location
- // finally, create a window with openGL context
- // Window will not displayed until glutMainLoop() is called
- // it returns a unique ID
- glutCreateWindow(argv[0]); // param is the title of window
- // register GLUT callback functions
- glutDisplayFunc(displayCB);
- glutReshapeFunc(reshapeCB);
- glutKeyboardFunc(keyboardCB);
- }
- ///////////////////////////////////////////////////////////////////////////////
- // initialize OpenGL
- // disable unused features
- ///////////////////////////////////////////////////////////////////////////////
- void initGL()
- {
- glDepthFunc(GL_LEQUAL);
- glEnable(GL_DEPTH_TEST);
- glClearColor(0, 0, 0, 1);
- createVBO();
- createIBO();
- glEnableClientState(GL_VERTEX_ARRAY);
- glBindBuffer(GL_ARRAY_BUFFER, vboId[0]);
- glVertexPointer(3, GL_FLOAT, 0, 0);
- glBindBuffer(GL_ARRAY_BUFFER, vboId[1]);
- glVertexPointer(3, GL_FLOAT, 0, 0);
- }
- ///////////////////////////////////////////////////////////////////////////////
- // generate vertex buffer object and bind it with its data
- // You must give 2 hints about data usage; target and mode, so that OpenGL can
- // decide which data should be stored and its location.
- // VBO works with 2 different targets; GL_ARRAY_BUFFER_ARB for vertex arrays
- // and GL_ELEMENT_ARRAY_BUFFER_ARB for index array in glDrawElements().
- // The default target is GL_ARRAY_BUFFER_ARB.
- // By default, usage mode is set as GL_STATIC_DRAW_ARB.
- // Other usages are GL_STREAM_DRAW_ARB, GL_STREAM_READ_ARB, GL_STREAM_COPY_ARB,
- // GL_STATIC_DRAW_ARB, GL_STATIC_READ_ARB, GL_STATIC_COPY_ARB,
- // GL_DYNAMIC_DRAW_ARB, GL_DYNAMIC_READ_ARB, GL_DYNAMIC_COPY_ARB.
- ///////////////////////////////////////////////////////////////////////////////
- void createVBO()
- {
- glGenBuffers(2, &vboId[0]);
- GLfloat verticesPlain[] = { -1.0f, -1.0f, 0.0f,
- 1.0f, -1.0f, 0.0f,
- 1.0f, 1.0f, 0.0f,
- -1.0f, 1.0f, 0.0f};
- vertexPositions.resize(verticesSize);
- std::copy(&verticesPlain[0], &verticesPlain[12], vertexPositions.begin());
- glBindBuffer(GL_ARRAY_BUFFER, vboId[0]);
- glBufferData(GL_ARRAY_BUFFER, verticesSize * sizeof(GLfloat), &vertexPositions[0], GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, vboId[1]);
- glBufferData(GL_ARRAY_BUFFER, verticesSize * sizeof(GLfloat), &vertexPositions[0], GL_STATIC_DRAW);
- vertexPositions.clear();
- }
- void createIBO()
- {
- glGenBuffers(1, &iboId);
- GLushort indicesPlain[] = { 0, 1, 2, 2, 3, 0};
- indices.resize(indicesSize);
- std::copy(&indicesPlain[0], &indicesPlain[indicesSize], indices.begin());
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboId);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesSize * sizeof(GLushort), &indices[0], GL_STATIC_DRAW); // upload data to video card
- indices.clear();
- }
- void cleanUp()
- {
- glDeleteBuffers(2, &vboId[0]);
- glDeleteBuffers(1, &iboId);
- }
- //=============================================================================
- // CALLBACKS
- //=============================================================================
- void displayCB()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glTranslatef(-2.0f, 0.0f, -5.0f);
- glBindBuffer(GL_ARRAY_BUFFER, vboId[0]);
- glDrawElements(GL_TRIANGLES, indicesSize, GL_UNSIGNED_SHORT, 0);
- glTranslatef(4.0f, 0.0f, 0.0f);
- glBindBuffer(GL_ARRAY_BUFFER, vboId[1]);
- glDrawElements(GL_TRIANGLES, indicesSize, GL_UNSIGNED_SHORT, 0);
- glutSwapBuffers();
- glutPostRedisplay();
- }
- void reshapeCB(int w, int h)
- {
- // set viewport to be the entire window
- glViewport(0, 0, (GLsizei)w, (GLsizei)h);
- // set perspective viewing frustum
- //float aspectRatio = (float)w / h;
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- //glFrustum(-aspectRatio, aspectRatio, -1, 1, 1, 100);
- gluPerspective(60.0f, (float)(w)/h, 1.0f, 1000.0f); // FOV, AspectRatio, NearClip, FarClip
- // switch to modelview matrix in order to set scene
- glMatrixMode(GL_MODELVIEW);
- }
- void keyboardCB(unsigned char key, int x, int y)
- {
- switch(key)
- {
- case 'q':
- case 'Q':
- cleanUp();
- glutLeaveMainLoop();
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement