adding goat3d to the project
[deeprace] / src / scr_game.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <GL/gl.h>
5 #include "miniglut.h"
6 #include "game.h"
7 #include "util.h"
8 #include "goat3d.h"
9
10 static int ginit(void);
11 static void gdestroy(void);
12 static int gstart(void);
13 static void gstop(void);
14 static void gdisplay(void);
15 static void greshape(int x, int y);
16 static void gkeyb(int key, int press);
17 static void gmouse(int bn, int press, int x, int y);
18 static void gmotion(int x, int y);
19
20 struct game_screen scr_game = {
21         "game",
22         ginit, gdestroy,
23         gstart, gstop,
24         gdisplay, greshape,
25         gkeyb, gmouse, gmotion
26 };
27
28 static float cam_theta, cam_phi = 20, cam_dist = 10;
29 static float cam_pan[3];
30
31 static struct goat3d *gscn;
32 static int dlist;
33
34
35 static int ginit(void)
36 {
37         int i, num, nfaces;
38
39         if(!(gscn = goat3d_create()) || goat3d_load(gscn, "data/track1.g3d")) {
40                 return -1;
41         }
42
43         dlist = glGenLists(1);
44         glNewList(dlist, GL_COMPILE);
45         num = goat3d_get_node_count(gscn);
46         for(i=0; i<num; i++) {
47                 struct goat3d_node *node = goat3d_get_node(gscn, i);
48                 if(goat3d_get_node_type(node) == GOAT3D_NODE_MESH) {
49                         struct goat3d_mesh *mesh = goat3d_get_node_object(node);
50
51                         glEnableClientState(GL_VERTEX_ARRAY);
52                         glVertexPointer(3, GL_FLOAT, 0, goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX));
53
54                         nfaces = goat3d_get_mesh_face_count(mesh) / 3;
55                         glDrawElements(GL_TRIANGLES, nfaces * 3, GL_UNSIGNED_INT, goat3d_get_mesh_faces(mesh));
56
57                         glDisableClientState(GL_VERTEX_ARRAY);
58
59                 }
60         }
61         glEndList();
62
63         return 0;
64 }
65
66 static void gdestroy(void)
67 {
68         goat3d_free(gscn);
69 }
70
71 static int gstart(void)
72 {
73         return 0;
74 }
75
76 static void gstop(void)
77 {
78 }
79
80 static void gdisplay(void)
81 {
82         glMatrixMode(GL_MODELVIEW);
83         glLoadIdentity();
84         glTranslatef(0, 0, -cam_dist);
85         glRotatef(cam_phi, 1, 0, 0);
86         glRotatef(cam_theta, 0, 1, 0);
87         glTranslatef(cam_pan[0], cam_pan[1], cam_pan[2]);
88
89         glCallList(dlist);
90 }
91
92 static void greshape(int x, int y)
93 {
94 }
95
96 static void gkeyb(int key, int press)
97 {
98 }
99
100 static void gmouse(int bn, int press, int x, int y)
101 {
102 }
103
104 static void gmotion(int x, int y)
105 {
106         int dx = x - mouse_x;
107         int dy = y - mouse_y;
108
109         if(!(dx | dy)) return;
110
111         if(mouse_state[0]) {
112                 cam_theta += dx * 0.5;
113                 cam_phi += dy * 0.5;
114                 if(cam_phi < -90) cam_phi = -90;
115                 if(cam_phi > 90) cam_phi = 90;
116         }
117         if(mouse_state[1]) {
118                 float up[3], right[3];
119                 float theta = cam_theta * M_PI / 180.0f;
120                 float phi = cam_phi * M_PI / 180.0f;
121
122                 up[0] = -sin(theta) * sin(phi);
123                 up[1] = -cos(phi);
124                 up[2] = cos(theta) * sin(phi);
125                 right[0] = cos(theta);
126                 right[1] = 0;
127                 right[2] = sin(theta);
128
129                 cam_pan[0] += (right[0] * dx + up[0] * dy) * 0.01;
130                 cam_pan[1] += up[1] * dy * 0.01;
131                 cam_pan[2] += (right[2] * dx + up[2] * dy) * 0.01;
132         }
133         if(mouse_state[2]) {
134                 cam_dist += dy * 0.1;
135                 if(cam_dist < 0) cam_dist = 0;
136         }
137 }