Skip to content

Instantly share code, notes, and snippets.

@waqaraqeel
Last active August 29, 2015 13:58
Show Gist options
  • Save waqaraqeel/10004777 to your computer and use it in GitHub Desktop.
Save waqaraqeel/10004777 to your computer and use it in GitHub Desktop.
#define PI 3.14159265
#define EARTH_RADIUS 0.02
#define MARS_PERIOD 1.881
#define MERCURY_PERIOD 0.241
#define VENUS_PERIOD 0.615
#define MOON_PERIOD 0.0748
#define EARTH_PERIOD 1
#define JUPITER_PERIOD 11.86
#define SATURN_PERIOD 29.46
#define URANUS_PERIOD 84.32
#define NEPTUNE_PERIOD 164.8
#define EARTH_TILT 23.5
#define MARS_TILT 25.0
#define MERCURY_TILT 0.0
#define VENUS_TILT 177.0
#define EARTH_TILT 23.5
#define JUPITER_TILT 3.0
#define SATURN_TILT 27.0
#define URANUS_TILT 98.0
#define NEPTUNE_TILT 30.0
/** @Radius **/
#define MARS_ORBIT_RADIUS 1.52
#define MERCURY_ORBIT_RADIUS 0.39
#define VENUS_ORBIT_RADIUS 0.72
#define MOON_ORBIT_RADIUS 0.0257
#define EARTH_ORBIT_RADIUS 1
#define SUN_ORBIT_RADIUS 0
#define JUPITER_ORBIT_RADIUS 5.2
#define SATURN_ORBIT_RADIUS 9.54
#define URANUS_ORBIT_RADIUS 19.18
#define NEPTUNE_ORBIT_RADIUS 30.06
/** @Diameter **/
#define MARS_DIAMETER 0.532
#define MERCURY_DIAMETER 0.382
#define VENUS_DIAMETER 0.949
#define MOON_DIAMETER 0.25
#define EARTH_DIAMETER 1
#define SUN_DIAMETER 15
#define JUPITER_DIAMETER 11.209
#define SATURN_DIAMETER 9.44
#define URANUS_DIAMETER 4.007
#define NEPTUNE_DIAMETER 3.883
/**@ Inclination **/
#define EARTH_INCLINATION 0
#define MARS_INCLINATION 1.85
#define MERCURY_INCLINATION 7.01
#define VENUS_INCLINATION 3.86
#define MOON_INCLINATION 5.15
#define SUN_INCLINATION 0
#define JUPITER_INCLINATION 1.31
#define SATURN_INCLINATION 2.49
#define URANUS_INCLINATION 0.77
#define NEPTUNE_INCLINATION 1.77
#define MARS_ROTATION_PERIOD 1.027
#define MERCURY_ROTATION_PERIOD 175.94
#define VENUS_ROTATION_PERIOD -116.75
#define EARTH_ROTATION_PERIOD 1
#define JUPITER_ROTATION_PERIOD 0.414
#define SATURN_ROTATION_PERIOD 0.439
#define URANUS_ROTATION_PERIOD -0.718
#define NEPTUNE_ROTATION_PERIOD 16.11
#define EARTH_DEGREES_PER_FRAME 0.05
#define PI 3.14159265
#define CUBE_MAP_TEX 3
GLfloat lightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };
GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
GLfloat diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specRef[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat diffuseRef[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat ambientRef[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f};
GLUquadric *orbitQuad;
GLUquadric *planetQuad;
GLfloat earthOrbitAngle=0;
GLfloat marsOrbitAngle=0;
GLfloat venusOrbitAngle=0;
GLfloat moonOrbitAngle=0;
GLfloat mercuryOrbitAngle=0;
GLfloat jupiterOrbitAngle=0;
GLfloat saturnOrbitAngle=0;
GLfloat uranusOrbitAngle=0;
GLfloat neptuneOrbitAngle=0;
GLfloat earthRotationAngle=0;
GLfloat marsRotationAngle=0;
GLfloat venusRotationAngle=0;
GLfloat moonRotationAngle=0;
GLfloat mercuryRotationAngle=0;
GLfloat jupiterRotationAngle=0;
GLfloat saturnRotationAngle=0;
GLfloat uranusRotationAngle=0;
GLfloat neptuneRotationAngle=0;
bool lookUp;
bool lookDown;
bool lookLeft;
bool lookRight;
bool walkForward;
bool walkBackward;
bool strafeLeft;
bool strafeRight;
float xTranslation;
float yTranslation;
float zTranslation=-30;
float yRotationAngle;
float zRotationAngle;
float xRotationAngle;
int mouseLasty=0;
int mouseLastx=0;
#define EARTH_TEXTURE 0
#define MARS_TEXTURE 1
#define MERCURY_TEXTURE 2
#define VENUS_TEXTURE 3
#define MOON_TEXTURE 4
#define JUPITER_TEXTURE 5
#define SATURN_TEXTURE 6
#define URANUS_TEXTURE 7
#define NEPTUNE_TEXTURE 8
#define SUN_TEXTURE 9
#define NUM_TEXTURES 10
GLuint textureObjects[NUM_TEXTURES];
////////////////////////////////////////////////////////////////////////////////////////////////////////
GLenum cube[6] = { GL_TEXTURE_CUBE_MAP_POSITIVE_X,
GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z };
const char *szTextureFiles[] = {"textures/earth.tga", "textures/mars.tga", "textures/mercury.tga",
"textures/venus.tga", "textures/moon.tga", "textures/jupiter.tga", "textures/saturn.tga", "textures/uranus.tga",
"textures/neptune.tga", "textures/sun.tga"};
const char *szCubeFaces[6] = { "textures/GalaxyRt.tga", "textures/GalaxyLf.tga", "textures/GalaxyUp.tga",
"textures/GalaxyDn.tga", "textures/GalaxyBk.tga", "textures/GalaxyFt.tga" };
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void DrawSkyBox(void)
{
GLfloat fExtent = 500.0f;
glEnable(GL_TEXTURE_CUBE_MAP);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBegin(GL_QUADS);
//////////////////////////////////////////////
// Negative X
glTexCoord3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-fExtent, -fExtent, fExtent);
glTexCoord3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-fExtent, -fExtent, -fExtent);
glTexCoord3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-fExtent, fExtent, -fExtent);
glTexCoord3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-fExtent, fExtent, fExtent);
///////////////////////////////////////////////
// Postive X
glTexCoord3f(1.0f, -1.0f, -1.0f);
glVertex3f(fExtent, -fExtent, -fExtent);
glTexCoord3f(1.0f, -1.0f, 1.0f);
glVertex3f(fExtent, -fExtent, fExtent);
glTexCoord3f(1.0f, 1.0f, 1.0f);
glVertex3f(fExtent, fExtent, fExtent);
glTexCoord3f(1.0f, 1.0f, -1.0f);
glVertex3f(fExtent, fExtent, -fExtent);
////////////////////////////////////////////////
// Negative Z
glTexCoord3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-fExtent, -fExtent, -fExtent);
glTexCoord3f(1.0f, -1.0f, -1.0f);
glVertex3f(fExtent, -fExtent, -fExtent);
glTexCoord3f(1.0f, 1.0f, -1.0f);
glVertex3f(fExtent, fExtent, -fExtent);
glTexCoord3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-fExtent, fExtent, -fExtent);
////////////////////////////////////////////////
// Positive Z
glTexCoord3f(1.0f, -1.0f, 1.0f);
glVertex3f(fExtent, -fExtent, fExtent);
glTexCoord3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-fExtent, -fExtent, fExtent);
glTexCoord3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-fExtent, fExtent, fExtent);
glTexCoord3f(1.0f, 1.0f, 1.0f);
glVertex3f(fExtent, fExtent, fExtent);
//////////////////////////////////////////////////
// Positive Y
glTexCoord3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-fExtent, fExtent, fExtent);
glTexCoord3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-fExtent, fExtent, -fExtent);
glTexCoord3f(1.0f, 1.0f, -1.0f);
glVertex3f(fExtent, fExtent, -fExtent);
glTexCoord3f(1.0f, 1.0f, 1.0f);
glVertex3f(fExtent, fExtent, fExtent);
///////////////////////////////////////////////////
// Negative Y
glTexCoord3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-fExtent, -fExtent, -fExtent);
glTexCoord3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-fExtent, -fExtent, fExtent);
glTexCoord3f(1.0f, -1.0f, 1.0f);
glVertex3f(fExtent, -fExtent, fExtent);
glTexCoord3f(1.0f, -1.0f, -1.0f);
glVertex3f(fExtent, -fExtent, -fExtent);
glEnd();
}
void mouseMovement(int x, int y)
{
int mouseDiffx=x-mouseLastx;
int mouseDiffy=y-mouseLasty;
mouseLastx=x;
mouseLasty=y; //set lasty to the current y position
xRotationAngle += (GLfloat) mouseDiffy;
yRotationAngle += (GLfloat) mouseDiffx;
if (xRotationAngle>=90)
xRotationAngle=90;
if (xRotationAngle<=-90)
xRotationAngle=-90;
}
void updateUserControls()
{
if (lookDown)
{
xRotationAngle+=1;
if (xRotationAngle>=90)
xRotationAngle=90;
}
if (lookUp)
{
xRotationAngle-=1;
if (xRotationAngle<=-90)
xRotationAngle=-90;
}
if (lookRight)
{
yRotationAngle+=1;
if (yRotationAngle>=360)
yRotationAngle=0;
}
if (lookLeft)
{
yRotationAngle-=1;
if (yRotationAngle<=-360)
yRotationAngle=0;
}
GLfloat horizontalMovement = cos(xRotationAngle*PI/180);
GLfloat verticalMovement = -sin(xRotationAngle*PI/180);
if (walkForward)
{
zTranslation+=horizontalMovement*cos(yRotationAngle*PI/180);
xTranslation-=horizontalMovement*sin(yRotationAngle*PI/180);
yTranslation-=verticalMovement;
}
if (walkBackward)
{
zTranslation-=horizontalMovement*cos(yRotationAngle*PI/180);
xTranslation+=horizontalMovement*sin(yRotationAngle*PI/180);
yTranslation+=verticalMovement;
}
if (strafeRight)
{
zTranslation+=cos((yRotationAngle+90)*PI/180);
xTranslation-=sin((yRotationAngle+90)*PI/180);
}
if (strafeLeft)
{
zTranslation-=cos((yRotationAngle+90)*PI/180);
xTranslation+=sin((yRotationAngle+90)*PI/180);
}
}
// Called to draw scene
void RenderScene(void)
{
glShadeModel(GL_SMOOTH);
// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
updateUserControls();
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glRotatef(xRotationAngle,1,0,0);
glRotatef(zRotationAngle,0,0,1);
glRotatef(yRotationAngle,0,1,0);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glDisable(GL_LIGHTING);
DrawSkyBox();
glEnable(GL_LIGHTING);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTranslatef(xTranslation,yTranslation,zTranslation);
glDisable(GL_TEXTURE_CUBE_MAP);
glTranslatef(0,0,-10);
glScalef(10,10,10);
//@sun
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glDisable(GL_LIGHTING);
glColor3f(1,1,1);
glBindTexture(GL_TEXTURE_2D, textureObjects[SUN_TEXTURE]);
gltDrawSphere(0.3,200,200);
glEnable(GL_LIGHTING);
//render the planets
//@jupiter
jupiterOrbitAngle += (EARTH_DEGREES_PER_FRAME/JUPITER_PERIOD);
if (jupiterOrbitAngle>=365.25)
{
jupiterOrbitAngle=0;
}
jupiterRotationAngle += (EARTH_DEGREES_PER_FRAME * (1/JUPITER_ROTATION_PERIOD)*365.25);
if (jupiterRotationAngle>=365.25)
{
jupiterRotationAngle=0;
}
glPushMatrix();
glColor3f(0,0,1);
glRotatef(-90,1,0,0);
glRotatef(JUPITER_INCLINATION,1,0,0);
gluDisk(orbitQuad, JUPITER_ORBIT_RADIUS, JUPITER_ORBIT_RADIUS, 100,100);
glPopMatrix();
glPushMatrix();
glRotatef(JUPITER_INCLINATION,1,0,0);
glRotatef(jupiterOrbitAngle,0,1,0);
glTranslatef(0,0,JUPITER_ORBIT_RADIUS);
glRotatef(JUPITER_TILT,1,0,0);
glRotatef(jupiterRotationAngle,0,1,0);
glRotatef(90,-1,0,0);
glBindTexture(GL_TEXTURE_2D,JUPITER_TEXTURE);
gltDrawSphere(JUPITER_DIAMETER*EARTH_RADIUS,100,100);
glPopMatrix();
//@saturn
saturnOrbitAngle += (EARTH_DEGREES_PER_FRAME/SATURN_PERIOD);
if (saturnOrbitAngle>=365.25)
{
saturnOrbitAngle=0;
}
saturnRotationAngle += (EARTH_DEGREES_PER_FRAME * (1/SATURN_ROTATION_PERIOD)*365.25);
if (saturnRotationAngle>=365.25)
{
saturnRotationAngle=0;
}
glPushMatrix();
glColor3f(0,0,1);
glRotatef(-90,1,0,0);
glRotatef(SATURN_INCLINATION,1,0,0);
gluDisk(orbitQuad, SATURN_ORBIT_RADIUS, SATURN_ORBIT_RADIUS, 100,100);
glPopMatrix();
glPushMatrix();
glRotatef(SATURN_INCLINATION,1,0,0);
glRotatef(saturnOrbitAngle,0,1,0);
glTranslatef(0,0,SATURN_ORBIT_RADIUS);
glRotatef(SATURN_TILT,1,0,0);
glRotatef(saturnRotationAngle,0,1,0);
glRotatef(90,-1,0,0);
glBindTexture(GL_TEXTURE_2D,SATURN_TEXTURE);
gltDrawSphere(SATURN_DIAMETER*EARTH_RADIUS,100,100);
glPopMatrix();
//@uranus
uranusOrbitAngle += (EARTH_DEGREES_PER_FRAME/URANUS_PERIOD);
if (uranusOrbitAngle>=365.25)
{
uranusOrbitAngle=0;
}
uranusRotationAngle += (EARTH_DEGREES_PER_FRAME * (1/URANUS_ROTATION_PERIOD)*365.25);
if (uranusRotationAngle>=365.25)
{
uranusRotationAngle=0;
}
glPushMatrix();
glColor3f(0,0,1);
glRotatef(-90,1,0,0);
glRotatef(URANUS_INCLINATION,1,0,0);
gluDisk(orbitQuad, URANUS_ORBIT_RADIUS, URANUS_ORBIT_RADIUS, 100,100);
glPopMatrix();
glPushMatrix();
glRotatef(URANUS_INCLINATION,1,0,0);
glRotatef(uranusOrbitAngle,0,1,0);
glTranslatef(0,0,URANUS_ORBIT_RADIUS);
glRotatef(URANUS_TILT,1,0,0);
glRotatef(uranusRotationAngle,0,1,0);
glRotatef(90,-1,0,0);
glBindTexture(GL_TEXTURE_2D,URANUS_TEXTURE);
gltDrawSphere(URANUS_DIAMETER*EARTH_RADIUS,100,100);
glPopMatrix();
//@mercury
mercuryOrbitAngle += (EARTH_DEGREES_PER_FRAME/MERCURY_PERIOD);
if (mercuryOrbitAngle>=365.25)
{
mercuryOrbitAngle=0;
}
mercuryRotationAngle += (EARTH_DEGREES_PER_FRAME * (1/MERCURY_ROTATION_PERIOD)*365.25);
if (mercuryRotationAngle>=365.25)
{
mercuryRotationAngle=0;
}
glPushMatrix();
glColor3f(0,0,1);
glRotatef(-90,1,0,0);
glRotatef(MERCURY_INCLINATION,1,0,0);
gluDisk(orbitQuad, MERCURY_ORBIT_RADIUS, MERCURY_ORBIT_RADIUS, 100,100);
glPopMatrix();
glPushMatrix();
glRotatef(MERCURY_INCLINATION,1,0,0);
glRotatef(mercuryOrbitAngle,0,1,0);
glTranslatef(0,0,MERCURY_ORBIT_RADIUS);
glRotatef(MERCURY_TILT,1,0,0);
glRotatef(mercuryRotationAngle,0,1,0);
glRotatef(90,-1,0,0);
glBindTexture(GL_TEXTURE_2D,MERCURY_TEXTURE);
gltDrawSphere(MERCURY_DIAMETER*EARTH_RADIUS,100,100);
glPopMatrix();
//@Venus
venusOrbitAngle += (EARTH_DEGREES_PER_FRAME/VENUS_PERIOD);
if (venusOrbitAngle>=365.25)
{
venusOrbitAngle=0;
}
venusRotationAngle += (EARTH_DEGREES_PER_FRAME * (1/VENUS_ROTATION_PERIOD)*365.25);
if (venusRotationAngle>=365.25)
{
venusRotationAngle=0;
}
glPushMatrix();
glColor3f(0,0,1);
glRotatef(-90,1,0,0);
glRotatef(VENUS_INCLINATION,1,0,0);
gluDisk(orbitQuad, VENUS_ORBIT_RADIUS,VENUS_ORBIT_RADIUS, 100,100);
glPopMatrix();
glPushMatrix();
glRotatef(VENUS_INCLINATION,1,0,0);
glRotatef(venusOrbitAngle,0,1,0);
glTranslatef(0,0,VENUS_ORBIT_RADIUS);
glRotatef(VENUS_TILT,1,0,0);
glRotatef(venusRotationAngle,0,1,0);
glRotatef(90,-1,0,0);
glBindTexture(GL_TEXTURE_2D,VENUS_TEXTURE);
gltDrawSphere(VENUS_DIAMETER*EARTH_RADIUS,100,100);
glPopMatrix();
//@mars
marsOrbitAngle += (EARTH_DEGREES_PER_FRAME/MARS_PERIOD);
if (marsOrbitAngle>=365.25)
{
marsOrbitAngle=0;
}
marsRotationAngle += (EARTH_DEGREES_PER_FRAME * (1/MARS_ROTATION_PERIOD)*365.25);
if (marsRotationAngle>=365.25)
{
marsRotationAngle=0;
}
glPushMatrix();
glColor3f(0,0,1);
glRotatef(-90,1,0,0);
glRotatef(MARS_INCLINATION,1,0,0);
gluDisk(orbitQuad, MARS_ORBIT_RADIUS,MARS_ORBIT_RADIUS, 100,100);
glPopMatrix();
glPushMatrix();
glRotatef(MARS_INCLINATION,1,0,0);
glRotatef(marsOrbitAngle,0,1,0);
glTranslatef(0,0,MARS_ORBIT_RADIUS);
glRotatef(MARS_TILT,1,0,0);
glRotatef(marsRotationAngle,0,1,0);
glRotatef(90,-1,0,0);
glBindTexture(GL_TEXTURE_2D,MARS_TEXTURE);
gltDrawSphere(MARS_DIAMETER*EARTH_RADIUS,100,100);
glPopMatrix();
//@neptune
neptuneOrbitAngle += (EARTH_DEGREES_PER_FRAME/NEPTUNE_PERIOD);
if (neptuneOrbitAngle>=365.25)
{
neptuneOrbitAngle=0;
}
neptuneRotationAngle += (EARTH_DEGREES_PER_FRAME * (1/NEPTUNE_ROTATION_PERIOD)*365.25);
if (neptuneRotationAngle>=365.25)
{
neptuneRotationAngle=0;
}
glPushMatrix();
glColor3f(0,0,1);
glRotatef(-90,1,0,0);
glRotatef(NEPTUNE_INCLINATION,1,0,0);
gluDisk(orbitQuad,NEPTUNE_ORBIT_RADIUS,NEPTUNE_ORBIT_RADIUS, 100,100);
glPopMatrix();
glPushMatrix();
glRotatef(NEPTUNE_INCLINATION,1,0,0);
glRotatef(neptuneOrbitAngle,0,1,0);
glTranslatef(0,0,NEPTUNE_ORBIT_RADIUS);
glRotatef(NEPTUNE_TILT,1,0,0);
glRotatef(neptuneRotationAngle,0,1,0);
glRotatef(90,-1,0,0);
glBindTexture(GL_TEXTURE_2D,NEPTUNE_TEXTURE);
gltDrawSphere(NEPTUNE_DIAMETER*EARTH_RADIUS,100,100);
glPopMatrix();
// Flush drawing commands
glutSwapBuffers();
}
void TimerFunction(int value)
{
// Redraw the scene with new coordinates
glutPostRedisplay();
glutTimerFunc(16,TimerFunction, 1);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Setup the rendering context
void SetupRC(void)
{
orbitQuad = gluNewQuadric();
gluQuadricDrawStyle(orbitQuad , GLU_LINE );
glEnable(GL_RESCALE_NORMAL);
planetQuad = gluNewQuadric();
// White background
glClearColor(0.0f,0.0f, 0.0f, 1.0f );
GLbyte *pBytes;
GLint iWidth, iHeight, iComponents;
GLenum eFormat;
lookUp=false;
lookDown=false;
lookLeft=false;
lookRight=false;
walkForward=false;
walkBackward=false;
strafeLeft=false;
strafeRight=false;
yRotationAngle=0;
xRotationAngle=0;
zRotationAngle=0;
xTranslation=0;
yTranslation=0;
zTranslation=0;
// White background
glClearColor(0.5f,0.95f, 1.0f, 1.0f );
// Set drawing color to green
glColor3f(0.0f, 1.0f, 0.0f);
// Set color shading model to flat
glShadeModel(GL_SMOOTH);
glEnable (GL_DEPTH_TEST);
// Enable lighting
glEnable(GL_LIGHTING);
// Setup and enable light 0
// Supply a slight ambient light so the objects can be seen
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
//glLightfv(GL_LIGHT0,GL_AMBIENT,diffuse);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
// Enable color tracking
//glEnable(GL_COLOR_MATERIAL);
// Set Material properties to follow glColor values
//glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_AMBIENT,ambientRef);
glMaterialfv(GL_FRONT, GL_DIFFUSE,diffuseRef);
glMaterialfv(GL_FRONT, GL_SPECULAR,specRef);
glMateriali(GL_FRONT, GL_SHININESS,128);
glFrontFace(GL_CCW); // Counter clock-wise polygons face out
glEnable(GL_CULL_FACE); // Do not try to display the back sides
// Enable this light in particular
glEnable(GL_LIGHT0);
// Set up texture maps
glEnable(GL_TEXTURE_2D);
glGenTextures(NUM_TEXTURES, textureObjects);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureObjects[CUBE_MAP_TEX]);
// Load Cube Map images
for(int i = 0; i < 6; i++)
{
// Load this texture map
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_GENERATE_MIPMAP, GL_TRUE);
pBytes = gltLoadTGA(szCubeFaces[i], &iWidth, &iHeight, &iComponents, &eFormat);
glTexImage2D(cube[i], 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes);
free(pBytes);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_GENERATE_MIPMAP, GL_TRUE);
pBytes = gltLoadTGA(szTextureFiles[0], &iWidth, &iHeight, &iComponents, &eFormat);
glTexImage2D(cube[0], 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes);
free(pBytes);
// Set up texture maps
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
for(int i = 0; i < NUM_TEXTURES; i++)
{
GLbyte *pBytes;
GLint iWidth, iHeight, iComponents;
GLenum eFormat;
glBindTexture(GL_TEXTURE_2D, textureObjects[i]);
// Load this texture map
pBytes = gltLoadTGA(szTextureFiles[i], &iWidth, &iHeight, &iComponents, &eFormat);
gluBuild2DMipmaps(GL_TEXTURE_2D, iComponents, iWidth, iHeight, eFormat, GL_UNSIGNED_BYTE, pBytes);
free(pBytes);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
}
////////////////////////////////////////////////////////////////////////
// Do shutdown for the rendering context
void ShutdownRC(void)
{
// Delete the textures
glDeleteTextures(NUM_TEXTURES, textureObjects);
}
void ChangeSize(int w, int h)
{
//GLfloat nRange = 100.0f;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
gluPerspective(45,(float)w/(float)h,0.01,1000);
// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Respond to arrow keys by moving the camera frame of reference
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
lookUp=true;
if(key == GLUT_KEY_DOWN)
lookDown=true;
if(key == GLUT_KEY_LEFT)
lookLeft=true;
if(key == GLUT_KEY_RIGHT)
lookRight=true;
// Refresh the Window
glutPostRedisplay();
}
void SpecialKeysUp(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
lookUp=false;
if(key == GLUT_KEY_DOWN)
lookDown=false;
if(key == GLUT_KEY_LEFT)
lookLeft=false;
if(key == GLUT_KEY_RIGHT)
lookRight=false;
// Refresh the Window
glutPostRedisplay();
}
void keyboardFunc(unsigned char key, int x, int y)
{
switch(key)
{
case 'w':
walkForward=true;
break;
case 's':
walkBackward=true;
break;
case 'a':
strafeLeft=true;
break;
case 'd':
strafeRight=true;
break;
default:
break;
}
}
void keyboardUpFunc(unsigned char key, int x, int y)
{
switch(key)
{
case 'w':
walkForward=false;
break;
case 's':
walkBackward=false;
break;
case 'a':
strafeLeft=false;
break;
case 'd':
strafeRight=false;
break;
default:
break;
}
}
///////////////////////////////////////////////////////////
// Main program entry point
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow(" Oluwaseun Ayinla");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
glutSpecialFunc(SpecialKeys);
glutSpecialUpFunc(SpecialKeysUp);
glutKeyboardUpFunc(keyboardUpFunc);
glutKeyboardFunc(keyboardFunc);
glutPassiveMotionFunc(mouseMovement);
glutTimerFunc(16, TimerFunction, 1);
SetupRC();
glutMainLoop();
ShutdownRC();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment