diff --git a/CMakeLists.txt b/CMakeLists.txt index aeccd94..8f2888d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ SET(srcs main.cpp gl/Shader.cpp gl/Shader.h blocks/RenderBase.cpp blocks/RenderBase.h gl/Camera.cpp gl/Camera.h bmploader.cpp bmploader.h - gl/Texture.cpp gl/Texture.h blocks/TextureLoader.cpp blocks/TextureLoader.h blocks/Stoneblock.cpp blocks/Stoneblock.h crosshair/CrossHair.cpp crosshair/CrossHair.h) + gl/Texture.cpp gl/Texture.h blocks/TextureLoader.cpp blocks/TextureLoader.h blocks/Stoneblock.cpp blocks/Stoneblock.h crosshair/CrossHair.cpp crosshair/CrossHair.h blocks/AirBlock.h) add_executable(opengltest ${srcs}) diff --git a/blocks/AirBlock.h b/blocks/AirBlock.h new file mode 100644 index 0000000..2754b9e --- /dev/null +++ b/blocks/AirBlock.h @@ -0,0 +1,18 @@ +// +// Created by lukas on 08.02.22. +// + +#ifndef OPENGLTEST_AIRBLOCK_H +#define OPENGLTEST_AIRBLOCK_H + +#include "BaseBlock.h" + +class AirBlock : public BaseBlock{ +public: + AirBlock(uint xpos, uint ypos, uint zpos) : BaseBlock(xpos, ypos, zpos, nullptr) {} + + void render() override { + } +}; + +#endif //OPENGLTEST_AIRBLOCK_H diff --git a/blocks/BaseBlock.cpp b/blocks/BaseBlock.cpp index 6bc0976..ff26004 100644 --- a/blocks/BaseBlock.cpp +++ b/blocks/BaseBlock.cpp @@ -2,9 +2,6 @@ // Created by lukas on 04.02.22. // -#define GL_GLEXT_PROTOTYPES - -#include #include #include #include @@ -12,6 +9,8 @@ #include "BlockRenderer.h" void BaseBlock::render() { + BlockRenderer::getInstance()->s.Bind(); + glm::mat4 position = glm::translate(glm::mat4(1.0f), glm::vec3((float) xpos * 2, (float) ypos * 2, (float) zpos * 2)); glUniformMatrix4fv(BlockRenderer::getInstance()->getUniformhandle("translation"), 1, GL_FALSE, &position[0][0]); diff --git a/blocks/BaseBlock.h b/blocks/BaseBlock.h index 016f230..d9c40d4 100644 --- a/blocks/BaseBlock.h +++ b/blocks/BaseBlock.h @@ -8,6 +8,8 @@ #include "BlockRenderer.h" #include "../gl/Texture.h" +#include +#include class BaseBlock { private: @@ -17,7 +19,7 @@ private: public: BaseBlock(uint xpos, uint ypos, uint zpos, Texture *texture); - void render(); + virtual void render(); }; diff --git a/blocks/RenderBase.h b/blocks/RenderBase.h index 104e8b5..7ebd55f 100644 --- a/blocks/RenderBase.h +++ b/blocks/RenderBase.h @@ -14,6 +14,12 @@ template class RenderBase { private: static RenderBase *instance; +protected: + RenderBase() { + if (instance) throw std::logic_error("Instance already exists"); + instance = this; + } + public: virtual VertexArray *setVertexArray() = 0; @@ -23,11 +29,10 @@ public: static RenderBase *getInstance() { if (instance == nullptr) { - instance = new T(); + new T(); instance->init(); } return instance; - } public: @@ -37,9 +42,9 @@ public: Shader s; public: - RenderBase() {}; - void render() { + + virtual void render() { r.render(*va, *ib, s); } @@ -49,7 +54,9 @@ public: ib = setIndexBuffer(); } - void deinit() {} + void deinit() { + glDeleteProgram(s.getHandle()); + } unsigned getMVPhandle() { return s.getUniformHandle("MVP"); diff --git a/crosshair/CrossHair.cpp b/crosshair/CrossHair.cpp index 73e2f69..5c9d703 100644 --- a/crosshair/CrossHair.cpp +++ b/crosshair/CrossHair.cpp @@ -43,7 +43,8 @@ IndexBuffer *CrossHair::setIndexBuffer() { return new IndexBuffer(indexx, 3); } -void CrossHair::renderr() { +void CrossHair::render() { + s.Bind(); glUniform3f(getUniformhandle("u_color"), 1.0f, 1.0f, 1.0f); RenderBase::render(); } diff --git a/crosshair/CrossHair.h b/crosshair/CrossHair.h index 636577b..f5dbf3b 100644 --- a/crosshair/CrossHair.h +++ b/crosshair/CrossHair.h @@ -16,7 +16,7 @@ public: IndexBuffer *setIndexBuffer() override; - void renderr(); + void render() override; }; diff --git a/gl/Camera.cpp b/gl/Camera.cpp index e68a983..26fb4a2 100644 --- a/gl/Camera.cpp +++ b/gl/Camera.cpp @@ -97,3 +97,15 @@ double Camera::getxangle() const { double Camera::getyangle() const { return ry; } + +double Camera::getXpos() { + return x; +} + +double Camera::getYpos() { + return y; +} + +double Camera::getZpos() { + return z; +} diff --git a/gl/Camera.h b/gl/Camera.h index 50f08c8..f10fc78 100644 --- a/gl/Camera.h +++ b/gl/Camera.h @@ -19,6 +19,10 @@ public: void setPos(double x, double y, double z); void addPos(double x, double y, double z); + double getXpos(); + double getYpos(); + double getZpos(); + void setRotation(double rotx, double roty); void addRotaion(double rotx, double roty); diff --git a/gl/Shader.cpp b/gl/Shader.cpp index 2a51fce..0ced4fe 100644 --- a/gl/Shader.cpp +++ b/gl/Shader.cpp @@ -12,6 +12,7 @@ #include #include #include +#include unsigned int Shader::compileShader(const char *source, unsigned int type) const { unsigned int shaderid = glCreateShader(type); diff --git a/gl/Shader.h b/gl/Shader.h index f5fed62..c5f75c7 100644 --- a/gl/Shader.h +++ b/gl/Shader.h @@ -7,6 +7,12 @@ #include #include +#include +#include +#include +#define GL_GLEXT_PROTOTYPES + +#include class Shader { private: @@ -31,7 +37,6 @@ public: void Bind() const; unsigned getHandle() const; - unsigned getUniformHandle(std::string name); private: diff --git a/main.cpp b/main.cpp index b3489aa..aa54cc0 100644 --- a/main.cpp +++ b/main.cpp @@ -13,10 +13,9 @@ #include "gl/Camera.h" #include "blocks/Stoneblock.h" #include "crosshair/CrossHair.h" +#include "blocks/AirBlock.h" #include -#include - //#define WIREFRAME void framebuffer_size_callback(GLFWwindow *window, int width, int height); @@ -32,6 +31,7 @@ double oldx = 0; double oldy = 0; bool menuopen = false; +std::vector blocks; void cursor_position_callback(GLFWwindow *window, double xpos, double ypos) { if (menuopen)return; @@ -45,6 +45,21 @@ void cursor_position_callback(GLFWwindow *window, double xpos, double 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(); @@ -75,6 +90,7 @@ GLFWwindow *initWindow() { glfwSetCursorPosCallback(window, cursor_position_callback); + glfwSetMouseButtonCallback(window, mouse_button_callback); if (glfwRawMouseMotionSupported()) glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE); @@ -97,11 +113,11 @@ int main() { glEnable(GL_DEPTH_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - std::vector blocks; - for (int x = 0; x < 15; x++) { - for (int y = 0; y < 15; ++y) { - blocks.push_back(DirtBlock(x, y, 1)); - blocks.push_back(Stoneblock(x, y, 0)); + 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)); } } @@ -119,15 +135,14 @@ int main() { - - glUniform3f(CrossHair::getInstance()->getUniformhandle("u_color"), 0.0f, 0.0f, 0.0f); +// glUniform3f(CrossHair::getInstance()->getUniformhandle("u_color"), 0.0f, 0.0f, 0.0f); CrossHair::getInstance()->render(); - CrossHair::getInstance()->s.Bind(); - glBindTexture(GL_TEXTURE_2D, 0); +// CrossHair::getInstance()->s.Bind(); +// glBindTexture(GL_TEXTURE_2D, 0); for (auto b: blocks) { - b.render(); + b->render(); } @@ -137,7 +152,10 @@ int main() { glfwPollEvents(); } - // glDeleteProgram(shaderProgram); + for(auto b : blocks){ + delete b; + } + BlockRenderer::getInstance()->deinit(); glfwTerminate(); return 0;