Subversion Repositories gelsvn

Rev

Rev 299 | Rev 341 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 299 Rev 337
Line 24... Line 24...
24
#endif
24
#endif
25
#include <string.h>
25
#include <string.h>
26
#include <stdlib.h>
26
#include <stdlib.h>
27
 
27
 
28
#include <iostream>
28
#include <iostream>
29
#include <CGLA/Vec2i.h>
-
 
30
#include <CGLA/Vec2f.h>
-
 
-
 
29
 
31
#include <IL/il.h>
30
#include <IL/il.h>
32
#include <IL/ilu.h>
31
#include <IL/ilu.h>
33
 
32
 
-
 
33
#include <CGLA/Vec2i.h>
-
 
34
#include <CGLA/Vec2f.h>
34
#include <CGLA/Vec3f.h>
35
#include <CGLA/Vec3f.h>
35
#include <CGLA/Mat4x4f.h>
36
#include <CGLA/Mat4x4f.h>
36
#include "GLGraphics/gel_glut.h"
37
#include "GLGraphics/gel_glut.h"
37
#include "GLGraphics/QuatTrackBall.h"
38
#include "GLGraphics/QuatTrackBall.h"
38
#include "GLGraphics/draw.h"
39
#include "GLGraphics/draw.h"
39
#include "Geometry/TriMesh.h"
40
#include "Geometry/TriMesh.h"
40
#include "Geometry/obj_load.h"
41
#include "Geometry/obj_load.h"
41
 
42
 
-
 
43
#include "ply_load.h"
-
 
44
 
42
using namespace std;
45
using namespace std;
43
using namespace CGLA;
46
using namespace CGLA;
44
using namespace Geometry;
47
using namespace Geometry;
45
using namespace GLGraphics;
48
using namespace GLGraphics;
46
 
49
 
47
namespace
50
namespace
48
{
51
{
49
int win_size_x = 800;
52
	int win_size_x = 800;
50
int win_size_y = 800;
53
	int win_size_y = 800;
51
 
54
	QuatTrackBall* ball;
52
QuatTrackBall* ball;
55
	int spin_timer = 20;
53
int spin_timer = 20;
56
	void spin(int x);
54
void spin(int x);
57
	int main_window;
55
 
58
	TriMesh mesh;
56
int main_window;
59
	
57
 
60
	
58
bool load_image_into_texture(const std::string& name, GLuint& id)
61
	bool load_image_into_texture(const std::string& name, GLuint& id)
59
{
62
	{
60
    unsigned char* image = 0;
63
		unsigned char* image = 0;
61
    unsigned int bpp = 0;
64
		unsigned int bpp = 0;
62
    unsigned int size_x = 0;
65
		unsigned int size_x = 0;
63
    unsigned int size_y = 0;
66
		unsigned int size_y = 0;
64
    unsigned int load_size_x = 0;
67
		unsigned int load_size_x = 0;
65
    unsigned int load_size_y = 0;
68
		unsigned int load_size_y = 0;
66
 
69
		
67
 
70
		
68
    ILenum Error;
71
		ILenum Error;
69
    ILuint  ImgId;
72
		ILuint  ImgId;
70
 
73
		
71
    static bool washere = false;
74
		static bool washere = false;
72
    if(!washere)
75
		if(!washere)
73
    {
76
		{
74
        ilInit();
77
			ilInit();
75
        iluInit();
78
			iluInit();
76
        washere=true;
79
			washere=true;
77
    }
80
		}
78
    ilEnable(IL_CONV_PAL);
81
		ilEnable(IL_CONV_PAL);
79
    ilEnable(IL_ORIGIN_SET);
82
		ilEnable(IL_ORIGIN_SET);
80
    ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
83
		ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
81
    ilGenImages(1, &ImgId);
84
		ilGenImages(1, &ImgId);
82
    ilBindImage(ImgId);
85
		ilBindImage(ImgId);
83
    char* name_cstr = const_cast<char*>(name.c_str());
86
		char* name_cstr = const_cast<char*>(name.c_str());
84
    if(!ilLoadImage(name_cstr))
87
		if(!ilLoadImage(name_cstr))
85
    {
88
		{
86
        cout << "could not load <" << name_cstr  << ">" << endl;
89
			cout << "could not load <" << name_cstr  << ">" << endl;
87
        return false;
90
			return false;
88
    }
91
		}
89
 
92
		
90
    load_size_x = ilGetInteger(IL_IMAGE_WIDTH);
93
		load_size_x = ilGetInteger(IL_IMAGE_WIDTH);
91
    load_size_y = ilGetInteger(IL_IMAGE_HEIGHT);
94
		load_size_y = ilGetInteger(IL_IMAGE_HEIGHT);
92
    bpp = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
95
		bpp = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
93
 
96
		
94
    if (bpp==24)
97
		if (bpp==24)
95
        ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
98
			ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
96
    else if (bpp==32)
99
		else if (bpp==32)
97
        ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
100
			ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
98
    else if (bpp==8)
101
		else if (bpp==8)
99
        ilConvertImage(IL_LUMINANCE, IL_UNSIGNED_BYTE);
102
			ilConvertImage(IL_LUMINANCE, IL_UNSIGNED_BYTE);
100
    else
103
		else
101
        assert(0);
104
			assert(0);
102
 
105
		
103
    unsigned int i;
106
		unsigned int i;
104
    for (i=2;i<=4096 ;i*=2)
107
		for (i=2;i<=4096 ;i*=2)
105
        if (i>=load_size_x)
108
			if (i>=load_size_x)
106
            break;
109
				break;
107
    size_x = i;
110
		size_x = i;
108
    for (i=2;i<=4096 ;i*=2)
111
		for (i=2;i<=4096 ;i*=2)
109
        if (i>=load_size_y)
112
			if (i>=load_size_y)
110
            break;
113
				break;
111
    size_y = i;
114
		size_y = i;
112
 
115
		
113
    if(size_x != load_size_x || size_y != load_size_y)
116
		if(size_x != load_size_x || size_y != load_size_y)
114
    {
117
		{
115
        iluImageParameter(ILU_FILTER, ILU_BILINEAR);
118
			iluImageParameter(ILU_FILTER, ILU_BILINEAR);
116
        iluScale(size_x, size_y, 1);
119
			iluScale(size_x, size_y, 1);
117
    }
120
		}
118
 
121
		
119
    const int image_size =size_x*size_y*ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
122
		const int image_size =size_x*size_y*ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
120
    image = new unsigned char[image_size];
123
		image = new unsigned char[image_size];
121
    memcpy(image, ilGetData(), image_size);
124
		memcpy(image, ilGetData(), image_size);
122
    ilDeleteImages(1, &ImgId);
125
		ilDeleteImages(1, &ImgId);
123
 
126
		
124
    bool any_errors=false;
127
		bool any_errors=false;
125
    while ((Error = ilGetError())) {
128
		while ((Error = ilGetError())) {
126
        cout << __LINE__ << "Error: " << iluErrorString(Error) << endl;
129
			cout << __LINE__ << "Error: " << iluErrorString(Error) << endl;
127
        any_errors = true;
130
			any_errors = true;
128
    }
131
		}
129
    if(any_errors) return false;
132
		if(any_errors) return false;
130
 
133
		
131
    GLint internalFormat=0;
134
		GLint internalFormat=0;
132
    GLenum format=0;
135
		GLenum format=0;
133
 
136
		
134
    glGenTextures(1, &id);
137
		glGenTextures(1, &id);
135
    switch (bpp)
138
		switch (bpp)
136
    {
139
		{
137
    case 8:
140
			case 8:
138
        internalFormat =  GL_LUMINANCE;
141
				internalFormat =  GL_LUMINANCE;
139
        format = GL_LUMINANCE;
142
				format = GL_LUMINANCE;
140
        break;
143
				break;
141
    case 24:
144
			case 24:
142
        internalFormat =  GL_RGB;
145
				internalFormat =  GL_RGB;
143
        format = GL_RGB;
146
				format = GL_RGB;
144
        break;
147
				break;
145
    case 32:
148
			case 32:
146
        internalFormat =  GL_RGBA;
149
				internalFormat =  GL_RGBA;
147
        format = GL_RGBA;
150
				format = GL_RGBA;
148
        break;
151
				break;
149
    default:
152
			default:
150
        return false;
153
				return false;
151
    }
154
		}
152
 
155
		
153
    glBindTexture(GL_TEXTURE_2D, id);
156
		glBindTexture(GL_TEXTURE_2D, id);
154
    glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, size_x, size_y, 0,
157
		glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, size_x, size_y, 0,
155
                 format, GL_UNSIGNED_BYTE, image);
158
					 format, GL_UNSIGNED_BYTE, image);
156
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
159
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
157
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
160
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
158
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
161
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
159
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
162
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
160
 
163
		
161
    return true;
164
		return true;
162
}
165
	}
