From 55bf10c2825eba81032079b8d1e1751067410c75 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 11 Oct 2011 11:35:23 -0400 Subject: [PATCH] reorganize source a bit, building but not rendering LogoBoxes --- LogoBox.cc | 68 ------------ SConstruct | 11 +- shaders/obj.fp | 39 +++++++ shaders/obj.vp | 13 +++ .../GnomeScreensaver.cc | 0 GnomeScreensaver.h => src/GnomeScreensaver.h | 0 src/LogoBox.cc | 102 ++++++++++++++++++ LogoBox.h => src/LogoBox.h | 5 +- src/WFObjLoadFile.cc | 16 +++ src/WFObjLoadFile.h | 9 ++ gs-theme-window.c => src/gs-theme-window.c | 0 gs-theme-window.h => src/gs-theme-window.h | 0 12 files changed, 191 insertions(+), 72 deletions(-) delete mode 100644 LogoBox.cc create mode 100644 shaders/obj.fp create mode 100644 shaders/obj.vp rename GnomeScreensaver.cc => src/GnomeScreensaver.cc (100%) rename GnomeScreensaver.h => src/GnomeScreensaver.h (100%) create mode 100644 src/LogoBox.cc rename LogoBox.h => src/LogoBox.h (80%) create mode 100644 src/WFObjLoadFile.cc create mode 100644 src/WFObjLoadFile.h rename gs-theme-window.c => src/gs-theme-window.c (100%) rename gs-theme-window.h => src/gs-theme-window.h (100%) diff --git a/LogoBox.cc b/LogoBox.cc deleted file mode 100644 index 459a29d..0000000 --- a/LogoBox.cc +++ /dev/null @@ -1,68 +0,0 @@ - -/* - * Author: Josh Holtrop - * DornerWorks ScreenSaver - * This module can be used to create "LogoBox" objects - * which consist of a 3D DW logo - */ - -#include -#include "WFObj.h" -#include "LogoBox.h" -#include "LoadFile.h" -#include /* memcpy() */ - -#include -using namespace std; - -static GLuint _logoList = 0; -static GLuint _drawList; -static LoadFile loadFile; -static float _width, _depth, _height; - -/* - * construct a LogoBox object - * The first time the constructor is called it loads the - * Alias Wavefront object model. Subsequent calls will - * reuse the same module. - */ -LogoBox::LogoBox() -{ - if (_logoList == 0) - { - WFObj obj(loadFile); - if (obj.load(FileLoader::Path("", "dwlogo.obj"))) - { - _logoList = obj.render(false); - if (_logoList == 0) - { - cerr << "Error rendering dwlogo.obj" << endl; - } - const float * aabb = obj.getAABB(); - - float c_x = (aabb[0] + aabb[3]) / 2.0f; - float c_y = (aabb[1] + aabb[4]) / 2.0f; - float c_z = (aabb[2] + aabb[5]) / 2.0f; - - _width = aabb[3] - aabb[0]; - _depth = aabb[4] - aabb[1]; - _height = aabb[5] - aabb[2]; - - _drawList = glGenLists(1); - glNewList(_drawList, GL_COMPILE); - glPushMatrix(); - glTranslatef(-c_x, -c_y, -c_z); - glCallList(_logoList); - glPopMatrix(); - glEndList(); - } - else - { - cerr << "Error loading dwlogo.obj" << endl; - } - } - m_drawList = _drawList; - m_width = _width; - m_depth = _depth; - m_height = _height; -} diff --git a/SConstruct b/SConstruct index c20de7b..15a90f7 100644 --- a/SConstruct +++ b/SConstruct @@ -10,6 +10,7 @@ load_files = ['logo/dwlogo.obj', 'logo/dwlogo.mtl', Glob('shaders/*')] env = Environment(CPPPATH = ['.']) env.ParseConfig("pkg-config --cflags --libs glib-2.0 gdk-2.0 atk gtk+-2.0 gtkglext-1.0") +env.Append(CPPFLAGS = ['-Isrc', '-DGL_GLEXT_PROTOTYPES']) # modes builder @@ -40,7 +41,15 @@ env.Append(BUILDERS = {'CFS' : Builder(action = CFS, emitter = CFS_emitter)}) # source file list -sources = [Glob('*.c'), Glob('*.cc'), Glob('LoadFile/*.cc'), Glob('modes/*.cc')] +sources = [ + 'cfs.cc', + Glob('src/*.c'), + Glob('src/*.cc'), + Glob('LoadFile/*.cc'), + Glob('modes/*.cc'), + Glob('wfobj/*.cc'), + Glob('glslUtil/*.c'), + ] env.CFS('cfs.cc', load_files) env.Depends('cfs.cc', 'cfs_gen/cfs_gen.py') diff --git a/shaders/obj.fp b/shaders/obj.fp new file mode 100644 index 0000000..35dc833 --- /dev/null +++ b/shaders/obj.fp @@ -0,0 +1,39 @@ + +#ifdef GL_FRAGMENT_PRECISION_HIGH +precision highp float; +#else +precision mediump float; +#endif + +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.4, 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; +} diff --git a/shaders/obj.vp b/shaders/obj.vp new file mode 100644 index 0000000..7d88a5f --- /dev/null +++ b/shaders/obj.vp @@ -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; +} diff --git a/GnomeScreensaver.cc b/src/GnomeScreensaver.cc similarity index 100% rename from GnomeScreensaver.cc rename to src/GnomeScreensaver.cc diff --git a/GnomeScreensaver.h b/src/GnomeScreensaver.h similarity index 100% rename from GnomeScreensaver.h rename to src/GnomeScreensaver.h diff --git a/src/LogoBox.cc b/src/LogoBox.cc new file mode 100644 index 0000000..bec312a --- /dev/null +++ b/src/LogoBox.cc @@ -0,0 +1,102 @@ +/* + * Author: Josh Holtrop + * DornerWorks ScreenSaver + * This module can be used to create "LogoBox" objects + * which consist of a 3D DW logo + */ + +#include + +#include + +#include "wfobj/WFObj.h" +#include "LogoBox.h" +#include "cfs.h" +#include "glslUtil/glslUtil.h" +#include "WFObjLoadFile.h" + +using namespace std; + +static WFObj obj; +static GLuint program; +static GLint ambient_loc, diffuse_loc, specular_loc, shininess_loc; +enum Locations { + LOC_POSITION, + LOC_NORMAL +}; +static float _width, _depth, _height; +static bool loaded = false; + +/* + * construct a LogoBox object + * The first time the constructor is called it loads the + * Alias Wavefront object model. Subsequent calls will + * reuse the same module. + */ +LogoBox::LogoBox() +{ + if (!loaded) + { + if (obj.load("logo/dwlogo.obj", WFObjLoadFile)) + { + const float * aabb = obj.getAABB(); + + float c_x = (aabb[0] + aabb[3]) / 2.0f; + float c_y = (aabb[1] + aabb[4]) / 2.0f; + float c_z = (aabb[2] + aabb[5]) / 2.0f; + + _width = aabb[3] - aabb[0]; + _depth = aabb[4] - aabb[1]; + _height = aabb[5] - aabb[2]; + + loaded = loadShaders(); + } + else + { + cerr << "Error loading dwlogo.obj" << endl; + } + } + m_width = _width; + m_depth = _depth; + m_height = _height; +} + +bool LogoBox::loadShaders() +{ + const guAttribBinding bindings[] = { + {LOC_POSITION, "pos"}, + {LOC_NORMAL, "normal"}, + {0, NULL} + }; + const char *v_shader_src = (const char *) getFile("shaders/obj.vp", NULL); + const char *f_shader_src = (const char *) getFile("shaders/obj.fp", NULL); + if (v_shader_src == NULL || f_shader_src == NULL) + { + cerr << "Error reading shader source files" << endl; + return false; + } + program = guMakeProgramFromSource(v_shader_src, f_shader_src, bindings); + if (program == 0) + { + cerr << "Error creating shaders." << endl; + return false; + } + + ambient_loc = glGetUniformLocation(program, "ambient"); + diffuse_loc = glGetUniformLocation(program, "diffuse"); + specular_loc = glGetUniformLocation(program, "specular"); + shininess_loc = glGetUniformLocation(program, "shininess"); + + glUseProgram(program); + /* set up a default material */ + glUniform4f(ambient_loc, 0.2, 0.2, 0.2, 1.0); + glUniform4f(diffuse_loc, 1.0, 0.6, 0.0, 1.0); + glUniform4f(specular_loc, 1.0, 1.0, 1.0, 1.0); + glUniform1f(shininess_loc, 85.0); +} + +void LogoBox::draw() +{ + if (!loaded) + return; +} diff --git a/LogoBox.h b/src/LogoBox.h similarity index 80% rename from LogoBox.h rename to src/LogoBox.h index 66fc056..92f7b87 100644 --- a/LogoBox.h +++ b/src/LogoBox.h @@ -1,4 +1,3 @@ - /* * Author: Josh Holtrop * DornerWorks screensaver @@ -14,12 +13,12 @@ class LogoBox { public: LogoBox(); - void draw() { glCallList(m_drawList); } + void draw(); float getWidth() const { return m_width; } float getDepth() const { return m_depth; } float getHeight() const { return m_height; } protected: - GLuint m_drawList; + bool loadShaders(); float m_width, m_depth, m_height; }; diff --git a/src/WFObjLoadFile.cc b/src/WFObjLoadFile.cc new file mode 100644 index 0000000..78355ac --- /dev/null +++ b/src/WFObjLoadFile.cc @@ -0,0 +1,16 @@ + +#include /* memcpy() */ + +#include "WFObjLoadFile.h" +#include "cfs.h" + +bool WFObjLoadFile(const char *fname, WFObj::Buffer & buff) +{ + unsigned int length; + const unsigned char *dat = getFile(fname, &length); + if (dat == NULL) + return false; + buff.alloc(length); + memcpy(buff.data, dat, length); + return true; +} diff --git a/src/WFObjLoadFile.h b/src/WFObjLoadFile.h new file mode 100644 index 0000000..4414faf --- /dev/null +++ b/src/WFObjLoadFile.h @@ -0,0 +1,9 @@ + +#ifndef WFOBJLOADFILE_H +#define WFOBJLOADFILE_H + +#include "wfobj/WFObj.h" + +bool WFObjLoadFile(const char *fname, WFObj::Buffer & buff); + +#endif /* WFOBJLOADFILE_H */ diff --git a/gs-theme-window.c b/src/gs-theme-window.c similarity index 100% rename from gs-theme-window.c rename to src/gs-theme-window.c diff --git a/gs-theme-window.h b/src/gs-theme-window.h similarity index 100% rename from gs-theme-window.h rename to src/gs-theme-window.h