summaryrefslogtreecommitdiff
path: root/foo.cpp
blob: c92424570c1a949c447e582db97cfbaf00086066 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#ifndef __APPLE__
#include <GL/gl.h>
#include <GL/glut.h>
#else
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
#endif
#include <AR/gsub.h>
#include <AR/video.h>
#include <AR/param.h>
#include <AR/ar.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];
			argConvGlpara(patt_trans_kake, gl_para);
			
			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;

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.
	ARParam cparam;
	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);
	
	// Open the graphics window.
	argInit(&cparam, 1.0, 0, 0, 0, 0);
	
	// 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();
	argCleanup();
}

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++;
	
	argDrawMode2D();
	argDispImage(dataPtr, 0, 0);
	
	ARMarkerInfo* marker_info;
	int marker_num;
	
	if(arDetectMarker(dataPtr, 100, &marker_info, &marker_num) < 0) {
		throw(std::runtime_error("arDetectMarker() failed."));
	}
	
	argDrawMode3D();
	argDraw3dCamera( 0, 0 );
	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();
	argSwapBuffers();
}

int main(int argc, char **argv)
{
	try {
		glutInit(&argc, argv);
		init();
		patt = new Pattern();
		arVideoCapStart();
		argMainLoop( NULL, key_event, main_loop );
	} catch(std::runtime_error e) {
		cleanup();
		std::cerr << "Exception caught: " << e.what() << std::endl;
	}
	return 0;
}