-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWindow.cpp
209 lines (179 loc) · 3.94 KB
/
Window.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
#include "Window.h"
Window::Window()
{
width = 800;
height = 600;
for (size_t i = 0; i < 1024; i++)
{
keys[i] = 0;
}
}
Window::Window(GLint windowWidth, GLint windowHeight)
{
width = windowWidth;
height = windowHeight;
muevex = 0.0f;
muevez = 0.0f;
mueveHelix = 30.0f;
mueveHeliy = 5.0f;
mueveHeliz = -65.0f;
apaga = 1.0f;
flag = 1.0f;
for (size_t i = 0; i < 1024; i++)
{
keys[i] = 0;
}
}
int Window::Initialise()
{
//Inicializaci�n de GLFW
if (!glfwInit())
{
printf("Fall� inicializar GLFW");
glfwTerminate();
return 1;
}
//Asignando variables de GLFW y propiedades de ventana
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
//para solo usar el core profile de OpenGL y no tener retrocompatibilidad
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
//CREAR VENTANA
mainWindow = glfwCreateWindow(width, height, "Primer ventana", NULL, NULL);
if (!mainWindow)
{
printf("Fallo en crearse la ventana con GLFW");
glfwTerminate();
return 1;
}
//Obtener tama�o de Buffer
glfwGetFramebufferSize(mainWindow, &bufferWidth, &bufferHeight);
//asignar el contexto
glfwMakeContextCurrent(mainWindow);
//MANEJAR TECLADO y MOUSE
createCallbacks();
//permitir nuevas extensiones
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
printf("Fall� inicializaci�n de GLEW");
glfwDestroyWindow(mainWindow);
glfwTerminate();
return 1;
}
glEnable(GL_DEPTH_TEST); //HABILITAR BUFFER DE PROFUNDIDAD
// Asignar valores de la ventana y coordenadas
//Asignar Viewport
glViewport(0, 0, bufferWidth, bufferHeight);
//Callback para detectar que se est� usando la ventana
glfwSetWindowUserPointer(mainWindow, this);
}
void Window::createCallbacks()
{
glfwSetKeyCallback(mainWindow, ManejaTeclado);
glfwSetCursorPosCallback(mainWindow, ManejaMouse);
}
GLfloat Window::getXChange()
{
GLfloat theChange = xChange;
xChange = 0.0f;
return theChange;
}
GLfloat Window::getYChange()
{
GLfloat theChange = yChange;
yChange = 0.0f;
return theChange;
}
void Window::ManejaTeclado(GLFWwindow* window, int key, int code, int action, int mode)
{
Window* theWindow = static_cast<Window*>(glfwGetWindowUserPointer(window));
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (key == GLFW_KEY_Y)
{
theWindow-> muevex += 1.0;
theWindow->flag = 1.0;
}
if (key == GLFW_KEY_U)
{
theWindow-> muevex -= 1.0;
theWindow->flag = 0.0;
}
if (key == GLFW_KEY_I)
{
theWindow->muevez += 1.0;
}
if (key == GLFW_KEY_O)
{
theWindow->muevez -= 1.0;
}
if (key == GLFW_KEY_G)
{
theWindow->mueveHelix += 1.0;
}
if (key == GLFW_KEY_H)
{
theWindow->mueveHelix -= 1.0;
}
if (key == GLFW_KEY_J)
{
theWindow->mueveHeliy += 1.0;
}
if (key == GLFW_KEY_K)
{
theWindow->mueveHeliy -= 1.0;
}
if (key == GLFW_KEY_Z)
{
theWindow->mueveHeliz += 1.0;
}
if (key == GLFW_KEY_X)
{
theWindow->mueveHeliz -= 1.0;
}
if (key == GLFW_KEY_M && action == GLFW_PRESS)
{
if (theWindow->apaga == 1.0) {
theWindow->apaga = 0.0;
}
else {
theWindow->apaga = 1.0;
}
}
if (key >= 0 && key < 1024)
{
if (action == GLFW_PRESS)
{
theWindow->keys[key] = true;
printf("se presiono la tecla %d'\n", key);
}
else if (action == GLFW_RELEASE)
{
theWindow->keys[key] = false;
printf("se solto la tecla %d'\n", key);
}
}
}
void Window::ManejaMouse(GLFWwindow* window, double xPos, double yPos)
{
Window* theWindow = static_cast<Window*>(glfwGetWindowUserPointer(window));
if (theWindow->mouseFirstMoved)
{
theWindow->lastX = xPos;
theWindow->lastY = yPos;
theWindow->mouseFirstMoved = false;
}
theWindow->xChange = xPos - theWindow->lastX;
theWindow->yChange = theWindow->lastY - yPos;
theWindow->lastX = xPos;
theWindow->lastY = yPos;
}
Window::~Window()
{
glfwDestroyWindow(mainWindow);
glfwTerminate();
}