source: opengl-game/new-game.cpp@ 8b7cfcf

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

Allow each vertex to have its own color

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