Subversion Repositories gelsvn

Rev

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