-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathworld3DPlus.cpp
More file actions
222 lines (169 loc) · 5.74 KB
/
world3DPlus.cpp
File metadata and controls
222 lines (169 loc) · 5.74 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
216
217
218
219
220
221
222
// Transformation.cpp: define el punto de entrada de la aplicación de consola.
//
#include "stdafx.h"
#include <iostream>
#include <string>
//GLEW
#include <GL/glew.h>
//GLFW
#include <GLFW/glfw3.h>
//Other libs
#include <SOIL2/SOIL2.h>
//GLM Mathematics
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
//Other includes
#include "Shader.h"
#include "Model.h"
#include "Camera.h"
//Define window dimension width and height
const GLint WIDTH = 800, HEIGHT = 600;
int SCREEN_WIDTH, SCREEN_HEIGHT;
void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mode);
void MouseCallback(GLFWwindow *window, double xPos, double yPos);
void DoMovement();
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
GLfloat lastX = WIDTH / 2.0f;
GLfloat lastY = WIDTH / 2.0f;
bool keys[1024];
bool firstMouse = true;
GLfloat deltaTime = 0.0f;
GLfloat lastFrame = 0.0f;
void configWindow()
{
//Parameters of the Windows GLFW. Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLU_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GLU_FALSE);
}
int main()
{
//================================================= TEMPLATE =============================================================
//Initialize GLFW
glfwInit();
configWindow();
//Create a GLFW window that we can use for GLFW's functions
GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Mundo", nullptr, nullptr);
int screenWidth, screenHeight;
glfwGetFramebufferSize(window, &SCREEN_WIDTH, &SCREEN_HEIGHT);
//In case of error or we can't creating the window the script gives terminating GLFW
if (nullptr == window)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return EXIT_FAILURE;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, KeyCallback);
glfwSetCursorPosCallback(window, MouseCallback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
glewExperimental = GL_TRUE;
//Initialize GLEW to setup OpenGL Function pointers
if (GLEW_OK != glewInit())
{
std::cout << "Failed to initialize GLEW" << std::endl;
return EXIT_FAILURE;
}
//Define the viewport dimensions
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
//Enable alpha support
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
// Setup and compile our shaders
Shader shader("res/shaders/modelLoading.vs", "res/shaders/modelLoading.frag");
// Load models
Model Model("res/models/obj_Grass/untitled.obj");
// Draw in wireframe
//glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
//Create a projection matrix
//glm::mat4 projection = glm::perspective(camera.GetZoom(), (GLfloat)SCREEN_WIDTH / (GLfloat)SCREEN_HEIGHT, 0.1f, 1000.0f);
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (GLfloat)SCREEN_WIDTH / (GLfloat)SCREEN_HEIGHT, 0.1f, 1000.0f);
//Game Loop
while (!glfwWindowShouldClose(window))
{
//lightPos.x -= 0.005f;
//lightPos.z -= 0.005f;
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents();
DoMovement();
//Render
//Clear color buffer
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader.use();
//glm::mat4 view = camera.GetviewMatrix();
glm::mat4 view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
glUniformMatrix4fv(glGetUniformLocation(shader.ID, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(shader.ID, "view"), 1, GL_FALSE, glm::value_ptr(view));
// Draw the loaded model
glm::mat4 model;
model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f)); // Translate it down a bit so it's at the center of the scene
model = glm::scale(model, glm::vec3(0.008f, 0.008f, 0.008f)); // It's a bit too big for our scene, so scale it down
glUniformMatrix4fv(glGetUniformLocation(shader.ID, "model"), 1, GL_FALSE, glm::value_ptr(model));
Model.Draw(shader);
//Swap screen buffers
glfwSwapBuffers(window);
}
// Terminate GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
return EXIT_SUCCESS;
}
void DoMovement()
{
if (keys[GLFW_KEY_W] || keys[GLFW_KEY_UP])
{
camera.ProcessKeyboard(FORWARD, deltaTime);
};
if (keys[GLFW_KEY_S] || keys[GLFW_KEY_DOWN])
{
camera.ProcessKeyboard(BACKWARD, deltaTime);
};
if (keys[GLFW_KEY_A] || keys[GLFW_KEY_LEFT])
{
camera.ProcessKeyboard(LEFT, deltaTime);
};
if (keys[GLFW_KEY_D] || keys[GLFW_KEY_RIGHT])
{
camera.ProcessKeyboard(RIGHT, deltaTime);
};
};
void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mode)
{
if (GLFW_KEY_ESCAPE == key && GLFW_PRESS == action)
{
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (key >= 0 && key < 1024)
{
if (GLFW_PRESS == action)
{
keys[key] = true;
}
else if (GLFW_RELEASE == action)
{
keys[key] = false;
}
}
};
void MouseCallback(GLFWwindow *window, double xPos, double yPos)
{
if (firstMouse)
{
lastX = xPos;
lastY = yPos;
firstMouse = false;
}
GLfloat xOffset = xPos - lastX;
GLfloat yOffset = lastY - yPos;
lastX = xPos;
lastY = yPos;
camera.ProcessMouseMovement(xOffset, yOffset);
};