1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
/*
* Kamil Kaminski
* kamilkss@gmail.com
*
* OBJ Loader
*
* Code:
* http://www.gamedev.net/community/forums/topic.asp?topic_id=312335
*
* todo: move obj files and texures into folder named e.g. "obj", or find some way
* of figuring out the current directory, hmm, could modify a path string, e.g.
* "./cube/cube.obj" -> "/cube/X", I would need to loop for last occurence of "/"
*
*/
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "math3d.h"
#include "obj.h"
/* global */
int program_running = 1;
static void resize(int w, int h)
{
printf("window: resizing to %dx%d\n", w, h);
GLfloat fAspect = (GLfloat) w / (GLfloat) h;
if (h == 0)
h = 1;
glViewport(0, 0, w, h);
/* reset coordinate system */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* produce the perspective projection */
gluPerspective(45.0f, fAspect, 1.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* this needs to be ran again, glut does it for you I suppose */
SDL_SetVideoMode(w, h, 32, SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_RESIZABLE);
}
static void setup_opengl(ObjModel *model)
{
/* light values and coordinates */
GLfloat whiteLight[] = { 0.05f, 0.05f, 0.05f, 1.0f };
GLfloat sourceLight[] = { 0.75f, 0.75f, 0.75f, 1.0f };
GLfloat lightPos[] = { -10.f, 5.0f, 5.0f, 1.0f };
glEnable(GL_DEPTH_TEST);/* hidden surface removal */
glFrontFace(GL_CCW);/* counter clock-wise polygons face out */
glEnable(GL_CULL_FACE); /* do not calculate inside of a pyramid */
/* enable lighting */
glEnable(GL_LIGHTING);
/* setup and enable light 0 */
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, whiteLight);
glLightfv(GL_LIGHT0, GL_AMBIENT, sourceLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, sourceLight);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glEnable(GL_LIGHT0);
/* enable color tracking */
glEnable(GL_COLOR_MATERIAL);
/* set Material properties to follow glColor values */
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
/* gray background */
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
/* example of loading a targa file, note: RLE compression is not supported */
GLbyte *pBytes;
GLint iWidth, iHeight, iComponents;
GLenum eFormat;
/* get the texture filename from obj */
char *fname = ObjGetPath(model->mtl->map_Ka); /* ambient texture map */
if (model->mtl != NULL)
printf("ambient texture map: %s\n", model->mtl->map_Ka);
/* load texture */
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
pBytes = gltLoadTGA(fname, &iWidth, &iHeight, &iComponents, &eFormat);
if (!pBytes)
fprintf(stderr, "gltLoadTGA: failed to load texture!\n");
/* load texture image */
glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat,
GL_UNSIGNED_BYTE, pBytes);
free(pBytes);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_TEXTURE_2D);
}
/* process SDL events */
void process_events()
{
SDL_Event event;
SDLKey sym;
while (SDL_PollEvent(&event))
{
sym = event.key.keysym.sym;
switch (event.type)
{
case SDL_KEYDOWN:
{
if (sym == SDLK_ESCAPE) program_running = 0;
break;
}
case SDL_VIDEORESIZE: { resize(event.resize.w, event.resize.h); break; }
case SDL_QUIT: { program_running = 0; break; }
default: break;
}
}
}
static void draw_ground(void)
{
GLfloat fExtent = 20.0f;
GLfloat fStep = 0.5f;
GLfloat y = -0.4f;
GLfloat iLine;
glBegin(GL_LINES);
for (iLine = -fExtent; iLine <= fExtent; iLine += fStep)
{
glVertex3f(iLine, y, fExtent);
glVertex3f(iLine, y, -fExtent);
glVertex3f(fExtent, y, iLine);
glVertex3f(-fExtent, y, iLine);
}
glEnd();
}
void render(ObjModel *model)
{
/* clear the window with current clearing color */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw_ground();
glPushMatrix();
glScalef(0.1f, 0.1f, 0.1f);
glTranslatef(5.0f, 0.0f, -150.0f);
glBegin(GL_QUADS);
int i;
for (i = 0; i < model->nFace; i++)
{
glNormal3f(model->NormalArray[model->FaceArray[i].Normal[0] - 1].x, model->NormalArray[model->FaceArray[i].Normal[0] - 1].y, model->NormalArray[model->FaceArray[i].Normal[0] - 1].z );
glTexCoord2f(model->TexCoordArray[model->FaceArray[i].TexCoord[0] - 1].u, model->TexCoordArray[model->FaceArray[i].TexCoord[0] - 1].v);
glVertex3f(model->VertexArray[model->FaceArray[i].Vertex[0] - 1].x, model->VertexArray[model->FaceArray[i].Vertex[0] - 1].y, model->VertexArray[model->FaceArray[i].Vertex[0] - 1].z );
glNormal3f(model->NormalArray[model->FaceArray[i].Normal[1] - 1].x, model->NormalArray[model->FaceArray[i].Normal[1] - 1].y, model->NormalArray[model->FaceArray[i].Normal[1] - 1].z );
glTexCoord2f(model->TexCoordArray[model->FaceArray[i].TexCoord[1] - 1].u, model->TexCoordArray[model->FaceArray[i].TexCoord[1] - 1].v);
glVertex3f(model->VertexArray[model->FaceArray[i].Vertex[1] - 1].x, model->VertexArray[model->FaceArray[i].Vertex[1] - 1].y, model->VertexArray[model->FaceArray[i].Vertex[1] - 1].z );
glNormal3f(model->NormalArray[model->FaceArray[i].Normal[2] - 1].x, model->NormalArray[model->FaceArray[i].Normal[2] - 1].y, model->NormalArray[model->FaceArray[i].Normal[2] - 1].z );
glTexCoord2f(model->TexCoordArray[model->FaceArray[i].TexCoord[2] - 1].u, model->TexCoordArray[model->FaceArray[i].TexCoord[2] - 1].v);
glVertex3f(model->VertexArray[model->FaceArray[i].Vertex[2] - 1].x, model->VertexArray[model->FaceArray[i].Vertex[2] - 1].y, model->VertexArray[model->FaceArray[i].Vertex[2] - 1].z );
glNormal3f(model->NormalArray[model->FaceArray[i].Normal[3] - 1].x, model->NormalArray[model->FaceArray[i].Normal[3] - 1].y, model->NormalArray[model->FaceArray[i].Normal[3] - 1].z );
glTexCoord2f(model->TexCoordArray[model->FaceArray[i].TexCoord[3] - 1].u, model->TexCoordArray[model->FaceArray[i].TexCoord[3] - 1].v);
glVertex3f(model->VertexArray[model->FaceArray[i].Vertex[3] - 1].x, model->VertexArray[model->FaceArray[i].Vertex[3] - 1].y, model->VertexArray[model->FaceArray[i].Vertex[3] - 1].z );
}
glEnd();
glPopMatrix();
/* buffer swap */
SDL_GL_SwapBuffers();
}
int main(void)
{
SDL_Surface *screen;
if (SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, "unable to init SDL: %s\n", SDL_GetError());
exit(-1);
}
atexit(SDL_Quit);
SDL_WM_SetCaption("OBJ Loader", NULL);
if ((screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL | SDL_GL_DOUBLEBUFFER
| SDL_RESIZABLE)) == NULL)
{
fprintf(stderr, "unable to set video mode: %s\n", SDL_GetError());
exit(-1);
}
SDL_EnableUNICODE(1);
/* SDL doesn't trigger off a ResizeEvent at startup, but as we need this
* for OpenGL, we do this ourselves */
SDL_Event resizeEvent;
resizeEvent.type = SDL_VIDEORESIZE;
resizeEvent.resize.w = 640;
resizeEvent.resize.h = 480;
SDL_PushEvent(&resizeEvent);
/* initalize glew */
GLenum glewerr = glewInit();
if (GLEW_OK != glewerr)
{
fprintf(stderr, "error: %s\n", glewGetErrorString(glewerr));
return -1;
}
else
fprintf(stdout, "status: using GLEW %s\n", glewGetString(GLEW_VERSION));
/* OBJ Loading */
char *memory = NULL;
size_t bytes = ObjLoadFile("./cube/cube.obj", &memory);
ObjModel *model = ObjLoadModel(memory, bytes);
/* setup few stuff before rendring */
setup_opengl(model);
/* main loop */
while (program_running)
{
process_events();
render(model);
}
/* free up the resources */
ObjFree(model);
puts("bye!");
return 0;
}
|