Guest User

Untitled

a guest
Nov 16th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. UInitWindow(argc, argv);
  2.  
  3. GlewInitResult = glewInit();
  4.  
  5. if (GLEW_OK != GlewInitResult) {
  6. fprintf(stderr, "ERROR: %sn", glewGetErrorString(GlewInitResult));
  7. exit(EXIT_FAILURE);
  8. }
  9.  
  10. fprintf(stdout,"INFO: OpenGl Version: %sn", glGetString(GL_VERSION));
  11.  
  12. UCreateVBO(); //Calls the function to create the Vertex Buffer Object
  13.  
  14. UCreateShaders(); //Calls the function to create the Shader Program
  15.  
  16. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  17.  
  18. glutInit(&argc, argv);
  19.  
  20. glutInitWindowSize(WindowWidth, WindowHeight);
  21.  
  22. glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  23.  
  24. glutCreateWindow(WINDOW_TITLE);
  25.  
  26. glutReshapeFunc(UResizeWindow);
  27. glutDisplayFunc(URenderGraphics);
  28.  
  29. /*Creates the Triangle*/
  30. GLuint totalVerticies = 6; //Specifies the number of verticies for the triangel i.e.3
  31. //glDrawArrays(GL_TRIANGLES, 0, totalVerticies); //Draws the Triangle
  32.  
  33. //Draw the triangles using the indicies
  34. glDrawElements(GL_TRIANGLES, totalVerticies, GL_UNSIGNED_SHORT, NULL);
  35.  
  36. glutSwapBuffers();
  37.  
  38. //Specifies the coordinates for triangle verticles on x and y
  39. GLfloat verts[]= {
  40. /*indices will be 0, 1, 2, 3, 4, 1
  41. * index 1 will be shared by both triangles
  42. */
  43.  
  44. //index 0
  45. -1.f, 1.0f, //top-center of the screen
  46. 1.0f, 0.0f, 0.0f, 1.0f, //Red Vertex
  47.  
  48. //index 1
  49. -0.5f, 0.0f, //bottom-left of the screen
  50. 0.0f , 1.0f, 0.0f, 1.0f, // Green Vertex
  51.  
  52. //index 2
  53. -1.0f, 0.0f, //bottom-right of the screen
  54. 0.0f, 0.0f, 1.0f, 1.0f, // Blue Vertex
  55.  
  56. //index 3
  57. 0.0, 0.0f, //topcenter of the screen
  58. 1.0f, 0.0f, 0.0f, 1.0f, //Red Vertex
  59.  
  60. //index 4
  61. 0.0f, -1.0f, // bottom-left of the screen
  62. 0.0f, 1.0f, 0.0f, 1.0f, // Green vertex
  63.  
  64. //0.0, -1.0f, //bottom-right of the screen
  65. //0.0f, 0.0f, 1.0f, 1.0f //Blue Vertex
  66. };
  67.  
  68. //stores the size of the verts array / number of coordinates needed for the triangle i.e 6
  69. float numVerticles = sizeof(verts);
  70.  
  71. GLuint myBufferID; //Variable for vertex buffer object id
  72. glGenBuffers(1, &myBufferID); //Creates 1 buffer
  73. glBindBuffer(GL_ARRAY_BUFFER, myBufferID); //Activates the buffer
  74. glBufferData(GL_ARRAY_BUFFER, numVerticles, verts, GL_STATIC_DRAW); //Sends vertex or coordinate data to the GPU
  75.  
  76. /* Creates the Vertex Attribute Pointer */
  77. GLuint floatsPerVertex = 2; // Number of coordinates per vertex
  78. glEnableVertexAttribArray(0); // Specifies the initial position of the coordinates in the buffer
  79.  
  80. /*Strides between vertex coordinates is 6 (x, y, r, g, b, a). A tightly packed stride is 0.*/
  81. GLint vertexStride = sizeof(float) * 6; //The number of floats before each vertex position is 6 i.e. xyrgba
  82.  
  83. /* Instructs the GPI on how to handle the vertex buffer object data
  84. * parameters attribPointerPosition | coordinates per vertex | data type | deactivate normalization | 0 strides | 0 offset |
  85. */
  86. glVertexAttribPointer(0, floatsPerVertex, GL_FLOAT, GL_FALSE, vertexStride, 0); //change the stride from 0 to 6
  87.  
  88. /* Sets an attribute pointer position for the vertex colors i.e Attribute 1 for rgba floats. Attribute 0 for postition x,y */
  89. glEnableVertexAttribArray(1); //Specifies position 1 for the color values in the buffer
  90.  
  91. GLint colorStride = sizeof(float) * 6; //The number of floats before each color is 6 i.e. rgba xy
  92.  
  93. //Parameters: attribPointerPosition 1 | floats per color is 4 i.e. rgba | data type | deactivate normalization | 6 strides until next color i.e. rgbazy | 2 floats until the beginning of each color i.e xy
  94. glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, colorStride, (char*)(sizeof(float)*2));
  95.  
  96. /*Creates a buffer for the indices*/
  97. GLushort indicies[] = {0,1,2, 3,4,1}; //Using index 1 twice
  98. float numIndicies = sizeof(indicies);
  99. GLuint indexBufferID;
  100. glGenBuffers(1, &indexBufferID);
  101. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
  102. glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndicies, indicies, GL_STATIC_DRAW);
  103.  
  104. GLuint vertexShaderId = glCreateShader(GL_VERTEX_SHADER); //Create a Vertex Shader object
  105. GLuint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER); //Create a Vertex Shader object
  106.  
  107. glShaderSource(vertexShaderId, 1, &VertexShader, NULL); //retrieves the vertex shader source code
  108. glShaderSource(fragmentShaderId, 1, &FragmentShader, NULL); //retrieves the fragment shader source code
  109.  
  110. glCompileShader(vertexShaderId); //compile the vertex shader
  111. glCompileShader(fragmentShaderId); //compile the fragment shader
  112.  
  113. //Attaches the vertex and fragment shader to the shader program
  114. glAttachShader(ProgramId, vertexShaderId);
  115. glAttachShader(ProgramId, fragmentShaderId);
  116.  
  117. glLinkProgram(ProgramId); //Links the shader program
  118. glUseProgram(ProgramId); //Uses Shader Program
Add Comment
Please, Sign In to add comment