Compare commits
No commits in common. "master" and "v2.0" have entirely different histories.
8
.gitmodules
vendored
8
.gitmodules
vendored
@ -1,9 +1,3 @@
|
|||||||
[submodule "wfobj"]
|
[submodule "wfobj"]
|
||||||
path = wfobj
|
path = wfobj
|
||||||
url = ../util/wfobj.git
|
url = ../wfobj.git
|
||||||
[submodule "loadTexture"]
|
|
||||||
path = loadTexture
|
|
||||||
url = ../util/loadTexture.git
|
|
||||||
[submodule "glslUtil"]
|
|
||||||
path = glslUtil
|
|
||||||
url = ../util/glslUtil.git
|
|
||||||
|
@ -3,14 +3,10 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
env = Environment(LIBS = ['GL', 'GLU', 'SDL_image'],
|
env = Environment(LIBS = ['GL', 'GLU'])
|
||||||
CFLAGS = ['-Wall', '-DGL_GLEXT_PROTOTYPES'],
|
|
||||||
CXXFLAGS = ['-Wall', '-DGL_GLEXT_PROTOTYPES'])
|
|
||||||
env.ParseConfig('sdl-config --cflags --libs')
|
env.ParseConfig('sdl-config --cflags --libs')
|
||||||
|
|
||||||
sources = [Glob('*.cc'), Glob('wfobj/WFObj.cc'), Glob('loadTexture/*.c'),
|
env.Program('wfobj-view', [Glob('*.cc'), Glob('wfobj/WFObj.cc')])
|
||||||
Glob('glslUtil/*.c')]
|
|
||||||
env.Program('wfobj-view', sources)
|
|
||||||
|
|
||||||
if len(os.listdir('wfobj')) == 0:
|
if len(os.listdir('wfobj')) == 0:
|
||||||
sys.stderr.write('Warning: wfobj submodule not initialized\n')
|
sys.stderr.write('Warning: wfobj submodule not initialized\n')
|
||||||
|
@ -1,33 +0,0 @@
|
|||||||
|
|
||||||
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;
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
|
|
||||||
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
1
glslUtil
@ -1 +0,0 @@
|
|||||||
Subproject commit 060f3115bf0f1f659dd44ef3738c12327ec2ef43
|
|
@ -1 +0,0 @@
|
|||||||
Subproject commit 87b5ff86910cc947fda91b271fa62b5b9276311d
|
|
@ -1,13 +0,0 @@
|
|||||||
|
|
||||||
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;
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
|
|
||||||
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 1ecc6d749be3bc3f5dc0de230b5e8e3ca079377a
|
Subproject commit 933c4b457a7e4824d30692c2cac7a947caa0720d
|
169
wfobj-view.cc
169
wfobj-view.cc
@ -1,15 +1,10 @@
|
|||||||
|
|
||||||
/* Libraries we use */
|
/* Libraries we use */
|
||||||
#include <stdlib.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <GL/gl.h>
|
#include <GL/gl.h>
|
||||||
#include <GL/glu.h>
|
#include <GL/glu.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "wfobj/WFObj.h"
|
#include "wfobj/WFObj.h"
|
||||||
#include "loadTexture/loadTexture.h"
|
|
||||||
#include "glslUtil/glslUtil.h"
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
/* Some definitions */
|
/* Some definitions */
|
||||||
@ -17,12 +12,6 @@ using namespace std;
|
|||||||
#define HEIGHT 800
|
#define HEIGHT 800
|
||||||
#define TITLE "Josh's Wavefront Object Viewer"
|
#define TITLE "Josh's Wavefront Object Viewer"
|
||||||
|
|
||||||
enum Locations {
|
|
||||||
LOC_POSITION,
|
|
||||||
LOC_NORMAL,
|
|
||||||
LOC_TEXTURE
|
|
||||||
};
|
|
||||||
|
|
||||||
class Viewer
|
class Viewer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -40,33 +29,8 @@ private:
|
|||||||
int m_startx, m_starty;
|
int m_startx, m_starty;
|
||||||
bool m_dragging;
|
bool m_dragging;
|
||||||
float m_dist;
|
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 */
|
/* The program's main entry point */
|
||||||
int main(int argc, char * argv[])
|
int main(int argc, char * argv[])
|
||||||
{
|
{
|
||||||
@ -104,62 +68,12 @@ Viewer::Viewer(const char * filename)
|
|||||||
{
|
{
|
||||||
m_dist = 5.0;
|
m_dist = 5.0;
|
||||||
m_dragging = false;
|
m_dragging = false;
|
||||||
if (!m_obj.load(filename, NULL, load_texture))
|
if (!m_obj.load(filename))
|
||||||
{
|
{
|
||||||
cerr << "Error loading " << filename << endl;
|
cerr << "Error loading " << filename << endl;
|
||||||
exit(1);
|
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 */
|
/* 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;
|
||||||
@ -171,10 +85,20 @@ void Viewer::initgl()
|
|||||||
{
|
{
|
||||||
glClearColor(0.0, 0.0, 0.0, 0.0);
|
glClearColor(0.0, 0.0, 0.0, 0.0);
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glShadeModel(GL_SMOOTH);
|
||||||
|
glEnable(GL_LIGHTING);
|
||||||
|
glEnable(GL_LIGHT0);
|
||||||
|
glEnable(GL_CULL_FACE);
|
||||||
glViewport(0, 0, WIDTH, HEIGHT);
|
glViewport(0, 0, WIDTH, HEIGHT);
|
||||||
setProjection();
|
setProjection();
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
glGetFloatv(GL_MODELVIEW_MATRIX, m_rotationMatrix);
|
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()
|
void Viewer::setProjection()
|
||||||
@ -192,76 +116,7 @@ void Viewer::display()
|
|||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
gluLookAt(0, -m_dist, 0, 0, 0, 0, 0, 0, 1);
|
gluLookAt(0, -m_dist, 0, 0, 0, 0, 0, 0, 1);
|
||||||
glMultMatrixf(m_rotationMatrix);
|
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();
|
SDL_GL_SwapBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user