Subversion Repositories gelsvn

Rev

Rev 132 | Rev 195 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 132 Rev 178
Line 16... Line 16...
16
#include <list>
16
#include <list>
17
#include <vector>
17
#include <vector>
18
 
18
 
19
#include <CGLA/Vec3f.h>
19
#include <CGLA/Vec3f.h>
20
#include <CGLA/Mat4x4f.h>
20
#include <CGLA/Mat4x4f.h>
21
#include "Graphics/gel_glut.h"
21
#include "GLGraphics/gel_glut.h"
-
 
22
#include "GLGraphics/SimpleTrackBall.h"
-
 
23
#include "GLGraphics/draw.h"
22
#include "Geometry/TriMesh.h"
24
#include "Geometry/TriMesh.h"
23
#include "Graphics/SimpleTrackBall.h"
-
 
24
#include "Geometry/obj_load.h"
25
#include "Geometry/obj_load.h"
25
 
26
 
26
using namespace std;
27
using namespace std;
27
using namespace CGLA;
28
using namespace CGLA;
28
using namespace Geometry;
29
using namespace Geometry;
29
using namespace Graphics;
30
using namespace GLGraphics;
30
 
31
 
31
namespace
32
namespace
32
{
33
{
33
	int win_size_x = 800;
34
int win_size_x = 800;
34
	int win_size_y = 800;
35
int win_size_y = 800;
35
 
36
 
36
	SimpleTrackBall* ball;
37
SimpleTrackBall* ball;
37
 
38
 
38
	int main_window; 
39
int main_window;
39
 
40
 
-
 
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>
-
 
49
 
-
 
50
#include <iostream>
-
 
51
#include <CGLA/Vec2i.h>
-
 
52
#include <CGLA/Vec2f.h>
-
 
53
#include <IL/il.h>
-
 
54
#include <IL/ilu.h>
-
 
55
 
-
 
56
 
-
 
57
using namespace std;
-
 
58
using namespace CGLA;
-
 
59
 
-
 
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;
-
 
68
 
-
 
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;
-
 
164
}
-
 
165
 
-
 
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
}
40
 
181
 
41
	bool depth_pick(int x, int y,Vec3f& wp)
182
bool depth_pick(int x, int y,Vec3f& wp)
42
	{
183
{
43
		// Enquire about the viewport dimensions
184
    // Enquire about the viewport dimensions
44
		GLint viewport[4];
185
    GLint viewport[4];
Line 117... Line 258...
117
			if(!washere)
258
    if(!washere)
118
			{
259
    {
119
					cout << "Creating display list" << endl;
260
        cout << "Creating display list" << endl;
120
					l = glGenLists(1);
261
        l = glGenLists(1);
121
					glNewList(l, GL_COMPILE);
262
        glNewList(l, GL_COMPILE);
122
					mesh.gl_draw();
263
        draw(mesh);
123
					glEndList();
264
        glEndList();
124
					washere = true;
265
        washere = true;
125
			}
266
    }
126
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
267
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
127
			glLoadIdentity();
268
    glLoadIdentity();
Line 171... Line 312...
171
		else
312
    else
172
			fn = "../../data/head.obj";
313
        fn = "../../data/head.obj";
173
 
314
 
174
	cout << "Loading " << fn << endl;
315
    cout << "Loading " << fn << endl;
175
	if(fn == "") exit(0);
316
    if(fn == "") exit(0);
-
 
317
 
176
	obj_load(fn, mesh);
318
    obj_load(fn, mesh);
-
 
319
    enable_textures(mesh);
177
	if(!mesh.has_normals())
320
    if(!mesh.has_normals())
178
		{
321
    {
179
			cout << "Computing normals" << endl;
322
        cout << "Computing normals" << endl;
180
			mesh.compute_normals();
323
        mesh.compute_normals();
181
		}
324
    }
182
	// Initialize textures
-
 
183
	mesh.gl_init_textures();
-
 
184
	
325
 
185
	// Initialize Trackball
326
    // Initialize Trackball
186
	Vec3f c;
327
    Vec3f c;
187
	float r;
328
    float r;
188
	mesh.get_bsphere(c,r);
329
    mesh.get_bsphere(c,r);