source: opengl-game/new-game.cpp@ d0b9596

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

Specify the attribute locations in the vertex shader

  • Property mode set to 100644
File size: 4.2 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
[644a2e4]119 glLinkProgram(shader_program);
120
121 while (!glfwWindowShouldClose(window)) {
122 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
123 glUseProgram(shader_program);
124 glBindVertexArray(vao);
125 glDrawArrays(GL_TRIANGLES, 0, 3);
[ec4456b]126
[644a2e4]127 glfwPollEvents();
128 glfwSwapBuffers(window);
[ec4456b]129
130 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
131 glfwSetWindowShouldClose(window, 1);
132 }
[644a2e4]133 }
134
[5272b6b]135 glfwTerminate();
136 return 0;
137}
[ec4456b]138
139GLuint loadShader(GLenum type, string file) {
140 cout << "Loading shader from file " << file << endl;
141
142 ifstream shaderFile(file);
143 GLuint shaderId = 0;
144
145 if (shaderFile.is_open()) {
146 string line, shaderString;
147
148 while(getline(shaderFile, line)) {
149 shaderString += line + "\n";
150 }
151 shaderFile.close();
152 const char* shaderCString = shaderString.c_str();
153
154 shaderId = glCreateShader(type);
155 glShaderSource(shaderId, 1, &shaderCString, NULL);
156 glCompileShader(shaderId);
157
158 cout << "Loaded successfully" << endl;
159 } else {
160 cout << "Failed to loade the file" << endl;
161 }
162
163 return shaderId;
164}
Note: See TracBrowser for help on using the repository browser.