Compare commits

...

26 Commits
v2.0 ... master

Author SHA1 Message Date
Josh Holtrop
c30dd6587f update submodule locations 2011-05-17 22:50:15 -04:00
Josh Holtrop
e53e626a63 build mipmaps with GLU for texture shader 2011-05-16 16:26:07 -04:00
Josh Holtrop
ab56320889 pass uniforms to texture program correctly 2011-05-16 16:12:41 -04:00
Josh Holtrop
e12a86f858 add texture shaders 2011-05-16 16:09:29 -04:00
Josh Holtrop
10b945dd6b only two texture coordinates 2011-05-16 16:05:34 -04:00
Josh Holtrop
1c5b95a9fd build and use texture program/shaders 2011-05-16 16:04:22 -04:00
Josh Holtrop
1a1bdf64ce import glslUtil; use for creating shaders/program 2011-05-16 15:55:22 -04:00
Josh Holtrop
f672782921 preparing for textures 2011-05-16 15:43:54 -04:00
Josh Holtrop
0a8691414e use swizzle syntax 2011-05-16 15:38:04 -04:00
Josh Holtrop
5f55756a08 inlined renderMaterial() 2011-05-16 15:37:14 -04:00
Josh Holtrop
f0e4bbd3b1 loading textures properly, not rendering yet 2011-05-16 15:33:23 -04:00
Josh Holtrop
913e8f6287 build with loadTexture(), not rendering textures yet 2011-05-16 15:25:03 -04:00
Josh Holtrop
834f0c59c9 update wfobj 2011-05-16 15:02:12 -04:00
Josh Holtrop
96d6bd9e17 compile with -Wall 2011-05-16 14:59:18 -04:00
Josh Holtrop
816e1fad8c add loadTexture submodule 2011-05-16 14:56:11 -04:00
Josh Holtrop
46372edc67 update wfobj 2011-05-16 13:43:14 -04:00
Josh Holtrop
f3be2efddc tone down ambient light a bit 2011-05-05 14:04:09 -04:00
Josh Holtrop
59d0d66c44 fix shaders for proper lighting computation 2011-05-05 13:56:57 -04:00
Josh Holtrop
2e2b8225be glUniform4fv() wants a count of 1 for a vec4 update 2011-05-05 13:45:20 -04:00
Josh Holtrop
0d70153bf9 update correct uniforms 2011-05-05 13:39:03 -04:00
Josh Holtrop
b2fdaedf70 set shader uniform variables for material properties 2011-05-05 13:33:48 -04:00
Josh Holtrop
fb1d90e168 add initial shaders, draw using vertex attributes 2011-05-05 13:07:14 -04:00
Josh Holtrop
f3d9a3b437 add renderMaterial(), call glDrawElements() 2011-05-04 20:05:06 -04:00
Josh Holtrop
aafacfb3f9 set up vertex array pointers 2011-05-04 19:49:21 -04:00
Josh Holtrop
e8776bb719 new wfobj 2011-05-04 19:46:34 -04:00
Josh Holtrop
66c58b1f9a update wfobj to new version, remove draw() call 2011-05-04 16:26:38 -04:00
10 changed files with 270 additions and 16 deletions

8
.gitmodules vendored
View File

@ -1,3 +1,9 @@
[submodule "wfobj"]
path = wfobj
url = ../wfobj.git
url = ../util/wfobj.git
[submodule "loadTexture"]
path = loadTexture
url = ../util/loadTexture.git
[submodule "glslUtil"]
path = glslUtil
url = ../util/glslUtil.git

View File

@ -3,10 +3,14 @@
import os
import sys
env = Environment(LIBS = ['GL', 'GLU'])
env = Environment(LIBS = ['GL', 'GLU', 'SDL_image'],
CFLAGS = ['-Wall', '-DGL_GLEXT_PROTOTYPES'],
CXXFLAGS = ['-Wall', '-DGL_GLEXT_PROTOTYPES'])
env.ParseConfig('sdl-config --cflags --libs')
env.Program('wfobj-view', [Glob('*.cc'), Glob('wfobj/WFObj.cc')])
sources = [Glob('*.cc'), Glob('wfobj/WFObj.cc'), Glob('loadTexture/*.c'),
Glob('glslUtil/*.c')]
env.Program('wfobj-view', sources)
if len(os.listdir('wfobj')) == 0:
sys.stderr.write('Warning: wfobj submodule not initialized\n')

