Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- UInitWindow(argc, argv);
- GlewInitResult = glewInit();
- if (GLEW_OK != GlewInitResult) {
- fprintf(stderr, "ERROR: %sn", glewGetErrorString(GlewInitResult));
- exit(EXIT_FAILURE);
- }
- fprintf(stdout,"INFO: OpenGl Version: %sn", glGetString(GL_VERSION));
- UCreateVBO(); //Calls the function to create the Vertex Buffer Object
- UCreateShaders(); //Calls the function to create the Shader Program
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- glutInit(&argc, argv);
- glutInitWindowSize(WindowWidth, WindowHeight);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
- glutCreateWindow(WINDOW_TITLE);
- glutReshapeFunc(UResizeWindow);
- glutDisplayFunc(URenderGraphics);
- /*Creates the Triangle*/
- GLuint totalVerticies = 6; //Specifies the number of verticies for the triangel i.e.3
- //glDrawArrays(GL_TRIANGLES, 0, totalVerticies); //Draws the Triangle
- //Draw the triangles using the indicies
- glDrawElements(GL_TRIANGLES, totalVerticies, GL_UNSIGNED_SHORT, NULL);
- glutSwapBuffers();
- //Specifies the coordinates for triangle verticles on x and y
- GLfloat verts[]= {
- /*indices will be 0, 1, 2, 3, 4, 1
- * index 1 will be shared by both triangles
- */
- //index 0
- -1.f, 1.0f, //top-center of the screen
- 1.0f, 0.0f, 0.0f, 1.0f, //Red Vertex
- //index 1
- -0.5f, 0.0f, //bottom-left of the screen
- 0.0f , 1.0f, 0.0f, 1.0f, // Green Vertex
- //index 2
- -1.0f, 0.0f, //bottom-right of the screen
- 0.0f, 0.0f, 1.0f, 1.0f, // Blue Vertex
- //index 3
- 0.0, 0.0f, //topcenter of the screen
- 1.0f, 0.0f, 0.0f, 1.0f, //Red Vertex
- //index 4
- 0.0f, -1.0f, // bottom-left of the screen
- 0.0f, 1.0f, 0.0f, 1.0f, // Green vertex
- //0.0, -1.0f, //bottom-right of the screen
- //0.0f, 0.0f, 1.0f, 1.0f //Blue Vertex
- };
- //stores the size of the verts array / number of coordinates needed for the triangle i.e 6
- float numVerticles = sizeof(verts);
- GLuint myBufferID; //Variable for vertex buffer object id
- glGenBuffers(1, &myBufferID); //Creates 1 buffer
- glBindBuffer(GL_ARRAY_BUFFER, myBufferID); //Activates the buffer
- glBufferData(GL_ARRAY_BUFFER, numVerticles, verts, GL_STATIC_DRAW); //Sends vertex or coordinate data to the GPU
- /* Creates the Vertex Attribute Pointer */
- GLuint floatsPerVertex = 2; // Number of coordinates per vertex
- glEnableVertexAttribArray(0); // Specifies the initial position of the coordinates in the buffer
- /*Strides between vertex coordinates is 6 (x, y, r, g, b, a). A tightly packed stride is 0.*/
- GLint vertexStride = sizeof(float) * 6; //The number of floats before each vertex position is 6 i.e. xyrgba
- /* Instructs the GPI on how to handle the vertex buffer object data
- * parameters attribPointerPosition | coordinates per vertex | data type | deactivate normalization | 0 strides | 0 offset |
- */
- glVertexAttribPointer(0, floatsPerVertex, GL_FLOAT, GL_FALSE, vertexStride, 0); //change the stride from 0 to 6
- /* Sets an attribute pointer position for the vertex colors i.e Attribute 1 for rgba floats. Attribute 0 for postition x,y */
- glEnableVertexAttribArray(1); //Specifies position 1 for the color values in the buffer
- GLint colorStride = sizeof(float) * 6; //The number of floats before each color is 6 i.e. rgba xy
- //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
- glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, colorStride, (char*)(sizeof(float)*2));
- /*Creates a buffer for the indices*/
- GLushort indicies[] = {0,1,2, 3,4,1}; //Using index 1 twice
- float numIndicies = sizeof(indicies);
- GLuint indexBufferID;
- glGenBuffers(1, &indexBufferID);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndicies, indicies, GL_STATIC_DRAW);
- GLuint vertexShaderId = glCreateShader(GL_VERTEX_SHADER); //Create a Vertex Shader object
- GLuint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER); //Create a Vertex Shader object
- glShaderSource(vertexShaderId, 1, &VertexShader, NULL); //retrieves the vertex shader source code
- glShaderSource(fragmentShaderId, 1, &FragmentShader, NULL); //retrieves the fragment shader source code
- glCompileShader(vertexShaderId); //compile the vertex shader
- glCompileShader(fragmentShaderId); //compile the fragment shader
- //Attaches the vertex and fragment shader to the shader program
- glAttachShader(ProgramId, vertexShaderId);
- glAttachShader(ProgramId, fragmentShaderId);
- glLinkProgram(ProgramId); //Links the shader program
- glUseProgram(ProgramId); //Uses Shader Program
Add Comment
Please, Sign In to add comment