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

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

@ -46,9 +46,9 @@ unsigned Shader::getHandle() const {
}
unsigned Shader::getUniformHandle(std::string name) {
if(uniformhandles.find(name) != uniformhandles.end()){
if (uniformhandles.find(name) != uniformhandles.end()) {
return uniformhandles[name];
}else{
} else {
const unsigned id = glGetUniformLocation(mProgHandle, name.c_str());
uniformhandles[name] = id;
return id;
@ -82,37 +82,56 @@ 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) {
// 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;
uint VertexShaderID;
uint GeometryShaderID;
uint FragmentShaderID;
if (!vertex_src.empty()) {
// Compile Vertex Shader
printf("Compiling vertex shader\n");
VertexShaderID = compileShader(vertex_src.c_str(), GL_VERTEX_SHADER);
if (VertexShaderID == 0) {
printf("Error Compiling vertex 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;
if (!geometry_src.empty()) {
printf("Compiling geometry shader\n");
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;
if (!fragment_src.empty()) {
printf("Compiling fragment shader\n");
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);
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
glDetachShader(ProgramID, VertexShaderID);
glDetachShader(ProgramID, FragmentShaderID);
if (!vertex_src.empty()){
glDetachShader(ProgramID, VertexShaderID);
glDeleteShader(VertexShaderID);
}
glDeleteShader(VertexShaderID);
glDeleteShader(FragmentShaderID);
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;
};