engine.cpp
void Engine::CreateMesh(void)
{
mesh = content.Load<Mesh>("Models/cube2.obj");
shader = content.Load<Shader>("Shaders/simpleshader.glsl");
ModelMatrixUniformLocation = shader->GetUniformLocation("ModelMatrix");
ViewMatrixUniformLocation = shader->GetUniformLocation("ViewMatrix");
ProjectionMatrixUniformLocation = shader->GetUniformLocation("ProjectionMatrix");
ExitOnGLError("ERROR: Could not get the uniform locations");
Texture* texture0 = content.Load<Texture>("Textures/cube.bmp");
Tex0Loc = shader->GetUniformLocation("texture0");
ExitOnGLError("Error: Could not load texture");
}
void Engine::DrawMesh(void)
{
glUseProgram(shader->ID);
ExitOnGLError("ERROR: Could not use the shader"); // Error 1282
glUniformMatrix4fv(ModelMatrixUniformLocation, 1, GL_FALSE, value_ptr(ModelMatrix));
ExitOnGLError("ERROR: Could not set the model matrix");
ViewMatrix = lookAt(
vec3(0.0, 2.0, 4.0), //eye
vec3(0.0 ,0.0 , 0.0), //direction
vec3(0.0, 1.0, 0.0) //up
);
glUniformMatrix4fv(ViewMatrixUniformLocation, 1, GL_FALSE, value_ptr(ViewMatrix));
ExitOnGLError("ERROR: Could not set the view matrix");
ProjectionMatrix = perspective(45.0f, (float) CurrentWidth / CurrentHeight , 0.1f, 100.0f);
glUniformMatrix4fv(ProjectionMatrixUniformLocation, 1, GL_FALSE, value_ptr(ProjectionMatrix));
ExitOnGLError("ERROR: Could not set the projection matrix");
glUniform1i(Tex0Loc, 0);
mesh->Draw();
glUseProgram(0);
}
Shader.cpp
void Shader::Load(const char* filename)
{
ifstream in(filename, ios::binary);
if(!in)
{
cerr << "Could not open the file" << filename << endl; // Lanzamos un mensaje de error
exit(1); // exit(1) usado para abortar el programa
}
//Leemos los datos
std::string glsl_source((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
// Terminamos de leer el documento
in.close();
//miramos si hay algo
if(glsl_source.length() == 0)
{
cerr<<"ERROR: Any data in the file"<<endl;
exit(1);
}
std::string fs_shader = "#version 330\n #define COMPILING_FS\n" + glsl_source;
std::string vs_shader = "#version 330\n #define COMPILING_VS\n" + glsl_source;
// Creamos el programa
ID = glCreateProgram();
fragmentShaderID = CreateShader(fs_shader,GL_FRAGMENT_SHADER);
vertexShaderID = CreateShader(vs_shader,GL_VERTEX_SHADER);
Initialize();
}
GLuint Shader::CreateShader(std::string shader, GLenum type_shader)
{
GLuint shader_id = 0;
const char* glsl_cstr;
shader_id = glCreateShader(type_shader);
glShaderSource(shader_id, 1, &(glsl_cstr=shader.c_str()),NULL);
glCompileShader(shader_id);
ExitOnGLError("Could not compile the shader");
CheckCompile(shader_id);
return shader_id;
}
void Shader::Initialize(void)
{
glAttachShader(ID, fragmentShaderID);
ExitOnGLError("No se puede vincular el Fragment Shader");
glAttachShader(ID, vertexShaderID);
ExitOnGLError("No se puede vincular el Vertex Shader");
glLinkProgram(ID);
ExitOnGLError("No se puede conectar al programa");
CheckLink(ID);
}
GLint Shader::GetUniformLocation(const char* param)
{
return glGetUniformLocation(ID, param);
}
void CheckCompile(GLuint id)
{
GLint compiled = 0;
GLint length = 0;
glGetShaderiv(id, GL_COMPILE_STATUS, &compiled);
if(compiled == 0)
{
std::cerr<<"ERROR: The shader´s compilation fail"<<std::endl;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
if(length > 1) {
std::string log(length, '\0');
glGetShaderInfoLog(id, length, &length, &log[0]);
std::cerr<<log<<std::endl;
exit(1);
}
}else
{
std::cout<<"The shader´s compilation was successfully"<<std::endl;
}
}
void CheckLink(GLuint id)
{
//CheckAndLog(id,GL_LINK_STATUS,"conexion");
GLint compiled = 0;
GLint length = 0;
glGetProgramiv(id, GL_LINK_STATUS, &compiled);
if(compiled == 0)
{
std::cerr<<"ERROR: The linking fail"<<std::endl;
glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length);
if(length > 1) {
std::string log(length, '\0');
glGetProgramInfoLog(id, length, &length, &log[0]);
std::cerr<<log<<std::endl;
exit(1);
}
}else
{
std::cout<<"The linking was successfully"<<std::endl;
}
}
Edited by ShotoReaper, 14 October 2012 - 08:25 AM.