marichan022

hello-triangle

Mar 25th, 2019
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. // Include GLEW PRIJE GLFW!
  4. #include <GL/glew.h>    // u ovom programu nije potrebno
  5.  
  6. // Include GLFW
  7. #include <GLFW/glfw3.h>
  8.  
  9. // GLM
  10. #include <glm/glm.hpp>  // u ovom programu nije potrebno
  11. using namespace glm;
  12.  
  13. //[5]
  14. #include "shader.h"
  15.  
  16. int main()
  17. {
  18.     // GLFW inicijalizacija
  19.     if( !glfwInit() )
  20.     {
  21.         std::cerr << "GLFW nije inicijaliziran\n";
  22.         getchar();
  23.         return -1;
  24.     }
  25.  
  26.     glfwWindowHint(GLFW_SAMPLES, 4);
  27.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  28.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  29.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // samo za MacOS
  30.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  31.  
  32.     // kreiranje prozora i OpenGL context
  33.     GLFWwindow* window = glfwCreateWindow( 1024, 768, "prog_01", nullptr, nullptr);
  34.     if( window == nullptr )
  35.     {
  36.         std::cerr << "Nije prošao init glfw prozora. Npr. neki Intel GPU nisu 3.3 kompatibilni.\n";
  37.         getchar();
  38.         glfwTerminate();
  39.         return -1;
  40.     }
  41.     glfwMakeContextCurrent(window);
  42.  
  43.     // GLEW inicijalizacija
  44.     if (glewInit() != GLEW_OK)
  45.     {
  46.         std::cerr << "GLEW nije inicijaliziran\n";
  47.         getchar();
  48.         glfwTerminate();
  49.         return -1;
  50.     }
  51.  
  52.     // sticky key press capture
  53.     glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  54.  
  55.     // pozadina
  56.     glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
  57.  
  58.     //[1 Vertex Array Object
  59.     GLuint VertexArrayID;
  60.     glGenVertexArrays(1, &VertexArrayID);
  61.     glBindVertexArray(VertexArrayID);
  62.     //]
  63.     // [2 An array of three vectors which represents three vertices
  64.     static const GLfloat g_vertex_buffer_data[] = {
  65.        -1.0f, -1.0f, 0.0f,
  66.        1.0f, -1.0f, 0.0f,
  67.        0.0f,  1.0f, 0.0f,
  68.     };
  69.     //]
  70.     //[3
  71.     GLuint vertexbuffer;    // This will identify our vertex buffer
  72.     glGenBuffers(1, &vertexbuffer);     // Generate 1 buffer, put the resulting identifier in vertexbuffer
  73.     glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);    // The following commands will talk about our 'vertexbuffer' buffer
  74.     glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);  // Give our vertices to OpenGL.
  75.     //]
  76.     // [5] Create and compile our GLSL program from the shaders
  77.     GLuint programID = loadShaders( "vertex.glsl", "fragment.glsl" );
  78.  
  79.     // napokon, loop
  80.     do {
  81.         // Brisanje ekrana
  82.         // glClear( GL_COLOR_BUFFER_BIT );
  83.         //[6
  84.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  85.         glUseProgram(programID);    // Use our shader
  86.         //]
  87.  
  88.         //[4 first attribute buffer : vertices
  89.         glEnableVertexAttribArray(0);
  90.         glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  91.         glVertexAttribPointer(
  92.            0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
  93.            3,                  // size
  94.            GL_FLOAT,           // type
  95.            GL_FALSE,           // normalized?
  96.            0,                  // stride
  97.            (void*)0            // array buffer offset
  98.         );
  99.         // Draw the triangle !
  100.         glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
  101.         glDisableVertexAttribArray(0);
  102.         //]
  103.  
  104.         // Swap buffers
  105.         glfwSwapBuffers(window);
  106.         glfwPollEvents();
  107.  
  108.     } // loop se vrti sve dok ne stisnemo ESC ili dok se prozor ne zatvori
  109.     while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
  110.            glfwWindowShouldClose(window) == 0 );
  111.  
  112.     // zatvaranje OpenGL prozora i GLFW
  113.     glfwTerminate();
  114.  
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment