Subversion Repositories gelsvn

Rev

Rev 299 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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