190 lines
4.3 KiB
C++
190 lines
4.3 KiB
C++
#include <iostream>
|
|
#include <cmath>
|
|
|
|
#define GLEW_STATIC
|
|
#include <GL/glew.h>
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include <vector>
|
|
#include <ctime>
|
|
|
|
// GLM
|
|
#include <glm/glm.hpp>
|
|
#include <glm/ext.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include "window/Window.h"
|
|
#include "window/Camera.h"
|
|
#include "window/Events.h"
|
|
#include "graphics/Mesh.h"
|
|
#include "graphics/Shader.h"
|
|
#include "graphics/MeshLoader.h"
|
|
|
|
|
|
#define SPEED_FACTOR 2.5f
|
|
|
|
|
|
static void updateCameraPosition(Camera& cam, float delta) {
|
|
static float camX = 0, camY= 0;
|
|
|
|
if (Events::_cursor_locked){
|
|
camY += -Events::deltaY / (float)Window::height * 2;
|
|
camX += -Events::deltaX / (float)Window::height * 2;
|
|
|
|
if (camY < -glm::radians(89.0f)){
|
|
camY = -glm::radians(89.0f);
|
|
}
|
|
if (camY > glm::radians(89.0f)){
|
|
camY = glm::radians(89.0f);
|
|
}
|
|
|
|
cam.rotation = glm::mat4(1.0f);
|
|
cam.rotate(camY, camX, 0);
|
|
}
|
|
|
|
cam.updateVectors();
|
|
|
|
glm::vec3 dir(0,0,0);
|
|
if (Events::pressed(GLFW_KEY_W)){
|
|
dir.x += cam.dir.x;
|
|
dir.z += cam.dir.z;
|
|
}
|
|
if (Events::pressed(GLFW_KEY_S)){
|
|
dir.x -= cam.dir.x;
|
|
dir.z -= cam.dir.z;
|
|
}
|
|
if (Events::pressed(GLFW_KEY_D)){
|
|
dir.x += cam.right.x;
|
|
dir.z += cam.right.z;
|
|
}
|
|
if (Events::pressed(GLFW_KEY_A)){
|
|
dir.x -= cam.right.x;
|
|
dir.z -= cam.right.z;
|
|
}
|
|
|
|
if (Events::pressed(GLFW_KEY_SPACE)) {
|
|
dir.y = 1;
|
|
}
|
|
|
|
if (Events::pressed(GLFW_KEY_LEFT_SHIFT)) {
|
|
dir.y = -1;
|
|
}
|
|
|
|
if (dir.x != 0 || dir.y != 0 || dir.z != 0) {
|
|
dir = glm::normalize(dir);
|
|
}
|
|
|
|
cam.position += dir * delta * SPEED_FACTOR;
|
|
}
|
|
|
|
|
|
static Mesh* world;
|
|
static Mesh* model_mesh;
|
|
static Shader* shader;
|
|
static void drawWorld(Camera& cam) {
|
|
auto projview = cam.getProjection() * cam.getView();
|
|
shader->use();
|
|
|
|
// shader->uniformMatrix("pr", cam.getProjection() * cam.getView());
|
|
shader->uniformMatrix("projview", projview);
|
|
world->draw();
|
|
|
|
auto model_scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.5f, 0.5f, 0.5f));
|
|
|
|
static float angle = 0.0f;
|
|
|
|
auto model_translate = glm::translate(glm::mat4(1.0f), glm::vec3(0, 0, -2));
|
|
auto model_rotate = glm::rotate(glm::mat4(1.0f), angle, glm::vec3(0, 1, 0));
|
|
angle += 0.001f;
|
|
|
|
shader->uniformMatrix("projview", projview * model_scale * model_translate * model_rotate);
|
|
// world->draw();
|
|
model_mesh->draw();
|
|
}
|
|
|
|
|
|
void mainloop(Camera& camera) {
|
|
long frame = 0;
|
|
float lastTime = Window::getTime();
|
|
float delta;
|
|
|
|
bool devdata = false;
|
|
Window::swapInterval(0);
|
|
while (!Window::isShouldClose()){
|
|
frame++;
|
|
float currentTime = Window::getTime();
|
|
delta = currentTime - lastTime;
|
|
lastTime = currentTime;
|
|
|
|
if (Events::jpressed(GLFW_KEY_ESCAPE)){
|
|
Window::setShouldClose(true);
|
|
}
|
|
if (Events::jpressed(GLFW_KEY_TAB)){
|
|
Events::toggleCursor();
|
|
}
|
|
if (Events::jpressed(GLFW_KEY_F3)){
|
|
devdata = !devdata;
|
|
}
|
|
|
|
updateCameraPosition(camera, delta);
|
|
|
|
drawWorld(camera);
|
|
|
|
// worldRenderer.draw(world, camera, occlusion);
|
|
// hud.draw(level, assets);
|
|
// if (devdata) {
|
|
// hud.drawDebug(level, assets, fps, occlusion);
|
|
// }
|
|
|
|
Window::swapBuffers();
|
|
Events::pullEvents();
|
|
}
|
|
}
|
|
|
|
void loadResources() {
|
|
const float buffer[] = {
|
|
// треугольник
|
|
// X Y Z nX nY nZ
|
|
-1, -1, 0.5, 0, 0, 1,
|
|
1, -1, 0.5, 0, 0, 1,
|
|
0, 1, 0.5, 0, 0, 1,
|
|
|
|
-1, -1, -0.5, 0, 0, -1,
|
|
1, -1, -0.5, 0, 0, -1,
|
|
0, 1, -0.5, 0, 0, -1,
|
|
};
|
|
const int mesh_attrs[] {
|
|
3, 3, 0
|
|
};
|
|
world = new Mesh(buffer, 6, mesh_attrs);
|
|
|
|
model_mesh = MeshLoader::loadMesh("res/cube");
|
|
|
|
shader = Shader::loadShader("res/main.vsh", "res/main.fsh");
|
|
}
|
|
|
|
void unloadResources() {
|
|
delete world;
|
|
delete shader;
|
|
}
|
|
|
|
int main() {
|
|
Window::initialize(800, 600, "Lab2");
|
|
Events::initialize();
|
|
|
|
loadResources();
|
|
|
|
// glDisable(GL_DEPTH_TEST);
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
Camera cam(glm::vec3(0, 0, 5), glm::radians(90.0f));
|
|
|
|
mainloop(cam);
|
|
|
|
unloadResources();
|
|
|
|
Events::finalize();
|
|
Window::terminate();
|
|
return 0;
|
|
}
|