-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowManager.cpp
197 lines (154 loc) · 7.45 KB
/
WindowManager.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
#include "WindowManager.hpp"
WindowManager::WindowManager(int width, int height, const char* windowName) :
p_width(width), p_height(height), p_windowName(windowName)
{
//Initialiaze OpenGL
initialize();
}
WindowManager::~WindowManager()
{
}
void WindowManager::initialize() {
glfwInit() ;
glfwInitHint(GLFW_VERSION_MINOR, 3);
glfwInitHint(GLFW_VERSION_MAJOR, 3);
glfwInitHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}
void WindowManager::GLFWindowGeneration() {
// window creation
p_window = glfwCreateWindow( p_width, p_height, p_windowName, NULL, NULL );
if (p_window == NULL){
std::cout <<"WINDOWMANAGER::BUILD::WINDOW::FAILED\n";
std::cout <<"WINDOWMANAGER::RETURN::VOID\n" ;
glfwTerminate();
return ;
}
glfwMakeContextCurrent(p_window) ;
// load the OpenGL Function through GLAD, wthout this, function like glClear does not work
int error = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress );
if (!error){
std::cout <<"WINDOWMANAGER::BUILD::GLAD::FAILED\n";
std::cout <<"WINDOWMANAGER::RETURN::VOID\n" ;
glfwTerminate();
return ;
}
}
void WindowManager::simulation(){
renderer();
}
void WindowManager:: renderer(){
[[maybe_unused]]float vertices[] = {
// positions // colors // texture coords
1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
[[maybe_unused]]unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
// compile the shader prgram
Shader programShader("./texture.vs", "./texture.fs") ;
// Creating an ID
glGenVertexArrays(1,&_vao) ;
glGenBuffers(1, &_vbo) ;
glGenBuffers(1, &_ebo) ;
// binding VAO
glBindVertexArray(_vao);
//bind and send data VBO
glBindBuffer(GL_ARRAY_BUFFER, _vbo) ;
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW );
//bind and send data EBO
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, 8*sizeof(float), (void*)(0*sizeof(float)));
glEnableVertexAttribArray(VertexAttributes::vao_position);
//color attribute
glVertexAttribPointer(1, 3,GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)(3*sizeof(float)) );
glEnableVertexAttribArray(VertexAttributes::vao_color);
//texture attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(VertexAttributes::vao_texture);
// load and create a texture
// -------------------------
glGenTextures(1, &_texture);
glBindTexture(GL_TEXTURE_2D, _texture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
p_kernelLauncher = new KernelLauncher(p_width, p_height) ;
drawing(p_window, programShader);
if(p_kernelLauncher) delete p_kernelLauncher ;
p_kernelLauncher = nullptr ;
}
void WindowManager::createFlashlightTexture(uchar *flashlightTexture) {
int textureSize = p_width; // Adjust the size as needed
float centerX = textureSize / 2.0f;
float centerY = textureSize / 2.0f;
float radius = textureSize / 4.0f; // Adjust the radius as needed
for (int y = 0; y < textureSize; y++) {
for (int x = 0; x < textureSize; x++) {
float distance = sqrt((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY));
int index = (y * textureSize + x) * 4; // Calculate the index for RGBA components
if (distance < radius) {
// Inside the circle, set RGB to white (fully lit) and A to 255 (fully opaque)
flashlightTexture[index] = 255; // Red (R)
flashlightTexture[index + 1] = 255; // Green (G)
flashlightTexture[index + 2] = 255; // Blue (B)
flashlightTexture[index + 3] = 255; // Alpha (A)
} else {
// Outside the circle, set RGB to black (no light) and A to 0 (fully transparent)
flashlightTexture[index] = 0; // Red (R)
flashlightTexture[index + 1] = 0; // Green (G)
flashlightTexture[index + 2] = 0; // Blue (B)
flashlightTexture[index + 3] = 0; // Alpha (A)
}
}
}
}
void WindowManager::drawing(GLFWwindow * window, Shader& program){
uchar*data_host = new uchar[4*p_height*p_width*sizeof(uchar)];
createFlashlightTexture(data_host);
GLuint mouseLocation = glGetUniformLocation(program.id, "mouse");
GLuint windowResolution = glGetUniformLocation(program.id, "window");
GLuint RadiusLocation = glGetUniformLocation(program.id, "flashlightRadius");
while (!glfwWindowShouldClose(window))
{
p_kernelLauncher->Launcher(data_host, p_kernelLauncher->mouse.x,p_kernelLauncher->mouse.y);
if (data_host){
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, p_width, p_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data_host);
glGenerateMipmap(GL_TEXTURE_2D);
}
else{
std::cout << "Failed to load texture" << std::endl;
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f) ;
glClear(GL_COLOR_BUFFER_BIT) ;
glfwGetCursorPos(window, &p_kernelLauncher->mouse.x, &p_kernelLauncher->mouse.y ) ;
p_kernelLauncher->mouse.x = (p_kernelLauncher->mouse.x/p_width)*2.0f- 1 ;
p_kernelLauncher->mouse.x = p_kernelLauncher->mouse.x > 1.0f ? 1.0f :p_kernelLauncher->mouse.x < -1.0f ? -1.0f:p_kernelLauncher->mouse.x ;
p_kernelLauncher->mouse.y = ((p_height- p_kernelLauncher->mouse.y)/p_height)*2- 1 ;
p_kernelLauncher->mouse.y = p_kernelLauncher->mouse.y > 1.0f ? 1.0f : p_kernelLauncher->mouse.y < -1.0f ? -1.0f:p_kernelLauncher->mouse.y ;
if (p_kernelLauncher->mouse.x ==1 or p_kernelLauncher->mouse.x == 0 or p_kernelLauncher->mouse.y ==1 or p_kernelLauncher->mouse.y == 1) ;
else glUniform2f(mouseLocation, p_kernelLauncher->mouse.x, p_kernelLauncher->mouse.y) ;
glUniform2f(windowResolution, p_width, p_height) ;
glUniform1f(RadiusLocation,p_radius);
glBindTexture(GL_TEXTURE_2D, _texture);
program.use() ;
glBindVertexArray(_vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glfwSwapBuffers(window);
glfwPollEvents() ;
}
if (data_host) {
delete[] data_host;
data_host = nullptr;
}
glfwTerminate() ;
}