163
 
166
	
164
void enable_textures(TriMesh& tm)
167
	void enable_textures(TriMesh& tm)
165
{
168
	{
166
    for(unsigned int i=0;i<tm.materials.size(); ++i)
169
		for(unsigned int i=0;i<tm.materials.size(); ++i)
167
    {
170
		{
168
        Material& mat = tm.materials[i];
171
			Material& mat = tm.materials[i];
169
        if(mat.tex_name != "")
172
			if(mat.tex_name != "")
170
        {
173
			{
171
            string name = mat.tex_path + mat.tex_name;
174
				string name = mat.tex_path + mat.tex_name;
172
 
175
				
173
            GLuint tex_id;
176
				GLuint tex_id;
174
            if(load_image_into_texture(name, tex_id))
177
				if(load_image_into_texture(name, tex_id))
175
							mat.tex_id = tex_id;
178
					mat.tex_id = tex_id;
176
				}
179
			}
177
    }
180
		}
178
}
181
	}
179
 
182
	
180
bool depth_pick(int x, int y,Vec3f& wp)
183
	bool depth_pick(int x, int y,Vec3f& wp)
181
{
184
	{
182
    // Enquire about the viewport dimensions
185
		// Enquire about the viewport dimensions
183
    GLint viewport[4];
186
		GLint viewport[4];
184
    glGetIntegerv(GL_VIEWPORT, viewport);
187
		glGetIntegerv(GL_VIEWPORT, viewport);
185
 
188
		
186
    // Get the minimum and maximum depth values.
189
		// Get the minimum and maximum depth values.
187
    float minmax_depth[2];
190
		float minmax_depth[2];
188
    glGetFloatv(GL_DEPTH_RANGE, minmax_depth);
191
		glGetFloatv(GL_DEPTH_RANGE, minmax_depth);
189
 
192
		
190
    // Read a single pixel at the position of the mouse cursor.
193
		// Read a single pixel at the position of the mouse cursor.
191
    float depth;
194
		float depth;
192
    glReadPixels(x, viewport[3]-y, 1,1, GL_DEPTH_COMPONENT,
195
		glReadPixels(x, viewport[3]-y, 1,1, GL_DEPTH_COMPONENT,
193
                 GL_FLOAT, (void*) &depth);
196
					 GL_FLOAT, (void*) &depth);
194
 
197
		
195
    // If the depth corresponds to the far plane, we clicked on the
198
		// If the depth corresponds to the far plane, we clicked on the
196
    // background.
199
		// background.
197
    if(depth == minmax_depth[1])
200
		if(depth == minmax_depth[1])
198
        return false;
201
			return false;
199
 
202
		
200
    // The lines below copy the viewing transformation from OpenGL
203
		// The lines below copy the viewing transformation from OpenGL
201
    // to local variables. The call to gluLookAt must have exactly
204
		// to local variables. The call to gluLookAt must have exactly
202
    // the same parameters as when the scene is drawn.
205
		// the same parameters as when the scene is drawn.
203
    glLoadIdentity();
206
		glLoadIdentity();
204
    ball->set_gl_modelview();
207
		ball->set_gl_modelview();
205
    double mvmat[16];
208
		double mvmat[16];
206
    glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
209
		glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
207
 
210
		
208
    // Copy the projection matrix. We assume it is unchanged.
211
		// Copy the projection matrix. We assume it is unchanged.
209
    double prjmat[16];
212
		double prjmat[16];
210
    glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
213
		glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
211
 
214
		
212
    // Now unproject the point from screen to world coordinates.
215
		// Now unproject the point from screen to world coordinates.
213
    double ox, oy, oz;
216
		double ox, oy, oz;
214
    gluUnProject(x,viewport[3]-y,depth,
217
		gluUnProject(x,viewport[3]-y,depth,
215
                 mvmat,prjmat,viewport,
218
					 mvmat,prjmat,viewport,
216
                 &ox, &oy, &oz);
219
					 &ox, &oy, &oz);
217
 
220
		
218
    wp = Vec3f(ox,oy,oz);
221
		wp = Vec3f(ox,oy,oz);
219
 
222
		
220
    return true;
223
		return true;
221
}
224
	}
