load different textures

This commit is contained in:
2022-02-06 22:53:29 +01:00
parent 9df12e0fe1
commit 68f28a3294
27 changed files with 335 additions and 165 deletions

View File

@@ -12,16 +12,14 @@
#include "BlockRenderer.h"
void BaseBlock::render() {
glUniform3f(BlockRenderer::getInstance()->getUniformhandle("u_color"), r, g, b);
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]);
texture->Bind();
BlockRenderer::getInstance()->render();
}
BaseBlock::BaseBlock(float r, float g, float b, uint xpos, uint ypos, uint zpos) : r(r), g(g), b(b), xpos(xpos), ypos(ypos), zpos(zpos) {
// BlockRenderer::init();
// this->getrenderer().init();
BaseBlock::BaseBlock(uint xpos, uint ypos, uint zpos, Texture *texture): xpos(xpos), ypos(ypos), zpos(zpos), texture(texture) {
}

View File

@@ -7,14 +7,15 @@
#include "BlockRenderer.h"
#include "../gl/Texture.h"
class BaseBlock {
private:
float r,g,b;
uint xpos,ypos,zpos;
Texture *texture;
uint xpos, ypos, zpos;
public:
BaseBlock(float r, float g, float b, uint xpos, uint ypos, uint zpos);
BaseBlock(uint xpos, uint ypos, uint zpos, Texture *texture);
void render();
};

View File

@@ -10,32 +10,6 @@
#include <GL/gl.h>
VertexArray *BlockRenderer::setVertexArray() {
// float cube_vertices[] = {
// // front
// -1.0, -1.0, 1.0, 0.0f, 0.0f,
// 1.0, -1.0, 1.0, 1.0f, 0.0f,
// 1.0, 1.0, 1.0, 0.0f, 1.0f,
// -1.0, 1.0, 1.0, 1.0f, 1.0f,
// // back
// -1.0, -1.0, -1.0, 0.0f, 0.0f,
// 1.0, -1.0, -1.0, 1.0f, 0.0f,
// 1.0, 1.0, -1.0, 0.0f, 1.0f,
// -1.0, 1.0, -1.0, 1.0f, 1.0f,
// };
float cube_vertices[] = {
// front
-1, -1, 1, 0, 0,
1, -1, 1, 1, 0,
1, 1, 1, 0, 1,
-1, 1, 1, 1, 1,
// back
-1, -1, -1, 0, 0,
1, -1, -1, 1, 0,
1, 1, -1, 0, 1,
-1, 1, -1, 1, 1,
};
float cubee[] = {
// face #1
1, 1, 1, 1, 0,
@@ -75,7 +49,11 @@ VertexArray *BlockRenderer::setVertexArray() {
// Generate a vertex buffer
auto *vb = new VertexBuffer(cubee, sizeof(cubee));
return new VertexArray(*vb);
auto *va = new VertexArray(*vb);
va->add(0,3,5,0);
va->add(1,2,5,12);
return va;
}
IndexBuffer *BlockRenderer::setIndexBuffer() {
@@ -105,27 +83,6 @@ IndexBuffer *BlockRenderer::setIndexBuffer() {
30, 31, 28
};
unsigned cube_elements[] = {
// front
0, 1, 2,
2, 3, 0,
// right
1, 5, 6,
6, 2, 1,
// back
7, 6, 5,
5, 4, 7,
// left
4, 0, 3,
3, 7, 4,
// bottom
4, 5, 1,
1, 0, 4,
// top
3, 2, 6,
6, 7, 3
};
return new IndexBuffer(indexx, 48);
}
@@ -146,11 +103,6 @@ Shader BlockRenderer::setShader() {
s.loadShader(vertsrc, geosrc, fragsrc);
s.Bind();
Texture t;
t.Bind();
glUniform1i(s.getUniformHandle("u_texture"), 0);
return s;
}

9
blocks/DirtBlock.cpp Normal file
View File

@@ -0,0 +1,9 @@
//
// Created by lukas on 04.02.22.
//
#include "DirtBlock.h"
#include "TextureLoader.h"
DirtBlock::DirtBlock(const uint &xpos, const uint &ypos, const uint &zpos) : BaseBlock(xpos, ypos, zpos, TextureLoader::loadTexture("../assets/blocks/dirt.bmp")) {
}

18
blocks/DirtBlock.h Normal file
View File

@@ -0,0 +1,18 @@
//
// Created by lukas on 04.02.22.
//
#ifndef OPENGLTEST_DIRTBLOCK_H
#define OPENGLTEST_DIRTBLOCK_H
#include <cstdlib>
#include "BaseBlock.h"
class DirtBlock : public BaseBlock {
public:
DirtBlock(const uint &xpos, const uint &ypos, const uint &zpos);
};
#endif //OPENGLTEST_DIRTBLOCK_H

View File

@@ -1,8 +0,0 @@
//
// Created by lukas on 04.02.22.
//
#include "GrasBlock.h"
GrasBlock::GrasBlock(const uint &xpos, const uint &ypos, const uint &zpos) : BaseBlock(0.0f, 1.0f, 0.0f, xpos, ypos, zpos) {
}

View File

@@ -1,18 +0,0 @@
//
// Created by lukas on 04.02.22.
//
#ifndef OPENGLTEST_GRASBLOCK_H
#define OPENGLTEST_GRASBLOCK_H
#include <cstdlib>
#include "BaseBlock.h"
class GrasBlock : public BaseBlock {
public:
GrasBlock(const uint &xpos, const uint &ypos, const uint &zpos);
};
#endif //OPENGLTEST_GRASBLOCK_H

View File

@@ -6,17 +6,22 @@
#define OPENGLTEST_RENDERBASE_H
#include <iostream>
#include "../gl/Renderer.h"
#include "../gl/Shader.h"
template <class T>
template<class T>
class RenderBase {
private:
static RenderBase *instance;
public:
virtual VertexArray* setVertexArray() = 0;
virtual Shader setShader() = 0;
virtual IndexBuffer* setIndexBuffer() = 0;
virtual VertexArray *setVertexArray() = 0;
static RenderBase* getInstance() {
virtual Shader setShader() = 0;
virtual IndexBuffer *setIndexBuffer() = 0;
static RenderBase *getInstance() {
if (instance == nullptr) {
instance = new T();
instance->init();
@@ -25,32 +30,32 @@ public:
}
private:
static RenderBase* instance;
private:
public:
Renderer r;
VertexArray* va;
IndexBuffer* ib;
VertexArray *va;
IndexBuffer *ib;
Shader s;
public:
RenderBase(){};
void render(){
RenderBase() {};
void render() {
r.render(*va, *ib, s);
}
void init() {
s = setShader();
va = setVertexArray();
ib = setIndexBuffer();
}
void deinit(){}
unsigned getMVPhandle(){
void deinit() {}
unsigned getMVPhandle() {
return s.getUniformHandle("MVP");
}
unsigned getUniformhandle(std::string name){
unsigned getUniformhandle(std::string name) {
return s.getUniformHandle(std::move(name));
}
};

8
blocks/Stoneblock.cpp Normal file
View File

@@ -0,0 +1,8 @@
//
// Created by lukas on 06.02.22.
//
#include "Stoneblock.h"
#include "TextureLoader.h"
Stoneblock::Stoneblock(uint xpos, uint ypos, uint zpos) : BaseBlock(xpos, ypos, zpos, TextureLoader::loadTexture("../assets/blocks/stone.bmp")) {}

17
blocks/Stoneblock.h Normal file
View File

@@ -0,0 +1,17 @@
//
// Created by lukas on 06.02.22.
//
#ifndef OPENGLTEST_STONEBLOCK_H
#define OPENGLTEST_STONEBLOCK_H
#include "BaseBlock.h"
class Stoneblock: public BaseBlock {
public:
Stoneblock(uint xpos, uint ypos, uint zpos);
};
#endif //OPENGLTEST_STONEBLOCK_H

22
blocks/TextureLoader.cpp Normal file
View File

@@ -0,0 +1,22 @@
//
// Created by lukas on 06.02.22.
//
#include <iostream>
#include "TextureLoader.h"
std::unordered_map<std::string, Texture> TextureLoader::texts;
Texture *TextureLoader::loadTexture(std::string path) {
if (texts.find(path) != texts.end()) {
return &texts[path];
} else {
std::cout << "Loading texture: " << path << std::endl;
auto *t = new Texture;
t->load(path);
t->Bind();
texts[path] = *t;
return t;
}
}

23
blocks/TextureLoader.h Normal file
View File

@@ -0,0 +1,23 @@
//
// Created by lukas on 06.02.22.
//
#ifndef OPENGLTEST_TEXTURELOADER_H
#define OPENGLTEST_TEXTURELOADER_H
#include <unordered_map>
#include "../gl/Texture.h"
class TextureLoader {
private:
static std::unordered_map<std::string, Texture> texts;
public:
static Texture* loadTexture(std::string path);
private:
};
#endif //OPENGLTEST_TEXTURELOADER_H