source: opengl-game/game02.cpp@ 92bc4fe

feature/imgui-sdl points-test
Last change on this file since 92bc4fe was 8a6d19d, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 7 years ago

Implement movement in a basic 3D scene and add some examples from the new OpenGL tutorials site

  • Property mode set to 100644
File size: 3.1 KB
Line 
1// Include standard headers
2#include <stdio.h>
3#include <stdlib.h>
4
5// Include GLEW
6#include <GL/glew.h>
7
8// Include GLFW
9#include <GLFW/glfw3.h>
10GLFWwindow* window;
11
12// Include GLM
13#include <glm/glm.hpp>
14using namespace glm;
15
16#include "common/shader.hpp"
17
18int main( void )
19{
20 // Initialise GLFW
21 if( !glfwInit() )
22 {
23 fprintf( stderr, "Failed to initialize GLFW\n" );
24 getchar();
25 return -1;
26 }
27
28 glfwWindowHint(GLFW_SAMPLES, 4);
29 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
30 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
31 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
32 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
33
34 // Open a window and create its OpenGL context
35 window = glfwCreateWindow( 1024, 768, "Tutorial 02 - Red triangle", NULL, NULL);
36 if( window == NULL ){
37 fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
38 getchar();
39 glfwTerminate();
40 return -1;
41 }
42 glfwMakeContextCurrent(window);
43
44 // Initialize GLEW
45 glewExperimental = true; // Needed for core profile
46 if (glewInit() != GLEW_OK) {
47 fprintf(stderr, "Failed to initialize GLEW\n");
48 getchar();
49 glfwTerminate();
50 return -1;
51 }
52
53 // Ensure we can capture the escape key being pressed below
54 glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
55
56 // Dark blue background
57 glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
58
59 GLuint VertexArrayID;
60 glGenVertexArrays(1, &VertexArrayID);
61 glBindVertexArray(VertexArrayID);
62
63 // Create and compile our GLSL program from the shaders
64 GLuint programID = LoadShaders("SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader");
65
66
67 static const GLfloat g_vertex_buffer_data[] = {
68 -1.0f, -1.0f, 0.0f,
69 1.0f, -1.0f, 0.0f,
70 0.0f, 1.0f, 0.0f,
71 };
72
73 GLuint vertexbuffer;
74 glGenBuffers(1, &vertexbuffer);
75 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
76 glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
77
78 do{
79
80 // Clear the screen
81 glClear( GL_COLOR_BUFFER_BIT );
82
83 // Use our shader
84 glUseProgram(programID);
85
86 // 1rst attribute buffer : vertices
87 glEnableVertexAttribArray(0);
88 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
89 glVertexAttribPointer(
90 0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
91 3, // size
92 GL_FLOAT, // type
93 GL_FALSE, // normalized?
94 0, // stride
95 (void*)0 // array buffer offset
96 );
97
98 // Draw the triangle !
99 glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle
100
101 glDisableVertexAttribArray(0);
102
103 // Swap buffers
104 glfwSwapBuffers(window);
105 glfwPollEvents();
106
107 } // Check if the ESC key was pressed or the window was closed
108 while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
109 glfwWindowShouldClose(window) == 0 );
110
111 // Cleanup VBO
112 glDeleteBuffers(1, &vertexbuffer);
113 glDeleteVertexArrays(1, &VertexArrayID);
114 glDeleteProgram(programID);
115
116 // Close OpenGL window and terminate GLFW
117 glfwTerminate();
118
119 return 0;
120}
Note: See TracBrowser for help on using the repository browser.