33
f_shader.glsl Normal file
View File

@ -0,0 +1,33 @@
uniform vec4 ambient, diffuse, specular;
uniform float shininess;
varying vec3 pos_i;
varying vec3 normal_i;
void main(void)
{
vec3 n, lightDir;
vec4 color;
float NdotL, RdotEye;
lightDir = normalize(vec3(-0.1, 0, -0.9));
color = vec4(0.2, 0.2, 0.2, 1.0) * ambient; /* ambient light */
n = normalize(normal_i);
NdotL = max(dot(n, -lightDir), 0.0);
if (NdotL > 0.0)
{
/* diffuse component */
color += diffuse * NdotL;
/* specular component */
RdotEye = dot(normalize(-pos_i), normalize(reflect(-lightDir, n)));
if (RdotEye > 0.0)
{
color += clamp(specular * pow(RdotEye, shininess), 0.0, 1.0);
}
}
gl_FragColor = color;
}

35
f_tex_shader.glsl Normal file
View File

@ -0,0 +1,35 @@
uniform vec4 ambient, specular;
uniform float shininess;
uniform sampler2D tex;
varying vec3 pos_i;
varying vec3 normal_i;
varying vec2 tex_coord_i;
void main(void)
{
vec3 n, lightDir;
vec4 color;
float NdotL, RdotEye;
lightDir = normalize(vec3(-0.1, 0, -0.9));
color = vec4(0.2, 0.2, 0.2, 1.0) * ambient; /* ambient light */
n = normalize(normal_i);
NdotL = max(dot(n, -lightDir), 0.0);
if (NdotL > 0.0)
{
/* diffuse component */
color += texture2D(tex, tex_coord_i) * NdotL;
/* specular component */
RdotEye = dot(normalize(-pos_i), normalize(reflect(-lightDir, n)));
if (RdotEye > 0.0)
{
color += clamp(specular * pow(RdotEye, shininess), 0.0, 1.0);
}
}
gl_FragColor = color;
}

1
glslUtil Submodule

@ -0,0 +1 @@
Subproject commit 060f3115bf0f1f659dd44ef3738c12327ec2ef43

1
loadTexture Submodule

@ -0,0 +1 @@
Subproject commit 87b5ff86910cc947fda91b271fa62b5b9276311d

13
v_shader.glsl Normal file
View File

@ -0,0 +1,13 @@
attribute vec3 pos;
attribute vec3 normal;
varying vec3 pos_i;
varying vec3 normal_i;
void main(void)
{
gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1);
pos_i = gl_Position.xyz;
normal_i = gl_NormalMatrix * normal;
}

16
v_tex_shader.glsl Normal file
View File

@ -0,0 +1,16 @@
attribute vec3 pos;
attribute vec3 normal;
attribute vec2 tex_coord;
varying vec3 pos_i;
varying vec3 normal_i;
varying vec2 tex_coord_i;
void main(void)
{
gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1);
pos_i = gl_Position.xyz;
tex_coord_i = tex_coord;
normal_i = gl_NormalMatrix * normal;
}

2
wfobj

@ -1 +1 @@
Subproject commit 933c4b457a7e4824d30692c2cac7a947caa0720d
Subproject commit 1ecc6d749be3bc3f5dc0de230b5e8e3ca079377a

View File

