Code:
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#define VIEWING_DISTANCE_MIN 3.0
#define TEXTURE_ID_CUBE 1
static struct timeval last_idle_time;
static GLfloat g_fTeapotAngle = 0.0;
static GLfloat g_fTeapotAngle2 = 0.0;
//static DWORD last_idle_time;
static GLfloat g_fViewDistance = 3 * VIEWING_DISTANCE_MIN;
static float g_lightPos[4] = { 10, 10, -100, 1 };
static GLfloat g_nearPlane = 1;
static GLfloat g_farPlane = 1000;
static int g_Width = 600;
static int g_Height = 600;
void DrawCubeFace(float fSize)
{
fSize /=1.0;
glBegin(GL_QUADS);
glVertex3f(-fSize, -fSize, fSize); glTexCoord2f (0, 0);
glVertex3f(fSize, -fSize, fSize); glTexCoord2f (1, 0);
glVertex3f(fSize, fSize, fSize); glTexCoord2f (1, 1);
glVertex3f(-fSize, fSize, fSize); glTexCoord2f (0, 1);
glEnd();
}
void DrawCubeWithTextureCoords (float fSize)
{
glPushMatrix();
DrawCubeFace (fSize);
glRotatef (90, 1, 0, 0);
DrawCubeFace (fSize);
glRotatef (90, 1, 0, 0);
DrawCubeFace (fSize);
glRotatef (90, 1, 0, 0);
DrawCubeFace (fSize);
glRotatef (90, 0, 1, 0);
DrawCubeFace (fSize);
glRotatef (180, 0, 1, 0);
DrawCubeFace (fSize);
glPopMatrix();
}
void RenderObjects(void)
{
float colorBronzeDiff[4] = { 0.9, 0.9, 0.0, 1.0 };
float colorBronzeSpec[4] = { 1.0, 1.0, 0.9, 1.0 };
float colorBlue[4] = { 0.0, 0.9, 1.0, 1.0 };
float colorNone[4] = { 0.0, 0.0, 0.0, 0.0 };
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glRotatef(25, 1, 0, 0);
glRotatef(45, 0, 1, 0);
glRotatef(g_fTeapotAngle, 0, 0, 1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, colorBlue);
glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone);
glColor4fv(colorBlue);
glBindTexture(GL_TEXTURE_2D, TEXTURE_ID_CUBE);
DrawCubeWithTextureCoords(1.0);
glPushMatrix();
// glTranslatef(2, 0, 0);
glRotatef(g_fTeapotAngle2, 1, 1, 0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, colorBronzeDiff);
glMaterialfv(GL_FRONT, GL_SPECULAR, colorBronzeSpec);
glMaterialf(GL_FRONT, GL_SHININESS, 50.0);
glColor4fv(colorBronzeDiff);
// glutSolidCone(0.3f, 0.8f,5, 4);
glBindTexture(GL_TEXTURE_2D, 0);
glPopMatrix();
glPopMatrix();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0, 0, -g_fViewDistance, 0, 0, -1, 0, 1, 0);
glLightfv(GL_LIGHT0, GL_POSITION, g_lightPos);
RenderObjects();
glutSwapBuffers();
}
void reshape(GLint width, GLint height)
{
g_Width = width;
g_Height = height;
glViewport(0, 0, g_Width, g_Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (float)g_Width / g_Height, g_nearPlane, g_farPlane);
glMatrixMode(GL_MODELVIEW);
}
void AnimateScene(void)
{ float dt;
#ifdef _WIN32
DWORD time_now;
time_now = GetTickCount();
dt = (float) (time_now - last_idle_time) / 1000.0;
#else
struct timeval time_now;
gettimeofday(&time_now, NULL);
dt = (float)(time_now.tv_sec - last_idle_time.tv_sec) +
1.0e-6*(time_now.tv_usec - last_idle_time.tv_usec);
#endif
g_fTeapotAngle += dt * 30.0;
g_fTeapotAngle2 += dt * 100.0;
last_idle_time = time_now;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit (&argc, argv);
glutInitWindowSize (g_Width, g_Height);
glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow ("RAVI");
// InitGraphics();
glutDisplayFunc (display);
glutReshapeFunc (reshape);
// glutKeyboardFunc (Keyboard);
// glutMouseFunc (MouseButton);
// glutMotionFunc (MouseMotion);
glutIdleFunc (AnimateScene);
glutAttachMenu (GLUT_RIGHT_BUTTON);
#ifdef _WIN32
last_idle_time = GetTickCount();
#else
gettimeofday (&last_idle_time, NULL);
#endif
glutMainLoop ();
return 0;
}