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

95
gl/Camera.cpp Normal file
View File

@ -0,0 +1,95 @@
//
// Created by lukas on 05.02.22.
//
#define GL_GLEXT_PROTOTYPES
#include <valarray>
#include <glm/vec3.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <GL/gl.h>
#include <glm/ext/matrix_clip_space.hpp>
#include "Camera.h"
#include "../blocks/BlockRenderer.h"
void Camera::setPos(double x, double y, double z) {
this->x = x;
this->y = y;
this->z = z;
updateCameraPos();
}
void Camera::setRotation(double rotx, double roty) {
rx = rotx;
ry = roty;
updateCameraPos();
}
void Camera::updateCameraPos() {
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
glm::mat4 Projection = glm::perspective(glm::radians(45.0f), (float) width / (float) height, 0.1f, 100.0f);
// Or, for an ortho camera :
//glm::mat4 Projection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.0f,100.0f); // In world coordinates
//
// float rotation = M_PI;
//
// float x = cos(rotation);
// float y = sin(rotation);
const float radius = 5.0f;
double camX = sin(rx) * radius;
double camZ = cos(rx) * radius;
double camY = sin(ry) * radius;
glm::mat4 View = glm::lookAt(
glm::vec3(2.0f, 2.0f, 3.0f), // and looks at the origin
glm::vec3(camX, camZ, camY), // Camera is at (4,3,3), in World Space
glm::vec3(0, 0, 1) // Head is up (set to 0,-1,0 to look upside-down)
);
auto View2 = glm::translate(View, glm::vec3(x, -y, -z));
// Model matrix : an identity matrix (model will be at the origin)
glm::mat4 Model = glm::mat4(1.0f);
// Our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 mvp = Projection * View2 * Model; // Remember, matrix multiplication is the other way around
// todo not really generic if we call blockrenderer here
glUniformMatrix4fv((int) BlockRenderer::getInstance()->getMVPhandle(), 1, GL_FALSE, &mvp[0][0]);
}
Camera::Camera(double width, double height) : width(width), height(height) {}
void Camera::setWindowSize(double width, double height) {
this->width = width;
this->height = height;
}
void Camera::addRotaion(double rotx, double roty) {
rx -= rotx / 300;
ry -= roty / 300;
// limit to 2pi
rx = std::fmod(rx, (2 * M_PI));
ry = std::fmod(ry, (2 * M_PI));
updateCameraPos();
}
void Camera::addPos(double x, double y, double z) {
this->x += x;
this->y += y;
this->z += z;
updateCameraPos();
}
double Camera::getxangle() const {
return rx;
}
double Camera::getyangle() const {
return ry;
}

30
gl/Camera.h Normal file
View File

@ -0,0 +1,30 @@
//
// Created by lukas on 05.02.22.
//
#ifndef OPENGLTEST_CAMERA_H
#define OPENGLTEST_CAMERA_H
class Camera {
private:
double x, y, z, rx, ry, width, height;
void updateCameraPos();
public:
Camera(double width, double height);
void setWindowSize(double width, double height);
void setPos(double x, double y, double z);
void addPos(double x, double y, double z);
void setRotation(double rotx, double roty);
void addRotaion(double rotx, double roty);
double getxangle() const;
double getyangle() const;
};
#endif //OPENGLTEST_CAMERA_H

28
gl/IndexBuffer.cpp Normal file
View File

@ -0,0 +1,28 @@
//
// Created by lukas on 03.02.22.
//
#include "IndexBuffer.h"
#include <GL/gl.h>
IndexBuffer::IndexBuffer(const unsigned *data, unsigned count): count(count) {
glGenBuffers(1, &handle);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned) * count , data, GL_STATIC_DRAW);
}
IndexBuffer::~IndexBuffer() {
glDeleteBuffers(1, &handle);
}
void IndexBuffer::Bind() const {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle);
}
void IndexBuffer::UnBind() const {
glBindVertexArray(0);
}
unsigned IndexBuffer::getcount() const {
return count;
}

26
gl/IndexBuffer.h Normal file
View File