222
 
225
	
223
 
226
 
224
TriMesh mesh;
-
 
225
 
227
 
226
void mouse_motion(int x, int y)
228
void mouse_motion(int x, int y)
227
{
229
{
228
    ball->roll_ball(Vec2i(x,y));
230
    ball->roll_ball(Vec2i(x,y));
229
}
231
}
230
 
232
 
231
void mouse(int btn, int state, int x, int y)
233
void mouse(int btn, int state, int x, int y)
232
{
234
{
233
  if(state == GLUT_DOWN) 
235
	if(state == GLUT_DOWN) 
234
  {
236
	{
235
	if(btn == GLUT_LEFT_BUTTON) 
237
		if(btn == GLUT_LEFT_BUTTON) 
236
	   ball->grab_ball(ROTATE_ACTION, Vec2i(x,y));
238
			ball->grab_ball(ROTATE_ACTION, Vec2i(x,y));
237
    else if(btn == GLUT_MIDDLE_BUTTON) 
239
		else if(btn == GLUT_MIDDLE_BUTTON) 
238
	  ball->grab_ball(ZOOM_ACTION, Vec2i(x, y));
240
			ball->grab_ball(ZOOM_ACTION, Vec2i(x, y));
239
    else if(btn == GLUT_RIGHT_BUTTON) 
241
		else if(btn == GLUT_RIGHT_BUTTON) 
240
	  ball->grab_ball(PAN_ACTION, Vec2i(x, y));
242
			ball->grab_ball(PAN_ACTION, Vec2i(x, y));
241
  }
243
	}
242
  else if(state == GLUT_UP)
244
	else if(state == GLUT_UP)
243
    ball->release_ball();	
245
		ball->release_ball();	
244
}
246
}
245
 
247
 
246
void spin(int x)
248
void spin(int x)
247
{
249
{
248
  ball->do_spin();
250
	ball->do_spin();
249
  glutTimerFunc(spin_timer, spin, 0);  
251
	glutTimerFunc(spin_timer, spin, 0);  
250
  glutPostRedisplay();
252
	glutPostRedisplay();
251
}
253
}
252
/*
-
 
253
void idle()
-
 
254
{
254
 
255
    if ( glutGetWindow() != main_window )
-
 
256
        glutSetWindow(main_window);
-
 
257
    glutPostRedisplay();
-
 
258
}
-
 
259
*/
-
 
260
void display()
255
void display()
261
{
256
{
262
    static bool washere = false;
257
    static bool washere = false;
263
    static unsigned int l;
258
    static unsigned int l;
264
    if(!washere)
259
    if(!washere)
Line 267... Line 262...
267
        l = glGenLists(1);
262
        l = glGenLists(1);
268
        glNewList(l, GL_COMPILE);
263
        glNewList(l, GL_COMPILE);
269
        draw(mesh);
264
        draw(mesh);
270
        glEndList();
265
        glEndList();
271
        washere = true;
266
        washere = true;
272
      glutTimerFunc(spin_timer, spin, 0);	
267
		glutTimerFunc(spin_timer, spin, 0);	
273
	}
268
	}
274
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
269
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
275
    glLoadIdentity();
270
    glLoadIdentity();
276
    ball->set_gl_modelview();
271
    ball->set_gl_modelview();
277
    glCallList(l);
272
    glCallList(l);
Line 280... Line 275...
280
 
275
 
