initial commit
[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.35, 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         /* draw floor */
72         glPushMatrix();
73         glTranslatef(0, -1, 0);
74
75         color[0] = 0.4;
76         color[1] = 0.75;
77         color[2] = 0.4;
78         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
79
80         glBegin(GL_QUADS);
81         glNormal3f(0, 1, 0);
82         glVertex3f(-2, 0, 2);
83         glVertex3f(2, 0, 2);
84         glVertex3f(2, 0, -2);
85         glVertex3f(-2, 0, -2);
86         glEnd();
87
88         glPopMatrix();
89
90         /* draw torus */
91         glPushMatrix();
92         glTranslatef(0, 0.35, 0);
93         glRotatef(t, 0, 1, 0);
94         glRotatef(t, 1, 0, 0);
95
96         color[0] = 0.2;
97         color[1] = 0.3;
98         color[2] = 0.9;
99         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
100
101         draw_mesh(&torus);
102
103         glPopMatrix();
104 }