Subversion Repositories gelsvn

Rev

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