set shader uniform variables for material properties
This commit is contained in:
parent
fb1d90e168
commit
b2fdaedf70
@ -1,7 +1,9 @@
|
|||||||
|
|
||||||
|
uniform vec4 ambient, diffuse, specular;
|
||||||
|
uniform float shininess;
|
||||||
|
|
||||||
varying vec3 eye_pos_i;
|
varying vec3 eye_pos_i;
|
||||||
varying vec3 normal_i;
|
varying vec3 normal_i;
|
||||||
varying vec4 diffuse_i;
|
|
||||||
|
|
||||||
void main(void)
|
void main(void)
|
||||||
{
|
{
|
||||||
@ -10,7 +12,7 @@ void main(void)
|
|||||||
float NdotL, RdotEye;
|
float NdotL, RdotEye;
|
||||||
|
|
||||||
lightDir = vec3(-0.1, 0, -0.9);
|
lightDir = vec3(-0.1, 0, -0.9);
|
||||||
color = vec4(0.2, 0.2, 0.2, 1.0); /* ambient light */
|
color = ambient; /* ambient light */
|
||||||
n = normalize(normal_i);
|
n = normalize(normal_i);
|
||||||
|
|
||||||
NdotL = max(dot(n, -lightDir), 0.0);
|
NdotL = max(dot(n, -lightDir), 0.0);
|
||||||
@ -18,12 +20,12 @@ void main(void)
|
|||||||
if (NdotL > 0.0)
|
if (NdotL > 0.0)
|
||||||
{
|
{
|
||||||
/* diffuse component */
|
/* diffuse component */
|
||||||
color += diffuse_i * NdotL;
|
color += diffuse * NdotL;
|
||||||
/* specular component */
|
/* specular component */
|
||||||
RdotEye = dot(normalize(eye_pos_i), normalize(reflect(-lightDir, n)));
|
RdotEye = dot(normalize(eye_pos_i), normalize(reflect(-lightDir, n)));
|
||||||
if (RdotEye > 0.0)
|
if (RdotEye > 0.0)
|
||||||
{
|
{
|
||||||
color += pow(RdotEye, 96.5);
|
color += specular * pow(RdotEye, shininess);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,16 +1,13 @@
|
|||||||
|
|
||||||
attribute vec3 pos;
|
attribute vec3 pos;
|
||||||
attribute vec3 color;
|
|
||||||
attribute vec3 normal;
|
attribute vec3 normal;
|
||||||
|
|
||||||
varying vec3 eye_pos_i;
|
varying vec3 eye_pos_i;
|
||||||
varying vec3 normal_i;
|
varying vec3 normal_i;
|
||||||
varying vec4 diffuse_i;
|
|
||||||
|
|
||||||
void main(void)
|
void main(void)
|
||||||
{
|
{
|
||||||
gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1);
|
gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1);
|
||||||
eye_pos_i = vec3(gl_Position.x, gl_Position.y, gl_Position.z);
|
eye_pos_i = vec3(gl_Position.x, gl_Position.y, gl_Position.z);
|
||||||
normal_i = gl_NormalMatrix * normal;
|
normal_i = gl_NormalMatrix * normal;
|
||||||
diffuse_i = vec4(color, 1);
|
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,7 @@ private:
|
|||||||
bool m_dragging;
|
bool m_dragging;
|
||||||
float m_dist;
|
float m_dist;
|
||||||
GLuint m_program, m_vs, m_fs;
|
GLuint m_program, m_vs, m_fs;
|
||||||
|
GLint m_ambient_loc, m_diffuse_loc, m_specular_loc, m_shininess_loc;
|
||||||
};
|
};
|
||||||
|
|
||||||
char * loadFile(const char *fname)
|
char * loadFile(const char *fname)
|
||||||
@ -193,6 +194,23 @@ Viewer::Viewer(const char * filename)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_ambient_loc = glGetUniformLocation(m_program, "ambient");
|
||||||
|
m_diffuse_loc = glGetUniformLocation(m_program, "diffuse");
|
||||||
|
m_specular_loc = glGetUniformLocation(m_program, "specular");
|
||||||
|
m_shininess_loc = glGetUniformLocation(m_program, "shininess");
|
||||||
|
if (m_ambient_loc < 0 || m_diffuse_loc < 0 || m_specular_loc < 0
|
||||||
|
|| m_shininess_loc < 0)
|
||||||
|
{
|
||||||
|
cerr << "glGetUniformLocation() returned < 0" << endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
glUseProgram(m_program);
|
||||||
|
glUniform4f(m_ambient_loc, 0.2, 0.2, 0.2, 1.0);
|
||||||
|
glUniform4f(m_diffuse_loc, 1.0, 1.0, 1.0, 1.0);
|
||||||
|
glUniform4f(m_specular_loc, 1.0, 1.0, 1.0, 1.0);
|
||||||
|
glUniform1f(m_shininess_loc, 85.0);
|
||||||
|
|
||||||
/* Print out the object's size */
|
/* Print out the object's size */
|
||||||
const float * aabb = m_obj.getAABB();
|
const float * aabb = m_obj.getAABB();
|
||||||
cout << "Object width: " << (aabb[3]-aabb[0]) << endl;
|
cout << "Object width: " << (aabb[3]-aabb[0]) << endl;
|
||||||
@ -321,13 +339,13 @@ void Viewer::run()
|
|||||||
void Viewer::renderMaterial(const WFObj::Material & m)
|
void Viewer::renderMaterial(const WFObj::Material & m)
|
||||||
{
|
{
|
||||||
if (m.flags & WFObj::Material::SHININESS_BIT)
|
if (m.flags & WFObj::Material::SHININESS_BIT)
|
||||||
glMaterialf(GL_FRONT, GL_SHININESS, m.shininess);
|
glUniform1f(m_shininess_loc, m.shininess);
|
||||||
if (m.flags & WFObj::Material::AMBIENT_BIT)
|
if (m.flags & WFObj::Material::AMBIENT_BIT)
|
||||||
glMaterialfv(GL_FRONT, GL_AMBIENT, &m.ambient[0]);
|
glUniform4fv(m_ambient_loc, 4, &m.ambient[0]);
|
||||||
if (m.flags & WFObj::Material::DIFFUSE_BIT)
|
if (m.flags & WFObj::Material::DIFFUSE_BIT)
|
||||||
glMaterialfv(GL_FRONT, GL_DIFFUSE, &m.diffuse[0]);
|
glUniform4fv(m_diffuse_loc, 4, &m.ambient[0]);
|
||||||
if (m.flags & WFObj::Material::SPECULAR_BIT)
|
if (m.flags & WFObj::Material::SPECULAR_BIT)
|
||||||
glMaterialfv(GL_FRONT, GL_SPECULAR, &m.specular[0]);
|
glUniform4fv(m_specular_loc, 4, &m.ambient[0]);
|
||||||
if (m.flags & WFObj::Material::TEXTURE_BIT)
|
if (m.flags & WFObj::Material::TEXTURE_BIT)
|
||||||
{
|
{
|
||||||
cerr << "error: textured materials not implemented yet" << endl;
|
cerr << "error: textured materials not implemented yet" << endl;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user