This commit is contained in:
2022-02-05 21:44:31 +01:00
commit a00022523f
27 changed files with 1178 additions and 0 deletions

27
blocks/BaseBlock.cpp Normal file
View File

@ -0,0 +1,27 @@
//
// Created by lukas on 04.02.22.
//
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <cstdlib>
#include <glm/ext/matrix_float4x4.hpp>
#include <glm/ext/matrix_transform.hpp>
#include "BaseBlock.h"
#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]);
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();
}

23
blocks/BaseBlock.h Normal file
View File

@ -0,0 +1,23 @@
//
// Created by lukas on 04.02.22.
//
#ifndef OPENGLTEST_BASEBLOCK_H
#define OPENGLTEST_BASEBLOCK_H
#include "BlockRenderer.h"
class BaseBlock {
private:
float r,g,b;
uint xpos,ypos,zpos;
public:
BaseBlock(float r, float g, float b, uint xpos, uint ypos, uint zpos);
void render();
};
#endif //OPENGLTEST_BASEBLOCK_H

89
blocks/BlockRenderer.cpp Normal file
View File

@ -0,0 +1,89 @@
//
// Created by lukas on 05.02.22.
//
#include "BlockRenderer.h"
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
VertexArray *BlockRenderer::setVertexArray() {
float cube_vertices[] = {
// front
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
// back
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0
};
GLfloat cube_colors[] = {
// front colors
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 1.0, 1.0,
// back colors
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 1.0, 1.0
};
// Generate a vertex buffer
auto *vb = new VertexBuffer(cube_vertices, sizeof(cube_vertices));
//
return new VertexArray(*vb);
}
IndexBuffer *BlockRenderer::setIndexBuffer() {
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(cube_elements, 36);;
}
Shader BlockRenderer::setShader() {
const std::string vertsrc =
#include "vertex.shader"
const std::string geosrc =
#include "geometry.shader"
const std::string fragsrc =
#include "fragment.shader"
Shader s;
s.loadShader(vertsrc, geosrc, fragsrc);
s.Bind();
return s;
}
BlockRenderer::BlockRenderer() {}

26
blocks/BlockRenderer.h Normal file
View File

@ -0,0 +1,26 @@
//
// Created by lukas on 05.02.22.
//
#ifndef OPENGLTEST_BLOCKRENDERER_H
#define OPENGLTEST_BLOCKRENDERER_H
#include "../gl/Renderer.h"
#include "../gl/Shader.h"
#include "RenderBase.h"
#include <iostream>
class BlockRenderer : public RenderBase<BlockRenderer> {
public:
BlockRenderer();
VertexArray* setVertexArray() override;
Shader setShader() override;
IndexBuffer *setIndexBuffer() override;
};
#endif //OPENGLTEST_BLOCKRENDERER_H

8
blocks/GrasBlock.cpp Normal file
View File

@ -0,0 +1,8 @@
//
// 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) {
}

18
blocks/GrasBlock.h Normal file
View File

@ -0,0 +1,18 @@
//
// 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

48
blocks/RenderBase.cpp Normal file
View File

@ -0,0 +1,48 @@
//
// Created by lukas on 05.02.22.
//
#include "RenderBase.h"
//template<class T>
//RenderBase<T>::RenderBase() {
//
//}
//template<class T>
//void RenderBase<T>::render() {
// r.render(*va, *ib);
//}
//template<class T>
//void RenderBase<T>::init() {
// s = setShader();
// va = setVertexArray();
// ib = setIndexBuffer();
//}
//template<class T>
//void RenderBase<T>::deinit() {
//
//}
//template<class T>
//unsigned RenderBase<T>::getMVPhandle() {
// return s.getUniformHandle("MVP");
//}
//template<class T>
//unsigned RenderBase<T>::getUniformhandle(std::string name) {
// return s.getUniformHandle(std::move(name));
//}
//template<class T>
//T *RenderBase<T>::getInstance() {
// if (instance == nullptr) {
// instance = new T();
// }
// return instance;
//
//}

61
blocks/RenderBase.h Normal file
View File

@ -0,0 +1,61 @@
//
// Created by lukas on 05.02.22.
//
#ifndef OPENGLTEST_RENDERBASE_H
#define OPENGLTEST_RENDERBASE_H
#include "../gl/Renderer.h"
#include "../gl/Shader.h"
template <class T>
class RenderBase {
public:
virtual VertexArray* setVertexArray() = 0;
virtual Shader setShader() = 0;
virtual IndexBuffer* setIndexBuffer() = 0;
static RenderBase* getInstance() {
if (instance == nullptr) {
instance = new T();
instance->init();
}
return instance;
}
private:
static RenderBase* instance;
private:
Renderer r;
VertexArray* va;
IndexBuffer* ib;
Shader s;
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));
}
};
template<class T>
RenderBase<T> *RenderBase<T>::instance = nullptr;
#endif //OPENGLTEST_RENDERBASE_H

20
blocks/fragment.shader Normal file
View File

@ -0,0 +1,20 @@
R"(#version 330 core
out vec4 color;
uniform vec3 u_color;
in vec3 normal;
in vec4 pos;
void main()
{
vec3 norm = normalize(normal);
vec3 lightDir = normalize(vec3(4.0,3.0,3.0) - pos.xyz);
float diff = max(dot(norm, lightDir), 0.0);
// set light color
vec3 diffuse = diff * vec3(1.0,1.0,1.0);
vec3 result = (diffuse) * u_color;
color = vec4(result,0.0);
})";

25
blocks/geometry.shader Normal file
View File

@ -0,0 +1,25 @@
R"(#version 330
layout(triangles) in;
layout(triangle_strip, max_vertices=3) out;
out vec3 normal;
out vec4 pos;
void main( void )
{
vec3 a = ( gl_in[1].gl_Position - gl_in[0].gl_Position ).xyz;
vec3 b = ( gl_in[2].gl_Position - gl_in[0].gl_Position ).xyz;
vec3 N = normalize( cross( b, a ) );
for( int i=0; i<gl_in.length( ); ++i )
{
gl_Position = gl_in[i].gl_Position;
normal = N;
pos = gl_in[i].gl_Position;
EmitVertex( );
}
EndPrimitive( );
})";

16
blocks/vertex.shader Normal file
View File

@ -0,0 +1,16 @@
R"(#version 330 core
// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
//// Values that stay constant for the whole mesh.
uniform mat4 MVP;
uniform mat4 translation;
out vec3 FragPos;
void main(){
// Output position of the vertex, in clip space : MVP * position
vec4 pos = MVP * translation * vec4(vertexPosition_modelspace,1);
gl_Position = pos;
FragPos = pos.xyz;
})";