fixed 6dof camera
[o2demo] / src / parts / example.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <GL/gl.h>
6 #include "demo.h"
7 #include "mesh.h"
8 #include "screen.h"
9
10 static int init(void);
11 static void destroy(void);
12 static void start(long trans_time);
13 static void draw(void);
14
15 static struct screen scr = {
16         "example",
17         init,
18         destroy,
19         start, 0,
20         draw
21 };
22
23 static float cam_theta = -29, cam_phi = 20;
24 static float cam_dist = 6;
25
26 static struct g3d_mesh torus;
27
28 struct screen *example_screen(void)
29 {
30         return &scr;
31 }
32
33
34 static int init(void)
35 {
36         if(gen_torus_mesh(&torus, 1.0f, 0.15, 24, 12) == -1) {
37                 return -1;
38         }
39         return 0;
40 }
41
42 static void destroy(void)
43 {
44 }
45
46 static void start(long trans_time)
47 {
48         glEnable(GL_LIGHTING);
49         glEnable(GL_LIGHT0);
50 }
51
52 static void update(void)
53 {
54 }
55
56 static void draw(void)
57 {
58         float color[4] = {1, 1, 1, 1};
59         float t = (float)time_msec / 10.0f;
60
61         update();
62
63         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
64
65         glMatrixMode(GL_MODELVIEW);
66         glLoadIdentity();
67         /*glTranslatef(0, 0, -cam_dist);
68         glRotatef(cam_phi, 1, 0, 0);
69         glRotatef(cam_theta, 0, 1, 0);*/
70
71         glMultMatrixf(sball_cam_matrix);
72
73         /* draw floor */
74         glPushMatrix();
75         glTranslatef(0, -1, 0);
76
77         color[0] = 0.4;
78         color[1] = 0.75;
79         color[2] = 0.4;
80         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
81
82         glBegin(GL_QUADS);
83         glNormal3f(0, 1, 0);
84         glVertex3f(-4, 0, 4);
85         glVertex3f(4, 0, 4);
86         glVertex3f(4, 0, -4);
87         glVertex3f(-4, 0, -4);
88         glEnd();
89
90         glPopMatrix();
91
92         /* draw torus */
93         glPushMatrix();
94         glTranslatef(0, 0.35, 0);
95         glRotatef(t, 0, 1, 0);
96         glRotatef(t, 1, 0, 0);
97
98         color[0] = 0.2;
99         color[1] = 0.3;
100         color[2] = 0.9;
101         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
102
103         draw_mesh(&torus);
104         glRotatef(90, 1, 0, 0);
105         draw_mesh(&torus);
106         glRotatef(90, 0, 0, 1);
107         draw_mesh(&torus);
108
109         glPopMatrix();
110 }