From 9466244a852d6620edf671a89e1a7188248d5482 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Thu, 25 Feb 2010 23:31:04 +0100 Subject: Seperated Application and Pattern classes. --- application.cpp | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 application.cpp (limited to 'application.cpp') 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 +#include +#include + +#include + +#include +#include + +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(); +} -- cgit v1.2.3