diff options
-rw-r--r-- | SConstruct | 2 | ||||
-rw-r--r-- | application.cpp | 136 | ||||
-rw-r--r-- | application.h | 29 | ||||
-rwxr-xr-x | foo.cpp | 210 | ||||
-rw-r--r-- | pattern.cpp | 57 | ||||
-rw-r--r-- | pattern.h | 36 |
6 files changed, 264 insertions, 206 deletions
@@ -2,7 +2,7 @@ env = Environment( LIBS = ['ARgsub_lite', 'ARvideo', 'AR', 'png'], ) -common_sources = ['texture.cpp', 'texturepng.cpp'] +common_sources = ['application.cpp', 'pattern.cpp', 'texture.cpp', 'texturepng.cpp'] if env['PLATFORM'] == 'darwin': env.Append(CPPFLAGS = '-m32') diff --git a/application.cpp b/application.cpp new file mode 100644 index 0000000..95027ac --- /dev/null +++ b/application.cpp @@ -0,0 +1,136 @@ +#include "application.h" + +#include <AR/gsub_lite.h> +#include <AR/video.h> +#include <AR/param.h> + +#include <SDL/SDL_opengl.h> + +#include <iostream> +#include <stdexcept> + +Application::Application() { + ARParam wparam; + + // Open video device. + if(arVideoOpen(NULL) < 0) { + throw(std::runtime_error("arVideoOpen() failed.")); + } + + // Find the size of the window. + int xsize, ysize; + if(arVideoInqSize(&xsize, &ysize) < 0) { + throw(std::runtime_error("arVideoInqSize() failed.")); + } + + std::cout << "Image size (x, y) = (" << xsize << ", " << ysize << ")" << std::endl; + + // Set the initial camera parameters. + if(arParamLoad("camera_para.dat", 1, &wparam) < 0) { + throw(std::runtime_error("arParamLoad() failed.")); + } + + arParamChangeSize(&wparam, xsize, ysize, &cparam); + arInitCparam(&cparam); + std::cout << "*** Camera Parameters ***" << std::endl; + arParamDisp(&cparam); + + // Initialize SDL + if(SDL_Init(SDL_INIT_VIDEO)) { + throw(std::runtime_error("SDL initialization failed")); + } + // Fetch the video info + const SDL_VideoInfo *info = SDL_GetVideoInfo(); + if(!info) { + throw(std::runtime_error("SDL info query failed")); + } + // The SDL mode-flags + int flags = SDL_OPENGL; // OpenGL in SDL + flags |= SDL_GL_DOUBLEBUFFER; // Double buffering + flags |= SDL_HWPALETTE; // Hardware palette + // Check for hardware surface aviability + if(info->hw_available) { + flags |= SDL_HWSURFACE; + } else { + flags |= SDL_SWSURFACE; + } + // Check for hardware blit ability + if(info->blit_hw) { + flags |= SDL_HWACCEL; + } + // Setup double buffering + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + // Get our surface + surface = SDL_SetVideoMode(xsize * 2, ysize * 2, 32, flags); + if(!surface) { + throw(std::runtime_error("Video mode set failed")); + } + + argl_ctx = arglSetupForCurrentContext(); + + // Texturing + glEnable(GL_TEXTURE_2D); + // Blending + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_BLEND); + // Smooth shading + glShadeModel(GL_SMOOTH); + + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); +} + +Application::~Application() { + arVideoCapStop(); + arVideoClose(); + arglCleanup(argl_ctx); +} + +void Application::run() { + arVideoCapStart(); + while(1) { + SDL_Event event; + SDL_PollEvent(&event); + if(event.type == SDL_QUIT || event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) { + break; + } + main_loop(); + } +} + +void Application::main_loop(void) { + ARUint8 *dataPtr; + + if((dataPtr = (ARUint8 *)arVideoGetImage()) == NULL) { + arUtilSleep(10); + return; + } + + if(count == 0) arUtilTimerReset(); + count++; + + arglDispImage(dataPtr, &cparam, 1.0, argl_ctx); + + ARMarkerInfo* marker_info; + int marker_num; + + if(arDetectMarker(dataPtr, 100, &marker_info, &marker_num) < 0) { + throw(std::runtime_error("arDetectMarker() failed.")); + } + + GLdouble p[16]; + arglCameraFrustum(&cparam, 0.1, 1000, p); + glMatrixMode(GL_PROJECTION); + glLoadMatrixd(p); + + glClearDepth( 1.0 ); + glClear(GL_DEPTH_BUFFER_BIT); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + + patt->update(marker_info, marker_num); + + glDisable( GL_DEPTH_TEST ); + + arVideoCapNext(); + SDL_GL_SwapBuffers(); +} diff --git a/application.h b/application.h new file mode 100644 index 0000000..5d821f9 --- /dev/null +++ b/application.h @@ -0,0 +1,29 @@ +#ifndef APPLICATION_H +#define APPLICATION_H + +#include "pattern.h" + +#include <AR/gsub_lite.h> +#include <AR/ar.h> +#include <SDL/SDL.h> + +class Application { + private: + ARParam cparam; + + ARGL_CONTEXT_SETTINGS_REF argl_ctx; + + SDL_Surface *surface; + + int count; + + public: + Pattern* patt; + + Application(); + ~Application(); + void run(); + void main_loop(void); +}; + +#endif @@ -1,217 +1,17 @@ -#include <AR/gsub_lite.h> -#include <AR/video.h> -#include <AR/param.h> -#include <AR/ar.h> - -#include <SDL/SDL.h> -#include <SDL/SDL_opengl.h> - #include <stdexcept> #include <iostream> #include "texturepng.h" -class Pattern { - private: - double patt_width; - double patt_center[2]; - int patt_id; - double patt_trans_kake[3][4]; - - Texture* tex; - - public: - Pattern() { - patt_width = 10.0; - patt_center[0] = 0.0; - patt_center[1] = 0.0; - - patt_id = arLoadPatt("patt.head"); - - tex = new TexturePNG("foo.png"); - } - - void update(ARMarkerInfo* marker_info, int marker_num) { - for(int j = 0; j < marker_num; j++) { - if( patt_id == marker_info[j].id ) { - arGetTransMat(&marker_info[j], patt_center, patt_width, patt_trans_kake); - draw(); - //else if( marker_info[k].cf < marker_info[j].cf ) k = j; - } - } - } - - void draw() { - double gl_para[16]; - arglCameraView(patt_trans_kake, gl_para, 1.0); - - glMatrixMode(GL_MODELVIEW); - glLoadMatrixd(gl_para); - - glTranslatef(0.0, -8.0, 0.0); - - glBindTexture(GL_TEXTURE_2D, tex->tex()); - glBegin(GL_QUADS); - glTexCoord2f(0, 1); - glVertex3f(-30, -30, 0); - glTexCoord2f(0, 0); - glVertex3f(-30, 30, 0); - glTexCoord2f(1, 0); - glVertex3f(30, 30, 0); - glTexCoord2f(1, 1); - glVertex3f(30, -30, 0); - glEnd(); - } -}; - -Pattern* patt; - -ARParam cparam; - -ARGL_CONTEXT_SETTINGS_REF argl_ctx; - -SDL_Surface *surface; - -static void init() { - ARParam wparam; - - // Open video device. - if(arVideoOpen(NULL) < 0) { - throw(std::runtime_error("arVideoOpen() failed.")); - } - - // Find the size of the window. - int xsize, ysize; - if(arVideoInqSize(&xsize, &ysize) < 0) { - throw(std::runtime_error("arVideoInqSize() failed.")); - } - - std::cout << "Image size (x, y) = (" << xsize << ", " << ysize << ")" << std::endl; - - // Set the initial camera parameters. - if(arParamLoad("camera_para.dat", 1, &wparam) < 0) { - throw(std::runtime_error("arParamLoad() failed.")); - } - - arParamChangeSize(&wparam, xsize, ysize, &cparam); - arInitCparam(&cparam); - std::cout << "*** Camera Parameters ***" << std::endl; - arParamDisp(&cparam); - - // Initialize SDL - if(SDL_Init(SDL_INIT_VIDEO)) { - throw(std::runtime_error("SDL initialization failed")); - } - // Fetch the video info - const SDL_VideoInfo *info = SDL_GetVideoInfo(); - if(!info) { - throw(std::runtime_error("SDL info query failed")); - } - // The SDL mode-flags - int flags = SDL_OPENGL; // OpenGL in SDL - flags |= SDL_GL_DOUBLEBUFFER; // Double buffering - flags |= SDL_HWPALETTE; // Hardware palette - // Check for hardware surface aviability - if(info->hw_available) { - flags |= SDL_HWSURFACE; - } else { - flags |= SDL_SWSURFACE; - } - // Check for hardware blit ability - if(info->blit_hw) { - flags |= SDL_HWACCEL; - } - // Setup double buffering - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - // Get our surface - surface = SDL_SetVideoMode(xsize, ysize, 32, flags); - if(!surface) { - throw(std::runtime_error("Video mode set failed")); - } - - argl_ctx = arglSetupForCurrentContext(); - - // Texturing - glEnable(GL_TEXTURE_2D); - // Blending - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_BLEND); - // Smooth shading - glShadeModel(GL_SMOOTH); - - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); -} - -static void cleanup(void) { - arVideoCapStop(); - arVideoClose(); - arglCleanup(argl_ctx); -} - -int count = 0; - -static void key_event(unsigned char key, int x, int y) { - switch(key) { - case 0x1b: - printf("*** %f (frame/sec)\n", (double)count/arUtilTimer()); - cleanup(); - exit(0); - } -} - -static void main_loop(void) { - ARUint8 *dataPtr; - - if((dataPtr = (ARUint8 *)arVideoGetImage()) == NULL) { - arUtilSleep(10); - return; - } - - if(count == 0) arUtilTimerReset(); - count++; - - arglDispImage(dataPtr, &cparam, 1.0, argl_ctx); - - ARMarkerInfo* marker_info; - int marker_num; - - if(arDetectMarker(dataPtr, 100, &marker_info, &marker_num) < 0) { - throw(std::runtime_error("arDetectMarker() failed.")); - } - - GLdouble p[16]; - arglCameraFrustum(&cparam, 0.1, 1000, p); - glMatrixMode(GL_PROJECTION); - glLoadMatrixd(p); - - glClearDepth( 1.0 ); - glClear(GL_DEPTH_BUFFER_BIT); - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_LEQUAL); - - patt->update(marker_info, marker_num); - - glDisable( GL_DEPTH_TEST ); - - arVideoCapNext(); - SDL_GL_SwapBuffers(); -} +#include "pattern.h" +#include "application.h" int main(int argc, char **argv) { try { - init(); - patt = new Pattern(); - arVideoCapStart(); - while(1) { - SDL_Event event; - SDL_PollEvent(&event); - if(event.type == SDL_QUIT) { - break; - } - main_loop(); - } + Application* app = new Application(); + app->patt = new KakePattern(); + app->run(); } catch(std::runtime_error e) { - cleanup(); std::cerr << "Exception caught: " << e.what() << std::endl; } return 0; diff --git a/pattern.cpp b/pattern.cpp new file mode 100644 index 0000000..87d39f3 --- /dev/null +++ b/pattern.cpp @@ -0,0 +1,57 @@ +#include "pattern.h" + +#include <AR/gsub_lite.h> +#include <AR/video.h> +#include <AR/param.h> +#include <AR/ar.h> + +#include <SDL/SDL.h> +#include <SDL/SDL_opengl.h> + +#include "texturepng.h" + +Pattern::Pattern(std::string filename) { + patt_width = 25.0; + patt_center[0] = 0.0; + patt_center[1] = 0.0; + + patt_id = arLoadPatt(filename.c_str()); + +} + +void Pattern::update(ARMarkerInfo* marker_info, int marker_num) { + for(int j = 0; j < marker_num; j++) { + if( patt_id == marker_info[j].id ) { + arGetTransMat(&marker_info[j], patt_center, patt_width, patt_trans_kake); + draw(); + //else if( marker_info[k].cf < marker_info[j].cf ) k = j; + } + } +} + +KakePattern::KakePattern() : Pattern("patt.hiro") { + + tex = new TexturePNG("foo.png"); +} + +void KakePattern::draw() { + double gl_para[16]; + arglCameraView(patt_trans_kake, gl_para, 1.0); + + glMatrixMode(GL_MODELVIEW); + glLoadMatrixd(gl_para); + + //glTranslatef(0.0, -8.0, 0.0); + + glBindTexture(GL_TEXTURE_2D, tex->tex()); + glBegin(GL_QUADS); + glTexCoord2f(0, 1); + glVertex3f(-30, -30, 0); + glTexCoord2f(0, 0); + glVertex3f(-30, 30, 0); + glTexCoord2f(1, 0); + glVertex3f(30, 30, 0); + glTexCoord2f(1, 1); + glVertex3f(30, -30, 0); + glEnd(); +} diff --git a/pattern.h b/pattern.h new file mode 100644 index 0000000..3e28d97 --- /dev/null +++ b/pattern.h @@ -0,0 +1,36 @@ +#ifndef PATTERN_H +#define PATTERN_H + +#include <string> +#include "texture.h" +#include <AR/ar.h> + +class Pattern { + private: + double patt_width; + double patt_center[2]; + int patt_id; + protected: + double patt_trans_kake[3][4]; + + public: + Pattern(std::string filename); + void update(ARMarkerInfo* marker_info, int marker_num); + + protected: + virtual void draw() = 0; +}; + +class KakePattern : public Pattern { + private: + + Texture* tex; + + public: + KakePattern(); + + protected: + virtual void draw(); +}; + +#endif |