add crosshair

blockgen with sin
This commit is contained in:
lukas 2022-02-10 18:24:56 +01:00
parent 68f28a3294
commit 360a1cc79d
12 changed files with 93 additions and 26 deletions

View File

@ -29,7 +29,7 @@ SET(srcs main.cpp gl/Shader.cpp gl/Shader.h
blocks/RenderBase.cpp blocks/RenderBase.h blocks/RenderBase.cpp blocks/RenderBase.h
gl/Camera.cpp gl/Camera.h gl/Camera.cpp gl/Camera.h
bmploader.cpp bmploader.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}) add_executable(opengltest ${srcs})

18
blocks/AirBlock.h Normal file
View File

@ -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

View File

@ -2,9 +2,6 @@
// Created by lukas on 04.02.22. // Created by lukas on 04.02.22.
// //
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <cstdlib> #include <cstdlib>
#include <glm/ext/matrix_float4x4.hpp> #include <glm/ext/matrix_float4x4.hpp>
#include <glm/ext/matrix_transform.hpp> #include <glm/ext/matrix_transform.hpp>
@ -12,6 +9,8 @@
#include "BlockRenderer.h" #include "BlockRenderer.h"
void BaseBlock::render() { 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)); 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]); glUniformMatrix4fv(BlockRenderer::getInstance()->getUniformhandle("translation"), 1, GL_FALSE, &position[0][0]);

View File

@ -8,6 +8,8 @@
#include "BlockRenderer.h" #include "BlockRenderer.h"
#include "../gl/Texture.h" #include "../gl/Texture.h"
#include <glm/ext/matrix_float4x4.hpp>
#include <glm/ext/matrix_transform.hpp>
class BaseBlock { class BaseBlock {
private: private:
@ -17,7 +19,7 @@ private:
public: public:
BaseBlock(uint xpos, uint ypos, uint zpos, Texture *texture); BaseBlock(uint xpos, uint ypos, uint zpos, Texture *texture);
void render(); virtual void render();
}; };

View File

@ -14,6 +14,12 @@ template<class T>
class RenderBase { class RenderBase {
private: private:
static RenderBase *instance; static RenderBase *instance;
protected:
RenderBase() {
if (instance) throw std::logic_error("Instance already exists");
instance = this;
}
public: public:
virtual VertexArray *setVertexArray() = 0; virtual VertexArray *setVertexArray() = 0;
@ -23,11 +29,10 @@ public:
static RenderBase *getInstance() { static RenderBase *getInstance() {
if (instance == nullptr) { if (instance == nullptr) {
instance = new T(); new T();
instance->init(); instance->init();
} }
return instance; return instance;
} }
public: public:
@ -37,9 +42,9 @@ public:
Shader s; Shader s;
public: public:
RenderBase() {};
void render() {
virtual void render() {
r.render(*va, *ib, s); r.render(*va, *ib, s);
} }
@ -49,7 +54,9 @@ public:
ib = setIndexBuffer(); ib = setIndexBuffer();
} }
void deinit() {} void deinit() {
glDeleteProgram(s.getHandle());
}
unsigned getMVPhandle() { unsigned getMVPhandle() {
return s.getUniformHandle("MVP"); return s.getUniformHandle("MVP");

View File

@ -43,7 +43,8 @@ IndexBuffer *CrossHair::setIndexBuffer() {
return new IndexBuffer(indexx, 3); return new IndexBuffer(indexx, 3);
} }
void CrossHair::renderr() { void CrossHair::render() {
s.Bind();
glUniform3f(getUniformhandle("u_color"), 1.0f, 1.0f, 1.0f); glUniform3f(getUniformhandle("u_color"), 1.0f, 1.0f, 1.0f);
RenderBase::render(); RenderBase::render();
} }

View File

@ -16,7 +16,7 @@ public:
IndexBuffer *setIndexBuffer() override; IndexBuffer *setIndexBuffer() override;
void renderr(); void render() override;
}; };

View File

