Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- // Open an OpenGL window
- static GLFWwindow* window;
- /****Step 1: define vertices in (x, y, z) form****/
- static const GLfloat coordinates[] = {
- -1.0f, -1.0f, 0.0f,
- 1.0f, -1.0f, 0.0f,
- 0.0f, 1.0f, 0.0f
- };
- /************************/
- /**Step 2: send this triangle vertices to OpenGL through a buffer**/
- static GLuint vertexBuffer; // identify vertex buffer
- static void Render(void){
- /************************/
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
- glVertexAttribPointer(0, 3 /*size*/, GL_FLOAT /*type*/, GL_FALSE, 0, (void*)0);
- glDrawArrays(GL_TRIANGLES, 0, 3);
- //glDisableVertexAttribArray(0);
- /************************/
- // Swap front and back rendering buffers
- glfwSwapBuffers(window);
- //Poll for and process events
- glfwPollEvents();
- }
- static void error_callback(int error, const char *description)
- {
- puts(description);
- }
- int main( void ) {
- /*Initializing steps here*/
- GLenum glewinit_res;
- glfwSetErrorCallback(error_callback);
- if (!glfwInit()) {
- fprintf(stderr, "Error on GLWF init\n");
- return EXIT_FAILURE;
- }
- // Create a windowed mode window and its OpenGL context
- window = glfwCreateWindow(700, 500, "Hello World", NULL, NULL);
- if (window == NULL) {
- fprintf(stderr, "Error in glfwCreateWindow\n");
- return EXIT_FAILURE;
- }
- // Make the window's context current
- glfwMakeContextCurrent(window);
- glewinit_res = glewInit();
- if (glewinit_res != GLEW_OK) {
- fprintf(stderr, "Glew init error: %s\n",
- glewGetErrorString(glewinit_res));
- return EXIT_FAILURE;
- }
- /**Step 2: send this triangle vertices to OpenGL through a buffer**/
- glGenBuffers(1, &vertexBuffer); // generating 1 buffer, put resulting identifier in this buffer
- glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(coordinates), coordinates, GL_STATIC_DRAW);
- /************************/
- // Main loop
- while( glfwWindowShouldClose(window) == 0) {
- // OpenGL rendering goes here...
- Render();
- }
- // Close window and terminate GLFW
- glfwDestroyWindow(window);
- glfwTerminate();
- // Exit program
- exit( EXIT_SUCCESS );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement