build and use texture program/shaders
This commit is contained in:
parent
1a1bdf64ce
commit
1c5b95a9fd
@ -40,8 +40,10 @@ private:
|
||||
int m_startx, m_starty;
|
||||
bool m_dragging;
|
||||
float m_dist;
|
||||
GLuint m_program;
|
||||
GLuint m_program, m_tex_program;
|
||||
GLint m_ambient_loc, m_diffuse_loc, m_specular_loc, m_shininess_loc;
|
||||
GLint m_tex_ambient_loc, m_tex_specular_loc, m_tex_shininess_loc,
|
||||
m_tex_tex_loc;
|
||||
};
|
||||
|
||||
static GLuint load_texture(const char *fname)
|
||||
@ -106,7 +108,16 @@ Viewer::Viewer(const char * filename)
|
||||
};
|
||||
m_program = guMakeProgramFromFiles("v_shader.glsl", "f_shader.glsl",
|
||||
bindings);
|
||||
if (m_program == 0)
|
||||
|
||||
const static guAttribBinding tex_bindings[] = {
|
||||
{LOC_POSITION, "pos"},
|
||||
{LOC_NORMAL, "normal"},
|
||||
{LOC_TEXTURE, "tex_coord"},
|
||||
{0, NULL}
|
||||
};
|
||||
m_tex_program = guMakeProgramFromFiles("v_tex_shader.glsl",
|
||||
"f_tex_shader.glsl", tex_bindings);
|
||||
if (m_program == 0 || m_tex_program == 0)
|
||||
{
|
||||
exit(1);
|
||||
}
|
||||
@ -119,11 +130,15 @@ Viewer::Viewer(const char * filename)
|
||||
{NULL, NULL}
|
||||
};
|
||||
guGetUniformLocations(m_program, uniforms);
|
||||
if (m_ambient_loc < 0 || m_diffuse_loc < 0 || m_specular_loc < 0
|
||||
|| m_shininess_loc < 0)
|
||||
{
|
||||
exit(1);
|
||||
}
|
||||
|
||||
guUniformLocation tex_uniforms[] = {
|
||||
{&m_tex_ambient_loc, "ambient"},
|
||||
{&m_tex_specular_loc, "specular"},
|
||||
{&m_tex_shininess_loc, "shininess"},
|
||||
{&m_tex_tex_loc, "tex"},
|
||||
{NULL, NULL}
|
||||
};
|
||||
guGetUniformLocations(m_tex_program, tex_uniforms);
|
||||
|
||||
glUseProgram(m_program);
|
||||
glUniform4f(m_ambient_loc, 0.2, 0.2, 0.2, 1.0);
|
||||
@ -131,6 +146,11 @@ Viewer::Viewer(const char * filename)
|
||||
glUniform4f(m_specular_loc, 1.0, 1.0, 1.0, 1.0);
|
||||
glUniform1f(m_shininess_loc, 85.0);
|
||||
|
||||
glUseProgram(m_tex_program);
|
||||
glUniform4f(m_tex_ambient_loc, 0.2, 0.2, 0.2, 1.0);
|
||||
glUniform4f(m_tex_specular_loc, 1.0, 1.0, 1.0, 1.0);
|
||||
glUniform1f(m_tex_shininess_loc, 85.0);
|
||||
|
||||
/* Print out the object's size */
|
||||
const float * aabb = m_obj.getAABB();
|
||||
cout << "Object width: " << (aabb[3]-aabb[0]) << endl;
|
||||
@ -165,6 +185,7 @@ void Viewer::display()
|
||||
glMultMatrixf(m_rotationMatrix);
|
||||
|
||||
m_obj.bindBuffers();
|
||||
GLuint program = m_program;
|
||||
glUseProgram(m_program);
|
||||
glEnableVertexAttribArray(LOC_POSITION);
|
||||
glEnableVertexAttribArray(LOC_NORMAL);
|
||||
@ -194,7 +215,21 @@ void Viewer::display()
|
||||
glUniform4fv(m_specular_loc, 1, &m.specular[0]);
|
||||
if (m.flags & WFObj::Material::TEXTURE_BIT)
|
||||
{
|
||||
cerr << "error: textured materials not implemented yet" << endl;
|
||||
if (program != m_tex_program)
|
||||
{
|
||||
glUseProgram(m_tex_program);
|
||||
program = m_tex_program;
|
||||
glEnableVertexAttribArray(LOC_TEXTURE);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (program != m_program)
|
||||
{
|
||||
glUseProgram(m_program);
|
||||
program = m_program;
|
||||
glDisableVertexAttribArray(LOC_TEXTURE);
|
||||
}
|
||||
}
|
||||
glDrawElements(GL_TRIANGLES, m.num_vertices,
|
||||
GL_UNSIGNED_SHORT,
|
||||
|
Loading…
x
Reference in New Issue
Block a user