Browse Source

shader class

testing
= 3 years ago
parent
commit
799e3dec75

+ 6
- 1
include/graphics/shader.h View File

@@ -4,12 +4,16 @@

#pragma once

#include <GL/glew.h>
#include <GL/gl.h>

#include <string>

namespace vtk {

class Shader {
public:
Shader();
bool activate(); //Must be called to use shader

//Load shader, type 0 = Vert, 1 = Frag, 2 = Geometry
@@ -18,9 +22,10 @@ public:
const std::string& geomShader = "");

GLuint loadShader(const std::string& code, const GLenum& shaderType);

protected:
GLuint mShaderID;

};


+ 1
- 1
src/fileutils.cpp View File

@@ -18,7 +18,7 @@ std::string loadFileIntoString(const std::string& fName) {
spdlog::get("file")->info("Loaded file: {}", fName);
return contents;
}
spdlog::get("file")->warn("Unable to load: {}", fName);
spdlog::get("file")->warn("Unable to load: {}, returning empty string", fName);
return "";
}


+ 89
- 1
src/graphics/shader.cpp View File

@@ -1,5 +1,93 @@
/*
* Shader source
*/
#include <stdlib.h>
#include "graphics/shader.h"
#include "graphics/glstate.h"
#include "spdlog/spdlog.h"
#include "fileutils.h"
#include "loadShader.h"


namespace vtk {

Shader::Shader() {
mShaderID = 0;
}

bool Shader::activate() {
if (mShaderID == 0){
spdlog::get("general")->warn("Attempted to load unloaded shader");
return false;
}
//gls::setShader(mShaderID);
glUseProgram(mShaderID);
return true;
}


bool Shader::loadShaderFiles(const std::string& vertShader,
const std::string& fragShader,
const std::string& geomShader)
{
auto log = spdlog::get("general");
bool hasGeomShader = geomShader != "";
// IDs
GLuint vShaderID;
GLuint fShaderID;
GLuint gShaderID;

auto vShaderCode = file::loadFileIntoString(vertShader);
auto fShaderCode = file::loadFileIntoString(fragShader);

std::string gShaderCode; //only load geometry shader if path is provided
if (hasGeomShader) {
gShaderCode = file::loadFileIntoString(geomShader);
}

log->debug("Compiling shader: {}", vertShader);
vShaderID = loadShader(vShaderCode, GL_VERTEX_SHADER);

log->debug("Compiling shader: {}", fragShader);
fShaderID = loadShader(fShaderCode, GL_FRAGMENT_SHADER);
// Link the program
mShaderID = glCreateProgram();
glAttachShader(mShaderID, vShaderID);
glAttachShader(mShaderID, fShaderID);
glLinkProgram(mShaderID);

GLint result;
int infoLogLength;

// Check the program
glGetProgramiv(mShaderID, GL_LINK_STATUS, &result);
glGetProgramiv(mShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
std::vector<char> ProgramErrorMessage(std::max(infoLogLength, int(1)) );
glGetProgramInfoLog(mShaderID, infoLogLength, NULL, &ProgramErrorMessage[0]);
log->debug("Linking errors: {}", &ProgramErrorMessage[0]);
glDeleteShader(vShaderID);
glDeleteShader(fShaderID);
}

GLuint Shader::loadShader(const std::string& code, const GLenum& shaderType) {

GLuint id = glCreateShader(shaderType);
GLint result = GL_FALSE;
int infoLogLength;

char const * sourcePointer = code.c_str();
glShaderSource(id, 1, &sourcePointer , NULL);
glCompileShader(id);

// Check Shader
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &infoLogLength);
std::vector<char> shaderErrorMessage(std::max(infoLogLength, int(1)));
glGetShaderInfoLog(id, infoLogLength, NULL, &shaderErrorMessage[0]);
spdlog::get("general")->debug("Shader errors: {}", &shaderErrorMessage[0]);
return id;
}

//namespace vtk
}

+ 1
- 0
src/loggersetup.cpp View File

@@ -18,5 +18,6 @@ namespace vtk {
auto fileLogger = std::make_shared<spdlog::logger>("file", begin(logSinks), end(logSinks));
spdlog::register_logger(generalLogger);
spdlog::register_logger(fileLogger);
spdlog::set_level(spdlog::level::debug);
}
}

+ 3
- 1
src/scenes/testscene.cpp View File

@@ -64,6 +64,7 @@ void TestScene::init() {
glEnableVertexAttribArray(0);

//shaders
mCursorShader.loadShaderFiles("res/shaders/test.vert.glsl", "res/shaders/test.frag.glsl");
cursorShaders = LoadShaders("res/shaders/test.vert.glsl", "res/shaders/test.frag.glsl");
shaders = LoadShaders("res/shaders/voxelvert.vert.glsl", "res/shaders/voxelfrag.frag.glsl");

@@ -190,7 +191,8 @@ void TestScene::draw() {
world.draw();
glDisable(GL_DEPTH_TEST);
glUseProgram(cursorShaders);
//glUseProgram(mCursorShader.mShaderID);
mCursorShader.activate();
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
glEnable(GL_DEPTH_TEST);

+ 2
- 0
src/scenes/testscene.h View File

@@ -24,6 +24,7 @@
#include "inputhandler.h"
#include "world.h"
#include "graphics/skybox.h"
#include "graphics/shader.h"


#include <SDL2/SDL.h>
@@ -49,6 +50,7 @@ protected:
unsigned vao;
unsigned shaders;
unsigned cursorShaders;
Shader mCursorShader;
float dTime;
Camera camera;
SDL_Event event;

Loading…
Cancel
Save