Line # Revision Author
1 2 moose // cube.cpp - implementation for Cube class
2 // (C)2001 Nicholas Davies
3
4 #include "cube.h"
5
6 const float Cube::Vertices[8][3] =
7 {
8 { -0.5, 0.5, 0.5 },
9 { -0.5, -0.5, 0.5 },
10 { 0.5, -0.5, 0.5 },
11 { 0.5, 0.5, 0.5 },
12 { -0.5, 0.5, -0.5 },
13 { -0.5, -0.5, -0.5 },
14 { 0.5, -0.5, -0.5 },
15 { 0.5, 0.5, -0.5 }
16 };
17
18 const int Cube::Indices[6][4] =
19 {
20 { 0, 1, 2, 3 },
21 { 7, 6, 5, 4 },
22 { 3, 2, 6, 7 },
23 { 1, 0, 4, 5 },
24 { 0, 3, 7, 4 },
25 { 5, 6, 2, 1 }
26 };
27
28 8 moose const float Cube::Colors[6][4] =
29 2 moose {
30 8 moose { 1.0, 0.0, 0.0, 0.5 },
31 { 0.0, 1.0, 0.0, 0.5 },
32 { 0.0, 0.0, 1.0, 0.5 },
33 { 1.0, 1.0, 0.0, 0.5 },
34 { 0.0, 1.0, 1.0, 0.5 },
35 { 1.0, 0.0, 1.0, 0.5 }
36 2 moose };
37
38 Cube::Cube(float x, float y, float z, float side_length)
39 : sl(side_length), x(x), y(y), z(z), xrot(0.0), yrot(0.0), zrot(0.0)
40 {
41 }
42
43 void Cube::Update()
44 {
45 if(key[KEY_UP]) xrot -= 2.0;
46 if(key[KEY_DOWN]) xrot += 2.0;
47 if(key[KEY_LEFT]) yrot -= 2.0;
48 if(key[KEY_RIGHT]) yrot += 2.0;
49 if(key[KEY_X]) zrot -= 2.0;
50 if(key[KEY_Z]) zrot += 2.0;
51 }
52
53 void Cube::Render()
54 {
55 glPushMatrix();
56
57 8 moose //glLoadIdentity();
58 2 moose
59 glTranslatef(x, y, z);
60 glScalef(sl, sl, sl);
61 glRotatef(xrot, 1.0, 0.0, 0.0);
62 glRotatef(yrot, 0.0, 1.0, 0.0);
63 glRotatef(zrot, 0.0, 0.0, 1.0);
64
65 glBegin(GL_QUADS);
66 for(int i = 0; i < 6; i++)
67 {
68 9 moose glColor4fv(Colors[i]);
69 8 moose
70 glTexCoord2f(Vertices[Indices[i][0]][0]+0.5, Vertices[Indices[i][0]][1]+0.5);
71 2 moose glVertex3fv(Vertices[Indices[i][0]]);
72 8 moose
73 glTexCoord2f(Vertices[Indices[i][1]][0]+0.5, Vertices[Indices[i][1]][1]+0.5);
74 2 moose glVertex3fv(Vertices[Indices[i][1]]);
75 8 moose
76 glTexCoord2f(Vertices[Indices[i][2]][0]+0.5, Vertices[Indices[i][2]][1]+0.5);
77 2 moose glVertex3fv(Vertices[Indices[i][2]]);
78 8 moose
79 glTexCoord2f(Vertices[Indices[i][3]][0]+0.5, Vertices[Indices[i][3]][1]+0.5);
80 2 moose glVertex3fv(Vertices[Indices[i][3]]);
81 }
82 glEnd();
83 8 moose // glFlush();
84 2 moose
85 glPopMatrix();
86 }