-
Notifications
You must be signed in to change notification settings - Fork 18
/
hello_triangle_glfw.cpp
283 lines (234 loc) · 8.83 KB
/
hello_triangle_glfw.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// GLM [https://glm.g-truc.net/0.9.9/api/index.html]
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// OpenGL
// GLEW [http://glew.sourceforge.net/]
#include <GL/glew.h>
#ifdef __APPLE__
//#include <OpenGL/gl.h>
//#include <OpenGL/glu.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#endif
// GLFW [https://www.glfw.org/]
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#define CONTEXT_INFORMATION_AND_DEBUG_OUTPUT
#ifdef CONTEXT_INFORMATION_AND_DEBUG_OUTPUT
#include "../gl_debug.h"
#endif
#include <chrono>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
const char* sh_vert = R"(
#version 410 core
layout (location = 0) in vec3 inPos;
layout (location = 1) in vec4 inColor;
out vec3 vertPos;
out vec4 vertCol;
uniform mat4 project;
uniform mat4 view;
uniform mat4 model;
void main()
{
vec4 view_pos = view * model * vec4(inPos, 1.0);
vertCol = inColor;
vertPos = view_pos.xyz;
gl_Position = project * vec4(view_pos.xyz, 1.0);
}
)";
const char* sh_frag = R"(
#version 410 core
in vec3 vertPos;
in vec4 vertCol;
out vec4 fragColor;
void main()
{
fragColor = vertCol;
}
)";
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
void saveScreenshotToFile(const char* filename, int width, int height);
GLuint CompileShader(GLenum ahader_type, const char* shader_code);
GLuint LinkProgram(std::initializer_list<GLuint> shader_objets);
bool debug_context = true;
bool screenshot = false;
int main()
{
if ( glfwInit() == 0 )
throw std::runtime_error( "error initializing glfw" );
//glfwWindowHint(GLFW_REFRESH_RATE, 10);
#ifdef __APPLE__
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glewExperimental = true;
#endif
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, debug_context ? GLFW_TRUE : GLFW_FALSE);
GLFWwindow *wnd = glfwCreateWindow(800, 600, "GLFW OGL window - Hello triangle", nullptr, nullptr);
if ( wnd == nullptr )
{
glfwTerminate();
throw std::runtime_error( "error initializing window" );
}
glfwSetKeyCallback(wnd, keyCallback);
glfwMakeContextCurrent(wnd);
if (glewInit() != GLEW_OK)
throw std::runtime_error( "error initializing glew" );
#ifdef CONTEXT_INFORMATION_AND_DEBUG_OUTPUT
OpenGL::CContext::TDebugLevel debug_level = debug_context ? OpenGL::CContext::TDebugLevel::all : OpenGL::CContext::TDebugLevel::off;
OpenGL::CContext context;
context.init(debug_level);
#endif
//glfwSwapInterval( 2 );
std::cout << "shader" << std::endl;
GLuint program_obj = LinkProgram({
CompileShader(GL_VERTEX_SHADER, sh_vert),
CompileShader(GL_FRAGMENT_SHADER, sh_frag)});
GLint project_loc = glGetUniformLocation(program_obj, "project");
GLint view_loc = glGetUniformLocation(program_obj, "view");
GLint model_loc = glGetUniformLocation(program_obj, "model");
glUseProgram(program_obj);
static const std::vector<float> varray
{
-0.866f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f,
0.866f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f
};
std::cout << "buffer" << std::endl;
GLuint vbo;
glGenBuffers( 1, &vbo );
glBindBuffer( GL_ARRAY_BUFFER, vbo );
//glBufferData( GL_ARRAY_BUFFER, varray.size()*sizeof(*varray.data()), varray.data(), GL_STATIC_DRAW );
glBufferData( GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(varray.size()*sizeof(*varray.data())), nullptr, GL_STATIC_DRAW );
glBufferSubData( GL_ARRAY_BUFFER, GL_ZERO, static_cast<GLsizeiptr>(varray.size()*sizeof(*varray.data())), varray.data() );
std::cout << "vertex specification" << std::endl;
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 6*sizeof(*varray.data()), nullptr);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 6*sizeof(*varray.data()), (void*)(2*sizeof(*varray.data())));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
std::cout << "application loop" << std::endl;
auto start_time = std::chrono::high_resolution_clock::now();
auto prev_time = start_time;
while (!glfwWindowShouldClose(wnd))
{
std::chrono::high_resolution_clock::time_point current_time = std::chrono::high_resolution_clock::now();
auto frame_time = current_time - prev_time;
prev_time = current_time;
auto all_time = current_time - start_time;
double delta_s = (double)std::chrono::duration_cast<std::chrono::milliseconds>(frame_time).count() / 1000.0;
double time_s = (double)std::chrono::duration_cast<std::chrono::milliseconds>(all_time).count() / 1000.0;
std::string title = "FPS: " + std::to_string(1/ delta_s);
glfwSetWindowTitle(wnd, title.c_str());
auto angle = (float)(time_s * 90.0);
int vpSize[2];
glfwGetFramebufferSize(wnd, &vpSize[0], &vpSize[1]);
float aspect = (float)vpSize[0] / (float)vpSize[1];
float orthoX = aspect > 1.0f ? aspect : 1.0f;
float orthoY = aspect > 1.0f ? 1.0f : aspect;
glm::mat4 project = glm::ortho(-orthoX, orthoX, -orthoY, orthoY, -1.0f, 1.0f);
float orthScale = 1.0f;
project = glm::scale(project, glm::vec3(orthScale, orthScale, 1.0f));
static bool invert = false;
glm::mat4 view = glm::lookAt(
glm::vec3(0.0f, 0.0f, 0.5f * (invert ? -1.0f : 1.0f)),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 model( 1.0f );
model = glm::translate(model, glm::vec3(0.1f, 0.0f, 1.0f));
model = glm::rotate(model, glm::radians(angle), glm::vec3(0.0f, 0.0f, 1.0f));
glUniformMatrix4fv(project_loc, 1, GL_FALSE, glm::value_ptr(project));
glUniformMatrix4fv(view_loc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(model_loc, 1, GL_FALSE, glm::value_ptr(model));
glViewport(0, 0, vpSize[0], vpSize[1]);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(wnd);
glfwPollEvents();
if (screenshot) {
screenshot = false;
saveScreenshotToFile("screenshot.tga", vpSize[0], vpSize[1]);
}
}
glfwDestroyWindow( wnd );
glfwTerminate();
return 0;
}
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_S && action == GLFW_PRESS)
screenshot = true;
}
void saveScreenshotToFile(const char* filename, int width, int height) {
short tgaHead[] = { 0, 2, 0, 0, 0, 0, (short)width, (short)height, 24 };
std::vector<char> buffer(width*height*3);
glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, buffer.data());
std::fstream outFile;
outFile.open(filename, std::ios::app | std::ios::binary);
outFile.write(reinterpret_cast<char*>(&tgaHead), sizeof(tgaHead));
outFile.write(buffer.data(), buffer.size());
outFile.close();
}
bool CompileStatus(GLuint shader)
{
GLint status = GL_TRUE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE)
{
GLint logLen;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLen);
std::vector< char >log(logLen);
GLsizei written;
glGetShaderInfoLog(shader, logLen, &written, log.data());
std::cout << "compile error:" << std::endl << log.data() << std::endl;
}
return status != GL_FALSE;
}
bool LinkStatus(GLuint program)
{
GLint status = GL_TRUE;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status == GL_FALSE)
{
GLint logLen;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLen);
std::vector< char >log(logLen);
GLsizei written;
glGetProgramInfoLog(program, logLen, &written, log.data());
std::cout << "link error:" << std::endl << log.data() << std::endl;
}
return status != GL_FALSE;
}
GLuint CompileShader(GLenum ahader_type, const char* shader_code)
{
auto shader_obj = glCreateShader(ahader_type);
glShaderSource(shader_obj, 1, &shader_code, nullptr);
glCompileShader(shader_obj);
CompileStatus(shader_obj);
return shader_obj;
}
GLuint LinkProgram(std::initializer_list<GLuint> shader_objets)
{
auto program_obj = glCreateProgram();
for (auto shader_object: shader_objets)
{
glAttachShader(program_obj, shader_object);
}
glLinkProgram(program_obj);
LinkStatus(program_obj);
return program_obj;
}