210 lines
5.4 KiB
C++
210 lines
5.4 KiB
C++
#include <iostream>
|
|
|
|
#define GL_GLEXT_PROTOTYPES
|
|
|
|
#include <GLFW/glfw3.h>
|
|
#include <GL/glcorearb.h>
|
|
#include "gl/Shader.h"
|
|
#include "gl/IndexBuffer.h"
|
|
#include "gl/VertexArray.h"
|
|
#include "gl/Renderer.h"
|
|
#include "blocks/DirtBlock.h"
|
|
#include "blocks/BlockRenderer.h"
|
|
#include "gl/Camera.h"
|
|
#include "blocks/Stoneblock.h"
|
|
#include "crosshair/CrossHair.h"
|
|
#include "blocks/AirBlock.h"
|
|
#include <cstdlib>
|
|
|
|
//#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;
|
|
std::vector<BaseBlock*> blocks;
|
|
|
|
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);
|
|
}
|
|
|
|
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
|
|
{
|
|
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
|
|
{
|
|
double xpos, ypos;
|
|
cam.getXpos();
|
|
|
|
blocks.erase(blocks.begin()+1);
|
|
|
|
//getting cursor position
|
|
glfwGetCursorPos(window, &xpos, &ypos);
|
|
std::cout << "Cursor Position at (" << xpos << " : " << ypos << std::endl;
|
|
}
|
|
}
|
|
|
|
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);
|
|
glfwSetMouseButtonCallback(window, mouse_button_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);
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
for (int x = 0; x < 35; x++) {
|
|
for (int y = 0; y < 35; ++y) {
|
|
blocks.push_back(new AirBlock(x,y,2));
|
|
blocks.push_back(new DirtBlock(x, y, (int)(sin(x / 3.0)*3)+ (int)(cos(y / 2.0)*2)) );
|
|
blocks.push_back(new Stoneblock(x, y, 0));
|
|
}
|
|
}
|
|
|
|
cam.setPos(0, 0, 3);
|
|
cam.setRotation(0, 0);
|
|
|
|
CrossHair::getInstance()->s.Bind();
|
|
|
|
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);
|
|
|
|
|
|
|
|
// glUniform3f(CrossHair::getInstance()->getUniformhandle("u_color"), 0.0f, 0.0f, 0.0f);
|
|
CrossHair::getInstance()->render();
|
|
|
|
// CrossHair::getInstance()->s.Bind();
|
|
// glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
for (auto b: blocks) {
|
|
b->render();
|
|
}
|
|
|
|
|
|
// swap buffer to calc and show
|
|
glfwSwapBuffers(window);
|
|
// poll for and execute events
|
|
glfwPollEvents();
|
|
}
|
|
|
|
for(auto b : blocks){
|
|
delete b;
|
|
}
|
|
|
|
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);
|
|
} |