load different textures

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

View File

@ -24,12 +24,12 @@ SET(srcs main.cpp gl/Shader.cpp gl/Shader.h
gl/VertexArray.cpp gl/VertexArray.h
gl/Renderer.cpp gl/Renderer.h
blocks/BaseBlock.cpp blocks/BaseBlock.h
blocks/GrasBlock.cpp blocks/GrasBlock.h
blocks/DirtBlock.cpp blocks/DirtBlock.h
blocks/BlockRenderer.cpp blocks/BlockRenderer.h
blocks/RenderBase.cpp blocks/RenderBase.h
gl/Camera.cpp gl/Camera.h
bmploader.cpp bmploader.h
gl/Texture.cpp gl/Texture.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)
add_executable(opengltest ${srcs})

BIN
assets/blocks/dirt.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

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;
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,14 +6,19 @@
#define OPENGLTEST_RENDERBASE_H
#include <iostream>
#include "../gl/Renderer.h"
#include "../gl/Shader.h"
template<class T>
class RenderBase {
private:
static RenderBase *instance;
public:
virtual VertexArray *setVertexArray() = 0;
virtual Shader setShader() = 0;
virtual IndexBuffer *setIndexBuffer() = 0;
static RenderBase *getInstance() {
@ -25,11 +30,7 @@ public:
}
private:
static RenderBase* instance;
private:
public:
Renderer r;
VertexArray *va;
IndexBuffer *ib;
@ -37,19 +38,23 @@ private:
public:
RenderBase() {};
void render() {
r.render(*va, *ib, s);
}
void init() {
s = setShader();
va = setVertexArray();
ib = setIndexBuffer();
}
void deinit() {}
unsigned getMVPhandle() {
return s.getUniformHandle("MVP");
}
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

49
crosshair/CrossHair.cpp Normal file
View File

@ -0,0 +1,49 @@
//
// Created by lukas on 06.02.22.
//
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include "CrossHair.h"
VertexArray *CrossHair::setVertexArray() {
float fade[] = {
// face #1
-0.02, -0.05,
0.02, 0.02,
-0.05, 0.05,
};
// Generate a vertex buffer
auto *vb = new VertexBuffer(fade, sizeof(fade));
auto *va = new VertexArray(*vb);
va->add(0,2,3,0);
return va;
}
Shader CrossHair::setShader() {
const std::string vertsrc =
#include "vertex.shader"
const std::string fragsrc =
#include "fragment.shader"
Shader s;
s.loadShader(vertsrc, "", fragsrc);
s.Bind();
return s;
}
IndexBuffer *CrossHair::setIndexBuffer() {
unsigned indexx[] = {
0,2,1
};
return new IndexBuffer(indexx, 3);
}
void CrossHair::renderr() {
glUniform3f(getUniformhandle("u_color"), 1.0f, 1.0f, 1.0f);
RenderBase::render();
}

23
crosshair/CrossHair.h Normal file
View File

@ -0,0 +1,23 @@
//
// Created by lukas on 06.02.22.
//
#ifndef OPENGLTEST_CROSSHAIR_H
#define OPENGLTEST_CROSSHAIR_H
#include "../blocks/RenderBase.h"
class CrossHair : public RenderBase<CrossHair> {
public:
VertexArray *setVertexArray() override;
Shader setShader() override;
IndexBuffer *setIndexBuffer() override;
void renderr();
};
#endif //OPENGLTEST_CROSSHAIR_H

View File

@ -0,0 +1,8 @@
R"(#version 330 core
out vec4 color;
uniform vec3 u_color;
void main()
{
color = vec4(u_color, 0.5);
})";

9
crosshair/vertex.shader Normal file
View File

@ -0,0 +1,9 @@
R"(#version 330 core
// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec2 vertexPosition_modelspace;
void main(){
// Output position of the vertex, in clip space : MVP * position
vec4 pos = vec4(vertexPosition_modelspace,0.0,1.0);
gl_Position = pos;
})";

View File