@ -1,10 +1,15 @@
/* Libraries we use */
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream>
#include "wfobj/WFObj.h"
#include "loadTexture/loadTexture.h"
#include "glslUtil/glslUtil.h"
using namespace std;
/* Some definitions */
@ -12,6 +17,12 @@ using namespace std;
#define HEIGHT 800
#define TITLE "Josh's Wavefront Object Viewer"
enum Locations {
LOC_POSITION,
LOC_NORMAL,
LOC_TEXTURE
};
class Viewer
{
public:
@ -29,8 +40,33 @@ private:
int m_startx, m_starty;
bool m_dragging;
float m_dist;
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)
{
GLuint id = loadTexture(fname);
if (id != 0)
{
int width, height;
glBindTexture(GL_TEXTURE_2D, id);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
char *data = (char *) malloc(4 * width * height);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, width, height, GL_RGBA,
GL_UNSIGNED_BYTE, data);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
free(data);
}
return id;
}
/* The program's main entry point */
int main(int argc, char * argv[])
{
@ -68,12 +104,62 @@ Viewer::Viewer(const char * filename)
{
m_dist = 5.0;
m_dragging = false;
if (!m_obj.load(filename))
if (!m_obj.load(filename, NULL, load_texture))
{
cerr << "Error loading " << filename << endl;
exit(1);
}
const static guAttribBinding bindings[] = {
{LOC_POSITION, "pos"},
{LOC_NORMAL, "normal"},
{0, NULL}
};
m_program = guMakeProgramFromFiles("v_shader.glsl", "f_shader.glsl",
bindings);
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);
}
guUniformLocation uniforms[] = {
{&m_ambient_loc, "ambient"},
{&m_diffuse_loc, "diffuse"},
{&m_specular_loc, "specular"},
{&m_shininess_loc, "shininess"},
{NULL, NULL}
};
guGetUniformLocations(m_program, uniforms);
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);
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);
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;
@ -85,20 +171,10 @@ void Viewer::initgl()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_CULL_FACE);
glViewport(0, 0, WIDTH, HEIGHT);
setProjection();
glLoadIdentity();
glGetFloatv(GL_MODELVIEW_MATRIX, m_rotationMatrix);
// float pos[] = {0.0, -1.0, 0.0, 0.0};
// glLightfv(GL_LIGHT0, GL_POSITION, pos);
GLfloat lightAmbient[] = {0.2, 0.2, 0.2, 1};
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
GLfloat ambient[] = {0.0, 0.0, 0.0, 1.0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
}
void Viewer::setProjection()
@ -116,7 +192,76 @@ void Viewer::display()
glLoadIdentity();
gluLookAt(0, -m_dist, 0, 0, 0, 0, 0, 0, 1);
glMultMatrixf(m_rotationMatrix);
m_obj.draw();
m_obj.bindBuffers();
GLuint program = m_program;
glUseProgram(m_program);
glEnableVertexAttribArray(LOC_POSITION);
glEnableVertexAttribArray(LOC_NORMAL);
int stride = m_obj.getStride();
glVertexAttribPointer(LOC_POSITION, 3, GL_FLOAT, GL_FALSE,
stride, (GLvoid *) m_obj.getVertexOffset());
glVertexAttribPointer(LOC_NORMAL, 3, GL_FLOAT, GL_FALSE,
stride, (GLvoid *) m_obj.getNormalOffset());
if (m_obj.doTextures())
{
glVertexAttribPointer(LOC_TEXTURE, 2, GL_FLOAT, GL_FALSE,
stride, (GLvoid *) m_obj.getTextureCoordOffset());
}
for (map<string, WFObj::Material>::iterator it =
m_obj.getMaterials().begin();
it != m_obj.getMaterials().end();
it++)
{
WFObj::Material & m = it->second;
if (m.flags & WFObj::Material::TEXTURE_BIT)
{
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);
}
}
if (m.flags & WFObj::Material::SHININESS_BIT)
{
if (program == m_tex_program)
glUniform1f(m_tex_shininess_loc, m.shininess);
else
glUniform1f(m_shininess_loc, m.shininess);
}
if (m.flags & WFObj::Material::AMBIENT_BIT)
{
if (program == m_tex_program)
glUniform4fv(m_tex_ambient_loc, 1, &m.ambient[0]);
else
glUniform4fv(m_ambient_loc, 1, &m.ambient[0]);
}
if (m.flags & WFObj::Material::DIFFUSE_BIT)
{
if (program != m_tex_program)
glUniform4fv(m_diffuse_loc, 1, &m.diffuse[0]);
}
if (m.flags & WFObj::Material::SPECULAR_BIT)
{
if (program == m_tex_program)
glUniform4fv(m_tex_specular_loc, 1, &m.specular[0]);
else
glUniform4fv(m_specular_loc, 1, &m.specular[0]);
}
glDrawElements(GL_TRIANGLES, m.num_vertices,
GL_UNSIGNED_SHORT,
(GLvoid *) (sizeof(GLushort) * m.first_vertex));
}
SDL_GL_SwapBuffers();
}