// cube.cpp - implementation for Cube class
// (C)2001 Nicholas Davies
#include "cube.h"
const float Cube::Vertices[8][3] =
{
{ -0.5, 0.5, 0.5 },
{ -0.5, -0.5, 0.5 },
{ 0.5, -0.5, 0.5 },
{ 0.5, 0.5, 0.5 },
{ -0.5, 0.5, -0.5 },
{ -0.5, -0.5, -0.5 },
{ 0.5, -0.5, -0.5 },
{ 0.5, 0.5, -0.5 }
};
const int Cube::Indices[6][4] =
{
{ 0, 1, 2, 3 },
{ 7, 6, 5, 4 },
{ 3, 2, 6, 7 },
{ 1, 0, 4, 5 },
{ 0, 3, 7, 4 },
{ 5, 6, 2, 1 }
};
const float Cube::Colors[6][4] =
{
{ 1.0, 0.0, 0.0, 0.5 },
{ 0.0, 1.0, 0.0, 0.5 },
{ 0.0, 0.0, 1.0, 0.5 },
{ 1.0, 1.0, 0.0, 0.5 },
{ 0.0, 1.0, 1.0, 0.5 },
{ 1.0, 0.0, 1.0, 0.5 }
};
Cube::Cube(float x, float y, float z, float side_length)
: sl(side_length), x(x), y(y), z(z), xrot(0.0), yrot(0.0), zrot(0.0)
{
}
void Cube::Update()
{
if(key[KEY_UP]) xrot -= 2.0;
if(key[KEY_DOWN]) xrot += 2.0;
if(key[KEY_LEFT]) yrot -= 2.0;
if(key[KEY_RIGHT]) yrot += 2.0;
if(key[KEY_X]) zrot -= 2.0;
if(key[KEY_Z]) zrot += 2.0;
}
void Cube::Render()
{
glPushMatrix();
//glLoadIdentity();
glTranslatef(x, y, z);
glScalef(sl, sl, sl);
glRotatef(xrot, 1.0, 0.0, 0.0);
glRotatef(yrot, 0.0, 1.0, 0.0);
glRotatef(zrot, 0.0, 0.0, 1.0);
glBegin(GL_QUADS);
for(int i = 0; i < 6; i++)
{
glColor4fv(Colors[i]);
glTexCoord2f(Vertices[Indices[i][0]][0]+0.5, Vertices[Indices[i][0]][1]+0.5);
glVertex3fv(Vertices[Indices[i][0]]);
glTexCoord2f(Vertices[Indices[i][1]][0]+0.5, Vertices[Indices[i][1]][1]+0.5);
glVertex3fv(Vertices[Indices[i][1]]);
glTexCoord2f(Vertices[Indices[i][2]][0]+0.5, Vertices[Indices[i][2]][1]+0.5);
glVertex3fv(Vertices[Indices[i][2]]);
glTexCoord2f(Vertices[Indices[i][3]][0]+0.5, Vertices[Indices[i][3]][1]+0.5);
glVertex3fv(Vertices[Indices[i][3]]);
}
glEnd();
// glFlush();
glPopMatrix();
}