· 8 years ago · Dec 07, 2017, 06:52 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
654// 3dsParser.c
655//
656// Copyright 2011 upd <upd@home>
657//
658// This program is free software; you can redistribute it and/or modify
659// it under the terms of the GNU General Public License as published by
660// the Free Software Foundation; either version 2 of the License, or
661// (at your option) any later version.
662//
663// This program is distributed in the hope that it will be useful,
664// but WITHOUT ANY WARRANTY; without even the implied warranty of
665// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
666// GNU General Public License for more details.
667//
668// You should have received a copy of the GNU General Public License
669// along with this program; if not, write to the Free Software
670// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
671// MA 02110-1301, USA.
672
673/*
674MAIN CHUNK 0x4D4D
675 3D EDITOR CHUNK 0x3D3D
676 OBJECT BLOCK 0x4000
677 TRIANGULAR MESH 0x4100
678 VERTICES LIST 0x4110
679 FACES DESCRIPTION 0x4120
680 FACES MATERIAL 0x4130
681 MAPPING COORDINATES LIST 0x4140
682 SMOOTHING GROUP LIST 0x4150
683 LOCAL COORDINATES SYSTEM 0x4160
684 LIGHT 0x4600
685 SPOTLIGHT 0x4610
686 CAMERA 0x4700
687 MATERIAL BLOCK 0xAFFF
688 MATERIAL NAME 0xA000
689 AMBIENT COLOR 0xA010
690 DIFFUSE COLOR 0xA020
691 SPECULAR COLOR 0xA030
692 TEXTURE MAP 1 0xA200
693 BUMP MAP 0xA230
694 REFLECTION MAP 0xA220
695 [SUB CHUNKS FOR EACH MAP]
696 MAPPING FILENAME 0xA300
697 MAPPING PARAMETERS 0xA351
698 KEYFRAMER CHUNK 0xB000
699 MESH INFORMATION BLOCK 0xB002
700 SPOT LIGHT INFORMATION BLOCK 0xB007
701 FRAMES (START AND END) 0xB008
702 OBJECT NAME 0xB010
703 OBJECT PIVOT POINT 0xB013
704 POSITION TRACK 0xB020
705 ROTATION TRACK 0xB021
706 SCALE TRACK 0xB022
707*/
708#include <stdio.h>
709#include <stdlib.h>
710#include <string.h>
711#include <sys/stat.h>
712#include "3dsData.h"
713#include "3dsModel.h"
714
715FILE *fp = NULL;
716Model *m;
717
718void ReadChunk(struct stChunk *pChunk)
719{
720 unsigned short ID = 0;
721 unsigned int bytesRead = 0;
722 unsigned int bChunkLength = 0;
723
724 bytesRead = (unsigned int)fread(&ID, 1, 2, fp);
725
726 bytesRead += (unsigned int)fread(&bChunkLength, 1, 4, fp);
727
728 pChunk->ID = ID;
729 pChunk->length = bChunkLength;
730 pChunk->bytesRead = bytesRead;
731
732 //printf("Chunk ID: 0x %04x Size of Chunk: %u\n", pChunk->ID, pChunk->length);
733}
734
735void SkipChunk(struct stChunk *pChunk)
736{
737 //printf("Skipping chunk ID: 0x %04x Size of Chunk: %u\n", pChunk->ID, pChunk->length);
738 fseek(fp, pChunk->length - pChunk->bytesRead, SEEK_CUR);
739}
740
741int GetString(char *pBuffer)
742{
743 int index = 0;
744 char buffer[128] = {0};
745
746 fread(buffer, 1, 1, fp);
747
748 while( *(buffer + index++) != '\0')
749 {
750 fread(buffer + index, 1, 1, fp);
751 }
752
753 strncpy(pBuffer, buffer, 127);
754
755 return (int)(strlen(buffer)+1);
756}
757
758// Mesh - OBJECT...
759void ReadMeshMaterials(struct stChunk *Chunk)
760{
761// printf("Reading mesh materials...\n");
762
763 char str[256];
764 unsigned short iNumFaces = 0;
765 unsigned int len = GetString(str);
766
767 //printf("Mesh Material %s\n", str);
768
769 Chunk->bytesRead += len;
770
771 Chunk->bytesRead += (unsigned int)fread(&iNumFaces, 1, 2, fp);
772
773 unsigned short *faceMaterial = malloc(sizeof(unsigned short) * iNumFaces);
774
775 Chunk->bytesRead += (unsigned int)fread(faceMaterial, 1, iNumFaces * sizeof(unsigned short), fp);
776
777 printf("\tMesh Material [%s] with iNumFaces [%d]\n", str, iNumFaces);
778
779 int MaterialID = 0, i = 0;
780 Material *texture = m->materials;
781
782 while(texture != NULL)
783 {
784 if(strcmp(str, texture->szName) == 0)
785 {
786// printf("[%s], [%s], [%d]\n", str, texture->szName, i);
787 MaterialID = i;
788 m->mesh->MaterialID = MaterialID;
789 printf("Mesh[%s] has material id [%d]:[%s]\n", m->mesh->name, m->mesh->MaterialID, str);
790 m->mesh->texture = texture;
791 }
792 i++;
793 texture = texture->next;
794 }
795
796 for( i = 0 ; i < iNumFaces ; i ++)
797 {
798 int iIndex = faceMaterial[i];
799
800 printf("iIndex %d\n", iIndex);
801
802// m->mesh->faces[iIndex]
803 }
804
805 return;
806}
807/*
808 // Material Name Where Referencing
809 char str[256];
810 unsigned int characterlen = GetString(str);
811 Chunk->bytesRead += characterlen;
812
813 unsigned short iNumFaces = 0;
814 Chunk->bytesRead += (unsigned int)fread(&iNumFaces, 1, 2, m_fp);
815
816 unsigned short *FaceAssignedThisMaterial = new unsigned short[iNumFaces];
817 Chunk->bytesRead += (unsigned int)fread(FaceAssignedThisMaterial, 1,
818 iNumFaces*sizeof(unsigned short), m_fp);
819
820 // Determine Which Material It Is In Our List
821 int MaterialID = 0;
822 for( int cc=0; cc<m_iNumMaterials; cc++)
823 {
824 if( strcmp( str, m_pMaterials[cc].szName ) == 0 )
825 MaterialID = cc;
826 }
827
828 stMesh* pMesh = &(m_pMeshs[m_iNumMeshs - 1]);
829 for(int i=0; i<iNumFaces; i++)
830 {
831 int iIndex = FaceAssignedThisMaterial[i];
832 pMesh->pFaces[iIndex].MaterialID = MaterialID;
833 }
834*/
835
836void ReadMeshVertices(struct stChunk *Chunk)
837{
838 unsigned int iNumberVertices = 0;
839
840 Chunk->bytesRead += (unsigned int)fread(&iNumberVertices, 1, 2, fp);
841
842 printf("\tVertices: [%d]\n", iNumberVertices);
843
844 m->mesh->vertices = malloc(sizeof(Vertex) * iNumberVertices);
845 m->mesh->iVertices = iNumberVertices;
846
847 Vertex *v = m->mesh->vertices;
848
849 Chunk->bytesRead += fread((void*)v, 1, iNumberVertices*sizeof(Vertex), fp);
850
851 int i;
852
853 if((strcmp(m->mesh->name, "Plane") == 0))
854 {
855 for ( i = 0; i < m->mesh->iVertices; i++)
856 {
857 printf("Vertices: \t %.2f, \t %.2f, \t %.2f\n",v[i].X, v[i].Y, v[i].Z);
858 }
859 }
860
861 SkipChunk(Chunk);
862}
863
864void ReadMeshFaces(struct stChunk *Chunk)
865{
866 unsigned short iNumberFaces = 0;
867 int i;
868
869 Chunk->bytesRead += (unsigned int)fread(&iNumberFaces, 1, 2, fp);
870
871 printf("\tFaces: [%d]\n", iNumberFaces);
872
873 m->mesh->faces = malloc(sizeof(Face) * iNumberFaces);
874 m->mesh->iFaces = iNumberFaces;
875
876 Face *f = m->mesh->faces;
877
878 Chunk->bytesRead += fread((void*)f, 1, sizeof(Face) * iNumberFaces, fp);
879
880 if((strcmp(m->mesh->name, "Plane") == 0))
881 {
882 for(i = 0; i < iNumberFaces ; i++)
883 {
884 printf("Faces: \t %d, \t %d, \t %d\n", f[i].A, f[i].B, f[i].C);
885 }
886 }
887
888// SkipChunk(Chunk);
889 ParseChunk(Chunk); // if this is active we have to set MESH_MATER, as is subchunk of this one!
890}
891
892void ReadMeshTextureCoords(struct stChunk *Chunk)
893{
894 unsigned short iNumberCoords = 0;
895 int i;
896
897 Chunk->bytesRead += (unsigned int)fread(&iNumberCoords, 1, 2, fp);
898
899 printf("\tTexCoords: [%d]\n", iNumberCoords);
900
901 m->mesh->mapCoord = malloc(sizeof(Mapcoord) * iNumberCoords);
902 m->mesh->iMapCoord = iNumberCoords;
903
904 Mapcoord *map = m->mesh->mapCoord;
905
906 Chunk->bytesRead += fread((void*)map, 1, iNumberCoords * sizeof(Mapcoord), fp);
907
908 if((strcmp(m->mesh->name, "Plane") == 0))
909 {
910 for(i = 0; i < iNumberCoords ; i++)
911 {
912 printf("TexCoords: \t u = %.2f, \t v = %.2f\n", map[i].u, map[i].v);
913 }
914 }
915
916 SkipChunk(Chunk);
917}
918
919ModelMesh *mesh_push(ModelMesh *next)
920{
921 ModelMesh *new = malloc(sizeof(ModelMesh));
922
923 if(new == NULL)
924 return NULL;
925
926 new->iVertices = 0;
927 new->iMapCoord = 0;
928 new->iFaces = 0;
929
930 new->vertices = NULL;
931 new->mapCoord = NULL;
932 new->faces = NULL;
933
934 new->next = next;
935 m->iMesh++;
936
937 return new;
938}
939/***************************************************************************/
940/* */
941/* Read in our objects name...as each object in our 3D world has a name, */
942/* for example Box1, HillMesh...whatever you called your object or object's*/
943/* in 3d max before you saved it. */
944/* */
945/***************************************************************************/
946void GetMeshObjectName(struct stChunk *Chunk)
947{
948 char str[256];
949 unsigned int len = GetString(str);
950
951 Chunk->bytesRead += len;
952
953 m->mesh = mesh_push(m->mesh);
954 m->mesh->name = malloc(sizeof(char) * len);
955 strncpy(m->mesh->name, str, len);
956
957 printf("\nMesh: [%s]\n", m->mesh->name);
958
959 ParseChunk(Chunk);
960}
961
962void GetTextureFileName(struct stChunk *Chunk)
963{
964 char str[256];
965 unsigned int len = GetString(str);
966
967 Chunk->bytesRead += len;
968
969 m->materials->szTextureFile = malloc(len+1);
970 strncpy(m->materials->szTextureFile, str, len+1);
971
972 printf("\tTexture filename: [%s]\n", str);
973}
974
975void GetDiffuseColour(struct stChunk *Chunk)
976{
977 ColorRGBA DColour = {0};
978 char ChunkHeader[6];
979
980 Chunk->bytesRead += (unsigned int)fread(ChunkHeader, 1, 6, fp);
981 Chunk->bytesRead += (unsigned int)fread(&DColour, 1, 3, fp);
982
983 printf("\tDiffuse Colour\t r: %x g: %x b: %x\n", DColour.r, DColour.g, DColour.b);
984
985 m->materials->Colour.r = DColour.r;// / 255;
986 m->materials->Colour.g = DColour.g;// / 255;
987 m->materials->Colour.b = DColour.b;// / 255;
988
989 SkipChunk(Chunk);
990}
991
992void GetMaterialName(struct stChunk *Chunk)
993{
994 char str[256];
995 unsigned int len = GetString(str);
996
997 Chunk->bytesRead += len;
998
999 printf("\tMaterial name is [%s]\n", str);
1000
1001 m->materials->szName = malloc(len+1);
1002 strncpy(m->materials->szName, str, len+1);
1003}
1004
1005void ParseChunk(struct stChunk *Chunk)
1006{
1007 while(Chunk->bytesRead < Chunk->length)
1008 {
1009 struct stChunk tempChunk = {0};
1010
1011 ReadChunk(&tempChunk);
1012
1013 switch(tempChunk.ID)
1014 {
1015 // HEADER OUR ENTRY POINT
1016 case EDIT3DS:
1017 ParseChunk(&tempChunk);
1018 break;
1019
1020 // MATERIALS
1021 case MATERIAL: // 0xAFFF
1022 printf("\nNew material detected.\n");
1023 Material *mat = malloc(sizeof(Material));
1024
1025 mat->szName = NULL;
1026 mat->szTextureFile = NULL;
1027 mat->next = m->materials;
1028 m->materials = mat;
1029 m->iMaterials++;
1030
1031 ParseChunk(&tempChunk);
1032 break;
1033
1034 case MAT_NAME: // 0xA000
1035 GetMaterialName(&tempChunk);
1036 break;
1037
1038 case MAT_DIFFUSE: // 0xA020 Diffuse Colour
1039 GetDiffuseColour(&tempChunk);
1040 break;
1041
1042 case MAT_TEXMAP: // 0xA200 texture wrapped to it where here
1043 ParseChunk(&tempChunk);
1044 break;
1045
1046 case MAT_TEXFLNM: // 0xA300 get filename of the material!
1047 GetTextureFileName(&tempChunk);
1048 break;
1049
1050 // OBJECT - MESH'S
1051 case NAMED_OBJECT:
1052 GetMeshObjectName(&tempChunk);
1053 break;
1054
1055 case OBJ_MESH: //0x4100
1056 ParseChunk(&tempChunk);
1057 break;
1058
1059 case MESH_VERTICES: //0x4110
1060 ReadMeshVertices(&tempChunk);
1061 break;
1062
1063 case MESH_FACES: //0x4120
1064 ReadMeshFaces(&tempChunk);
1065 break;
1066
1067 case MESH_TEX_VERT: //0x4140
1068 ReadMeshTextureCoords(&tempChunk);
1069 break;
1070
1071 case MESH_MATER: //0x4130
1072 ReadMeshMaterials(&tempChunk);
1073 break;
1074
1075 default:
1076 SkipChunk(&tempChunk);
1077 }
1078
1079 Chunk->bytesRead += tempChunk.length;
1080 }
1081}
1082
1083int Load_Model(Model **model, char *filePath)
1084{
1085 struct stChunk Chunk = {0};
1086
1087 fp = fopen(filePath, "rb");
1088
1089 if(fp == NULL)
1090 return -1;
1091
1092 m = malloc(sizeof(Model));
1093 if(m == NULL)
1094 return -1;
1095
1096 m->mesh = NULL;
1097 m->materials = NULL;
1098 m->iMesh = 0;
1099 m->iMaterials = 0;
1100
1101 strncpy(m->modelName, "OMFG", 5);
1102
1103 // Let's read first chunk...
1104 ReadChunk(&Chunk);
1105
1106 // Parse that chunk and other now...
1107 ParseChunk(&Chunk);
1108 fclose(fp);
1109
1110 *model = m;
1111
1112 return 1;
1113}
1114
1115// matrix.c
1116//
1117// Copyright 2011 upd <upd@home>
1118//
1119// This program is free software; you can redistribute it and/or modify
1120// it under the terms of the GNU General Public License as published by
1121// the Free Software Foundation; either version 2 of the License, or
1122// (at your option) any later version.
1123//
1124// This program is distributed in the hope that it will be useful,
1125// but WITHOUT ANY WARRANTY; without even the implied warranty of
1126// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1127// GNU General Public License for more details.
1128//
1129// You should have received a copy of the GNU General Public License
1130// along with this program; if not, write to the Free Software
1131// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
1132// MA 02110-1301, USA.
1133//
1134//
1135
1136#include "Matrix.h"
1137
1138Matrix Matrix_CreateRotationX(float angle);
1139Matrix Matrix_CreateRotationY(float angle);
1140Matrix Matrix_CreateRotationZ(float angle);
1141/*
1142 * |--------------------|
1143 * | Matrix functions: | Opengl Use Right-Handed Cartesian coordinate system.
1144 * |--------------------|
1145 *
1146 * Te tri funkcije ustvarijo matriko, ki je rotirana okoli konstantne osi.
1147 * Matrika je rotirana za podan kot, ki pa je doloÄŤen v stopinjah.
1148 */
1149
1150// Now the funny stuff again, three function's that creates rotation's matrix..
1151// If angle is positive > 0, then rotation is counter-clockwise, v nasprotni smeri urinega kazalca...
1152// If angle is negative < 0, then rotation is clockwise, v smeri urinega kazalca...
1153// Rotation around any axis for 0 degrees is equal to identity matrix!!!
1154Matrix Matrix_CreateRotationX(float angle)
1155{
1156 if(angle == 0.0f)
1157 return IdentityM;
1158
1159 Matrix result = IdentityM;
1160
1161 result.m[5] = DegCos(angle);
1162 result.m[6] = -DegSin(angle);
1163 result.m[9] = DegSin(angle);
1164 result.m[10] = DegCos(angle);
1165
1166 return result;
1167}
1168
1169Matrix Matrix_CreateRotationY(float angle)
1170{
1171 if(angle == 0.0f)
1172 return IdentityM;
1173
1174 Matrix result = IdentityM;
1175
1176 result.m[0] = DegCos(angle);
1177 result.m[2] = DegSin(angle);
1178 result.m[8] = -DegSin(angle);
1179 result.m[10] = DegCos(angle);
1180
1181 //printf("Angle is %f, DegCos(%f) is %f, and result.m[10] is %f\n", angle, angle, DegCos(angle), result.m[10]);
1182
1183 return result;
1184}
1185
1186Matrix Matrix_CreateRotationZ(float angle)
1187{
1188 if(angle == 0.0f)
1189 return IdentityM;
1190
1191 Matrix result = IdentityM;
1192
1193 result.m[0] = DegCos(angle);
1194 result.m[1] = -DegSin(angle);
1195 result.m[4] = DegSin(angle);
1196 result.m[5] = DegCos(angle);
1197
1198 return result;
1199}
1200
1201// Naredi matriko, ki je rotirana okoli podanega vektorja, za dani kot.
1202// Now the funny part, create rotation matrix around an arbitrary vector.
1203// Creates a new Matrix that rotates around an arbitrary vector.
1204Matrix Matrix_CreateFromAxisAngle(Vector3 axisRot, float angle)
1205{
1206 Matrix result = IdentityM;
1207 Vector3 axis = Vector3_Normalize(axisRot); // zelo pomembno, saj drugaÄŤe dobimo napaÄŤne rezultate!
1208
1209 // if not axis yet normalized, then normalize it.
1210 float s = DegSin(angle);
1211 float c = DegCos(angle);
1212 float t = 1.0f - c;
1213
1214 result.m[0] = t * axis.x * axis.x + c;
1215 result.m[1] = t * axis.x * axis.y + s * axis.z;
1216 result.m[2] = t * axis.x * axis.z - c * axis.y;
1217
1218 result.m[4] = t * axis.y * axis.x - s * axis.z;
1219 result.m[5] = t * axis.y * axis.y + c;
1220 result.m[6] = t * axis.y * axis.z + s * axis.x;
1221
1222 result.m[8] = t * axis.z * axis.x + s * axis.y;
1223 result.m[9] = t * axis.z * axis.y - s * axis.x;
1224 result.m[10] = t * axis.z * axis.z + c;
1225
1226 return result;
1227}
1228
1229// Seštevanje dveh matrix, seštejemo vseh 16 elementov.
1230Matrix Matrix_Add(Matrix matrix1, Matrix matrix2)
1231{
1232 Matrix result = IdentityM;
1233
1234 result.m[0] = matrix1.m[0] + matrix2.m[0];
1235 result.m[1] = matrix1.m[1] + matrix2.m[1];
1236 result.m[2] = matrix1.m[2] + matrix2.m[2];
1237 result.m[3] = matrix1.m[3] + matrix2.m[3];
1238 result.m[4] = matrix1.m[4] + matrix2.m[4];
1239 result.m[5] = matrix1.m[5] + matrix2.m[5];
1240 result.m[6] = matrix1.m[6] + matrix2.m[6];
1241 result.m[7] = matrix1.m[7] + matrix2.m[7];
1242 result.m[8] = matrix1.m[8] + matrix2.m[8];
1243 result.m[9] = matrix1.m[9] + matrix2.m[9];
1244 result.m[10] = matrix1.m[10] + matrix2.m[10];
1245 result.m[11] = matrix1.m[11] + matrix2.m[11];
1246 result.m[12] = matrix1.m[12] + matrix2.m[12];
1247 result.m[13] = matrix1.m[13] + matrix2.m[13];
1248 result.m[14] = matrix1.m[14] + matrix2.m[14];
1249 result.m[15] = matrix1.m[15] + matrix2.m[15];
1250
1251 return result;
1252}
1253
1254// Odštevanje druge matrike od prve, odštejemo vseh 16 elementov.
1255Matrix Matrix_Sub(Matrix matrix1, Matrix matrix2)
1256{
1257 Matrix result = IdentityM;
1258
1259 result.m[0] = matrix1.m[0] - matrix2.m[0];
1260 result.m[1] = matrix1.m[1] - matrix2.m[1];
1261 result.m[2] = matrix1.m[2] - matrix2.m[2];
1262 result.m[3] = matrix1.m[3] - matrix2.m[3];
1263 result.m[4] = matrix1.m[4] - matrix2.m[4];
1264 result.m[5] = matrix1.m[5] - matrix2.m[5];
1265 result.m[6] = matrix1.m[6] - matrix2.m[6];
1266 result.m[7] = matrix1.m[7] - matrix2.m[7];
1267 result.m[8] = matrix1.m[8] - matrix2.m[8];
1268 result.m[9] = matrix1.m[9] - matrix2.m[9];
1269 result.m[10] = matrix1.m[10] - matrix2.m[10];
1270 result.m[11] = matrix1.m[11] - matrix2.m[11];
1271 result.m[12] = matrix1.m[12] - matrix2.m[12];
1272 result.m[13] = matrix1.m[13] - matrix2.m[13];
1273 result.m[14] = matrix1.m[14] - matrix2.m[14];
1274 result.m[15] = matrix1.m[15] - matrix2.m[15];
1275
1276 return result;
1277}
1278
1279// Množenje dveh matrik, število stolpcev in število vrstic mora biti enako!
1280// 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]
1281// Kot lahko vidimo se v prvi matriki pomikamo po vrsticah(iz leve proti desni) v drugi matriki
1282// pa po stolpcu navzdol!
1283Matrix Matrix_Multiply(Matrix matrix2, Matrix matrix1)
1284{
1285 Matrix result = IdentityM;
1286
1287 // Column 1
1288 result.m[0] =
1289 matrix1.m[0] * matrix2.m[0] +
1290 matrix1.m[1] * matrix2.m[4] +
1291 matrix1.m[2] * matrix2.m[8] +
1292 matrix1.m[3] * matrix2.m[12];
1293
1294 result.m[4] =
1295 matrix1.m[4] * matrix2.m[0] +
1296 matrix1.m[5] * matrix2.m[4] +
1297 matrix1.m[6] * matrix2.m[8] +
1298 matrix1.m[7] * matrix2.m[12];
1299
1300 result.m[8] =
1301 matrix1.m[8] * matrix2.m[0] +
1302 matrix1.m[9] * matrix2.m[4] +
1303 matrix1.m[10] * matrix2.m[8] +
1304 matrix1.m[11] * matrix2.m[12];
1305
1306 result.m[12] =
1307 matrix1.m[12] * matrix2.m[0] +
1308 matrix1.m[13] * matrix2.m[4] +
1309 matrix1.m[14] * matrix2.m[8] +
1310 matrix1.m[15] * matrix2.m[12];
1311
1312 // Column 2
1313 result.m[1] =
1314 matrix1.m[0] * matrix2.m[1] +
1315 matrix1.m[1] * matrix2.m[5] +
1316 matrix1.m[2] * matrix2.m[9] +
1317 matrix1.m[3] * matrix2.m[13];
1318
1319 result.m[5] =
1320 matrix1.m[4] * matrix2.m[1] +
1321 matrix1.m[5] * matrix2.m[5] +
1322 matrix1.m[6] * matrix2.m[9] +
1323 matrix1.m[7] * matrix2.m[13];
1324
1325 result.m[9] =
1326 matrix1.m[8] * matrix2.m[1] +
1327 matrix1.m[9] * matrix2.m[5] +
1328 matrix1.m[10] * matrix2.m[9] +
1329 matrix1.m[11] * matrix2.m[13];
1330
1331 result.m[13] =
1332 matrix1.m[12] * matrix2.m[1] +
1333 matrix1.m[13] * matrix2.m[5] +
1334 matrix1.m[14] * matrix2.m[9] +
1335 matrix1.m[15] * matrix2.m[13];
1336
1337 // Column 3
1338 result.m[2] =
1339 matrix1.m[0] * matrix2.m[2] +
1340 matrix1.m[1] * matrix2.m[6] +
1341 matrix1.m[2] * matrix2.m[10] +
1342 matrix1.m[3] * matrix2.m[14];
1343
1344 result.m[6] =
1345 matrix1.m[4] * matrix2.m[2] +
1346 matrix1.m[5] * matrix2.m[6] +
1347 matrix1.m[6] * matrix2.m[10] +
1348 matrix1.m[7] * matrix2.m[14];
1349
1350 result.m[10] =
1351 matrix1.m[8] * matrix2.m[2] +
1352 matrix1.m[9] * matrix2.m[6] +
1353 matrix1.m[10] * matrix2.m[10] +
1354 matrix1.m[11] * matrix2.m[14];
1355
1356 result.m[14] =
1357 matrix1.m[12] * matrix2.m[2] +
1358 matrix1.m[13] * matrix2.m[6] +
1359 matrix1.m[14] * matrix2.m[10] +
1360 matrix1.m[15] * matrix2.m[14];
1361
1362 // Column 4
1363 result.m[3] =
1364 matrix1.m[0] * matrix2.m[3] +
1365 matrix1.m[1] * matrix2.m[7] +
1366 matrix1.m[2] * matrix2.m[11] +
1367 matrix1.m[3] * matrix2.m[15];
1368
1369 result.m[7] =
1370 matrix1.m[4] * matrix2.m[3] +
1371 matrix1.m[5] * matrix2.m[7] +
1372 matrix1.m[6] * matrix2.m[11] +
1373 matrix1.m[7] * matrix2.m[15];
1374
1375 result.m[11] =
1376 matrix1.m[8] * matrix2.m[3] +
1377 matrix1.m[9] * matrix2.m[7] +
1378 matrix1.m[10] * matrix2.m[11] +
1379 matrix1.m[11] * matrix2.m[15];
1380
1381 result.m[15] =
1382 matrix1.m[12] * matrix2.m[3] +
1383 matrix1.m[13] * matrix2.m[7] +
1384 matrix1.m[14] * matrix2.m[11] +
1385 matrix1.m[15] * matrix2.m[15];
1386
1387 return result;
1388}
1389
1390Matrix Matrix_CreateScale(float scale)
1391{
1392 Matrix result = IdentityM;
1393
1394 // Set elements 0,5,10 to scale value.
1395 result.m[0] = result.m[5] = result.m[10] = scale;
1396
1397 return result;
1398}
1399
1400Matrix Matrix_CreateScales(Vector3 scales)
1401{
1402 Matrix result = IdentityM;
1403
1404 // Just set elements 0, 5, 10, to scales values x,y,z
1405 result.m[0] = scales.x;
1406 result.m[5] = scales.y;
1407 result.m[10] = scales.z;
1408
1409 return result;
1410}
1411
1412Matrix Matrix_CreateTranslation(Vector3 translation)
1413{
1414 Matrix result = IdentityM;
1415
1416 result.m[12] = translation.x;
1417 result.m[13] = translation.y;
1418 result.m[14] = translation.z;
1419
1420 return result;
1421}
1422
1423// The one of most important matrix.. the view matrix.
1424Matrix Matrix_CreateLookAt(Vector3 cPosition, Vector3 cTarget, Vector3 cUpVector)
1425{
1426 Matrix result = IdentityM;
1427
1428 return result;
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// vector.h
2123//
2124// Copyright 2011 upd <upd@home>
2125//
2126// This program is free software; you can redistribute it and/or modify
2127// it under the terms of the GNU General Public License as published by
2128// the Free Software Foundation; either version 2 of the License, or
2129// (at your option) any later version.
2130//
2131// This program is distributed in the hope that it will be useful,
2132// but WITHOUT ANY WARRANTY; without even the implied warranty of
2133// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2134// GNU General Public License for more details.
2135//
2136// You should have received a copy of the GNU General Public License
2137// along with this program; if not, write to the Free Software
2138// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
2139// MA 02110-1301, USA.
2140//
2141//
2142
2143typedef struct _Vector
2144{
2145 float x;
2146 float y;
2147 float z;
2148}Vector3;
2149
2150
2151Vector3 Up = { 0.0, 1.0, 0.0 };
2152Vector3 Down = { 0.0, -1.0, 0.0 };
2153Vector3 Left = { -1.0, 0.0, 0.0 };
2154Vector3 Right = { 1.0, 0.0, 0.0 };
2155Vector3 Forward = { 0.0, 0.0, -1.0 };
2156Vector3 Backward = { 0.0, 0.0, 1.0 };
2157Vector3 One = { 1.0, 1.0, 1.0 };
2158Vector3 Zero = { 0.0, 0.0, 0.0 };
2159
2160Vector3 Vector3_Transform(Vector3 a, Matrix M);
2161Vector3 Vector3_Mul(Vector3 a, Vector3 b);
2162Vector3 Vector3_Mulf(Vector3 a, Vector3 b, float f);
2163Vector3 Vector3_Add(Vector3 a, Vector3 b);
2164Vector3 Vector3_Addf(Vector3 a, Vector3 b, float f);
2165float Vector3_Dot(Vector3 a, Vector3 b);
2166Vector3 Vector3_Cross(Vector3 a, Vector3 b);
2167float Magnitude(Vector3 a);
2168Vector3 Vector3_Normalize(Vector3 a);
2169
2170CC = gcc
2171OBJ = main.o 3dsParser.o 3dsDraw.o TextureLoad.o Matrix.o Vector.o
2172FLAGS = -lGL -lGLU -lSDL_image `sdl-config --cflags --libs`
2173
2174TARGET = game
2175
2176$(TARGET): $(OBJ)
2177 $(CC) -o $(TARGET) $(OBJ) $(FLAGS)
2178
2179main.o: main.c
2180 $(CC) -c main.c $(FLAGS)
2181
21823dsParser.o: 3dsParser.c
2183 $(CC) -c 3dsParser.c $(FLAGS)
2184
21853dsDraw.o: 3dsDraw.c
2186 $(CC) -c 3dsDraw.c $(FLAGS)
2187
2188TextureLoad.o: TextureLoad.c
2189 $(CC) -c TextureLoad.c $(FLAGS)
2190
2191Matrix.o: Matrix.c
2192 $(CC) -c Matrix.c $(FLAGS)
2193
2194Vector.o: Vector.c
2195 $(CC) -c Vector.c $(FLAGS)
2196
2197clean:
2198 @echo Cleaning up...
2199 @rm -f $(TARGET) $(OBJ)
2200 @echo Done.