test/light-path.cpp

Go to the documentation of this file.
00001 /*
00002  * light-path.cpp
00003  *
00004  * Copyright (C) 2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  * Program to test the opengl-effects library---light path routines.
00008  */
00009 
00010 // includes --------------------------------------------------------------------
00011 #include <iostream>
00012 #include <fstream>
00013 
00014 #include "glut-demo/glut-demo.h"
00015 #include "opengl-effects/opengl-effects.h"
00016 #include "perf/perf.h"
00017 #include "wave-glut/texture.h"
00018 
00019 
00020 
00021 static const int s_nSegments    = 3;
00022 static const int s_nPoints      = s_nSegments + 1;
00023 
00024 
00025 ////////////////////////////////////////////////////////////////////////////////
00026 //
00027 //      static helper methods
00028 //
00029 ////////////////////////////////////////////////////////////////////////////////
00030 
00031 static float
00032 deltaX
00033 (
00034 IN float dx
00035 )
00036 throw()
00037 {
00038         float x = (2.0 * rand()) / RAND_MAX;
00039         return (x - 1) * dx;
00040 }
00041 
00042 
00043 
00044 static void
00045 addRandomDelta
00046 (
00047 IO point3d_t& p,
00048 IN float f
00049 )
00050 throw()
00051 {
00052         p.x += deltaX(f);
00053         p.y += deltaX(f);
00054         p.z += deltaX(f);
00055 }
00056 
00057 
00058 
00059 static void
00060 getPoint
00061 (
00062 IN int idx,
00063 IN int nPoints,
00064 IN float rho,
00065 IN float phi,
00066 OUT point3d_t& p
00067 )
00068 throw()
00069 {
00070         ASSERT(nPoints > 1, "Bad nPoints");
00071         ASSERT(idx >= 0 && idx < nPoints, "bad idx");
00072         ASSERT(rho > 0, "Bad rho");
00073 
00074         p.x = -2 + 4 * idx;
00075         p.y = 1;
00076         p.z = -2 + 0.5 * idx;
00077 
00078 //      phi += deltaX(0.05);
00079 //      float theta = 2.0 * M_PI * idx / (nPoints - 3);
00080 //      theta += deltaX(0.05);
00081 //      float q = rho * sin(theta);
00082 
00083 //      p.x = q * sin(phi);
00084 //      p.y = q * cos(phi);
00085 //      p.z = rho * cos(theta);
00086 
00087 //      addRandomDelta(p, 0.02);
00088 }
00089 
00090 
00091 
00092 class Drawer : public glut::DemoHost {
00093 public:
00094         Drawer(IN int size) throw() : m_size(size), m_textureId(0) { }
00095         ~Drawer(void) throw() { }
00096 
00097         // glut::Host class interface methods ----------------------------------
00098         void onInit(void) {
00099                         int width = 128;
00100                         // create density map
00101                         media::image_t img;
00102                         img_color_t color(200, 200, 255);
00103                         glut::createLineSegmentDensityMap(width, color, img);
00104                         img.dump("test image == horizontal density map");
00105 
00106                         // create texture (used for generating glow)
00107                         m_textureId = glut::createTextureFromImage(img);
00108                         DPRINTF("Texture ID: %d", m_textureId);
00109 
00110                         // create target textures, one per segment
00111                         for (int i = 0; i < s_nSegments; ++i) {
00112                                 glGenTextures(1, &m_texture[i]);
00113                                 DPRINTF("  output texture %d: %d", i, m_texture[i]);
00114                                 glBindTexture(GL_TEXTURE_2D, m_texture[i]);
00115                                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00116                                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00117                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00118                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00119                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,  width, width, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
00120                         }
00121 
00122                         // create framebuffer
00123                         m_framebuffer = glut::Framebuffer::create(width, width);
00124                         ASSERT(m_framebuffer, "failed to create framebuffer");
00125                 }
00126 
00127         void display3D(IN const glut::render_context_t& rc,
00128                                 IN glut::RenderQueue * rq) {
00129                         ASSERT(rq, "null");
00130 
00131                         // first, render a standard sphere at the origin
00132                         glutSolidSphere(1.5, 16, 16);
00133 
00134                         static float s_phi = 0.0;
00135                 //      s_phi += 0.01;
00136 
00137                         for (int i = 0; i < s_nPoints; ++i) {
00138                                 getPoint(i, s_nPoints, 2.0, s_phi, m_point[i]);
00139                         }
00140                         
00141                         glut::drawTransparentPath(m_point, (int *) m_texture,
00142                             s_nSegments, m_framebuffer, 0.4, m_textureId, rc,
00143                             rq);
00144                 }
00145 
00146 private:
00147         int             m_size;
00148         int             m_textureId;
00149         point3d_t       m_point[s_nPoints];
00150         GLuint          m_texture[s_nSegments];
00151         smart_ptr<glut::Framebuffer> m_framebuffer;
00152 };
00153 
00154 
00155 
00156 ////////////////////////////////////////////////////////////////////////////////
00157 //
00158 //      entry point
00159 //
00160 ////////////////////////////////////////////////////////////////////////////////
00161 
00162 int
00163 main
00164 (
00165 IN int argc,
00166 IN const char * argv[]
00167 )
00168 {
00169         ASSERT(2 == argc, "Usage: opengl-effects-test <size>\n");
00170         int size = atoi(argv[1]);
00171         ASSERT(size > 0, "Bad size: %d", size);
00172 
00173         try {
00174                 // main loop
00175                 smart_ptr<glut::DemoHost> host = new Drawer(size);
00176                 ASSERT(host, "out of memory");
00177                 glut::startDemo(argc, argv, "Light Path Test", host);
00178 
00179         } catch (std::exception& e) {
00180                 DPRINTF("Exception: %s", e.what());
00181         }
00182 
00183         // only reach here on error
00184         return -1;
00185 }
00186