This repository has been archived by the owner on Sep 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
main.cpp
360 lines (287 loc) · 9.1 KB
/
main.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* OpenGL Example - Using binary SPIR-V shaders (GL_ARB_gl_spirv)
*
* Extension: https://www.opengl.org/registry/specs/ARB/gl_spirv.txt
* Supported GPUs: http://opengl.gpuinfo.org/gl_listreports.php?listreportsbyextension=GL_ARB_gl_spirv
*
* Copyright (C) 2015 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <Windows.h>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <array>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
const std::string appTitle = "OpenGL example - GL_ARB_gl_spirv";
struct
{
glm::mat4 projection;
glm::mat4 model;
glm::mat4 view;
} uboVS;
class OpenGLExample
{
public:
GLuint shader;
GLuint VBO[2];
GLuint IBO;
GLuint UBO;
uint32_t indices;
float zoom = -2.0f;
glm::vec3 rotation = glm::vec3(0.0f);
GLFWwindow* window;
OpenGLExample(GLFWwindow *window)
{
this->window = window;
}
void printShaderLog(GLuint shader)
{
GLint result = GL_FALSE;
int logLength;
glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0)
{
GLchar* strInfoLog = new GLchar[logLength + 1];
glGetShaderInfoLog(shader, logLength, NULL, strInfoLog);
std::cout << "Shaderlog: " << strInfoLog << std::endl;
};
}
void printProgramLog(GLuint program)
{
GLint result = GL_FALSE;
int logLength;
glGetProgramiv(program, GL_LINK_STATUS, &result);
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0)
{
GLchar* strInfoLog = new GLchar[logLength + 1];
glGetProgramInfoLog(program, logLength, NULL, strInfoLog);
std::cout << "Programlog: " << strInfoLog << std::endl;
};
}
bool loadBinaryShader(const char *fileName, GLuint stage, GLuint binaryFormat, GLuint &shader)
{
std::ifstream shaderFile;
shaderFile.open(fileName, std::ios::binary | std::ios::ate);
if (shaderFile.is_open())
{
size_t size = shaderFile.tellg();
shaderFile.seekg(0, std::ios::beg);
char* bin = new char[size];
shaderFile.read(bin, size);
GLint status;
shader = glCreateShader(stage); // Create a new shader
glShaderBinary(1, &shader, binaryFormat, bin, size); // Load the binary shader file
glSpecializeShaderARB(shader, "main", 0, nullptr, nullptr); // Set main entry point (required, no specialization used in this example)
glGetShaderiv(shader, GL_COMPILE_STATUS, &status); // Check compilation status
return status;
}
else
{
std::cerr << "Could not open \"" << fileName << "\"" << std::endl;
return false;
}
}
GLuint loadShader(const char* vsFileName, const char* fsFileName)
{
GLuint vertShader, fragShader;
GLint result = GL_TRUE;
result &= loadBinaryShader(vsFileName, GL_VERTEX_SHADER, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, vertShader);
result &= loadBinaryShader(fsFileName, GL_FRAGMENT_SHADER, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, fragShader);
if (!result)
{
std::cerr << "Could not load all binary shaders required for this program" << std::endl;
return GL_FALSE;
}
std::cout << "Linking shader program" << std::endl;
GLuint program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
// Bind vertex attributes to VBO indices
glBindAttribLocation(program, 0, "inPos");
glBindAttribLocation(program, 1, "inColor");
glLinkProgram(program);
printProgramLog(program);
glDeleteShader(vertShader);
glDeleteShader(fragShader);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, UBO);
glUseProgram(program);
return program;
}
void loadAssets()
{
shader = loadShader("../data/shader/triangle.vert.spv", "../data/shader/triangle.frag.spv");
}
void updateUBO()
{
// Update ubo
// Update matrices
uboVS.projection = glm::perspective(glm::radians(60.0f), (float)1280 / (float)720, 0.1f, 256.0f);
uboVS.view = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, zoom));
uboVS.model = glm::mat4();
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
glBindBuffer(GL_UNIFORM_BUFFER, UBO);
GLvoid* p = glMapBuffer(GL_UNIFORM_BUFFER, GL_WRITE_ONLY);
memcpy(p, &uboVS, sizeof(uboVS));
glUnmapBuffer(GL_UNIFORM_BUFFER);
}
void generateBuffers()
{
// Default VAO needed for OpenGL 3.3+ core profiles
GLuint VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// Setup vertex data
const GLfloat vPos[] = {
1.0f, -1.0f, 0.0f,
-1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
const GLfloat vCol[] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f
};
uint32_t vBufferSize = 9 * sizeof(float);
// Setup indices
std::vector<uint32_t> indexBuffer = { 0, 1, 2 };
indices = static_cast<uint32_t>(indexBuffer.size());
uint32_t iBufferSize = indices * sizeof(uint32_t);
glGenBuffers(2, VBO);
glGenBuffers(1, &IBO);
// Position
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER, vBufferSize, &vPos, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
// Color
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glBufferData(GL_ARRAY_BUFFER, vBufferSize, &vCol, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(1);
// Indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, iBufferSize, indexBuffer.data(), GL_STATIC_DRAW);
// Uniform buffer object
glGenBuffers(1, &UBO);
glBindBuffer(GL_UNIFORM_BUFFER, UBO);
glBufferData(GL_UNIFORM_BUFFER, sizeof(uboVS), &uboVS, GL_DYNAMIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
updateUBO();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_DEPTH_TEST);
}
void draw(double deltaT)
{
double frameTimeStart = glfwGetTime();
glClearColor(0.0f, 0.0f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 9);
glfwSwapBuffers(window);
rotation.y += deltaT * 50.0f;
updateUBO();
}
};
static void error_callback(int error, const char* description)
{
fputs(description, stderr);
_fgetchar();
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
int main(void)
{
glfwSetErrorCallback(error_callback);
if (!glfwInit())
{
exit(EXIT_FAILURE);
}
GLFWwindow* window = glfwCreateWindow(1280, 720, appTitle.c_str(), NULL, NULL);
if (!window)
{
std::cerr << "Failed to open GLFW window" << std::endl;
glfwTerminate();
exit(EXIT_FAILURE);
}
//This function makes the context of the specified window current on the calling thread.
glfwMakeContextCurrent(window);
//Set callbacks
glfwSetKeyCallback(window, key_callback);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//Initialize GLEW
glewExperimental = GL_TRUE;
GLenum err = glewInit();
//If GLEW hasn't initialized
if (err != GLEW_OK)
{
std::cerr << "GLEW Error: " << glewGetErrorString(err) << std::endl;
exit(-1);
}
std::cout << "Vendor:" << glGetString(GL_VENDOR) << std::endl;
std::cout << "Renderer:" << glGetString(GL_RENDERER) << std::endl;
std::cout << "Version:" << glGetString(GL_VERSION) << std::endl;
std::cout << std::boolalpha << "SPIRV supported: " << (GLEW_ARB_gl_spirv == true) << std::endl;
if (!GLEW_ARB_gl_spirv)
{
std::cerr << "This examples requires support for SPIR-V (GL_ARB_gl_spirv)!" << std::endl;
glfwDestroyWindow(window);
glfwTerminate();
exit(-1);
}
glfwSwapInterval(0);
OpenGLExample example(window);
example.generateBuffers();
example.loadAssets();
glDisable(GL_CULL_FACE);
double lastFPStime = glfwGetTime();
double lastFrameTime = glfwGetTime();
int frameCounter = 0;
//Main Loop
do
{
double thisFPStime = glfwGetTime();
frameCounter++;
if (thisFPStime - lastFPStime >= 1.0)
{
lastFPStime = thisFPStime;
std::string windowTitle = appTitle +" (";
windowTitle += std::to_string(frameCounter);
windowTitle += " fps) - 2016 by Sascha Willems (www.saschawillems.de)";
const char* windowCaption = windowTitle.c_str();
glfwSetWindowTitle(window, windowCaption);
frameCounter = 0;
}
double currTime = glfwGetTime();
example.draw(currTime - lastFrameTime);
lastFrameTime = currTime;
//Get and organize events, like keyboard and mouse input, window resizing, etc...
glfwPollEvents();
} //Check if the ESC key had been pressed or if the window had been closed
while (!glfwWindowShouldClose(window));
//Close OpenGL window and terminate GLFW
glfwDestroyWindow(window);
//Finalize and clean up GLFW
glfwTerminate();
exit(EXIT_SUCCESS);
}