Advertisement
Guest User

Untitled

a guest
Feb 18th, 2015
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.22 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <GL/glew.h>
  4. #include <GLFW/glfw3.h>
  5.  
  6. // Open an OpenGL window
  7. static GLFWwindow* window;
  8.  
  9. /****Step 1: define vertices in (x, y, z) form****/
  10. static const GLfloat coordinates[] = {
  11.     -1.0f, -1.0f, 0.0f,
  12.     1.0f, -1.0f, 0.0f,
  13.     0.0f, 1.0f, 0.0f
  14. };
  15. /************************/
  16.  
  17. /**Step 2: send this triangle vertices to OpenGL through a buffer**/
  18. static GLuint vertexBuffer; // identify vertex buffer
  19.  
  20. static void Render(void){
  21.     /************************/
  22.     glEnableVertexAttribArray(0);
  23.     glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  24.     glVertexAttribPointer(0, 3 /*size*/, GL_FLOAT /*type*/, GL_FALSE, 0, (void*)0);
  25.     glDrawArrays(GL_TRIANGLES, 0, 3);
  26.     //glDisableVertexAttribArray(0);
  27.     /************************/
  28.  
  29.     // Swap front and back rendering buffers
  30.     glfwSwapBuffers(window);
  31.     //Poll for and process events
  32.     glfwPollEvents();
  33. }
  34.  
  35. static void error_callback(int error, const char *description)
  36. {
  37.     puts(description);
  38. }
  39.  
  40. int main( void ) {
  41.     /*Initializing steps here*/
  42.     GLenum glewinit_res;
  43.  
  44.     glfwSetErrorCallback(error_callback);
  45.  
  46.     if (!glfwInit()) {
  47.         fprintf(stderr, "Error on GLWF init\n");
  48.         return EXIT_FAILURE;
  49.     }
  50.  
  51.     // Create a windowed mode window and its OpenGL context
  52.     window = glfwCreateWindow(700, 500, "Hello World", NULL, NULL);
  53.     if (window == NULL) {
  54.         fprintf(stderr, "Error in glfwCreateWindow\n");
  55.         return EXIT_FAILURE;
  56.     }
  57.  
  58.     // Make the window's context current
  59.     glfwMakeContextCurrent(window);
  60.  
  61.     glewinit_res = glewInit();
  62.     if (glewinit_res != GLEW_OK) {
  63.         fprintf(stderr, "Glew init error: %s\n",
  64.                 glewGetErrorString(glewinit_res));
  65.         return EXIT_FAILURE;
  66.     }
  67.  
  68.     /**Step 2: send this triangle vertices to OpenGL through a buffer**/
  69.     glGenBuffers(1, &vertexBuffer); // generating 1 buffer, put resulting identifier in this buffer
  70.     glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  71.     glBufferData(GL_ARRAY_BUFFER, sizeof(coordinates), coordinates, GL_STATIC_DRAW);
  72.     /************************/
  73.  
  74.     // Main loop
  75.     while( glfwWindowShouldClose(window) == 0) {
  76.         // OpenGL rendering goes here...
  77.         Render();
  78.     }
  79.     // Close window and terminate GLFW
  80.     glfwDestroyWindow(window);
  81.     glfwTerminate();
  82.  
  83.     // Exit program
  84.     exit( EXIT_SUCCESS );
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement