source: opengl-game/new-game.cpp@ 93baa0e

feature/imgui-sdl points-test
Last change on this file since 93baa0e was 93baa0e, checked in by Dmitry Portnoy <dmp1488@…>, 7 years ago

Add face culling and a model matrix that can be changed to move the scene

  • Property mode set to 100644
File size: 5.0 KB
RevLine 
[8b7cfcf]1// NEXT STEP; Modify the vertex shader
2
[22b2c37]3#include "logger.h"
[5272b6b]4
5#include <GL/glew.h>
6#include <GLFW/glfw3.h>
7
[22b2c37]8#include <cstdio>
9#include <iostream>
[ec4456b]10#include <fstream>
[93baa0e]11#include <cmath>
[22b2c37]12
[5272b6b]13using namespace std;
14
[ec4456b]15GLuint loadShader(GLenum type, string file);
16
17const bool FULLSCREEN = false;
18
19void glfw_error_callback(int error, const char* description) {
20 gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
21}
22
[5272b6b]23int main(int argc, char* argv[]) {
24 cout << "New OpenGL Game" << endl;
25
[ec4456b]26 if (!restart_gl_log()) {}
27 gl_log("starting GLFW\n%s\n", glfwGetVersionString());
[22b2c37]28
[ec4456b]29 glfwSetErrorCallback(glfw_error_callback);
[5272b6b]30 if (!glfwInit()) {
31 fprintf(stderr, "ERROR: could not start GLFW3\n");
32 return 1;
[be246ad]33 }
34
35#ifdef __APPLE__
36 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
37 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
38 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
39 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
40#endif
[5272b6b]41
[ec4456b]42 glfwWindowHint(GLFW_SAMPLES, 4);
43
44 GLFWwindow* window = NULL;
45
46 int width = 640;
47 int height = 480;
48
49 if (FULLSCREEN) {
50 GLFWmonitor* mon = glfwGetPrimaryMonitor();
51 const GLFWvidmode* vmode = glfwGetVideoMode(mon);
52
53 cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
54 window = glfwCreateWindow(vmode->width, vmode->height, "Extended GL Init", mon, NULL);
55
56 width = vmode->width;
57 height = vmode->height;
58 } else {
59 window = glfwCreateWindow(width, height, "Hello Triangle", NULL, NULL);
60 }
61
[5272b6b]62 if (!window) {
63 fprintf(stderr, "ERROR: could not open window with GLFW3\n");
64 glfwTerminate();
65 return 1;
66 }
[644a2e4]67 glfwMakeContextCurrent(window);
[5272b6b]68 glewExperimental = GL_TRUE;
69 glewInit();
70
[ec4456b]71 // glViewport(0, 0, width*2, height*2);
72
[5272b6b]73 const GLubyte* renderer = glGetString(GL_RENDERER);
74 const GLubyte* version = glGetString(GL_VERSION);
75 printf("Renderer: %s\n", renderer);
76 printf("OpenGL version supported %s\n", version);
[93baa0e]77
[5272b6b]78 glEnable(GL_DEPTH_TEST);
79 glDepthFunc(GL_LESS);
[516668e]80
[93baa0e]81 glEnable(GL_CULL_FACE);
82 // glCullFace(GL_BACK);
83 // glFrontFace(GL_CW);
84
[516668e]85 GLfloat points[] = {
86 0.0f, 0.5f, 0.0f,
87 -0.5f, -0.5f, 0.0f,
[93baa0e]88 0.5f, -0.5f, 0.0f,
[516668e]89 };
90
[8b7cfcf]91 GLfloat colors[] = {
92 1.0, 0.0, 0.0,
93 0.0, 0.0, 1.0,
[93baa0e]94 0.0, 1.0, 0.0,
95 };
96
97 GLfloat model[] = {
98 1.0f, 0.0f, 0.0f, 0.0f, // column 1
99 0.0f, 1.0f, 0.0f, 0.0f, // column 2
100 0.0f, 0.0f, 1.0f, 0.0f, // column 3
101 0.5f, 0.0f, 0.0f, 1.0f, // column 4
[8b7cfcf]102 };
103
104 GLuint points_vbo = 0;
105 glGenBuffers(1, &points_vbo);
106 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
[516668e]107 glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
108
[8b7cfcf]109 GLuint colors_vbo = 0;
110 glGenBuffers(1, &colors_vbo);
111 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
112 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
113
[644a2e4]114 GLuint vao = 0;
[516668e]115 glGenVertexArrays(1, &vao);
116 glBindVertexArray(vao);
[8b7cfcf]117 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
[516668e]118 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
[8b7cfcf]119 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
120 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
[516668e]121
[8b7cfcf]122 glEnableVertexAttribArray(0);
123 glEnableVertexAttribArray(1);
[644a2e4]124
[ec4456b]125 GLuint vs = loadShader(GL_VERTEX_SHADER, "./test.vert");
126 GLuint fs = loadShader(GL_FRAGMENT_SHADER, "./test.frag");
[644a2e4]127
128 GLuint shader_program = glCreateProgram();
129 glAttachShader(shader_program, vs);
130 glAttachShader(shader_program, fs);
[8b7cfcf]131
[644a2e4]132 glLinkProgram(shader_program);
133
[93baa0e]134 GLint location = glGetUniformLocation(shader_program, "model");
135
136 float speed = 1.0f;
137 float last_position = 0.0f;
138
139 double previous_seconds = glfwGetTime();
[644a2e4]140 while (!glfwWindowShouldClose(window)) {
[93baa0e]141 double current_seconds = glfwGetTime();
142 double elapsed_seconds = current_seconds - previous_seconds;
143 previous_seconds = current_seconds;
144
145 if (fabs(last_position) > 1.0f) {
146 speed = -speed;
147 }
148
149 model[12] = last_position + speed*elapsed_seconds;
150 last_position = model[12];
[644a2e4]151 glUseProgram(shader_program);
[93baa0e]152 glUniformMatrix4fv(location, 1, GL_FALSE, model);
153
154 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[644a2e4]155 glBindVertexArray(vao);
[93baa0e]156
[644a2e4]157 glDrawArrays(GL_TRIANGLES, 0, 3);
[ec4456b]158
[644a2e4]159 glfwPollEvents();
160 glfwSwapBuffers(window);
[ec4456b]161
162 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
163 glfwSetWindowShouldClose(window, 1);
164 }
[644a2e4]165 }
166
[5272b6b]167 glfwTerminate();
168 return 0;
169}
[ec4456b]170
171GLuint loadShader(GLenum type, string file) {
172 cout << "Loading shader from file " << file << endl;
173
174 ifstream shaderFile(file);
175 GLuint shaderId = 0;
176
177 if (shaderFile.is_open()) {
178 string line, shaderString;
179
180 while(getline(shaderFile, line)) {
181 shaderString += line + "\n";
182 }
183 shaderFile.close();
184 const char* shaderCString = shaderString.c_str();
185
186 shaderId = glCreateShader(type);
187 glShaderSource(shaderId, 1, &shaderCString, NULL);
188 glCompileShader(shaderId);
189
190 cout << "Loaded successfully" << endl;
191 } else {
192 cout << "Failed to loade the file" << endl;
193 }
194
195 return shaderId;
196}
Note: See TracBrowser for help on using the repository browser.