allow creating shaders from file sources and specifying shader source length
This commit is contained in:
parent
f7955dea0b
commit
71cff1c45e
@ -12,7 +12,9 @@ namespace glcxx
|
|||||||
|
|
||||||
~Shader();
|
~Shader();
|
||||||
|
|
||||||
void create(GLenum shaderType, const char *source);
|
void create(GLenum shader_type, const char * source, int length = -1);
|
||||||
|
|
||||||
|
void create_from_file(GLenum shader_type, const char * filename);
|
||||||
|
|
||||||
GLuint id() const { return m_id; }
|
GLuint id() const { return m_id; }
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
#include "glcxx/Shader.hpp"
|
#include "glcxx/Shader.hpp"
|
||||||
#include "glcxx/Error.hpp"
|
#include "glcxx/Error.hpp"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace glcxx
|
namespace glcxx
|
||||||
{
|
{
|
||||||
@ -17,17 +19,18 @@ namespace glcxx
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::create(GLenum shaderType, const char *source)
|
void Shader::create(GLenum shader_type, const char * source, int length)
|
||||||
{
|
{
|
||||||
GLint status;
|
GLint status;
|
||||||
|
|
||||||
m_id = glCreateShader(shaderType);
|
m_id = glCreateShader(shader_type);
|
||||||
if (m_id == 0u)
|
if (m_id == 0u)
|
||||||
{
|
{
|
||||||
throw Error("Failed to allocate an OpenGL shader");
|
throw Error("Failed to allocate an OpenGL shader");
|
||||||
}
|
}
|
||||||
|
|
||||||
glShaderSource(m_id, 1, &source, NULL);
|
GLint lengths[1] = {length};
|
||||||
|
glShaderSource(m_id, 1, &source, &lengths[0]);
|
||||||
|
|
||||||
glCompileShader(m_id);
|
glCompileShader(m_id);
|
||||||
|
|
||||||
@ -38,7 +41,7 @@ namespace glcxx
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string message = "Error compiling ";
|
std::string message = "Error compiling ";
|
||||||
switch (shaderType)
|
switch (shader_type)
|
||||||
{
|
{
|
||||||
case GL_VERTEX_SHADER:
|
case GL_VERTEX_SHADER:
|
||||||
message += "vertex";
|
message += "vertex";
|
||||||
@ -66,4 +69,20 @@ namespace glcxx
|
|||||||
glDeleteShader(m_id);
|
glDeleteShader(m_id);
|
||||||
throw Error(message);
|
throw Error(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Shader::create_from_file(GLenum shader_type, const char * filename)
|
||||||
|
{
|
||||||
|
std::ifstream ifs;
|
||||||
|
ifs.open(filename);
|
||||||
|
if (!ifs.is_open())
|
||||||
|
{
|
||||||
|
throw Error(std::string("Error opening ") + filename);
|
||||||
|
}
|
||||||
|
ifs.seekg(0, ifs.end);
|
||||||
|
int length = ifs.tellg();
|
||||||
|
ifs.seekg(0, ifs.beg);
|
||||||
|
std::vector<char> file_contents(length);
|
||||||
|
ifs.read(&file_contents[0], length);
|
||||||
|
create(shader_type, &file_contents[0], length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user