/* * 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; }