@ -97,3 +97,15 @@ double Camera::getxangle() const {
double Camera::getyangle() const { double Camera::getyangle() const {
return ry; return ry;
} }
double Camera::getXpos() {
return x;
}
double Camera::getYpos() {
return y;
}
double Camera::getZpos() {
return z;
}

View File

@ -19,6 +19,10 @@ public:
void setPos(double x, double y, double z); void setPos(double x, double y, double z);
void addPos(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 setRotation(double rotx, double roty);
void addRotaion(double rotx, double roty); void addRotaion(double rotx, double roty);

View File

@ -12,6 +12,7 @@
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include <sstream> #include <sstream>
#include <iostream>
unsigned int Shader::compileShader(const char *source, unsigned int type) const { unsigned int Shader::compileShader(const char *source, unsigned int type) const {
unsigned int shaderid = glCreateShader(type); unsigned int shaderid = glCreateShader(type);

View File

@ -7,6 +7,12 @@
#include <unordered_map> #include <unordered_map>
#include <string> #include <string>
#include <stdexcept>
#include <glm/ext/matrix_float4x4.hpp>
#include <iostream>
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
class Shader { class Shader {
private: private:
@ -31,7 +37,6 @@ public:
void Bind() const; void Bind() const;
unsigned getHandle() const; unsigned getHandle() const;
unsigned getUniformHandle(std::string name); unsigned getUniformHandle(std::string name);
private: private:

View File

@ -13,10 +13,9 @@
#include "gl/Camera.h" #include "gl/Camera.h"
#include "blocks/Stoneblock.h" #include "blocks/Stoneblock.h"
#include "crosshair/CrossHair.h" #include "crosshair/CrossHair.h"
#include "blocks/AirBlock.h"
#include <cstdlib> #include <cstdlib>
#include <glm/gtc/matrix_transform.hpp>
//#define WIREFRAME //#define WIREFRAME
void framebuffer_size_callback(GLFWwindow *window, int width, int height); void framebuffer_size_callback(GLFWwindow *window, int width, int height);
@ -32,6 +31,7 @@ double oldx = 0;
double oldy = 0; double oldy = 0;
bool menuopen = false; bool menuopen = false;
std::vector<BaseBlock*> blocks;
void cursor_position_callback(GLFWwindow *window, double xpos, double ypos) { void cursor_position_callback(GLFWwindow *window, double xpos, double ypos) {
if (menuopen)return; if (menuopen)return;
@ -45,6 +45,21 @@ void cursor_position_callback(GLFWwindow *window, double xpos, double ypos) {
cam.addRotaion(xdiff, ydiff); 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() { GLFWwindow *initWindow() {
glfwInit(); glfwInit();
@ -75,6 +90,7 @@ GLFWwindow *initWindow() {
glfwSetCursorPosCallback(window, cursor_position_callback); glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
if (glfwRawMouseMotionSupported()) if (glfwRawMouseMotionSupported())
glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE); glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
@ -97,11 +113,11 @@ int main() {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
std::vector<BaseBlock> blocks; for (int x = 0; x < 35; x++) {
for (int x = 0; x < 15; x++) { for (int y = 0; y < 35; ++y) {
for (int y = 0; y < 15; ++y) { blocks.push_back(new AirBlock(x,y,2));
blocks.push_back(DirtBlock(x, y, 1)); blocks.push_back(new DirtBlock(x, y, (int)(sin(x / 3.0)*3)+ (int)(cos(y / 2.0)*2)) );
blocks.push_back(Stoneblock(x, y, 0)); 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()->render();
CrossHair::getInstance()->s.Bind(); // CrossHair::getInstance()->s.Bind();
glBindTexture(GL_TEXTURE_2D, 0); // glBindTexture(GL_TEXTURE_2D, 0);
for (auto b: blocks) { for (auto b: blocks) {
b.render(); b->render();
} }
@ -137,7 +152,10 @@ int main() {
glfwPollEvents(); glfwPollEvents();
} }
// glDeleteProgram(shaderProgram); for(auto b : blocks){
delete b;
}
BlockRenderer::getInstance()->deinit(); BlockRenderer::getInstance()->deinit();
glfwTerminate(); glfwTerminate();
return 0; return 0;