-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
215 lines (169 loc) · 7.37 KB
/
main.cpp
File metadata and controls
215 lines (169 loc) · 7.37 KB
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
#include <iostream>
#include <string>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <cstdlib>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#include "shader.h"
#include "utils.h"
#include "camera.h"
#include "mover.h"
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
const int N_SAMPLES = 2;
float vertices[] = {
// positions // texture coords
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // top right
1.0f, -1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, // bottom left
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
unsigned int VBO, VAO, EBO;
void initGLDataObject(){
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
}
void initSceneTexture(){
unsigned int specular, diffuse, ambient;
unsigned int tex_units[3][2] = {GL_TEXTURE0, GL_TEXTURE1,
GL_TEXTURE2, GL_TEXTURE3,
GL_TEXTURE4, GL_TEXTURE5};
for (int i = 0; i < N_SAMPLES; i++){
specular = genTexture(SCR_WIDTH, SCR_HEIGHT, tex_units[0][i]);
diffuse = genTexture(SCR_WIDTH, SCR_HEIGHT, tex_units[1][i]);
ambient = genTexture(SCR_WIDTH, SCR_HEIGHT, tex_units[2][i]);
glBindImageTexture(i, specular, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
glBindImageTexture(i + N_SAMPLES, diffuse, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
glBindImageTexture(i + N_SAMPLES * 2, ambient, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
}
}
void initObjectTexture(){
genTexturefromPath("textures/sun.jpg", GL_TEXTURE6);
genTexturefromPath("textures/mercury.jpg", GL_TEXTURE7);
genTexturefromPath("textures/venus.jpg", GL_TEXTURE8);
genTexturefromPath("textures/earth.jpg", GL_TEXTURE9);
genTexturefromPath("textures/mars.jpg", GL_TEXTURE10);
genTexturefromPath("textures/jupiter.jpg", GL_TEXTURE11);
genTexturefromPath("textures/saturn.jpg", GL_TEXTURE12);
genTexturefromPath("textures/uranus.jpg", GL_TEXTURE13);
genTexturefromPath("textures/neptune.jpg", GL_TEXTURE14);
}
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
GLenum err = glewInit();
std::cout << "ERR:" << err << std::endl;
Shader renderShader("./raytracer.vert", "./raytracer.frag");
Shader computeShader("./raytracer.comp");
initGLDataObject();
// load and create a texture
// -------------------------
// unsigned int specular, diffuse, ambient;
initSceneTexture();
std::cout << "DONE INIT RT SCENE TEXTURE" << std::endl;
initObjectTexture();
std::cout << "DONE INIT OBJECT TEXTURE" << std::endl;
Mover mover;
mover.addSphere("sphere0", 0, randomIn(0.04, 0.05), 0, 0);
mover.addSphere("sphere1", 0.1, randomIn(0.02, 0.04), randomIn(10, 15), randomIn(10, 20));
mover.addSphere("sphere2", 0.2, randomIn(0.02, 0.04), randomIn(10, 15), randomIn(10, 20));
mover.addSphere("sphere3", 0.3, randomIn(0.02, 0.04), randomIn(10, 15), randomIn(10, 20));
// mover.addSphere("sphere4", 0.4, randomIn(0.02, 0.04), randomIn(10, 15), randomIn(10, 20));
// mover.addSphere("sphere5", 0.5, randomIn(0.02, 0.04), randomIn(10, 15), randomIn(10, 20));
// mover.addSphere("sphere6", 0.6, randomIn(0.02, 0.04), randomIn(10, 15), randomIn(10, 20));
// mover.addSphere("sphere7", 0.7, randomIn(0.02, 0.04), randomIn(10, 15), randomIn(10, 20));
// mover.addSphere("sphere8", 0.8, randomIn(0.02, 0.04), randomIn(10, 15), randomIn(10, 20));
for (int i = 0; i < 5; i++){
std::cout << random01() << std::endl;
}
Camera camera(glm::vec3(0.0, 0.0, 0.5),
glm::vec3(0.0, 0.0, -0.5),
glm::vec3(0.0, 1.0, 0.0),
window=window,
0.2, 20);
std::cout << "Start rendering" << std::endl;
double tic = glfwGetTime();
while (!glfwWindowShouldClose(window))
{
double toc = glfwGetTime();
std::string title = std::string("FPS: ") + double2str(1 / (toc - tic));
glfwSetWindowTitle(window, title.c_str());
tic = toc;
// input
// -----
processInput(window);
camera.update();
// render
// ------
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
computeShader.use();
computeShader.setUniVec3("cam_origin", camera.origin());
computeShader.setUniVec3("cam_lookat", camera.lookat());
computeShader.setUniVec3("cam_lookup", camera.lookup());
for (int i = 0; i < mover.getNum(); i++){
glm::vec4 vec;
std::string name = mover.getSphereVec4(i, vec);
computeShader.setUniVec4(name, vec);
}
mover.update();
glDispatchCompute(SCR_WIDTH, SCR_HEIGHT, 1);
// render container
renderShader.use();
renderShader.setUniInt("tex_specular[0]", 0);
renderShader.setUniInt("tex_diffuse[0]", 2);
renderShader.setUniInt("tex_ambient[0]", 4);
renderShader.setUniInt("tex_specular[1]", 1);
renderShader.setUniInt("tex_diffuse[1]", 3);
renderShader.setUniInt("tex_ambient[1]", 5);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}