summaryrefslogtreecommitdiff
path: root/application.cpp
blob: 3ff3ab3af1ae4faea8fe5f233836a4bf6b08bddc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "application.h"

#include <AR/gsub_lite.h>
#include <AR/param.h>

#include <SDL/SDL_opengl.h>

#include <iostream>
#include <stdexcept>

Application::Application() {
	ARParam  wparam;
	
	vp = new VideoProvider();
	
	// Set the initial camera parameters.
	if(arParamLoad("camera_para.dat", 1, &wparam) < 0) {
		throw(std::runtime_error("arParamLoad() failed."));
	}

	arParamChangeSize(&wparam, vp->xsize, vp->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);
	
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
	
	// Get our surface
	surface = SDL_SetVideoMode(vp->xsize, vp->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);
	
	please_quit = false;
}

Application::~Application() {
	delete vp;
	arglCleanup(argl_ctx);
}

void Application::run() {
	vp->start();
	
	while(1) {
		SDL_Event event;
		SDL_PollEvent(&event);
		if(event.type == SDL_QUIT) {
			break;
		} else if(event.type == SDL_KEYDOWN) {
			event_keypress(event.key.keysym.sym);
		}
		if(please_quit) {
			return;
		}
		
		main_loop();
	}
}

void Application::main_loop(void) {
	uint8_t         *dataPtr;

	if((dataPtr = vp->get()) == 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, 50, &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);
	patt2->update(marker_info, marker_num);

	glDisable( GL_DEPTH_TEST );

	vp->next();
	SDL_GL_SwapBuffers();
}

void Application::quit() {
	please_quit = true;
}

void Application::event_keypress(SDLKey key) {
	switch(key) {
		case SDLK_ESCAPE:
			quit();
			break;
	}
}