@ -0,0 +1,26 @@
//
// Created by lukas on 03.02.22.
//
#ifndef OPENGLTEST_INDEXBUFFER_H
#define OPENGLTEST_INDEXBUFFER_H
#define GL_GLEXT_PROTOTYPES
class IndexBuffer {
private:
unsigned handle;
unsigned count;
public:
IndexBuffer(const unsigned *data, unsigned count);
virtual ~IndexBuffer();
void Bind() const;
void UnBind() const;
unsigned getcount() const;
};
#endif //OPENGLTEST_INDEXBUFFER_H

15
gl/Renderer.cpp Normal file
View File

@ -0,0 +1,15 @@
//
// Created by lukas on 04.02.22.
//
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include "Renderer.h"
void Renderer::render(const VertexArray& va, const IndexBuffer& ib, const Shader& s) {
va.Bind();
ib.Bind();
s.Bind();
glDrawElements(GL_TRIANGLES, ib.getcount(), GL_UNSIGNED_INT, nullptr);
}

19
gl/Renderer.h Normal file
View File

@ -0,0 +1,19 @@
//
// Created by lukas on 04.02.22.
//
#ifndef OPENGLTEST_RENDERER_H
#define OPENGLTEST_RENDERER_H
#include "VertexArray.h"
#include "IndexBuffer.h"
#include "Shader.h"
class Renderer {
public:
void render(const VertexArray& va, const IndexBuffer& ib, const Shader& s);
};
#endif //OPENGLTEST_RENDERER_H

136
gl/Shader.cpp Normal file
View File

@ -0,0 +1,136 @@
//
// Created by lukas on 03.02.22.
//
#include "Shader.h"
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glcorearb.h>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
unsigned int Shader::compileShader(const char *source, unsigned int type) const {
unsigned int shaderid = glCreateShader(type);
// Compile Shader
glShaderSource(shaderid, 1, &source, NULL);
glCompileShader(shaderid);
// Check Shader
int Result = GL_FALSE;
int InfoLogLength;
glGetShaderiv(shaderid, GL_COMPILE_STATUS, &Result);
glGetShaderiv(shaderid, GL_INFO_LOG_LENGTH, &InfoLogLength);
if (InfoLogLength > 0) {
std::vector<char> VertexShaderErrorMessage(InfoLogLength + 1);
glGetShaderInfoLog(shaderid, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
printf("%s\n", &VertexShaderErrorMessage[0]);
return 0;
}
return shaderid;
}
void Shader::Bind() const {
glUseProgram(mProgHandle);
}
unsigned Shader::getHandle() const {
return mProgHandle;
}
unsigned Shader::getUniformHandle(std::string name) {
if(uniformhandles.find(name) != uniformhandles.end()){
return uniformhandles[name];
}else{
const unsigned id = glGetUniformLocation(mProgHandle, name.c_str());
uniformhandles[name] = id;
return id;
}
}
std::string Shader::readFile(std::string path) {
// Read the Vertex Shader code from the file
std::string VertexShaderCode;
std::ifstream VertexShaderStream(path, std::ios::in);
if (VertexShaderStream.is_open()) {
std::stringstream sstr;
sstr << VertexShaderStream.rdbuf();
VertexShaderCode = sstr.str();
VertexShaderStream.close();
} else {
printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", path.c_str());
getchar();
return "";
}
return VertexShaderCode;
}
unsigned int Shader::loadShaderFromFile(const std::string vertex_file_path, const std::string geometry_file_path, const std::string fragment_file_path) {
// Read the Vertex Shader code from the file
std::string VertexShaderCode = readFile(vertex_file_path);
std::string FragmentShaderCode = readFile(fragment_file_path);
std::string GeometryShaderCode = readFile("../geometry.shader");
return loadShader(VertexShaderCode, GeometryShaderCode, FragmentShaderCode);
}
unsigned int Shader::loadShader(const std::string vertex_src, const std::string geometry_src, const std::string fragment_src) {
// Compile Vertex Shader
printf("Compiling vertex shader\n");
uint VertexShaderID = compileShader(vertex_src.c_str(), GL_VERTEX_SHADER);
if (VertexShaderID == 0) {
printf("Error Compiling shader\n");
return 0;
}
printf("Compiling geometry shader\n");
uint GeometryShaderID = compileShader(geometry_src.c_str(), GL_GEOMETRY_SHADER);
if (GeometryShaderID == 0) {
printf("Error Compiling shader\n");
return 0;
}
printf("Compiling fragment shader\n");
uint 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;
int InfoLogLength;
printf("Linking shader program\n");
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, GeometryShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);
// Check the program
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if (InfoLogLength > 0) {
std::vector<char> ProgramErrorMessage(InfoLogLength + 1);
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
printf("%s\n", &ProgramErrorMessage[0]);
}
// cleanup shaders
glDetachShader(ProgramID, VertexShaderID);
glDetachShader(ProgramID, FragmentShaderID);
glDeleteShader(VertexShaderID);
glDeleteShader(FragmentShaderID);
mProgHandle = ProgramID;
return ProgramID;
}

