· 8 years ago · Dec 07, 2017, 06:54 PM
1// main.c
2//
3// Copyright 2011 upd <zaloznik.robert [at] gmail.com>
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18// MA 02110-1301, USA.
19
20/*
21 * -----------------------------------------------
22 * Komutativnost:
23 * Dvočlena operacija * na množici S je komutativna, če za vsak x, y € S velja:
24 * x * y = y * x
25 * -----------------------------------------------
26 * Distributivnost:
27 * Distributivnost se v matematiki imenuje posebno razmerje med dvema dvoÄŤlenima operacijama.
28 * Pravimo, da je operacija * distributivna nad +, ÄŤe veljata distributivnostna zakona
29 * (a + b) * c = (a * c) + (b * c) in
30 * c * (a + b) = (c * a) + (c * a).
31 * -----------------------------------------------
32 * Homogenost:
33 * Homogenost pomeni, da lahko pri razliÄŤnih raÄŤunskih operacijah vrstni red raÄŤunanja zamenjamo.
34 * Zgled za to je homogenost vektorskega produkta in...
35 * podobno velja za skalarni produkt.... glej wiki!
36 * ----------------------------------------------
37 * Asociativnost:
38 * Dvočlena operacija * na množici S je asociativna, če za vsak x, y, z € S velja:
39 * (x * y) * z = x * (y * z).
40 * ----------------------------------------------
41 * Compile:
42 * gcc main.c -o game -lGL -lGLU `sdl-config --cflags --libs`
43*/
44#include <stdio.h>
45#include <stdlib.h>
46#include <GL/gl.h>
47#include <GL/glu.h>
48#include <math.h>
49#include <time.h>
50#include "SDL.h"
51#include "3dsModel.h"
52
53#include "Matrix.h"
54#include "Vector.h"
55
56// Define Constants
57#define PI (3.141592653589793)
58
59/* Define our booleans */
60#define TRUE 1
61#define FALSE 0
62
63#define DegCos(x) cos((x*PI)/180)
64#define DegSin(x) sin((x*PI)/180)
65#define DegTan(x) tan((x*PI)/180)
66
67#define WindowWidth 1024
68#define WindowHeight 768
69
70void MoveForward();
71void MoveBarkward();
72void MoveLeft();
73void MoveRight();
74void NoMove();
75void NoTurn();
76
77void UpdatePosition();
78void UpdateMouse();
79void UpdateCamera();
80void DrawFloor();
81void Info();
82
83// For camera...
84Vector3 cameraPosition = { 0.0, 0.0, 0.0 };
85Vector3 cameraLookAt = { 0.0, 0.0, -1.0 };
86
87// For moving around...
88Vector3 direction = { 0.0, 0.0, 0.0 };
89Vector3 position = { 0.0, 30.0, 0.0 };
90
91// Static Members
92static float moveSpeed = 0.8f;
93static float turnSpeed = 0.4f;
94
95// Global variables...
96SDL_Surface *surface;
97
98// Mouse
99float mouseX, mouseY;
100float CameraYaw = 0.0f;
101float CameraPitch = 0.0f;
102float UpDownRot = 0.0f;
103float LeftRightRot = 0.0f;
104
105int x = WindowWidth/2;
106int y = WindowHeight/2;
107
108int CenterX = WindowWidth/2;
109int CenterY = WindowHeight/2;
110
111Vector3 veleocity;
112Matrix moveDirection;
113
114// Model Stuff
115Model *model;
116
117/* function to reset our viewport after a window resize */
118int resizeWindow( int width, int height )
119{
120 /* Height / width ration */
121 GLfloat ratio;
122
123 /* Protect against a divide by zero */
124 if ( height == 0 )
125 height = 1;
126
127 ratio = ( GLfloat )width / ( GLfloat )height;
128
129 /* Setup our viewport. */
130 glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height );
131
132 /* change to the projection matrix and set our viewing volume. */
133 glMatrixMode( GL_PROJECTION );
134 glLoadIdentity();
135
136 /* Set our perspective */
137 gluPerspective( 45.0f, ratio, 0.1f, 10000.0f );
138
139 /* Make sure we're chaning the model view and not the projection */
140 // glMatrixMode( GL_MODELVIEW );
141
142 /* Reset The View */
143 // glLoadIdentity();
144
145 return( TRUE );
146}
147
148void KeyboardInputDown( SDL_keysym *keysym )
149{
150 if(keysym->sym == SDLK_ESCAPE)
151 {
152 SDL_Quit();
153 }
154
155 if(keysym->sym == SDLK_w || keysym->sym == SDLK_UP)
156 {
157 MoveForward();
158 printf("Move forward\n");
159 }
160 else if(keysym->sym == SDLK_s || keysym->sym == SDLK_DOWN)
161 {
162 MoveBarkward();
163 printf("Move backward\n");
164 }
165
166 if(keysym->sym == SDLK_a || keysym->sym == SDLK_LEFT)
167 {
168 MoveLeft();
169 printf("Move left\n");
170 }
171 else if(keysym->sym == SDLK_d || keysym->sym == SDLK_RIGHT)
172 {
173 MoveRight();
174 printf("Move right\n");
175 }
176}
177
178void KeyboardInputUp( SDL_keysym *keysym )
179{
180 if(keysym->sym == SDLK_w || keysym->sym == SDLK_s
181 || keysym->sym == SDLK_UP || keysym->sym == SDLK_DOWN)
182 {
183 printf("No move...\n");
184 NoMove();
185 }
186
187 if(keysym->sym == SDLK_a || keysym->sym == SDLK_d
188 || keysym->sym == SDLK_LEFT || keysym->sym == SDLK_RIGHT)
189 {
190 printf("No turn...\n");
191 NoTurn();
192 }
193}
194
195void MoveForward()
196{
197 direction.z = -1.0f;
198// position.z += -1.0f * moveSpeed;
199}
200
201void MoveBarkward()
202{
203 direction.z = 1.0f;
204// position.z += 1.0f * moveSpeed;
205}
206
207void MoveLeft()
208{
209 direction.x = -1.0f;
210// position.x += -1.0f * moveSpeed;
211}
212
213void MoveRight()
214{
215 direction.x = 1.0f;
216// position.x += 1.0f * moveSpeed;
217}
218
219void NoMove()
220{
221 direction.z = 0.0f;
222}
223
224void NoTurn()
225{
226 direction.x = 0.0f;
227}
228
229/* general OpenGL initialization function */
230int initGL( GLvoid )
231{
232 /* Enable smooth shading */
233 glShadeModel( GL_SMOOTH );
234
235 /* Set the background black */
236 glClearColor( 0.0f, 0.0f, 0.1f, 0.0f );
237
238 /* Depth buffer setup */
239 glClearDepth( 1.0f );
240
241 /* Enables Depth Testing */
242 glEnable( GL_DEPTH_TEST );
243
244 /* The Type Of Depth Test To Do */
245 glDepthFunc( GL_LEQUAL );
246
247 /* Really Nice Perspective Calculations */
248 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
249
250 // glEnable( GL_DEPTH_TEST );
251 // glClearColor(0.0, 0.0, 0.2, 0.0); // This clear the background color to dark blue
252 // glShadeModel(GL_SMOOTH); // Type of shading for the polygons
253 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); // Polygon rasterization mode (polygon filled)
254
255 glEnable(GL_TEXTURE_2D); // This Enable the Texture mapping
256
257 return( TRUE );
258}
259
260void LoadAllStuff()
261{
262 int i = 0;
263
264 i = Load_Model(&model, "House Draft 29A 191110.3DS");//demo.3ds");
265 printf("Model name is %s\n", model->modelName);
266
267 if(i == 1)
268 {
269 printf("Model Succesfully loaded.\n");
270 i = Load_Model_Textures(model);
271 if(i == 1)
272 {
273 printf("Texture Succesfully loaded.\n");
274 }
275 else
276 {
277 printf("Error in loading textures.\n");
278 exit(0);
279 }
280 }
281 else
282 {
283 printf("Load_Model returned error code.\n");
284 exit(0);
285 }
286}
287
288int main( int argc, char* args[] )
289{
290 int loop = TRUE;
291 int err = 0;
292 SDL_Event event; // KEyboard, mouse, input...
293 const SDL_VideoInfo *videoInfo;
294
295 //Start SDL
296 err = SDL_Init( SDL_INIT_VIDEO );
297 if( err < 0 )
298 {
299 fprintf(stderr, "Video initialization failed: %s.\n", SDL_GetError());
300 SDL_Quit();
301 }
302 SDL_WM_SetCaption("Robert Zaloznik OpenGl & SDL Game", "No Icon Title.");
303
304 videoInfo = SDL_GetVideoInfo();
305
306 if (!videoInfo)
307 {
308 fprintf(stderr, "Video query failed: %s.\n", SDL_GetError());
309 SDL_Quit();
310 }
311
312 int videoFlags;
313
314 videoFlags = SDL_OPENGL; /* Enable OpenGl in SDL */
315 videoFlags |= SDL_GL_DOUBLEBUFFER; /* Enable double buffering */
316 videoFlags |= SDL_HWPALETTE; /* Store the palette in hardware */
317
318 /* This checks to see if surfaces can be stored in memory */
319 if ( videoInfo->hw_available )
320 videoFlags |= SDL_HWSURFACE;
321 else
322 videoFlags |= SDL_SWSURFACE;
323
324 /* This checks if hardware blits can be done */
325 if ( videoInfo->blit_hw )
326 videoFlags |= SDL_HWACCEL;
327
328 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
329
330 surface = SDL_SetVideoMode( WindowWidth, WindowHeight, 32, videoFlags );
331
332 if (!surface)
333 {
334 fprintf(stderr, "Video mode set failed: %s\n", SDL_GetError());
335 SDL_Quit();
336 }
337
338 /* Initialize OpenGl */
339 initGL();
340
341 /* resize the initial window */
342 resizeWindow( WindowWidth, WindowHeight );
343
344 SDL_ShowCursor(0);
345
346 SDL_WarpMouse(CenterX, CenterY);
347 // SDL_GetMouseState(&x, &y);
348
349 //printf("1) CenterX %d, CenterY %d, x = %d, y = %d\n", CenterX, CenterY, x, y);
350
351 LoadAllStuff();
352
353 glEnable(GL_LIGHTING);
354
355 while ( loop )
356 {
357 while( SDL_PollEvent(&event) )
358 {
359 switch( event.type )
360 {
361 case SDL_KEYDOWN:
362 KeyboardInputDown( &event.key.keysym );
363 break;
364 case SDL_KEYUP:
365 KeyboardInputUp( &event.key.keysym );
366 break;
367 case SDL_QUIT:
368 loop = FALSE;
369 break;
370 case SDL_MOUSEMOTION:
371 UpdateMouse();
372 break;
373 default:
374 break;
375 }
376 }
377 //UpdateMouse();
378 UpdatePosition();
379
380 //UpdatePosition();
381 UpdateCamera();
382
383 //glDisable(GL_TEXTURE_2D);
384 //DrawFloor();
385
386 //glEnable(GL_TEXTURE_2D);
387 glClearColor(0.0, 0.0, 0.2, 0.0);
388
389 Draw3d(model);
390
391 Info();
392
393 glFlush();
394 SDL_GL_SwapBuffers();
395
396 //UpdatePosition();
397 //UpdateMouse();
398 }
399
400 //Quit SDL
401 SDL_Quit();
402
403 return 0;
404}
405
406void UpdateCamera()
407{
408 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
409
410 // Model Matrix + View Matrix, set to active.
411 glMatrixMode(GL_MODELVIEW);
412
413 glLoadIdentity();
414
415 if(LeftRightRot < -360.0f)
416 {
417 LeftRightRot += 360.0f;
418 }
419 else if(LeftRightRot > 360.0f)
420 {
421 LeftRightRot -= 360.0f;
422 }
423
424 if(UpDownRot > 89.8f)
425 {
426 UpDownRot = 89.8f;
427 }
428 else if(UpDownRot < -89.8f)
429 {
430 UpDownRot = -89.8f;
431 }
432
433// -------------------------------------------
434// glRotatef(UpDownRot, -1.0f, 0.0f, 0.0f);
435// glRotatef(LeftRightRot, 0.0f, -1.0f, 0.0f);
436// -------------------------------------------
437
438// glPushMatrix();
439
440 Matrix cameraMatrix = Matrix_Multiply(
441 Matrix_CreateRotationX(UpDownRot),
442 Matrix_CreateRotationY(LeftRightRot)
443 );
444 glMultMatrixf(cameraMatrix.m);
445
446 moveDirection = Matrix_CreateRotationY(LeftRightRot);
447
448 Vector3 cameraReference = { direction.x, 0.0f, direction.z };
449
450 veleocity = Vector3_Transform(cameraReference, moveDirection);
451
452 position.x += veleocity.x * moveSpeed;
453 position.z += veleocity.z * moveSpeed;
454
455 position.y = 20.0f;
456
457 // printf("Veleocity: %f, %f, %f\n", veleocity.x, veleocity.y, veleocity.z);
458 // printf("Direction: %f, %f, %f\n", direction.x, direction.y, direction.z);
459 //printf("Position: %f, %f, %f\n", position.x, position.y, position.z);
460
461 glTranslatef(-position.x, -position.y, -position.z);
462}
463
464void UpdateMouse()
465{
466 SDL_GetMouseState(&x, &y);
467
468 CameraYaw = (CenterX - x);
469 CameraPitch = (CenterY - y);
470
471
472 //printf("CenterX %d, CenterY %d, x = %d, y = %d\n", CenterX, CenterY, x, y);
473// printf("CameraYaw %f, CameraPitch %f\n", CameraYaw, CameraPitch);
474
475 /*
476 if(CameraYaw < -10)
477 CameraYaw = -10;
478 else if(CameraYaw > 10)
479 CameraYaw = 10;
480
481 if(CameraPitch < -10)
482 CameraPitch = -10;
483 else if(CameraPitch > 10)
484 CameraPitch = 10;
485 */
486// CameraYaw *= 0.5f;
487// CameraPitch *= 0.5f;
488
489// if(x < CenterX)
490// {
491 LeftRightRot += CameraYaw * turnSpeed;
492 // printf("LeftRightRot is %f\n", LeftRightRot);
493// }
494// else if(x > CenterX)
495// {
496// LeftRightRot += CameraYaw;
497// }
498
499// if(y < CenterY)
500// {
501 UpDownRot += CameraPitch * turnSpeed;
502
503 // LeftRightRot *= 0.2f;
504 // UpDownRot *= 0.2f;
505// }
506// else if(y > CenterY)
507// {
508// UpDownRot += CameraPitch;
509// }
510
511 SDL_WarpMouse(CenterX, CenterY);
512}
513
514void DrawFloor()
515{
516// Clockwise vertices order.
517//glPushMatrix();
518 // Bottom
519 glBegin(GL_QUADS);
520 glColor3f(1.0f, 0.0f, 0.0f);
521 glVertex3f(-50.0f, 0.0f, -50.0f);
522
523 glColor3f(0.0f, 1.0f, 0.0f);
524 glVertex3f(50.0f, 0.0f, -50.0f);
525
526 glColor3f(0.0f, 0.0f, 1.0f);
527 glVertex3f(50.0f, 0.0f, 50.0f);
528
529 glColor3f(1.0f, 1.0f, 0.0f);
530 glVertex3f(-50.0f, 0.0f, 50.0f);
531 glEnd();
532//glPopMatrix();
533
534 // global var. up...
535 // glTranslatef(0.0f, 50.0f, 0.0f); // blah move up 50.0f units...
536
537 // Back
538glPushMatrix();
539 glTranslatef(0.0f, 50.0f, 0.0f);
540 glTranslatef(0.0f, 0.0f, -50.0f);
541 glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
542
543 glBegin(GL_QUADS);
544
545 glColor3f(0.1f, 0.1f, 0.1f); // white...
546
547 glVertex3f(-50.0f, 0.0f, -50.0f);
548 glVertex3f(50.0f, 0.0f, -50.0f);
549 glVertex3f(50.0f, 0.0f, 50.0f);
550 glVertex3f(-50.0f, 0.0f, 50.0f);
551
552 glEnd();
553glPopMatrix();
554
555
556 // Front
557glPushMatrix();
558 glTranslatef(0.0f, 50.0f, 0.0f);
559 glTranslatef(0.0f, 0.0f, 50.0f);
560 glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
561
562 glBegin(GL_QUADS);
563
564 glColor3f(1.0f, 0.5f, 0.5f); // white...
565
566 glVertex3f(-50.0f, 0.0f, -50.0f);
567 glVertex3f(50.0f, 0.0f, -50.0f);
568 glVertex3f(50.0f, 0.0f, 50.0f);
569 glVertex3f(-50.0f, 0.0f, 50.0f);
570
571 glEnd();
572glPopMatrix();
573
574
575 // Left
576glPushMatrix();
577 glTranslatef(-50.0f, 50.0f, 0.0f);
578 glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
579 //glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
580
581 glBegin(GL_QUADS);
582
583 glColor3f(0.4f, 0.1f, 0.8f); // white...
584
585 glVertex3f(-50.0f, 0.0f, -50.0f);
586 glVertex3f(50.0f, 0.0f, -50.0f);
587 glVertex3f(50.0f, 0.0f, 50.0f);
588 glVertex3f(-50.0f, 0.0f, 50.0f);
589
590 glEnd();
591glPopMatrix();
592
593 // Right
594glPushMatrix();
595 glTranslatef(50.0f, 50.0f, 0.0f);
596 glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
597
598 glBegin(GL_QUADS);
599
600 glColor3f(0.2f, 0.4f, 0.7f); // white...
601
602 glVertex3f(-50.0f, 0.0f, -50.0f);
603 glVertex3f(50.0f, 0.0f, -50.0f);
604 glVertex3f(50.0f, 0.0f, 50.0f);
605 glVertex3f(-50.0f, 0.0f, 50.0f);
606
607 glEnd();
608glPopMatrix();
609
610
611// glPopMatrix();
612 // SDL_GL_SwapBuffers( );
613}
614
615void Info()
616{
617 static GLint T0 = 0;
618 static GLint Frames = 0;
619
620 Frames++;
621 {
622 GLint t = SDL_GetTicks();
623
624 if (t - T0 >= 1000)
625 {
626 GLfloat seconds = (t - T0) / 1000.0;
627 GLfloat fps = Frames / seconds;
628
629 printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
630
631 T0 = t;
632 Frames = 0;
633
634 printf("Position: %f, %f, %f\n", position.x, position.y, position.z);
635 // printf("Direction: %f, %f, %f\n", direction.x, direction.y, direction.z);
636 // printf("Veleocity: %f, %f, %f\n", veleocity.x, veleocity.y, veleocity.z);
637 // printf("Movedirection.m[10] = %f\n", moveDirection.m[10]);
638
639 printf("LeftRight=[%f], UpDown=[%f]\n", LeftRightRot, UpDownRot);
640
641
642
643 //printf("Mouse X=%d, Y=%d\n", x, y);
644 //printf("Mouse Yaw=%f, Pitch=%f\n", CameraYaw, CameraPitch);
645 // printf("Camera rotation: LeftRight=%f, UpDown=%f\n", LeftRightRot, UpDownRot);
646 // printf("My direction is %f, %f, %f\n", direction.x, direction.y, direction.z);
647
648 //printf("FaceAt is %f, %f\n", faceAt.x, faceAt.z);
649 }
650 }
651}
652
653// matrix.c
654//
655// Copyright 2011 upd <upd@home>
656//
657// This program is free software; you can redistribute it and/or modify
658// it under the terms of the GNU General Public License as published by
659// the Free Software Foundation; either version 2 of the License, or
660// (at your option) any later version.
661//
662// This program is distributed in the hope that it will be useful,
663// but WITHOUT ANY WARRANTY; without even the implied warranty of
664// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
665// GNU General Public License for more details.
666//
667// You should have received a copy of the GNU General Public License
668// along with this program; if not, write to the Free Software
669// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
670// MA 02110-1301, USA.
671//
672//
673
674#include "Matrix.h"
675
676Matrix Matrix_CreateRotationX(float angle);
677Matrix Matrix_CreateRotationY(float angle);
678Matrix Matrix_CreateRotationZ(float angle);
679/*
680 * |--------------------|
681 * | Matrix functions: | Opengl Use Right-Handed Cartesian coordinate system.
682 * |--------------------|
683 *
684 * Te tri funkcije ustvarijo matriko, ki je rotirana okoli konstantne osi.
685 * Matrika je rotirana za podan kot, ki pa je doloÄŤen v stopinjah.
686 */
687
688// Now the funny stuff again, three function's that creates rotation's matrix..
689// If angle is positive > 0, then rotation is counter-clockwise, v nasprotni smeri urinega kazalca...
690// If angle is negative < 0, then rotation is clockwise, v smeri urinega kazalca...
691// Rotation around any axis for 0 degrees is equal to identity matrix!!!
692Matrix Matrix_CreateRotationX(float angle)
693{
694 if(angle == 0.0f)
695 return IdentityM;
696
697 Matrix result = IdentityM;
698
699 result.m[5] = DegCos(angle);
700 result.m[6] = -DegSin(angle);
701 result.m[9] = DegSin(angle);
702 result.m[10] = DegCos(angle);
703
704 return result;
705}
706
707Matrix Matrix_CreateRotationY(float angle)
708{
709 if(angle == 0.0f)
710 return IdentityM;
711
712 Matrix result = IdentityM;
713
714 result.m[0] = DegCos(angle);
715 result.m[2] = DegSin(angle);
716 result.m[8] = -DegSin(angle);
717 result.m[10] = DegCos(angle);
718
719 //printf("Angle is %f, DegCos(%f) is %f, and result.m[10] is %f\n", angle, angle, DegCos(angle), result.m[10]);
720
721 return result;
722}
723
724Matrix Matrix_CreateRotationZ(float angle)
725{
726 if(angle == 0.0f)
727 return IdentityM;
728
729 Matrix result = IdentityM;
730
731 result.m[0] = DegCos(angle);
732 result.m[1] = -DegSin(angle);
733 result.m[4] = DegSin(angle);
734 result.m[5] = DegCos(angle);
735
736 return result;
737}
738
739// Naredi matriko, ki je rotirana okoli podanega vektorja, za dani kot.
740// Now the funny part, create rotation matrix around an arbitrary vector.
741// Creates a new Matrix that rotates around an arbitrary vector.
742Matrix Matrix_CreateFromAxisAngle(Vector3 axisRot, float angle)
743{
744 Matrix result = IdentityM;
745 Vector3 axis = Vector3_Normalize(axisRot); // zelo pomembno, saj drugaÄŤe dobimo napaÄŤne rezultate!
746
747 // if not axis yet normalized, then normalize it.
748 float s = DegSin(angle);
749 float c = DegCos(angle);
750 float t = 1.0f - c;
751
752 result.m[0] = t * axis.x * axis.x + c;
753 result.m[1] = t * axis.x * axis.y + s * axis.z;
754 result.m[2] = t * axis.x * axis.z - c * axis.y;
755
756 result.m[4] = t * axis.y * axis.x - s * axis.z;
757 result.m[5] = t * axis.y * axis.y + c;
758 result.m[6] = t * axis.y * axis.z + s * axis.x;
759
760 result.m[8] = t * axis.z * axis.x + s * axis.y;
761 result.m[9] = t * axis.z * axis.y - s * axis.x;
762 result.m[10] = t * axis.z * axis.z + c;
763
764 return result;
765}
766
767// Seštevanje dveh matrix, seštejemo vseh 16 elementov.
768Matrix Matrix_Add(Matrix matrix1, Matrix matrix2)
769{
770 Matrix result = IdentityM;
771
772 result.m[0] = matrix1.m[0] + matrix2.m[0];
773 result.m[1] = matrix1.m[1] + matrix2.m[1];
774 result.m[2] = matrix1.m[2] + matrix2.m[2];
775 result.m[3] = matrix1.m[3] + matrix2.m[3];
776 result.m[4] = matrix1.m[4] + matrix2.m[4];
777 result.m[5] = matrix1.m[5] + matrix2.m[5];
778 result.m[6] = matrix1.m[6] + matrix2.m[6];
779 result.m[7] = matrix1.m[7] + matrix2.m[7];
780 result.m[8] = matrix1.m[8] + matrix2.m[8];
781 result.m[9] = matrix1.m[9] + matrix2.m[9];
782 result.m[10] = matrix1.m[10] + matrix2.m[10];
783 result.m[11] = matrix1.m[11] + matrix2.m[11];
784 result.m[12] = matrix1.m[12] + matrix2.m[12];
785 result.m[13] = matrix1.m[13] + matrix2.m[13];
786 result.m[14] = matrix1.m[14] + matrix2.m[14];
787 result.m[15] = matrix1.m[15] + matrix2.m[15];
788
789 return result;
790}
791
792// Odštevanje druge matrike od prve, odštejemo vseh 16 elementov.
793Matrix Matrix_Sub(Matrix matrix1, Matrix matrix2)
794{
795 Matrix result = IdentityM;
796
797 result.m[0] = matrix1.m[0] - matrix2.m[0];
798 result.m[1] = matrix1.m[1] - matrix2.m[1];
799 result.m[2] = matrix1.m[2] - matrix2.m[2];
800 result.m[3] = matrix1.m[3] - matrix2.m[3];
801 result.m[4] = matrix1.m[4] - matrix2.m[4];
802 result.m[5] = matrix1.m[5] - matrix2.m[5];
803 result.m[6] = matrix1.m[6] - matrix2.m[6];
804 result.m[7] = matrix1.m[7] - matrix2.m[7];
805 result.m[8] = matrix1.m[8] - matrix2.m[8];
806 result.m[9] = matrix1.m[9] - matrix2.m[9];
807 result.m[10] = matrix1.m[10] - matrix2.m[10];
808 result.m[11] = matrix1.m[11] - matrix2.m[11];
809 result.m[12] = matrix1.m[12] - matrix2.m[12];
810 result.m[13] = matrix1.m[13] - matrix2.m[13];
811 result.m[14] = matrix1.m[14] - matrix2.m[14];
812 result.m[15] = matrix1.m[15] - matrix2.m[15];
813
814 return result;
815}
816
817// Množenje dveh matrik, število stolpcev in število vrstic mora biti enako!
818// m[0][0] = m1[0][0] * m2[0][0] + m1[0][1] * m2[1][0] + m1[0][2] * m2[2][0] + m1[0][3] * m1[3][0]
819// Kot lahko vidimo se v prvi matriki pomikamo po vrsticah(iz leve proti desni) v drugi matriki
820// pa po stolpcu navzdol!
821Matrix Matrix_Multiply(Matrix matrix2, Matrix matrix1)
822{
823 Matrix result = IdentityM;
824
825 // Column 1
826 result.m[0] =
827 matrix1.m[0] * matrix2.m[0] +
828 matrix1.m[1] * matrix2.m[4] +
829 matrix1.m[2] * matrix2.m[8] +
830 matrix1.m[3] * matrix2.m[12];
831
832 result.m[4] =
833 matrix1.m[4] * matrix2.m[0] +
834 matrix1.m[5] * matrix2.m[4] +
835 matrix1.m[6] * matrix2.m[8] +
836 matrix1.m[7] * matrix2.m[12];
837
838 result.m[8] =
839 matrix1.m[8] * matrix2.m[0] +
840 matrix1.m[9] * matrix2.m[4] +
841 matrix1.m[10] * matrix2.m[8] +
842 matrix1.m[11] * matrix2.m[12];
843
844 result.m[12] =
845 matrix1.m[12] * matrix2.m[0] +
846 matrix1.m[13] * matrix2.m[4] +
847 matrix1.m[14] * matrix2.m[8] +
848 matrix1.m[15] * matrix2.m[12];
849
850 // Column 2
851 result.m[1] =
852 matrix1.m[0] * matrix2.m[1] +
853 matrix1.m[1] * matrix2.m[5] +
854 matrix1.m[2] * matrix2.m[9] +
855 matrix1.m[3] * matrix2.m[13];
856
857 result.m[5] =
858 matrix1.m[4] * matrix2.m[1] +
859 matrix1.m[5] * matrix2.m[5] +
860 matrix1.m[6] * matrix2.m[9] +
861 matrix1.m[7] * matrix2.m[13];
862
863 result.m[9] =
864 matrix1.m[8] * matrix2.m[1] +
865 matrix1.m[9] * matrix2.m[5] +
866 matrix1.m[10] * matrix2.m[9] +
867 matrix1.m[11] * matrix2.m[13];
868
869 result.m[13] =
870 matrix1.m[12] * matrix2.m[1] +
871 matrix1.m[13] * matrix2.m[5] +
872 matrix1.m[14] * matrix2.m[9] +
873 matrix1.m[15] * matrix2.m[13];
874
875 // Column 3
876 result.m[2] =
877 matrix1.m[0] * matrix2.m[2] +
878 matrix1.m[1] * matrix2.m[6] +
879 matrix1.m[2] * matrix2.m[10] +
880 matrix1.m[3] * matrix2.m[14];
881
882 result.m[6] =
883 matrix1.m[4] * matrix2.m[2] +
884 matrix1.m[5] * matrix2.m[6] +
885 matrix1.m[6] * matrix2.m[10] +
886 matrix1.m[7] * matrix2.m[14];
887
888 result.m[10] =
889 matrix1.m[8] * matrix2.m[2] +
890 matrix1.m[9] * matrix2.m[6] +
891 matrix1.m[10] * matrix2.m[10] +
892 matrix1.m[11] * matrix2.m[14];
893
894 result.m[14] =
895 matrix1.m[12] * matrix2.m[2] +
896 matrix1.m[13] * matrix2.m[6] +
897 matrix1.m[14] * matrix2.m[10] +
898 matrix1.m[15] * matrix2.m[14];
899
900 // Column 4
901 result.m[3] =
902 matrix1.m[0] * matrix2.m[3] +
903 matrix1.m[1] * matrix2.m[7] +
904 matrix1.m[2] * matrix2.m[11] +
905 matrix1.m[3] * matrix2.m[15];
906
907 result.m[7] =
908 matrix1.m[4] * matrix2.m[3] +
909 matrix1.m[5] * matrix2.m[7] +
910 matrix1.m[6] * matrix2.m[11] +
911 matrix1.m[7] * matrix2.m[15];
912
913 result.m[11] =
914 matrix1.m[8] * matrix2.m[3] +
915 matrix1.m[9] * matrix2.m[7] +
916 matrix1.m[10] * matrix2.m[11] +
917 matrix1.m[11] * matrix2.m[15];
918
919 result.m[15] =
920 matrix1.m[12] * matrix2.m[3] +
921 matrix1.m[13] * matrix2.m[7] +
922 matrix1.m[14] * matrix2.m[11] +
923 matrix1.m[15] * matrix2.m[15];
924
925 return result;
926}
927
928Matrix Matrix_CreateScale(float scale)
929{
930 Matrix result = IdentityM;
931
932 // Set elements 0,5,10 to scale value.
933 result.m[0] = result.m[5] = result.m[10] = scale;
934
935 return result;
936}
937
938Matrix Matrix_CreateScales(Vector3 scales)
939{
940 Matrix result = IdentityM;
941
942 // Just set elements 0, 5, 10, to scales values x,y,z
943 result.m[0] = scales.x;
944 result.m[5] = scales.y;
945 result.m[10] = scales.z;
946
947 return result;
948}
949
950Matrix Matrix_CreateTranslation(Vector3 translation)
951{
952 Matrix result = IdentityM;
953
954 result.m[12] = translation.x;
955 result.m[13] = translation.y;
956 result.m[14] = translation.z;
957
958 return result;
959}
960
961// The one of most important matrix.. the view matrix.
962Matrix Matrix_CreateLookAt(Vector3 cPosition, Vector3 cTarget, Vector3 cUpVector)
963{
964 Matrix result = IdentityM;
965
966 return result;
967}
968
969
970// 3dsParser.c
971//
972// Copyright 2011 upd <upd@home>
973//
974// This program is free software; you can redistribute it and/or modify
975// it under the terms of the GNU General Public License as published by
976// the Free Software Foundation; either version 2 of the License, or
977// (at your option) any later version.
978//
979// This program is distributed in the hope that it will be useful,
980// but WITHOUT ANY WARRANTY; without even the implied warranty of
981// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
982// GNU General Public License for more details.
983//
984// You should have received a copy of the GNU General Public License
985// along with this program; if not, write to the Free Software
986// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
987// MA 02110-1301, USA.
988
989/*
990MAIN CHUNK 0x4D4D
991 3D EDITOR CHUNK 0x3D3D
992 OBJECT BLOCK 0x4000
993 TRIANGULAR MESH 0x4100
994 VERTICES LIST 0x4110
995 FACES DESCRIPTION 0x4120
996 FACES MATERIAL 0x4130
997 MAPPING COORDINATES LIST 0x4140
998 SMOOTHING GROUP LIST 0x4150
999 LOCAL COORDINATES SYSTEM 0x4160
1000 LIGHT 0x4600
1001 SPOTLIGHT 0x4610
1002 CAMERA 0x4700
1003 MATERIAL BLOCK 0xAFFF
1004 MATERIAL NAME 0xA000
1005 AMBIENT COLOR 0xA010
1006 DIFFUSE COLOR 0xA020
1007 SPECULAR COLOR 0xA030
1008 TEXTURE MAP 1 0xA200
1009 BUMP MAP 0xA230
1010 REFLECTION MAP 0xA220
1011 [SUB CHUNKS FOR EACH MAP]
1012 MAPPING FILENAME 0xA300
1013 MAPPING PARAMETERS 0xA351
1014 KEYFRAMER CHUNK 0xB000
1015 MESH INFORMATION BLOCK 0xB002
1016 SPOT LIGHT INFORMATION BLOCK 0xB007
1017 FRAMES (START AND END) 0xB008
1018 OBJECT NAME 0xB010
1019 OBJECT PIVOT POINT 0xB013
1020 POSITION TRACK 0xB020
1021 ROTATION TRACK 0xB021
1022 SCALE TRACK 0xB022
1023*/
1024#include <stdio.h>
1025#include <stdlib.h>
1026#include <string.h>
1027#include <sys/stat.h>
1028#include "3dsData.h"
1029#include "3dsModel.h"
1030
1031FILE *fp = NULL;
1032Model *m;
1033
1034void ReadChunk(struct stChunk *pChunk)
1035{
1036 unsigned short ID = 0;
1037 unsigned int bytesRead = 0;
1038 unsigned int bChunkLength = 0;
1039
1040 bytesRead = (unsigned int)fread(&ID, 1, 2, fp);
1041
1042 bytesRead += (unsigned int)fread(&bChunkLength, 1, 4, fp);
1043
1044 pChunk->ID = ID;
1045 pChunk->length = bChunkLength;
1046 pChunk->bytesRead = bytesRead;
1047
1048 //printf("Chunk ID: 0x %04x Size of Chunk: %u\n", pChunk->ID, pChunk->length);
1049}
1050
1051void SkipChunk(struct stChunk *pChunk)
1052{
1053 //printf("Skipping chunk ID: 0x %04x Size of Chunk: %u\n", pChunk->ID, pChunk->length);
1054 fseek(fp, pChunk->length - pChunk->bytesRead, SEEK_CUR);
1055}
1056
1057int GetString(char *pBuffer)
1058{
1059 int index = 0;
1060 char buffer[128] = {0};
1061
1062 fread(buffer, 1, 1, fp);
1063
1064 while( *(buffer + index++) != '\0')
1065 {
1066 fread(buffer + index, 1, 1, fp);
1067 }
1068
1069 strncpy(pBuffer, buffer, 127);
1070
1071 return (int)(strlen(buffer)+1);
1072}
1073
1074// Mesh - OBJECT...
1075void ReadMeshMaterials(struct stChunk *Chunk)
1076{
1077// printf("Reading mesh materials...\n");
1078
1079 char str[256];
1080 unsigned short iNumFaces = 0;
1081 unsigned int len = GetString(str);
1082
1083 //printf("Mesh Material %s\n", str);
1084
1085 Chunk->bytesRead += len;
1086
1087 Chunk->bytesRead += (unsigned int)fread(&iNumFaces, 1, 2, fp);
1088
1089 unsigned short *faceMaterial = malloc(sizeof(unsigned short) * iNumFaces);
1090
1091 Chunk->bytesRead += (unsigned int)fread(faceMaterial, 1, iNumFaces * sizeof(unsigned short), fp);
1092
1093 printf("\tMesh Material [%s] with iNumFaces [%d]\n", str, iNumFaces);
1094
1095 int MaterialID = 0, i = 0;
1096 Material *texture = m->materials;
1097
1098 while(texture != NULL)
1099 {
1100 if(strcmp(str, texture->szName) == 0)
1101 {
1102// printf("[%s], [%s], [%d]\n", str, texture->szName, i);
1103 MaterialID = i;
1104 m->mesh->MaterialID = MaterialID;
1105 printf("Mesh[%s] has material id [%d]:[%s]\n", m->mesh->name, m->mesh->MaterialID, str);
1106 m->mesh->texture = texture;
1107 }
1108 i++;
1109 texture = texture->next;
1110 }
1111
1112 for( i = 0 ; i < iNumFaces ; i ++)
1113 {
1114 int iIndex = faceMaterial[i];
1115
1116 printf("iIndex %d\n", iIndex);
1117
1118// m->mesh->faces[iIndex]
1119 }
1120
1121 return;
1122}
1123/*
1124 // Material Name Where Referencing
1125 char str[256];
1126 unsigned int characterlen = GetString(str);
1127 Chunk->bytesRead += characterlen;
1128
1129 unsigned short iNumFaces = 0;
1130 Chunk->bytesRead += (unsigned int)fread(&iNumFaces, 1, 2, m_fp);
1131
1132 unsigned short *FaceAssignedThisMaterial = new unsigned short[iNumFaces];
1133 Chunk->bytesRead += (unsigned int)fread(FaceAssignedThisMaterial, 1,
1134 iNumFaces*sizeof(unsigned short), m_fp);
1135
1136 // Determine Which Material It Is In Our List
1137 int MaterialID = 0;
1138 for( int cc=0; cc<m_iNumMaterials; cc++)
1139 {
1140 if( strcmp( str, m_pMaterials[cc].szName ) == 0 )
1141 MaterialID = cc;
1142 }
1143
1144 stMesh* pMesh = &(m_pMeshs[m_iNumMeshs - 1]);
1145 for(int i=0; i<iNumFaces; i++)
1146 {
1147 int iIndex = FaceAssignedThisMaterial[i];
1148 pMesh->pFaces[iIndex].MaterialID = MaterialID;
1149 }
1150*/
1151
1152void ReadMeshVertices(struct stChunk *Chunk)
1153{
1154 unsigned int iNumberVertices = 0;
1155
1156 Chunk->bytesRead += (unsigned int)fread(&iNumberVertices, 1, 2, fp);
1157
1158 printf("\tVertices: [%d]\n", iNumberVertices);
1159
1160 m->mesh->vertices = malloc(sizeof(Vertex) * iNumberVertices);
1161 m->mesh->iVertices = iNumberVertices;
1162
1163 Vertex *v = m->mesh->vertices;
1164
1165 Chunk->bytesRead += fread((void*)v, 1, iNumberVertices*sizeof(Vertex), fp);
1166
1167 int i;
1168
1169 if((strcmp(m->mesh->name, "Plane") == 0))
1170 {
1171 for ( i = 0; i < m->mesh->iVertices; i++)
1172 {
1173 printf("Vertices: \t %.2f, \t %.2f, \t %.2f\n",v[i].X, v[i].Y, v[i].Z);
1174 }
1175 }
1176
1177 SkipChunk(Chunk);
1178}
1179
1180void ReadMeshFaces(struct stChunk *Chunk)
1181{
1182 unsigned short iNumberFaces = 0;
1183 int i;
1184
1185 Chunk->bytesRead += (unsigned int)fread(&iNumberFaces, 1, 2, fp);
1186
1187 printf("\tFaces: [%d]\n", iNumberFaces);
1188
1189 m->mesh->faces = malloc(sizeof(Face) * iNumberFaces);
1190 m->mesh->iFaces = iNumberFaces;
1191
1192 Face *f = m->mesh->faces;
1193
1194 Chunk->bytesRead += fread((void*)f, 1, sizeof(Face) * iNumberFaces, fp);
1195
1196 if((strcmp(m->mesh->name, "Plane") == 0))
1197 {
1198 for(i = 0; i < iNumberFaces ; i++)
1199 {
1200 printf("Faces: \t %d, \t %d, \t %d\n", f[i].A, f[i].B, f[i].C);
1201 }
1202 }
1203
1204// SkipChunk(Chunk);
1205 ParseChunk(Chunk); // if this is active we have to set MESH_MATER, as is subchunk of this one!
1206}
1207
1208void ReadMeshTextureCoords(struct stChunk *Chunk)
1209{
1210 unsigned short iNumberCoords = 0;
1211 int i;
1212
1213 Chunk->bytesRead += (unsigned int)fread(&iNumberCoords, 1, 2, fp);
1214
1215 printf("\tTexCoords: [%d]\n", iNumberCoords);
1216
1217 m->mesh->mapCoord = malloc(sizeof(Mapcoord) * iNumberCoords);
1218 m->mesh->iMapCoord = iNumberCoords;
1219
1220 Mapcoord *map = m->mesh->mapCoord;
1221
1222 Chunk->bytesRead += fread((void*)map, 1, iNumberCoords * sizeof(Mapcoord), fp);
1223
1224 if((strcmp(m->mesh->name, "Plane") == 0))
1225 {
1226 for(i = 0; i < iNumberCoords ; i++)
1227 {
1228 printf("TexCoords: \t u = %.2f, \t v = %.2f\n", map[i].u, map[i].v);
1229 }
1230 }
1231
1232 SkipChunk(Chunk);
1233}
1234
1235ModelMesh *mesh_push(ModelMesh *next)
1236{
1237 ModelMesh *new = malloc(sizeof(ModelMesh));
1238
1239 if(new == NULL)
1240 return NULL;
1241
1242 new->iVertices = 0;
1243 new->iMapCoord = 0;
1244 new->iFaces = 0;
1245
1246 new->vertices = NULL;
1247 new->mapCoord = NULL;
1248 new->faces = NULL;
1249
1250 new->next = next;
1251 m->iMesh++;
1252
1253 return new;
1254}
1255/***************************************************************************/
1256/* */
1257/* Read in our objects name...as each object in our 3D world has a name, */
1258/* for example Box1, HillMesh...whatever you called your object or object's*/
1259/* in 3d max before you saved it. */
1260/* */
1261/***************************************************************************/
1262void GetMeshObjectName(struct stChunk *Chunk)
1263{
1264 char str[256];
1265 unsigned int len = GetString(str);
1266
1267 Chunk->bytesRead += len;
1268
1269 m->mesh = mesh_push(m->mesh);
1270 m->mesh->name = malloc(sizeof(char) * len);
1271 strncpy(m->mesh->name, str, len);
1272
1273 printf("\nMesh: [%s]\n", m->mesh->name);
1274
1275 ParseChunk(Chunk);
1276}
1277
1278void GetTextureFileName(struct stChunk *Chunk)
1279{
1280 char str[256];
1281 unsigned int len = GetString(str);
1282
1283 Chunk->bytesRead += len;
1284
1285 m->materials->szTextureFile = malloc(len+1);
1286 strncpy(m->materials->szTextureFile, str, len+1);
1287
1288 printf("\tTexture filename: [%s]\n", str);
1289}
1290
1291void GetDiffuseColour(struct stChunk *Chunk)
1292{
1293 ColorRGBA DColour = {0};
1294 char ChunkHeader[6];
1295
1296 Chunk->bytesRead += (unsigned int)fread(ChunkHeader, 1, 6, fp);
1297 Chunk->bytesRead += (unsigned int)fread(&DColour, 1, 3, fp);
1298
1299 printf("\tDiffuse Colour\t r: %x g: %x b: %x\n", DColour.r, DColour.g, DColour.b);
1300
1301 m->materials->Colour.r = DColour.r;// / 255;
1302 m->materials->Colour.g = DColour.g;// / 255;
1303 m->materials->Colour.b = DColour.b;// / 255;
1304
1305 SkipChunk(Chunk);
1306}
1307
1308void GetMaterialName(struct stChunk *Chunk)
1309{
1310 char str[256];
1311 unsigned int len = GetString(str);
1312
1313 Chunk->bytesRead += len;
1314
1315 printf("\tMaterial name is [%s]\n", str);
1316
1317 m->materials->szName = malloc(len+1);
1318 strncpy(m->materials->szName, str, len+1);
1319}
1320
1321void ParseChunk(struct stChunk *Chunk)
1322{
1323 while(Chunk->bytesRead < Chunk->length)
1324 {
1325 struct stChunk tempChunk = {0};
1326
1327 ReadChunk(&tempChunk);
1328
1329 switch(tempChunk.ID)
1330 {
1331 // HEADER OUR ENTRY POINT
1332 case EDIT3DS:
1333 ParseChunk(&tempChunk);
1334 break;
1335
1336 // MATERIALS
1337 case MATERIAL: // 0xAFFF
1338 printf("\nNew material detected.\n");
1339 Material *mat = malloc(sizeof(Material));
1340
1341 mat->szName = NULL;
1342 mat->szTextureFile = NULL;
1343 mat->next = m->materials;
1344 m->materials = mat;
1345 m->iMaterials++;
1346
1347 ParseChunk(&tempChunk);
1348 break;
1349
1350 case MAT_NAME: // 0xA000
1351 GetMaterialName(&tempChunk);
1352 break;
1353
1354 case MAT_DIFFUSE: // 0xA020 Diffuse Colour
1355 GetDiffuseColour(&tempChunk);
1356 break;
1357
1358 case MAT_TEXMAP: // 0xA200 texture wrapped to it where here
1359 ParseChunk(&tempChunk);
1360 break;
1361
1362 case MAT_TEXFLNM: // 0xA300 get filename of the material!
1363 GetTextureFileName(&tempChunk);
1364 break;
1365
1366 // OBJECT - MESH'S
1367 case NAMED_OBJECT:
1368 GetMeshObjectName(&tempChunk);
1369 break;
1370
1371 case OBJ_MESH: //0x4100
1372 ParseChunk(&tempChunk);
1373 break;
1374
1375 case MESH_VERTICES: //0x4110
1376 ReadMeshVertices(&tempChunk);
1377 break;
1378
1379 case MESH_FACES: //0x4120
1380 ReadMeshFaces(&tempChunk);
1381 break;
1382
1383 case MESH_TEX_VERT: //0x4140
1384 ReadMeshTextureCoords(&tempChunk);
1385 break;
1386
1387 case MESH_MATER: //0x4130
1388 ReadMeshMaterials(&tempChunk);
1389 break;
1390
1391 default:
1392 SkipChunk(&tempChunk);
1393 }
1394
1395 Chunk->bytesRead += tempChunk.length;
1396 }
1397}
1398
1399int Load_Model(Model **model, char *filePath)
1400{
1401 struct stChunk Chunk = {0};
1402
1403 fp = fopen(filePath, "rb");
1404
1405 if(fp == NULL)
1406 return -1;
1407
1408 m = malloc(sizeof(Model));
1409 if(m == NULL)
1410 return -1;
1411
1412 m->mesh = NULL;
1413 m->materials = NULL;
1414 m->iMesh = 0;
1415 m->iMaterials = 0;
1416
1417 strncpy(m->modelName, "OMFG", 5);
1418
1419 // Let's read first chunk...
1420 ReadChunk(&Chunk);
1421
1422 // Parse that chunk and other now...
1423 ParseChunk(&Chunk);
1424 fclose(fp);
1425
1426 *model = m;
1427
1428 return 1;
1429}
1430
1431// vector.c
1432//
1433// Copyright 2011 upd <upd@home>
1434//
1435// This program is free software; you can redistribute it and/or modify
1436// it under the terms of the GNU General Public License as published by
1437// the Free Software Foundation; either version 2 of the License, or
1438// (at your option) any later version.
1439//
1440// This program is distributed in the hope that it will be useful,
1441// but WITHOUT ANY WARRANTY; without even the implied warranty of
1442// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1443// GNU General Public License for more details.
1444//
1445// You should have received a copy of the GNU General Public License
1446// along with this program; if not, write to the Free Software
1447// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
1448// MA 02110-1301, USA.
1449//
1450//
1451
1452#include "Vector.h"
1453
1454// Mathematical function's...
1455/*
1456 * |---------------------|
1457 * | Vector3 functions: |
1458 * |---------------------|
1459 */
1460
1461/*
1462 * Transforms a 3D vector by the given matrix.
1463 * Vsekakor ena izmed, najbolj uporabnih funkcij.
1464 * Potem, ko imamo nek vektor in matriko, ju seveda hoÄŤemo zmnoĹľiti.
1465 * Npr. Najprej ustvarimo matriko, z rotirajoÄŤo funkcijo okoli Z osi za 90 stopinj.
1466 * Nato imamo nek vektor, ki ga Ĺľelimo rotirati okoli Z osi za 90 stopinj.
1467 * Da se izognemo vsem raÄŤunom, uporabimo to funkcijo, ki uporabi matriko
1468 * za rotacijo vektorja okoli neke osi. Nekak tko ;)
1469 */
1470Vector3 Vector3_Transform(Vector3 a, Matrix M)
1471{
1472 Vector3 result = Zero;
1473/*
1474 * // For DirectX
1475 result = new Vector4(
1476 (vector.X * transform.M11) + (vector.Y * transform.M21) + (vector.Z * transform.M31) + transform.M41,
1477 (vector.X * transform.M12) + (vector.Y * transform.M22) + (vector.Z * transform.M32) + transform.M42,
1478 (vector.X * transform.M13) + (vector.Y * transform.M23) + (vector.Z * transform.M33) + transform.M43,
1479 (vector.X * transform.M14) + (vector.Y * transform.M24) + (vector.Z * transform.M34) + transform.M44);
1480*/
1481// Probably wrong!
1482// result.x = a.x * M.m[0] + a.y * M.m[4] + a.z * M.m[8];
1483// result.y = a.x * M.m[1] + a.y * M.m[5] + a.z * M.m[9];
1484// result.z = a.x * M.m[2] + a.y * M.m[6] + a.z * M.m[10];
1485
1486 // Should be correct...
1487
1488// printf("Vector.z is %f, and M.m[10] is %f\n", a.z, M.m[10]);
1489
1490 result.x = a.x * M.m[0] + a.y * M.m[1] + a.z * M.m[2];
1491 result.y = a.x * M.m[4] + a.y * M.m[5] + a.z * M.m[6];
1492 result.z = a.x * M.m[8] + a.y * M.m[9] + a.z * M.m[10];
1493
1494// printf("And the final resul
1495// result.z *= -1.0f;
1496
1497 return result;
1498}
1499
1500Vector3 Vector3_Mul(Vector3 a, Vector3 b)
1501{
1502 a.x *= b.x;
1503 a.y *= b.y;
1504 a.z *= b.z;
1505
1506 return a;
1507}
1508
1509Vector3 Vector3_Mulf(Vector3 a, Vector3 b, float f)
1510{
1511 a.x *= b.x * f;
1512 a.y *= b.y * f;
1513 a.z *= b.z * f;
1514
1515 return a;
1516}
1517
1518Vector3 Vector3_Add(Vector3 a, Vector3 b)
1519{
1520 a.x += b.x;
1521 a.y += b.y;
1522 a.z += b.z;
1523
1524 return a;
1525}
1526
1527Vector3 Vector3_Addf(Vector3 a, Vector3 b, float f)
1528{
1529 a.x += b.x * f;
1530 a.y += b.y * f;
1531 a.z += b.z * f;
1532
1533 return a;
1534}
1535
1536/* Skalarni produkt je matematična operacija, ki dvema vektorjema priredi število (skalar).
1537 * Rezultat izraÄŤunamo kot produkt dolĹľin obeh vektorjev in kosinusa vmesnega kota
1538 * (vmesni kot je kot φ, ki ga vektorja oklepata, če izhajata iz skupne začetne točke).
1539 * Simbol za skalarni produkt je pika, ki pa jo lahko tudi izpuščamo.
1540 */
1541float Vector3_Dot(Vector3 a, Vector3 b)
1542{
1543 float result = 0.0f;
1544
1545 result = a.x * b.x + a.y * b.y + a.z * b.z;
1546// result += a.x * b.x;
1547// result += a.y * b.y;
1548// result += a.z * b.z;
1549
1550 return result;
1551}
1552
1553
1554/* Vektorski produkt je binarni operator v trirazseĹľnem prostoru.
1555 * Rezultat je trirazseĹľni vektor, ki je pravokoten na oba vektorja.
1556 * Operacija ni komutativna; ÄŤe zamenjamo vrstni red vektorjev,
1557 * bo rezultat vektor z enako dolĹľino, vendar bo usmerjen v nasprotno smer.
1558 * Dolžina vektorja je enaka ploščini paralelograma,
1559 * katerega nevzporedni stranici sta vektorja.
1560 */
1561Vector3 Vector3_Cross(Vector3 a, Vector3 b)
1562{
1563 Vector3 result = Zero;
1564
1565 result.x = a.y * b.z - a.z * b.y;
1566 result.y = a.z * b.x - a.x * b.z;
1567 result.z = a.x * b.y - a.y * b.x;
1568
1569 return result;
1570}
1571
1572// LAla, dolĹľina ... vektorja... used for normalization of an vector.
1573// Komponente vektorja delimo, z njegovo dolĹľino vseh komponent, da dobimo
1574// vektor, ki ima dolĹľino 1. Ta funkcija je prav tako lahko uporabljena
1575// samo za pridobitev neke dolĹľine, danega vektorja.
1576float Magnitude(Vector3 a)
1577{
1578 float result = 0.0f;
1579
1580 result = sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
1581
1582 return result;
1583}
1584
1585/* This function set the lenght of vector to 1, but it keep the direction of vector.
1586 * V evklidskem prostoru je skalarni produkt dveh enotskih vektorjev v1 in v2 kar kosinus kota med njima.
1587 * To sledi iz enaÄŤbe za skalarni produkt, saj sta njuni dolĹľini enaki 1.
1588 * Creates a unit vector from the specified vector.
1589 * The result is a vector one unit in length pointing in the same direction as the original vector.
1590 */
1591Vector3 Vector3_Normalize(Vector3 a)
1592{
1593 Vector3 result = a;
1594
1595 float M = Magnitude(result);
1596
1597 if(M != 0.0f)
1598 {
1599 result.x /= M;
1600 result.y /= M;
1601 result.z /= M;
1602 }
1603
1604 return result;
1605}
1606
1607// 3dsData.h
1608//
1609// Copyright 2011 upd <upd@home>
1610//
1611// This program is free software; you can redistribute it and/or modify
1612// it under the terms of the GNU General Public License as published by
1613// the Free Software Foundation; either version 2 of the License, or
1614// (at your option) any later version.
1615//
1616// This program is distributed in the hope that it will be useful,
1617// but WITHOUT ANY WARRANTY; without even the implied warranty of
1618// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1619// GNU General Public License for more details.
1620//
1621// You should have received a copy of the GNU General Public License
1622// along with this program; if not, write to the Free Software
1623// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
1624// MA 02110-1301, USA.
1625
1626/*
1627 * Start of 3ds Chunk numbers
1628 */
1629//>----- Entry point (Primary Chunk at the start of the file ----------------
1630#define PRIMARY 0x4D4D
1631
1632//>----- Main Chunks --------------------------------------------------------
1633#define EDIT3DS 0x3D3D // Start of our actual objects
1634#define KEYF3DS 0xB000 // Start of the keyframe information
1635
1636//>----- General Chunks -----------------------------------------------------
1637#define VERSION 0x0002
1638#define MESH_VERSION 0x3D3E
1639#define KFVERSION 0x0005
1640#define COLOR_F 0x0010
1641#define COLOR_24 0x0011
1642#define LIN_COLOR_24 0x0012
1643#define LIN_COLOR_F 0x0013
1644#define INT_PERCENTAGE 0x0030
1645#define FLOAT_PERC 0x0031
1646#define MASTER_SCALE 0x0100
1647#define IMAGE_FILE 0x1100
1648#define AMBIENT_LIGHT 0X2100
1649
1650//>----- Object Chunks -----------------------------------------------------
1651#define NAMED_OBJECT 0x4000
1652#define OBJ_MESH 0x4100
1653#define MESH_VERTICES 0x4110
1654#define VERTEX_FLAGS 0x4111
1655#define MESH_FACES 0x4120
1656#define MESH_MATER 0x4130
1657#define MESH_TEX_VERT 0x4140
1658#define MESH_XFMATRIX 0x4160
1659#define MESH_COLOR_IND 0x4165
1660#define MESH_TEX_INFO 0x4170
1661#define HEIRARCHY 0x4F00
1662
1663
1664//>----- Material Chunks ---------------------------------------------------
1665#define MATERIAL 0xAFFF
1666#define MAT_NAME 0xA000
1667#define MAT_AMBIENT 0xA010
1668#define MAT_DIFFUSE 0xA020
1669#define MAT_SPECULAR 0xA030
1670#define MAT_SHININESS 0xA040
1671#define MAT_FALLOFF 0xA052
1672#define MAT_EMISSIVE 0xA080
1673#define MAT_SHADER 0xA100
1674#define MAT_TEXMAP 0xA200
1675#define MAT_TEXFLNM 0xA300
1676
1677#define OBJ_LIGHT 0x4600
1678#define OBJ_CAMERA 0x4700
1679
1680//>----- KeyFrames Chunks --------------------------------------------------
1681#define ANIM_HEADER 0xB00A
1682#define ANIM_OBJ 0xB002
1683
1684#define ANIM_NAME 0xB010
1685#define ANIM_POS 0xB020
1686#define ANIM_ROT 0xB021
1687#define ANIM_SCALE 0xB022
1688/*
1689 * End of 3ds Chunk numbers
1690 */
1691
1692/*
1693struct stMaterial
1694{
1695 char szName[256];
1696 struct{ unsigned char r,g,b ; }Colour;
1697 char szTextureFile[256];
1698};
1699
1700struct stVert
1701{
1702 float x, y, z;
1703};
1704struct stFace
1705{ // 3 Sides of a triangle make a face.
1706 unsigned int A, B, C;
1707 int MaterialID;
1708};
1709struct stTex
1710{
1711 float tu, tv;
1712};
1713
1714struct stMesh
1715{
1716 char szMeshName[256];
1717 int iNumVerts;
1718 stVert* pVerts;
1719 int iNumFaces;
1720 stFace* pFaces;
1721 stTex* pTexs;
1722
1723 stMesh()
1724 {
1725 iNumVerts = 0;
1726 pVerts = NULL;
1727 iNumFaces = 0;
1728 pFaces = NULL;
1729 pTexs = NULL;
1730 }
1731
1732};
1733
1734struct stObject
1735{
1736 int iNumMeshs;
1737 vector<stMesh> pMeshs;
1738
1739 int iNumMaterials;
1740 vector<stMaterial> pMaterials;
1741
1742 stObject()
1743 {
1744 iNumMeshs = 0;
1745 iNumMaterials = 0;
1746 };
1747};
1748*/
1749
1750// 3dsDraw.c
1751//
1752// Copyright 2011 upd <upd@home>
1753//
1754// This program is free software; you can redistribute it and/or modify
1755// it under the terms of the GNU General Public License as published by
1756// the Free Software Foundation; either version 2 of the License, or
1757// (at your option) any later version.
1758//
1759// This program is distributed in the hope that it will be useful,
1760// but WITHOUT ANY WARRANTY; without even the implied warranty of
1761// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1762// GNU General Public License for more details.
1763//
1764// You should have received a copy of the GNU General Public License
1765// along with this program; if not, write to the Free Software
1766// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
1767// MA 02110-1301, USA.
1768//
1769
1770#include <stdio.h>
1771#include <stdlib.h>
1772#include <GL/gl.h>
1773#include <GL/glu.h>
1774#include <math.h>
1775#include <time.h>
1776#include "SDL.h"
1777#include "3dsModel.h"
1778
1779void Draw3d(Model *m)
1780{
1781 int i;
1782 float scale = 20.0f;
1783 unsigned short A, B, C;
1784
1785 ModelMesh *mesh = m->mesh;
1786
1787 glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
1788
1789 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
1790
1791 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
1792
1793 while(mesh != NULL)
1794 {
1795 // printf("Mesh name: %s\n", mesh->name);
1796
1797 if(mesh->texture->szTextureFile != NULL)
1798 {
1799 printf("Enabling texture...\n");
1800 glEnable(GL_TEXTURE_2D);
1801// glBindTexture(GL_TEXTURE_2D, 0); the corresponding id for texture
1802 }
1803 else
1804 {
1805 //printf("Texture disabled\n");
1806 glDisable(GL_TEXTURE_2D);
1807 glColor3ub(mesh->texture->Colour.r, mesh->texture->Colour.g, mesh->texture->Colour.b);
1808 }
1809
1810 glBegin(GL_TRIANGLES);
1811 for(i = 0 ; i < mesh->iFaces ; i++)
1812 {
1813 A = mesh->faces[i].A;
1814 B = mesh->faces[i].B;
1815 C = mesh->faces[i].C;
1816
1817 if((strcmp(mesh->name, "SpaceFight") == 0))
1818 {
1819 printf("Faces: \t %d, \t %d, \t %d\n", A, B, C);
1820 printf("TexCoords A: \t u = %.2f, \t v = %.2f\n", mesh->mapCoord[A].v, mesh->mapCoord[A].u);
1821 printf("TexCoords B: \t u = %.2f, \t v = %.2f\n", mesh->mapCoord[B].v, mesh->mapCoord[B].u);
1822 printf("TexCoords C: \t u = %.2f, \t v = %.2f\n", mesh->mapCoord[C].v, mesh->mapCoord[C].u);
1823
1824 printf("Vertices A: \t %.2f, \t %.2f, \t %.2f\n", mesh->vertices[A].X, mesh->vertices[A].Y, mesh->vertices[A].Z);
1825 printf("Vertices B: \t %.2f, \t %.2f, \t %.2f\n", mesh->vertices[B].X, mesh->vertices[B].Y, mesh->vertices[B].Z);
1826 printf("Vertices C: \t %.2f, \t %.2f, \t %.2f\n\n", mesh->vertices[C].X, mesh->vertices[C].Y, mesh->vertices[C].Z);
1827 }
1828
1829 if(mesh->iMapCoord != 0 && mesh->texture != NULL)
1830 glTexCoord2f(mesh->mapCoord[A].u, mesh->mapCoord[A].v);
1831 glVertex3f(
1832 mesh->vertices[A].X * scale,
1833 mesh->vertices[A].Y * scale,
1834 mesh->vertices[A].Z * scale);
1835
1836 if(mesh->iMapCoord != 0 && mesh->texture != NULL)
1837 glTexCoord2f(mesh->mapCoord[B].u, mesh->mapCoord[B].v);
1838 glVertex3f(
1839 mesh->vertices[B].X * scale,
1840 mesh->vertices[B].Y * scale,
1841 mesh->vertices[B].Z * scale);
1842
1843 if(mesh->iMapCoord != 0 && mesh->texture != NULL)
1844 glTexCoord2f(mesh->mapCoord[C].u, mesh->mapCoord[C].v);
1845 glVertex3f(
1846 mesh->vertices[C].X * scale,
1847 mesh->vertices[C].Y * scale,
1848 mesh->vertices[C].Z * scale);
1849 }
1850 glEnd();
1851
1852 mesh = mesh->next;
1853 }
1854
1855 //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
1856}
1857
1858#include <stdio.h>
1859#include <stdlib.h>
1860#include <GL/gl.h>
1861#include <GL/glu.h>
1862#include <math.h>
1863#include <time.h>
1864#include <ctype.h>
1865#include "SDL.h"
1866#include "SDL_image.h"
1867#include "3dsModel.h"
1868
1869GLuint LoadTexture(char *fileName);
1870
1871char *lowercase(char string[])
1872{
1873 int i = 0;
1874
1875 while ( string[i] )
1876 {
1877 string[i] = tolower(string[i]);
1878 i++;
1879 }
1880
1881 return string;
1882}
1883
1884int Load_Model_Textures(Model *model)
1885{
1886 Material *texture = model->materials;
1887 printf("Loading textures...\n\n");
1888
1889 while(texture != NULL)
1890 {
1891 printf("Material name is %s\n", texture->szName);
1892
1893 if(texture->szTextureFile != NULL)
1894 {
1895 printf("Texture filepath %s\n", lowercase(texture->szTextureFile));
1896 unsigned int ID = LoadTexture(lowercase(texture->szTextureFile));
1897 printf("Image ID is %d\n", ID);
1898 }
1899 else
1900 {
1901 printf("The texture don't have an image.\n");
1902 }
1903
1904 texture = texture->next;
1905 printf("\n");
1906 }
1907
1908 return 1;
1909}
1910unsigned int Texture = -1;
1911
1912GLuint LoadTexture(char *fileName)
1913{
1914 int mode;
1915
1916 SDL_Surface *Image = IMG_Load(fileName);
1917 if (!Image)
1918 {
1919 printf ( "IMG_Load: %s\n", IMG_GetError () );
1920 return -1;
1921 }
1922
1923 SDL_DisplayFormatAlpha(Image);
1924
1925// unsigned int Texture = 0;
1926
1927 // Gives 'Texture' and ID which will be used to refer to a texture. w00t w00t duck in the but.
1928// glGenTextures(1, &Texture);
1929 Texture++;
1930
1931 glBindTexture(GL_TEXTURE_2D, Texture);
1932
1933 // The magnification function ("linear" produces better results)
1934 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1935 //The minifying function
1936 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
1937
1938 //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1939 // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1940
1941 // If the u,v coordinates overflow the range 0,1 the image is repeated!
1942 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1943 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1944
1945// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1946// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1947
1948// We don't combine the color with the original surface color, use only the texture map.
1949 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1950
1951 if (Image->format->BytesPerPixel == 3)
1952 { // RGB 24bit
1953 mode = GL_RGB;
1954 }
1955 else if (Image->format->BytesPerPixel == 4)
1956 { // RGBA 32bit
1957 mode = GL_RGBA;
1958 }
1959 else
1960 {
1961 SDL_FreeSurface(Image);
1962 return -1;
1963 }
1964
1965 // Finally we define the 2d texture
1966 // As we specified the pixel data, this will actually upload the texture data to GPU's memory.
1967 glTexImage2D(GL_TEXTURE_2D, 0, mode, Image->w, Image->h, 0, mode, GL_UNSIGNED_BYTE, Image->pixels);
1968
1969 // And create 2d mipmaps for the minifying function
1970 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, Image->w, Image->h, mode, GL_UNSIGNED_BYTE, Image->pixels);
1971
1972 SDL_FreeSurface(Image);
1973
1974 return Texture;
1975}
1976
1977// matrix.h
1978//
1979// Copyright 2011 upd <upd@home>
1980//
1981// This program is free software; you can redistribute it and/or modify
1982// it under the terms of the GNU General Public License as published by
1983// the Free Software Foundation; either version 2 of the License, or
1984// (at your option) any later version.
1985//
1986// This program is distributed in the hope that it will be useful,
1987// but WITHOUT ANY WARRANTY; without even the implied warranty of
1988// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1989// GNU General Public License for more details.
1990//
1991// You should have received a copy of the GNU General Public License
1992// along with this program; if not, write to the Free Software
1993// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
1994// MA 02110-1301, USA.
1995
1996typedef struct _Matrix
1997{
1998 float m[16];
1999}Matrix;
2000
2001// In linear algebra, the identity matrix or unit matrix of size n is the nĂ—n square matrix with ones on
2002// the main diagonal and zeros elsewhere. It is denoted by In, or simply by I if the size is immaterial or
2003// can be trivially determined by the context. (In some fields, such as quantum mechanics, the identity
2004// matrix is denoted by a boldface one, 1; otherwise it is identical to I.)
2005// The identity matrix also has the property that, when it is the product of two square matrices, the
2006// matrices can be said to be the inverse of one another.
2007Matrix IdentityM = { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 };
2008
2009// The zero matrix represents the linear transformation sending all vectors to the zero vector.
2010Matrix ZeroM = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
2011
2012
2013Matrix Matrix_CreateRotationX(float angle);
2014Matrix Matrix_CreateRotationY(float angle);
2015Matrix Matrix_CreateRotationZ(float angle);
2016Matrix Matrix_CreateFromAxisAngle(Vector3 axisRot, float angle);
2017Matrix Matrix_Add(Matrix matrix1, Matrix matrix2);
2018Matrix Matrix_Sub(Matrix matrix1, Matrix matrix2);
2019Matrix Matrix_Multiply(Matrix matrix2, Matrix matrix1);
2020Matrix Matrix_CreateScale(float scale);
2021Matrix Matrix_CreateScales(Vector3 scales);
2022Matrix Matrix_CreateTranslation(Vector3 translation);
2023Matrix Matrix_CreateLookAt(Vector3 cPosition, Vector3 cTarget, Vector3 cUpVector);
2024
2025// 3dsModel.h
2026//
2027// Copyright 2011 upd <upd@home>
2028//
2029// This program is free software; you can redistribute it and/or modify
2030// it under the terms of the GNU General Public License as published by
2031// the Free Software Foundation; either version 2 of the License, or
2032// (at your option) any later version.
2033//
2034// This program is distributed in the hope that it will be useful,
2035// but WITHOUT ANY WARRANTY; without even the implied warranty of
2036// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2037// GNU General Public License for more details.
2038//
2039// You should have received a copy of the GNU General Public License
2040// along with this program; if not, write to the Free Software
2041// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
2042// MA 02110-1301, USA.
2043//
2044
2045#define MAX_VERTICES 65536
2046#define MAX_POLYGONS 65536
2047
2048typedef struct
2049{
2050 unsigned char r;
2051 unsigned char g;
2052 unsigned char b;
2053 unsigned char a;
2054}ColorRGBA;
2055
2056typedef struct stack_material
2057{
2058 char *szName;
2059 ColorRGBA Colour;
2060 char *szTextureFile;
2061 struct stack_material *next;
2062}Material;
2063
2064typedef struct
2065{
2066 float X;
2067 float Y;
2068 float Z;
2069}Vertex;
2070
2071typedef struct
2072{
2073 unsigned short A;
2074 unsigned short B;
2075 unsigned short C;
2076 unsigned short visibityflag;
2077// int MaterialID;
2078}Face;
2079
2080typedef struct
2081{
2082 float u;
2083 float v;
2084}Mapcoord;
2085
2086typedef struct stack_mesh
2087{
2088 char *name;
2089
2090 unsigned int iVertices;
2091 unsigned int iMapCoord;
2092 unsigned int iFaces; // polygons...
2093 Material *texture;
2094 int MaterialID;
2095
2096 Vertex *vertices;
2097 Mapcoord *mapCoord;
2098 Face *faces;
2099
2100 struct stack_mesh *next;
2101}ModelMesh;
2102
2103typedef struct
2104{
2105 char modelName[256];
2106 unsigned int iMesh;
2107 unsigned int iMaterials;
2108
2109 Material *materials;
2110 ModelMesh *mesh;
2111}Model;
2112
2113struct stChunk
2114{
2115 unsigned short ID;
2116 unsigned int length;
2117 unsigned int bytesRead;
2118};
2119
2120void ParseChunk(struct stChunk *Chunk);
2121
2122
2123// vector.h
2124//
2125// Copyright 2011 upd <upd@home>
2126//
2127// This program is free software; you can redistribute it and/or modify
2128// it under the terms of the GNU General Public License as published by
2129// the Free Software Foundation; either version 2 of the License, or
2130// (at your option) any later version.
2131//
2132// This program is distributed in the hope that it will be useful,
2133// but WITHOUT ANY WARRANTY; without even the implied warranty of
2134// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2135// GNU General Public License for more details.
2136//
2137// You should have received a copy of the GNU General Public License
2138// along with this program; if not, write to the Free Software
2139// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
2140// MA 02110-1301, USA.
2141//
2142//
2143
2144typedef struct _Vector
2145{
2146 float x;
2147 float y;
2148 float z;
2149}Vector3;
2150
2151
2152Vector3 Up = { 0.0, 1.0, 0.0 };
2153Vector3 Down = { 0.0, -1.0, 0.0 };
2154Vector3 Left = { -1.0, 0.0, 0.0 };
2155Vector3 Right = { 1.0, 0.0, 0.0 };
2156Vector3 Forward = { 0.0, 0.0, -1.0 };
2157Vector3 Backward = { 0.0, 0.0, 1.0 };
2158Vector3 One = { 1.0, 1.0, 1.0 };
2159Vector3 Zero = { 0.0, 0.0, 0.0 };
2160
2161Vector3 Vector3_Transform(Vector3 a, Matrix M);
2162Vector3 Vector3_Mul(Vector3 a, Vector3 b);
2163Vector3 Vector3_Mulf(Vector3 a, Vector3 b, float f);
2164Vector3 Vector3_Add(Vector3 a, Vector3 b);
2165Vector3 Vector3_Addf(Vector3 a, Vector3 b, float f);
2166float Vector3_Dot(Vector3 a, Vector3 b);
2167Vector3 Vector3_Cross(Vector3 a, Vector3 b);
2168float Magnitude(Vector3 a);
2169Vector3 Vector3_Normalize(Vector3 a);
2170
2171
2172CC = gcc
2173OBJ = main.o 3dsParser.o 3dsDraw.o TextureLoad.o Matrix.o Vector.o
2174FLAGS = -lGL -lGLU -lSDL_image `sdl-config --cflags --libs`
2175
2176TARGET = game
2177
2178$(TARGET): $(OBJ)
2179 $(CC) -o $(TARGET) $(OBJ) $(FLAGS)
2180
2181main.o: main.c
2182 $(CC) -c main.c $(FLAGS)
2183
21843dsParser.o: 3dsParser.c
2185 $(CC) -c 3dsParser.c $(FLAGS)
2186
21873dsDraw.o: 3dsDraw.c
2188 $(CC) -c 3dsDraw.c $(FLAGS)
2189
2190TextureLoad.o: TextureLoad.c
2191 $(CC) -c TextureLoad.c $(FLAGS)
2192
2193Matrix.o: Matrix.c
2194 $(CC) -c Matrix.c $(FLAGS)
2195
2196Vector.o: Vector.c
2197 $(CC) -c Vector.c $(FLAGS)
2198
2199clean:
2200 @echo Cleaning up...
2201 @rm -f $(TARGET) $(OBJ)
2202 @echo Done.