Compare commits
26 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
c30dd6587f | ||
|
e53e626a63 | ||
|
ab56320889 | ||
|
e12a86f858 | ||
|
10b945dd6b | ||
|
1c5b95a9fd | ||
|
1a1bdf64ce | ||
|
f672782921 | ||
|
0a8691414e | ||
|
5f55756a08 | ||
|
f0e4bbd3b1 | ||
|
913e8f6287 | ||
|
834f0c59c9 | ||
|
96d6bd9e17 | ||
|
816e1fad8c | ||
|
46372edc67 | ||
|
f3be2efddc | ||
|
59d0d66c44 | ||
|
2e2b8225be | ||
|
0d70153bf9 | ||
|
b2fdaedf70 | ||
|
fb1d90e168 | ||
|
f3d9a3b437 | ||
|
aafacfb3f9 | ||
|
e8776bb719 | ||
|
66c58b1f9a |
8
.gitmodules
vendored
8
.gitmodules
vendored
@ -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
|
||||
|
@ -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
33
f_shader.glsl
Normal 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
35
f_tex_shader.glsl
Normal 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
1
glslUtil
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 060f3115bf0f1f659dd44ef3738c12327ec2ef43
|
1
loadTexture
Submodule
1
loadTexture
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 87b5ff86910cc947fda91b271fa62b5b9276311d
|
13
v_shader.glsl
Normal file
13
v_shader.glsl
Normal 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
16
v_tex_shader.glsl
Normal 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
2
wfobj
@ -1 +1 @@
|
||||
Subproject commit 933c4b457a7e4824d30692c2cac7a947caa0720d
|
||||
Subproject commit 1ecc6d749be3bc3f5dc0de230b5e8e3ca079377a
|
169
wfobj-view.cc
169
wfobj-view.cc
@ -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();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user