@ -2,6 +2,7 @@
// Created by lukas on 05.02.22.
//
#define GL_GLEXT_PROTOTYPES
#include <valarray>
#include <glm/vec3.hpp>
#include <glm/ext/matrix_transform.hpp>
@ -68,13 +69,16 @@ void Camera::setWindowSize(double width, double height) {
void Camera::addRotaion(double rotx, double roty) {
rx -= rotx / 300;
ry -= roty / 300;
ry += roty / 300;
// limit to 2pi
rx = std::fmod(rx, (2 * M_PI));
ry = std::fmod(ry, (2 * M_PI));
ry = fmin(ry, M_PI / 2.0f);
ry = fmax(ry, -M_PI / 2.0f);
updateCameraPos();
}

View File

@ -82,27 +82,39 @@ unsigned int Shader::loadShaderFromFile(const std::string vertex_file_path, cons
}
unsigned int Shader::loadShader(const std::string vertex_src, const std::string geometry_src, const std::string fragment_src) {
uint VertexShaderID;
uint GeometryShaderID;
uint FragmentShaderID;
if (!vertex_src.empty()) {
// Compile Vertex Shader
printf("Compiling vertex shader\n");
uint VertexShaderID = compileShader(vertex_src.c_str(), GL_VERTEX_SHADER);
VertexShaderID = compileShader(vertex_src.c_str(), GL_VERTEX_SHADER);
if (VertexShaderID == 0) {
printf("Error Compiling shader\n");
printf("Error Compiling vertex shader\n");
return 0;
}
}
if (!geometry_src.empty()) {
printf("Compiling geometry shader\n");
uint GeometryShaderID = compileShader(geometry_src.c_str(), GL_GEOMETRY_SHADER);
GeometryShaderID = compileShader(geometry_src.c_str(), GL_GEOMETRY_SHADER);
if (GeometryShaderID == 0) {
printf("Error Compiling shader\n");
return 0;
}
}
if (!fragment_src.empty()) {
printf("Compiling fragment shader\n");
uint FragmentShaderID = compileShader(fragment_src.c_str(), GL_FRAGMENT_SHADER);
FragmentShaderID = compileShader(fragment_src.c_str(), GL_FRAGMENT_SHADER);
if (FragmentShaderID == 0) {
printf("Error Compiling shader\n");
return 0;
}
}
// Link the program
GLint Result = GL_FALSE;
@ -110,9 +122,16 @@ unsigned int Shader::loadShader(const std::string vertex_src, const std::string
printf("Linking shader program\n");
GLuint ProgramID = glCreateProgram();
if (!vertex_src.empty()) {
glAttachShader(ProgramID, VertexShaderID);
}
if (!geometry_src.empty()) {
glAttachShader(ProgramID, GeometryShaderID);
}
if (!fragment_src.empty()) {
glAttachShader(ProgramID, FragmentShaderID);
}
glLinkProgram(ProgramID);
// Check the program
@ -125,11 +144,20 @@ unsigned int Shader::loadShader(const std::string vertex_src, const std::string
}
// cleanup shaders
if (!vertex_src.empty()){
glDetachShader(ProgramID, VertexShaderID);
glDetachShader(ProgramID, FragmentShaderID);
glDeleteShader(VertexShaderID);
}
if (!geometry_src.empty()){
glDetachShader(ProgramID, GeometryShaderID);
glDeleteShader(GeometryShaderID);
}
if (!fragment_src.empty()){
glDetachShader(ProgramID, FragmentShaderID);
glDeleteShader(FragmentShaderID);
}
mProgHandle = ProgramID;
return ProgramID;

View File

@ -24,13 +24,7 @@ public:
unsigned int loadShader(const std::string vertex_src, const std::string geometry_src, const std::string fragment_src);
/**
* comile a specific shader source
* @param source source string
* @param type shader tpye
* @return id of copiled shader
*/
unsigned int compileShader(const char *source, unsigned int type) const;
std::string readFile(std::string path);
@ -40,6 +34,15 @@ public:
unsigned getUniformHandle(std::string name);
private:
/**
* comile a specific shader source
* @param source source string
* @param type shader tpye
* @return id of copiled shader
*/
unsigned int compileShader(const char *source, unsigned int type) const;
};
#endif //OPENGLTEST_SHADER_H

View File

@ -4,12 +4,12 @@
#include "Texture.h"
#include "../bmploader.h"
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
Texture::Texture() {
mTexturehandle = bmploader().loadBMP("../assets/blocks/block.bmp");
}
void Texture::Bind() {
@ -20,3 +20,7 @@ void Texture::Bind() {
void Texture::UnBind() {
glBindTexture(GL_TEXTURE_2D, 0);
}
void Texture::load(std::string path) {
mTexturehandle = bmploader().loadBMP(path.c_str());
}

View File

@ -6,11 +6,14 @@
#define OPENGLTEST_TEXTURE_H
#include <string>
class Texture {
private:
unsigned mTexturehandle;
public:
Texture();
void load(std::string path);
void Bind();
void UnBind();
};

View File

@ -17,14 +17,19 @@ VertexArray::VertexArray(const VertexBuffer& buff) {
Bind();
// specify syntax of my data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) nullptr);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) 12);
glEnableVertexAttribArray(1);
// // specify syntax of my data
// glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) nullptr);
// glEnableVertexAttribArray(0);
//
// glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) 12);
// glEnableVertexAttribArray(1);
}
unsigned VertexArray::getHandle() const {
return handle;
}
void VertexArray::add(int index, int size, int stride, int pos) {
glVertexAttribPointer(index, size, GL_FLOAT, GL_FALSE, stride * sizeof(float), (void *) pos);
glEnableVertexAttribArray(index);
}

View File

@ -17,6 +17,8 @@ public:
void Bind() const;
void add(int index, int size, int stride, int pos);
unsigned getHandle() const;
};

View File

@ -3,15 +3,16 @@
#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/DirtBlock.h"
#include "blocks/BlockRenderer.h"
#include "gl/Camera.h"
#include "blocks/Stoneblock.h"
#include "crosshair/CrossHair.h"
#include <cstdlib>
#include <glm/gtc/matrix_transform.hpp>
@ -96,25 +97,19 @@ int main() {
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
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));
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));
}
}
cam.setPos(0, 0, 0);
cam.setPos(0, 0, 3);
cam.setRotation(0, 0);
CrossHair::getInstance()->s.Bind();
while (!glfwWindowShouldClose(window)) {
// process user input events
processInput(window);
@ -122,10 +117,20 @@ int main() {
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