1 | // NEXT STEP; Modify the vertex shader
|
---|
2 |
|
---|
3 | #include "logger.h"
|
---|
4 |
|
---|
5 | #include <GL/glew.h>
|
---|
6 | #include <GLFW/glfw3.h>
|
---|
7 |
|
---|
8 | #include <cstdio>
|
---|
9 | #include <iostream>
|
---|
10 | #include <fstream>
|
---|
11 |
|
---|
12 | using namespace std;
|
---|
13 |
|
---|
14 | GLuint loadShader(GLenum type, string file);
|
---|
15 |
|
---|
16 | const bool FULLSCREEN = false;
|
---|
17 |
|
---|
18 | void glfw_error_callback(int error, const char* description) {
|
---|
19 | gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
|
---|
20 | }
|
---|
21 |
|
---|
22 | int main(int argc, char* argv[]) {
|
---|
23 | cout << "New OpenGL Game" << endl;
|
---|
24 |
|
---|
25 | if (!restart_gl_log()) {}
|
---|
26 | gl_log("starting GLFW\n%s\n", glfwGetVersionString());
|
---|
27 |
|
---|
28 | glfwSetErrorCallback(glfw_error_callback);
|
---|
29 | if (!glfwInit()) {
|
---|
30 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
---|
31 | return 1;
|
---|
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
|
---|
40 |
|
---|
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 |
|
---|
61 | if (!window) {
|
---|
62 | fprintf(stderr, "ERROR: could not open window with GLFW3\n");
|
---|
63 | glfwTerminate();
|
---|
64 | return 1;
|
---|
65 | }
|
---|
66 | glfwMakeContextCurrent(window);
|
---|
67 | glewExperimental = GL_TRUE;
|
---|
68 | glewInit();
|
---|
69 |
|
---|
70 | // glViewport(0, 0, width*2, height*2);
|
---|
71 |
|
---|
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);
|
---|
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 |
|
---|
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);
|
---|
94 | glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
|
---|
95 |
|
---|
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 |
|
---|
101 | GLuint vao = 0;
|
---|
102 | glGenVertexArrays(1, &vao);
|
---|
103 | glBindVertexArray(vao);
|
---|
104 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
105 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
106 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
107 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
108 |
|
---|
109 | glEnableVertexAttribArray(0);
|
---|
110 | glEnableVertexAttribArray(1);
|
---|
111 |
|
---|
112 | GLuint vs = loadShader(GL_VERTEX_SHADER, "./test.vert");
|
---|
113 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, "./test.frag");
|
---|
114 |
|
---|
115 | GLuint shader_program = glCreateProgram();
|
---|
116 | glAttachShader(shader_program, vs);
|
---|
117 | glAttachShader(shader_program, fs);
|
---|
118 |
|
---|
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);
|
---|
126 |
|
---|
127 | glfwPollEvents();
|
---|
128 | glfwSwapBuffers(window);
|
---|
129 |
|
---|
130 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
---|
131 | glfwSetWindowShouldClose(window, 1);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | glfwTerminate();
|
---|
136 | return 0;
|
---|
137 | }
|
---|
138 |
|
---|
139 | GLuint 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 | }
|
---|