-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
194 lines (141 loc) · 5.83 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
// GLEngine by Joshua Senouf - 2016
// Credits to Joey de Vries (LearnOpenGL) and Kevin Fung (Glitter)
#include "shader.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <imgui.h>
#include <imgui_impl_glfw_gl3.h>
//---------------
// GLFW Callbacks
//---------------
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
//---------------------
// Functions prototypes
//---------------------
void imGuiSetup();
//---------------------------------
// Variables & objects declarations
//---------------------------------
bool guiIsOpen = true;
GLfloat WIDTH = 512.f;
GLfloat HEIGHT = 512.f;
GLfloat zNear = 0.1f;
GLfloat zFar = 100.f;
glm::mat4 view = glm::lookAt(glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.f, 0.f, -1.f), glm::vec3(0.f, 1.f, 0.f));
glm::mat4 projection = glm::perspective(glm::radians(90.0f), WIDTH / HEIGHT, zNear, zFar);
auto worldToCamera = projection * view;
auto cameraToWorld = glm::inverse(worldToCamera);
glm::vec3 screenSpacePos = glm::vec3(WIDTH/2.f, HEIGHT/2.f, 0.f);
GLfloat quadVertices[] = {
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
};
int main(int argc, char* argv[])
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWmonitor* glfwMonitor = glfwGetPrimaryMonitor();
const GLFWvidmode* glfwMode = glfwGetVideoMode(glfwMonitor);
glfwWindowHint(GLFW_RED_BITS, glfwMode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, glfwMode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, glfwMode->blueBits);
glfwWindowHint(GLFW_REFRESH_RATE, glfwMode->refreshRate);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "OpenGL Perspective Test", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSwapInterval(1);
gladLoadGL();
glViewport(0, 0, WIDTH, HEIGHT);
ImGui_ImplGlfwGL3_Init(window, true);
glfwSetKeyCallback(window, key_callback);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Shader shader;
//Shape render;
shader.setShader("simple.vert", "simple.frag");
shader.useShader();
GLuint shapeVAO, shapeVBO;
glGenVertexArrays(1, &shapeVAO);
glGenBuffers(1, &shapeVBO);
glBindBuffer(GL_ARRAY_BUFFER, shapeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), quadVertices, GL_STATIC_DRAW);
glBindVertexArray(shapeVAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glBindVertexArray(0);
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniform4fv(glGetUniformLocation(shader.Program, "lightColor"), 1, glm::value_ptr(glm::vec4(0.f, 1.f, 0.f, 1.f)));
do {
glm::vec3 normScreenspace = glm::vec3(
screenSpacePos.x / WIDTH * 2.f - 1.f,
screenSpacePos.y / HEIGHT * 2.f - 1.f,
screenSpacePos.z
);
// screen space to word space
auto worldPos = glm::vec4(normScreenspace.x, normScreenspace.y, normScreenspace.z, 1.f) * cameraToWorld;
// reverse perspective division
worldPos.w = 1.0 / worldPos.w;
worldPos.x *= worldPos.w;
worldPos.y *= worldPos.w;
worldPos.z *= worldPos.w;
glClear(GL_COLOR_BUFFER_BIT);
imGuiSetup();
shader.useShader();
glm::mat4 model = glm::translate(glm::mat4(1.f), glm::vec3(worldPos));
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
// draw quad
glBindVertexArray(shapeVAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
ImGui::Render();
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
} // Check if the ESC key was pressed or the window was closed
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
//---------
// Cleaning
//---------
ImGui_ImplGlfwGL3_Shutdown();
glfwTerminate();
return 0;
}
void imGuiSetup()
{
ImGui_ImplGlfwGL3_NewFrame();
ImGui::Begin("GLEngine", &guiIsOpen, ImVec2(0, 0), 0.5f, ImGuiWindowFlags_AlwaysUseWindowPadding | ImGuiWindowFlags_NoSavedSettings);
ImGui::SetWindowSize(ImVec2(350, HEIGHT));
if (ImGui::CollapsingHeader("Rendering", 0, true, true))
{
ImGui::SliderFloat("x (screen)", (float*)&screenSpacePos.x, 0, WIDTH);
ImGui::SliderFloat("y (screen)", (float*)&screenSpacePos.y, 0, HEIGHT);
ImGui::SliderFloat("z", (float*)&screenSpacePos.z, 0.f, zFar);
}
if (ImGui::CollapsingHeader("Application Info", 0, true, true))
{
char* glInfos = (char*)glGetString(GL_VERSION);
char* hardwareInfos = (char*)glGetString(GL_RENDERER);
ImGui::Text("OpenGL Version :");
ImGui::Text(glInfos);
ImGui::Text("Hardware Informations :");
ImGui::Text(hardwareInfos);
ImGui::Text("\nFramerate %.2f FPS / Frametime %.4f ms", ImGui::GetIO().Framerate, 1000.0f / ImGui::GetIO().Framerate);
}
ImGui::End();
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}