Compare commits
33 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 | ||
|
6a8e7b4b4b | ||
|
b06bef6351 | ||
|
132d481fef | ||
|
54e3970362 | ||
|
bead7f9690 | ||
|
698c7e99b7 | ||
|
e305a264f1 |
14
.gitmodules
vendored
14
.gitmodules
vendored
@ -1,9 +1,9 @@
|
|||||||
[submodule "wfobj"]
|
[submodule "wfobj"]
|
||||||
path = wfobj
|
path = wfobj
|
||||||
url = ../wfobj.git
|
url = ../util/wfobj.git
|
||||||
[submodule "FileLoader"]
|
[submodule "loadTexture"]
|
||||||
path = FileLoader
|
path = loadTexture
|
||||||
url = ../FileLoader.git
|
url = ../util/loadTexture.git
|
||||||
[submodule "TextureLoader"]
|
[submodule "glslUtil"]
|
||||||
path = TextureLoader
|
path = glslUtil
|
||||||
url = ../TextureLoader.git
|
url = ../util/glslUtil.git
|
||||||
|
@ -1 +0,0 @@
|
|||||||
Subproject commit 8abf3ff02b6a8cc7024006a9379fd7e8b07cd4a6
|
|
39
Makefile
39
Makefile
@ -1,39 +1,8 @@
|
|||||||
|
|
||||||
WINCHECK := $(shell which msys-1.0.dll >/dev/null 2>&1; if [ $$? -eq 0 ]; then echo MSYS; fi)
|
export SCONSFLAGS := -Q
|
||||||
ifeq ($(strip $(WINCHECK)),)
|
|
||||||
WINDOWS := 0
|
|
||||||
else
|
|
||||||
WINDOWS := 1
|
|
||||||
endif
|
|
||||||
|
|
||||||
ifeq ($(WINDOWS),1)
|
all:
|
||||||
GLLIBS := -lopengl32 -lglu32
|
@scons
|
||||||
WINDOWSLIBS := -lmingw32
|
|
||||||
else
|
|
||||||
GLLIBS := -lGL -lGLU
|
|
||||||
endif
|
|
||||||
|
|
||||||
export CXXFLAGS := -O2 $(shell sdl-config --cflags) -I$(CURDIR)
|
|
||||||
LDFLAGS := $(GLLIBS) $(WINDOWSLIBS) $(shell sdl-config --libs) -lSDL_image
|
|
||||||
TARGET := wfobj-view
|
|
||||||
|
|
||||||
.PHONY: all clean TextureCache wfobj
|
|
||||||
|
|
||||||
all: $(TARGET)
|
|
||||||
|
|
||||||
$(TARGET): $(TARGET).o TextureCache wfobj
|
|
||||||
$(CXX) -o $(TARGET) $< TextureCache/TextureCache.o wfobj/WFObj.o $(LDFLAGS)
|
|
||||||
|
|
||||||
%.o: %.cc
|
|
||||||
$(CXX) -c -o $@ $< $(CXXFLAGS)
|
|
||||||
|
|
||||||
TextureCache:
|
|
||||||
make -C TextureCache
|
|
||||||
|
|
||||||
wfobj:
|
|
||||||
make -C wfobj
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
make -C TextureCache clean
|
@scons -c
|
||||||
make -C wfobj clean
|
|
||||||
-rm -f *.o *~ $(TARGET)
|
|
||||||
|
17
SConstruct
Normal file
17
SConstruct
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# vim:filetype=python
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
env = Environment(LIBS = ['GL', 'GLU', 'SDL_image'],
|
||||||
|
CFLAGS = ['-Wall', '-DGL_GLEXT_PROTOTYPES'],
|
||||||
|
CXXFLAGS = ['-Wall', '-DGL_GLEXT_PROTOTYPES'])
|
||||||
|
env.ParseConfig('sdl-config --cflags --libs')
|
||||||
|
|
||||||
|
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')
|
||||||
|
sys.stderr.write('Warning: perhaps you should run `git submodule update --init`\n')
|
@ -1,12 +0,0 @@
|
|||||||
|
|
||||||
CXX := g++
|
|
||||||
CXXFLAGS ?= -O2
|
|
||||||
OBJS := TextureCache.o
|
|
||||||
|
|
||||||
all: $(OBJS)
|
|
||||||
|
|
||||||
%.o: %.cc %.hh
|
|
||||||
$(CXX) -c -o $@ $< $(CXXFLAGS)
|
|
||||||
|
|
||||||
clean:
|
|
||||||
-rm -f *~ *.o
|
|
@ -1,94 +0,0 @@
|
|||||||
|
|
||||||
#include <SDL.h>
|
|
||||||
#include <SDL_image.h>
|
|
||||||
#include <GL/gl.h>
|
|
||||||
#include <GL/glu.h>
|
|
||||||
#include <iostream>
|
|
||||||
#include "TextureCache.h"
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
GLuint TextureCache::load(const string & filename)
|
|
||||||
{
|
|
||||||
map<string,GLuint>::iterator it = m_cache.find(filename);
|
|
||||||
if (it != m_cache.end())
|
|
||||||
return it->second;
|
|
||||||
GLuint tex = loadTexture(filename.c_str());
|
|
||||||
m_cache[filename] = tex;
|
|
||||||
return tex;
|
|
||||||
}
|
|
||||||
|
|
||||||
GLuint TextureCache::loadTexture(const char * filename,
|
|
||||||
bool mipmaps,
|
|
||||||
int mode,
|
|
||||||
int quality)
|
|
||||||
{
|
|
||||||
GLuint texture;
|
|
||||||
SDL_Surface * temp = IMG_Load(filename);
|
|
||||||
if (!temp)
|
|
||||||
{
|
|
||||||
cerr << "Failed to load image '" << filename << "'!" << endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_PixelFormat fmt;
|
|
||||||
fmt.palette = NULL;
|
|
||||||
fmt.BitsPerPixel = 32;
|
|
||||||
fmt.BytesPerPixel = 4;
|
|
||||||
fmt.Rmask = 0x000000FF;
|
|
||||||
fmt.Gmask = 0x0000FF00;
|
|
||||||
fmt.Bmask = 0x00FF0000;
|
|
||||||
fmt.Amask = 0xFF000000;
|
|
||||||
fmt.Rshift = 0;
|
|
||||||
fmt.Gshift = 8;
|
|
||||||
fmt.Bshift = 16;
|
|
||||||
fmt.Ashift = 24;
|
|
||||||
|
|
||||||
SDL_Surface * texsurf = SDL_ConvertSurface(temp, &fmt, SDL_SWSURFACE);
|
|
||||||
SDL_FreeSurface(temp);
|
|
||||||
if (!texsurf)
|
|
||||||
{
|
|
||||||
cerr << '\'' << filename << "' was not converted properly!" << endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
unsigned int * pixels = new unsigned int[texsurf->w * texsurf->h];
|
|
||||||
int y;
|
|
||||||
unsigned int dstOffset = texsurf->w * (texsurf->h - 1);
|
|
||||||
unsigned int srcOffset = 0;
|
|
||||||
for (y = 0; y < texsurf->h; y++)
|
|
||||||
{
|
|
||||||
memcpy(pixels + dstOffset,
|
|
||||||
((unsigned int *)texsurf->pixels) + srcOffset,
|
|
||||||
texsurf->w << 2);
|
|
||||||
dstOffset -= texsurf->w;
|
|
||||||
srcOffset += texsurf->w;
|
|
||||||
}
|
|
||||||
glGenTextures(1, &texture);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, texture);
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, 4, texsurf->w, texsurf->h, 0,
|
|
||||||
GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
|
||||||
|
|
||||||
if (quality > 0)
|
|
||||||
{
|
|
||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
|
|
||||||
mipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
|
|
||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
|
||||||
mipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
|
|
||||||
mipmaps ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST);
|
|
||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
|
||||||
mipmaps ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST);
|
|
||||||
}
|
|
||||||
|
|
||||||
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode);
|
|
||||||
|
|
||||||
if (mipmaps)
|
|
||||||
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA,
|
|
||||||
texsurf->w, texsurf->h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
|
||||||
|
|
||||||
SDL_FreeSurface(texsurf);
|
|
||||||
delete[] pixels;
|
|
||||||
return texture;
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
|
|
||||||
#include <GL/gl.h>
|
|
||||||
#include <map>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class TextureCache
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
GLuint load(const std::string & filename);
|
|
||||||
|
|
||||||
private:
|
|
||||||
/* methods */
|
|
||||||
GLuint loadTexture(const char * filename,
|
|
||||||
bool mipmaps = false,
|
|
||||||
int mode = GL_DECAL,
|
|
||||||
int quality = 1);
|
|
||||||
|
|
||||||
/* data */
|
|
||||||
std::map< std::string, GLuint > m_cache;
|
|
||||||
};
|
|
@ -1 +0,0 @@
|
|||||||
Subproject commit 9855d3e577208d7c1ebc73f31489c7da9d6f7d07
|
|
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 d06888128c5a645a8815cb86bb4b3dfe1c76f454
|
Subproject commit 1ecc6d749be3bc3f5dc0de230b5e8e3ca079377a
|
428
wfobj-view.cc
428
wfobj-view.cc
@ -1,11 +1,15 @@
|
|||||||
|
|
||||||
/* 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 "TextureCache/TextureCache.h"
|
#include "loadTexture/loadTexture.h"
|
||||||
|
#include "glslUtil/glslUtil.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
/* Some definitions */
|
/* Some definitions */
|
||||||
@ -13,188 +17,316 @@ using namespace std;
|
|||||||
#define HEIGHT 800
|
#define HEIGHT 800
|
||||||
#define TITLE "Josh's Wavefront Object Viewer"
|
#define TITLE "Josh's Wavefront Object Viewer"
|
||||||
|
|
||||||
TextureCache textureCache;
|
enum Locations {
|
||||||
|
LOC_POSITION,
|
||||||
class LoadTexture : public TextureLoader
|
LOC_NORMAL,
|
||||||
{
|
LOC_TEXTURE
|
||||||
public:
|
|
||||||
GLuint load(const FileLoader::Path & path,
|
|
||||||
FileLoader & fl, bool mipmaps, int mode, int quality)
|
|
||||||
{
|
|
||||||
return textureCache.load(path.fullPath);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Viewer
|
class Viewer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Viewer(const char * filename);
|
Viewer(const char * filename);
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void initgl();
|
void initgl();
|
||||||
void display();
|
void display();
|
||||||
void setProjection();
|
void setProjection();
|
||||||
|
|
||||||
GLuint m_list;
|
WFObj m_obj;
|
||||||
float m_rotationMatrix[16];
|
GLuint m_list;
|
||||||
int m_startx, m_starty;
|
float m_rotationMatrix[16];
|
||||||
bool m_dragging;
|
int m_startx, m_starty;
|
||||||
float m_dist;
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* The program's main entry point */
|
static GLuint load_texture(const char *fname)
|
||||||
int main(int argc, char * argv[])
|
|
||||||
{
|
{
|
||||||
if (argc < 2)
|
GLuint id = loadTexture(fname);
|
||||||
{
|
if (id != 0)
|
||||||
cerr << "Usage: " << argv[0] << " <filename>" << endl;
|
{
|
||||||
return -1;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
if (SDL_Init(SDL_INIT_VIDEO))
|
/* The program's main entry point */
|
||||||
{
|
int main(int argc, char * argv[])
|
||||||
printf("Failed to initialize SDL!\n");
|
{
|
||||||
return -2;
|
if (argc < 2)
|
||||||
}
|
{
|
||||||
|
cerr << "Usage: " << argv[0] << " <filename>" << endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
atexit(SDL_Quit);
|
if (SDL_Init(SDL_INIT_VIDEO))
|
||||||
|
{
|
||||||
|
printf("Failed to initialize SDL!\n");
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
|
||||||
SDL_Surface * screen;
|
atexit(SDL_Quit);
|
||||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
||||||
if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL)))
|
|
||||||
{
|
|
||||||
printf("Failed to set video mode!\n");
|
|
||||||
SDL_Quit();
|
|
||||||
return -3;
|
|
||||||
}
|
|
||||||
SDL_WM_SetCaption(TITLE, TITLE);
|
|
||||||
|
|
||||||
Viewer v(argv[1]);
|
SDL_Surface * screen;
|
||||||
v.run();
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||||
|
if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL)))
|
||||||
|
{
|
||||||
|
printf("Failed to set video mode!\n");
|
||||||
|
SDL_Quit();
|
||||||
|
return -3;
|
||||||
|
}
|
||||||
|
SDL_WM_SetCaption(TITLE, TITLE);
|
||||||
|
|
||||||
return 0;
|
Viewer v(argv[1]);
|
||||||
|
v.run();
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Viewer::Viewer(const char * filename)
|
Viewer::Viewer(const char * filename)
|
||||||
{
|
{
|
||||||
m_dist = 5.0;
|
m_dist = 5.0;
|
||||||
m_dragging = false;
|
m_dragging = false;
|
||||||
LoadTexture lt;
|
if (!m_obj.load(filename, NULL, load_texture))
|
||||||
WFObj m_obj(lt);
|
{
|
||||||
m_obj.load(FileLoader::Path(filename, ""));
|
cerr << "Error loading " << filename << endl;
|
||||||
m_list = m_obj.render();
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
/* Print out the object's size */
|
const static guAttribBinding bindings[] = {
|
||||||
const float * aabb = m_obj.getAABB();
|
{LOC_POSITION, "pos"},
|
||||||
cout << "Object width: " << (aabb[3]-aabb[0]) << endl;
|
{LOC_NORMAL, "normal"},
|
||||||
cout << "Object depth: " << (aabb[4]-aabb[1]) << endl;
|
{0, NULL}
|
||||||
cout << "Object height: " << (aabb[5]-aabb[2]) << endl;
|
};
|
||||||
|
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;
|
||||||
|
cout << "Object depth: " << (aabb[4]-aabb[1]) << endl;
|
||||||
|
cout << "Object height: " << (aabb[5]-aabb[2]) << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Viewer::initgl()
|
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);
|
glViewport(0, 0, WIDTH, HEIGHT);
|
||||||
glEnable(GL_LIGHTING);
|
setProjection();
|
||||||
glEnable(GL_LIGHT0);
|
glLoadIdentity();
|
||||||
glEnable(GL_CULL_FACE);
|
glGetFloatv(GL_MODELVIEW_MATRIX, m_rotationMatrix);
|
||||||
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()
|
void Viewer::setProjection()
|
||||||
{
|
{
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
gluPerspective(60.0, (GLfloat)WIDTH/(GLfloat)WIDTH, 0.01, 10000.0);
|
gluPerspective(60.0, (GLfloat)WIDTH/(GLfloat)WIDTH, 0.01, 10000.0);
|
||||||
// gluLookAt(0, m_dist, 0, 0, 0, 0, 0, 0, 1);
|
// gluLookAt(0, m_dist, 0, 0, 0, 0, 0, 0, 1);
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Viewer::display()
|
void Viewer::display()
|
||||||
{
|
{
|
||||||
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
||||||
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);
|
||||||
if (m_list)
|
|
||||||
glCallList(m_list);
|
m_obj.bindBuffers();
|
||||||
SDL_GL_SwapBuffers();
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Viewer::run()
|
void Viewer::run()
|
||||||
{
|
{
|
||||||
initgl();
|
initgl();
|
||||||
display();
|
display();
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_WaitEvent(&event))
|
while (SDL_WaitEvent(&event))
|
||||||
{
|
{
|
||||||
if (event.type == SDL_QUIT)
|
if (event.type == SDL_QUIT)
|
||||||
break;
|
break;
|
||||||
else if (event.type == SDL_KEYDOWN)
|
else if (event.type == SDL_KEYDOWN)
|
||||||
{
|
{
|
||||||
if (event.key.keysym.sym == SDLK_ESCAPE)
|
if (event.key.keysym.sym == SDLK_ESCAPE)
|
||||||
break;
|
break;
|
||||||
if (event.key.keysym.sym == SDLK_q)
|
if (event.key.keysym.sym == SDLK_q)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (event.type == SDL_MOUSEBUTTONDOWN)
|
else if (event.type == SDL_MOUSEBUTTONDOWN)
|
||||||
{
|
{
|
||||||
if (event.button.button == SDL_BUTTON_LEFT)
|
if (event.button.button == SDL_BUTTON_LEFT)
|
||||||
{
|
{
|
||||||
if (event.button.state == SDL_PRESSED)
|
if (event.button.state == SDL_PRESSED)
|
||||||
{
|
{
|
||||||
m_dragging = true;
|
m_dragging = true;
|
||||||
m_startx = event.button.x;
|
m_startx = event.button.x;
|
||||||
m_starty = event.button.y;
|
m_starty = event.button.y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (event.button.button == 4)
|
else if (event.button.button == 4)
|
||||||
{
|
{
|
||||||
m_dist *= 0.85;
|
m_dist *= 0.85;
|
||||||
if (m_dist < 1.0f)
|
if (m_dist < 1.0f)
|
||||||
m_dist = 1.0f;
|
m_dist = 1.0f;
|
||||||
setProjection();
|
setProjection();
|
||||||
display();
|
display();
|
||||||
}
|
}
|
||||||
else if (event.button.button == 5)
|
else if (event.button.button == 5)
|
||||||
{
|
{
|
||||||
m_dist *= 1.2;
|
m_dist *= 1.2;
|
||||||
setProjection();
|
setProjection();
|
||||||
display();
|
display();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (event.type == SDL_MOUSEBUTTONUP)
|
else if (event.type == SDL_MOUSEBUTTONUP)
|
||||||
{
|
{
|
||||||
if (event.button.button == SDL_BUTTON_LEFT)
|
if (event.button.button == SDL_BUTTON_LEFT)
|
||||||
{
|
{
|
||||||
m_dragging = false;
|
m_dragging = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (event.type == SDL_MOUSEMOTION)
|
else if (event.type == SDL_MOUSEMOTION)
|
||||||
{
|
{
|
||||||
if (m_dragging)
|
if (m_dragging)
|
||||||
{
|
{
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
glRotatef(event.motion.y - m_starty, 1, 0, 0);
|
glRotatef(event.motion.y - m_starty, 1, 0, 0);
|
||||||
glRotatef(event.motion.x - m_startx, 0, 0, 1);
|
glRotatef(event.motion.x - m_startx, 0, 0, 1);
|
||||||
glMultMatrixf(m_rotationMatrix);
|
glMultMatrixf(m_rotationMatrix);
|
||||||
glGetFloatv(GL_MODELVIEW_MATRIX, m_rotationMatrix);
|
glGetFloatv(GL_MODELVIEW_MATRIX, m_rotationMatrix);
|
||||||
m_startx = event.motion.x;
|
m_startx = event.motion.x;
|
||||||
m_starty = event.motion.y;
|
m_starty = event.motion.y;
|
||||||
display();
|
display();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user