281
void keyboard(unsigned char key, int x, int y)
276
void keyboard(unsigned char key, int x, int y)
282
{
277
{
283
    switch(key)
278
    switch(key)
284
    {
279
    {
285
    case '\033': exit(0); break;
280
		case '\033': exit(0); break;
286
    //case '+': ball->closer(); break;
281
			//case '+': ball->closer(); break;
287
    //case '-': ball->farther(); break;
282
			//case '-': ball->farther(); break;
288
    //default:
283
			//default:
289
    //    ball->up_axis(key);
284
			//    ball->up_axis(key);
290
    }
285
    }
291
}
286
}
292
}
287
}
293
 
288
 
294
int main(int argc, char** argv)
289
int main(int argc, char** argv)
Line 301... Line 296...
301
    glutDisplayFunc(display);
296
    glutDisplayFunc(display);
302
    glutKeyboardFunc(keyboard);
297
    glutKeyboardFunc(keyboard);
303
    glutMotionFunc(mouse_motion);
298
    glutMotionFunc(mouse_motion);
304
    glutMouseFunc(mouse);
299
    glutMouseFunc(mouse);
305
    //glutIdleFunc(idle);
300
    //glutIdleFunc(idle);
306
 
301
	
307
    // GL INIT
302
    // GL INIT
308
    glClearColor(.8f, 0.9f, 1.0f, 0.f);
303
    glClearColor(.8f, 0.9f, 1.0f, 0.f);
309
    glEnable(GL_DEPTH_TEST);
304
    glEnable(GL_DEPTH_TEST);
310
    glEnable(GL_LIGHTING);
305
    glEnable(GL_LIGHTING);
311
    glEnable(GL_LIGHT0);
306
    glEnable(GL_LIGHT0);
312
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
307
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
313
    glShadeModel(GL_SMOOTH);
308
    glShadeModel(GL_SMOOTH);
314
 
309
	
315
    // LOAD OBJ
310
    // LOAD OBJ
316
    string fn;
311
    string fn;
317
    if(argc>1)
312
    if(argc>1)
318
        fn = argv[1];
313
        fn = argv[1];
319
    else
314
    else
320
        fn = "../../data/head.obj";
315
        fn = "../../data/head.obj";
321
 
316
		
322
    cout << "Loading " << fn << endl;
317
	if(fn.substr(fn.length()-4,fn.length())==".obj")
-
 
318
	{
323
    if(fn == "") exit(0);
319
		obj_load(fn, mesh);
324
 
320
	}
-
 
321
	else if(fn.substr(fn.length()-4,fn.length())==".ply")
-
 
322
	{
325
    obj_load(fn, mesh);
323
		ply_load(fn, mesh);
-
 
324
	}	
-
 
325
	else
-
 
326
	{
-
 
327
		cout << "Either the format was unrecognized or the file did not have the appropriate extension" << endl;
-
 
328
		exit(0);
-
 
329
	}		
326
    enable_textures(mesh);
330
/*    enable_textures(mesh);	
327
    if(!mesh.has_normals())
331
    if(!mesh.has_normals())
328
    {
332
    {
329
        cout << "Computing normals" << endl;
333
        cout << "Computing normals" << endl;
330
        mesh.compute_normals();
334
        mesh.compute_normals();
331
    }
335
    }
332
 
336
*/	
333
    // Initialize Trackball
337
    // Initialize Trackball
334
    Vec3f c;
338
    Vec3f c;
335
    float r;
339
    float r;
336
    mesh.get_bsphere(c,r);
340
    mesh.get_bsphere(c,r);
337
    r *= 1.5;
341
    r *= 1.5;
338
    ball = new QuatTrackBall(c,r,800,800);
342
    ball = new QuatTrackBall(c,r,800,800);
339
 
343
	
340
    // Setup projection
344
    // Setup projection
341
    glMatrixMode(GL_PROJECTION);
345
    glMatrixMode(GL_PROJECTION);
342
    glLoadIdentity();
346
    glLoadIdentity();
343
    gluPerspective(53,1.0f,r/100.0,r*3.0);
347
    gluPerspective(53,1.0f,r/100.0,r*3.0);
344
    glMatrixMode(GL_MODELVIEW);
348
    glMatrixMode(GL_MODELVIEW);
345
 
349
	
346
    // Pass control to GLUT
350
    // Pass control to GLUT
347
    glutMainLoop();
351
    glutMainLoop();
348
 
352
	
349
    return 0;
353
    return 0;
350
}
354
}