Subversion Repositories gelsvn

Rev

Rev 198 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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