185 lines
4.6 KiB
C++
185 lines
4.6 KiB
C++
|
#include <iostream>
|
||
|
|
||
|
#define GL_GLEXT_PROTOTYPES
|
||
|
|
||
|
#include <GLFW/glfw3.h>
|
||
|
#include <GL/gl.h>
|
||
|
#include <GL/glcorearb.h>
|
||
|
#include "gl/Shader.h"
|
||
|
#include "gl/IndexBuffer.h"
|
||
|
#include "gl/VertexArray.h"
|
||
|
#include "gl/Renderer.h"
|
||
|
#include "blocks/GrasBlock.h"
|
||
|
#include "blocks/BlockRenderer.h"
|
||
|
#include "gl/Camera.h"
|
||
|
#include <cstdlib>
|
||
|
|
||
|
#include <glm/gtc/matrix_transform.hpp>
|
||
|
|
||
|
//#define WIREFRAME
|
||
|
|
||
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
||
|
|
||
|
void processInput(GLFWwindow *window);
|
||
|
|
||
|
int width = 1920;
|
||
|
int height = 1080;
|
||
|
|
||
|
Camera cam(width, height);
|
||
|
|
||
|
double oldx = 0;
|
||
|
double oldy = 0;
|
||
|
|
||
|
bool menuopen = false;
|
||
|
|
||
|
void cursor_position_callback(GLFWwindow *window, double xpos, double ypos) {
|
||
|
if (menuopen)return;
|
||
|
|
||
|
double xdiff = oldx - xpos;
|
||
|
oldx = xpos;
|
||
|
|
||
|
double ydiff = oldy - ypos;
|
||
|
oldy = ypos;
|
||
|
|
||
|
cam.addRotaion(xdiff, ydiff);
|
||
|
}
|
||
|
|
||
|
GLFWwindow *initWindow() {
|
||
|
|
||
|
glfwInit();
|
||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||
|
|
||
|
// create new window
|
||
|
GLFWwindow *window = glfwCreateWindow(width, height, "LearnOpenGL", NULL, NULL);
|
||
|
if (window == NULL) {
|
||
|
std::cout << "Failed to create GLFW window" << std::endl;
|
||
|
glfwTerminate();
|
||
|
exit(-1);
|
||
|
}
|
||
|
glfwMakeContextCurrent(window);
|
||
|
|
||
|
// limit frame rate
|
||
|
glfwSwapInterval(1);
|
||
|
|
||
|
// set window viewport
|
||
|
glViewport(0, 0, width, height);
|
||
|
|
||
|
// callback when window was resized
|
||
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||
|
|
||
|
|
||
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||
|
|
||
|
|
||
|
glfwSetCursorPosCallback(window, cursor_position_callback);
|
||
|
|
||
|
if (glfwRawMouseMotionSupported())
|
||
|
glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
|
||
|
|
||
|
|
||
|
std::cout << "[GLFW] " << glfwGetVersionString() << std::endl;
|
||
|
std::cout << "[GL] " << glGetString(GL_VERSION) << std::endl;
|
||
|
|
||
|
return window;
|
||
|
}
|
||
|
|
||
|
|
||
|
int main() {
|
||
|
GLFWwindow *window = initWindow();
|
||
|
|
||
|
#ifdef WIREFRAME
|
||
|
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
|
||
|
#endif
|
||
|
|
||
|
glEnable(GL_DEPTH_TEST);
|
||
|
|
||
|
GrasBlock gb(0, 0, 0);
|
||
|
BaseBlock bb(0.0f, (float) rand() / RAND_MAX, 1.0f, 1, 0, 0);
|
||
|
|
||
|
// BlockRenderer::getInstance()->init();
|
||
|
|
||
|
std::vector<BaseBlock> blocks;
|
||
|
|
||
|
blocks.push_back(gb);
|
||
|
blocks.push_back(bb);
|
||
|
blocks.emplace_back(1.0f, .0f, 1.0f, 0, 0, 1);
|
||
|
|
||
|
for (int i = 0; i < 5; i++) {
|
||
|
blocks.push_back(GrasBlock(i, i, 0));
|
||
|
}
|
||
|
|
||
|
cam.setPos(0, 0, 0);
|
||
|
cam.setRotation(0,0);
|
||
|
|
||
|
while (!glfwWindowShouldClose(window)) {
|
||
|
// process user input events
|
||
|
processInput(window);
|
||
|
|
||
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||
|
|
||
|
for (auto b: blocks) {
|
||
|
b.render();
|
||
|
}
|
||
|
|
||
|
// swap buffer to calc and show
|
||
|
glfwSwapBuffers(window);
|
||
|
// poll for and execute events
|
||
|
glfwPollEvents();
|
||
|
}
|
||
|
|
||
|
// glDeleteProgram(shaderProgram);
|
||
|
BlockRenderer::getInstance()->deinit();
|
||
|
glfwTerminate();
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
// callback when user resizes image
|
||
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
||
|
glViewport(0, 0, width, height);
|
||
|
cam.setWindowSize(width, height);
|
||
|
}
|
||
|
|
||
|
void processInput(GLFWwindow *window) {
|
||
|
// 45c view range
|
||
|
double viewrange = M_PI / 8;
|
||
|
|
||
|
if (!menuopen) {
|
||
|
|
||
|
if (glfwGetKey(window, GLFW_KEY_W)) {
|
||
|
cam.addPos(-sin(cam.getxangle() - viewrange) * 0.1, cos(cam.getxangle() + viewrange) * 0.1, 0.0);
|
||
|
}
|
||
|
|
||
|
if (glfwGetKey(window, GLFW_KEY_S)) {
|
||
|
cam.addPos(sin(cam.getxangle() + viewrange) * 0.1, -cos(cam.getxangle() - viewrange) * 0.1, 0.0);
|
||
|
}
|
||
|
|
||
|
if (glfwGetKey(window, GLFW_KEY_D)) {
|
||
|
cam.addPos(sin(cam.getxangle() - M_PI / 2) * 0.1, -cos(cam.getxangle() - M_PI / 2) * 0.1, 0.0);
|
||
|
}
|
||
|
|
||
|
if (glfwGetKey(window, GLFW_KEY_A)) {
|
||
|
cam.addPos(sin(cam.getxangle() + M_PI / 2) * 0.1, -cos(cam.getxangle() + M_PI / 2) * 0.1, 0.0);
|
||
|
}
|
||
|
|
||
|
if (glfwGetKey(window, GLFW_KEY_SPACE)) {
|
||
|
cam.addPos(0.0,0.0,0.1);
|
||
|
}
|
||
|
|
||
|
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT)) {
|
||
|
cam.addPos(0.0,0.0,-0.1);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||
|
menuopen = !menuopen;
|
||
|
|
||
|
glfwSetInputMode(window, GLFW_CURSOR, menuopen ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_DISABLED);
|
||
|
|
||
|
// reset old cursor pos to new one to avoid a jump
|
||
|
glfwGetCursorPos(window, &oldx, &oldy);
|
||
|
}
|
||
|
// glfwSetWindowShouldClose(window, true);
|
||
|
}
|