45
gl/Shader.h Normal file
View File

@ -0,0 +1,45 @@
//
// Created by lukas on 03.02.22.
//
#ifndef OPENGLTEST_SHADER_H
#define OPENGLTEST_SHADER_H
#include <unordered_map>
#include <string>
class Shader {
private:
unsigned mProgHandle;
std::unordered_map<std::string, unsigned> uniformhandles;
public:
/**
* load the compiled shader pipeline
* @param vertex_file_path path to vertex source file
* @param fragment_file_path path to fragment source file
* @return program id
*/
unsigned int loadShaderFromFile(const std::string vertex_file_path, const std::string geometry_file_path, const std::string fragment_file_path);
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);
void Bind() const;
unsigned getHandle() const;
unsigned getUniformHandle(std::string name);
};
#endif //OPENGLTEST_SHADER_H

26
gl/VertexArray.cpp Normal file
View File

@ -0,0 +1,26 @@
//
// Created by lukas on 04.02.22.
//
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include "VertexArray.h"
void VertexArray::Bind() const {
glBindVertexArray(handle);
}
VertexArray::VertexArray(const VertexBuffer& buff) {
buff.Bind();
// generate new vertex array object
glGenVertexArrays(1, &handle);
Bind();
// specify syntax of my data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) nullptr);
glEnableVertexAttribArray(0);
}
unsigned VertexArray::getHandle() const {
return handle;
}

24
gl/VertexArray.h Normal file
View File

@ -0,0 +1,24 @@
//
// Created by lukas on 04.02.22.
//
#ifndef OPENGLTEST_VERTEXARRAY_H
#define OPENGLTEST_VERTEXARRAY_H
#include <vector>
#include "VertexBuffer.h"
class VertexArray {
private:
unsigned handle;
public:
VertexArray(const VertexBuffer& buff);
void Bind() const;
unsigned getHandle() const;
};
#endif //OPENGLTEST_VERTEXARRAY_H

25
gl/VertexBuffer.cpp Normal file
View File

@ -0,0 +1,25 @@
//
// Created by lukas on 03.02.22.
//
#include "VertexBuffer.h"
#include <GL/gl.h>
VertexBuffer::VertexBuffer(const void* data, const unsigned size) {
glGenBuffers(1, &handle);
glBindBuffer(GL_ARRAY_BUFFER, handle);
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
}
VertexBuffer::~VertexBuffer() {
glDeleteBuffers(1, &handle);
}
void VertexBuffer::Bind() const {
glBindBuffer(GL_ARRAY_BUFFER, handle);
}
void VertexBuffer::UnBind() const {
glBindVertexArray(0);
}

23
gl/VertexBuffer.h Normal file
View File

@ -0,0 +1,23 @@
//
// Created by lukas on 03.02.22.
//
#ifndef OPENGLTEST_VERTEXBUFFER_H
#define OPENGLTEST_VERTEXBUFFER_H
#define GL_GLEXT_PROTOTYPES
class VertexBuffer {
private:
unsigned handle;
public:
VertexBuffer(const void* data, const unsigned size);
virtual ~VertexBuffer();
void Bind() const;
void UnBind() const;
};
#endif //OPENGLTEST_VERTEXBUFFER_H