base setup for osx in place
This commit is contained in:
97
engine/shader.cpp
Normal file
97
engine/shader.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#include "pch.h"
|
||||
#include "shader.hpp"
|
||||
|
||||
bool Shader::create(std::string vertex, std::string fragment)
|
||||
{
|
||||
GLint status;
|
||||
static char infolog[4096];
|
||||
int infolen;
|
||||
const GLchar* source;
|
||||
|
||||
auto vs = glCreateShader(GL_VERTEX_SHADER);
|
||||
if (!vs)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
source = vertex.c_str();
|
||||
glShaderSource(vs, 1, &source, nullptr);
|
||||
glCompileShader(vs);
|
||||
glGetShaderiv(vs, GL_COMPILE_STATUS, &status);
|
||||
glGetShaderInfoLog(vs, sizeof(infolog), &infolen, infolog);
|
||||
if (infolen > 0)
|
||||
printf("VERTEX SHADER:\n%s", infolog);
|
||||
if (status == 0)
|
||||
{
|
||||
glDeleteShader(vs);
|
||||
return false;
|
||||
}
|
||||
|
||||
auto fs = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
if (!fs)
|
||||
{
|
||||
glDeleteShader(vs);
|
||||
return false;
|
||||
}
|
||||
source = fragment.c_str();
|
||||
glShaderSource(fs, 1, &source, nullptr);
|
||||
glCompileShader(fs);
|
||||
glGetShaderiv(fs, GL_COMPILE_STATUS, &status);
|
||||
glGetShaderInfoLog(fs, sizeof(infolog), &infolen, infolog);
|
||||
if (infolen > 0)
|
||||
printf("FRAGMENT SHADER:\n%s", infolog);
|
||||
if (status == 0)
|
||||
{
|
||||
glDeleteShader(vs);
|
||||
glDeleteShader(fs);
|
||||
return false;
|
||||
}
|
||||
|
||||
auto ps = glCreateProgram();
|
||||
if (!ps)
|
||||
{
|
||||
glDeleteShader(vs);
|
||||
glDeleteShader(fs);
|
||||
return false;
|
||||
}
|
||||
glAttachShader(ps, vs);
|
||||
glAttachShader(ps, fs);
|
||||
glDeleteShader(vs);
|
||||
glDeleteShader(fs);
|
||||
|
||||
glBindAttribLocation(ps, 0, "pos");
|
||||
glBindAttribLocation(ps, 1, "uvs");
|
||||
|
||||
glLinkProgram(ps);
|
||||
glGetShaderiv(ps, GL_LINK_STATUS, &status);
|
||||
glGetShaderInfoLog(ps, sizeof(infolog), &infolen, infolog);
|
||||
if (infolen > 0)
|
||||
printf("LINK SHADER:\n%s", infolog);
|
||||
if (status == 0)
|
||||
{
|
||||
glDeleteProgram(ps);
|
||||
return false;
|
||||
}
|
||||
|
||||
prog = ps;
|
||||
|
||||
return true;
|
||||
}
|
||||
void Shader::use()
|
||||
{
|
||||
glUseProgram(prog);
|
||||
}
|
||||
void Shader::u_vec4(std::string name, const glm::vec4& v)
|
||||
{
|
||||
auto loc = glGetUniformLocation(prog, name.c_str());
|
||||
glUniform4fv(loc, 1, glm::value_ptr(v));
|
||||
}
|
||||
void Shader::u_mat4(std::string name, const glm::mat4& m)
|
||||
{
|
||||
auto loc = glGetUniformLocation(prog, name.c_str());
|
||||
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(m));
|
||||
}
|
||||
void Shader::u_int(std::string name, int i)
|
||||
{
|
||||
auto loc = glGetUniformLocation(prog, name.c_str());
|
||||
glUniform1i(loc, i);
|
||||
}
|
||||
Reference in New Issue
Block a user