Subversion Repositories gelsvn

Rev

Rev 195 | Rev 299 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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