Skip to content

Instantly share code, notes, and snippets.

@jgarlick
Created December 11, 2012 10:50
Show Gist options
  • Save jgarlick/4257714 to your computer and use it in GitHub Desktop.
Save jgarlick/4257714 to your computer and use it in GitHub Desktop.
draw scene
/* draw_scene.c
*
* Code to draw the main scene when in flight
*
*/
#include "main.h"
/* highly unoptimised scene rendering */
void draw_scene( void )
{
int a;
Vector view;
GLfloat params;
float r;
GLfloat light_position[] = { 80000, 0.0, -150000.0, 1.0 }; // light at the sun position
/* material properties */
GLfloat sun_emission[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat sun_specular[] = { 1.0, 1.0, 0.6, 1.0 };
GLfloat sun_shininess[] = { 10.0 };
GLfloat no_emission[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat planet_diff[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat atmos_diff[] = { 0.5, 0.5, 1.0, 0.5 };
GLfloat planet_spec[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat sun_glow_diff[] = { 1.0, 0.4, 0.4, 0.4 };
GLfloat sun_glow_diff2[] = { 1.0, 1.0, 0.4, 0.6 };
GLfloat sun_diff[] = { 1.0, 1.0, 0.5, 1.0 };
GLfloat sun_shine_pos[4];
GLfloat ship_emission[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat ship_diff[] = { 0.5, 0.0, 0.5, 1.0 };
GLfloat ship_spec[] = { 0.0, 0.0, 0.0, 1.0 };
glViewport( 0, 0, width, height );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* calculate the aspect ratio of the window */
/* really big far point to accomodate the planets, it probably shouldn't be done like this */
gluPerspective(fov, (GLfloat)width / (GLfloat)height, 0.1f, 3000000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
if (view_dir == REAR_VIEW)
object_rotate_y (&camera, 180.0); // this is crap
/* calculate the view vector */
vector_add (&view, &(camera.pos), &(camera.view));
/* set the camera orientation */
gluLookAt (camera.pos.x, camera.pos.y, camera.pos.z,
view.x, view.y, view.z,
camera.up.x, camera.up.y, camera.up.z);
if (view_dir == REAR_VIEW)
object_rotate_y (&camera, 180.0); // this is crap
/** Skybox plotting **/
glEnable (GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, texture[2]); // the stars texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
/* draw the skybox as a cube around the camera */
glPushMatrix ();
glTranslatef (camera.pos.x, camera.pos.y, camera.pos.z);
glBegin (GL_QUADS);
/* front */
glTexCoord2f (0.0, 0.0); glVertex3f (-40, -40, -40);
glTexCoord2f (3.0, 0.0); glVertex3f ( 40, -40, -40);
glTexCoord2f (3.0, 3.0); glVertex3f ( 40, 40, -40);
glTexCoord2f (0.0, 3.0); glVertex3f (-40, 40, -40);
/* back */
glTexCoord2f (0.0, 0.0); glVertex3f ( 40, -40, 40);
glTexCoord2f (3.0, 0.0); glVertex3f (-40, -40, 40);
glTexCoord2f (3.0, 3.0); glVertex3f (-40, 40, 40);
glTexCoord2f (0.0, 3.0); glVertex3f ( 40, 40, 40);
/* right */
glTexCoord2f (0.0, 0.0); glVertex3f (40, -40, -40);
glTexCoord2f (3.0, 0.0); glVertex3f (40, -40, 40);
glTexCoord2f (3.0, 3.0); glVertex3f (40, 40, 40);
glTexCoord2f (0.0, 3.0); glVertex3f (40, 40, -40);
/* left */
glTexCoord2f (0.0, 0.0); glVertex3f (-40, -40, 40);
glTexCoord2f (3.0, 0.0); glVertex3f (-40, -40, -40);
glTexCoord2f (3.0, 3.0); glVertex3f (-40, 40, -40);
glTexCoord2f (0.0, 3.0); glVertex3f (-40, 40, 40);
/* top */
glTexCoord2f (0.0, 0.0); glVertex3f (-40, 40, -40);
glTexCoord2f (3.0, 0.0); glVertex3f ( 40, 40, -40);
glTexCoord2f (3.0, 3.0); glVertex3f ( 40, 40, 40);
glTexCoord2f (0.0, 3.0); glVertex3f (-40, 40, 40);
/* bottom */
glTexCoord2f (0.0, 0.0); glVertex3f (-40, -40, 40);
glTexCoord2f (3.0, 0.0); glVertex3f ( 40, -40, 40);
glTexCoord2f (3.0, 3.0); glVertex3f ( 40, -40, -40);
glTexCoord2f (0.0, 3.0); glVertex3f (-40, -40, -40);
glEnd ();
glPopMatrix ();
/** planet / sun plotting **/
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glLightfv (GL_LIGHT0, GL_POSITION, light_position);
sun_shine_pos[0] = camera.pos.x;
sun_shine_pos[1] = camera.pos.y;
sun_shine_pos[2] = camera.pos.z;
sun_shine_pos[3] = 1.0;
glLightfv (GL_LIGHT1, GL_POSITION, sun_shine_pos);
glEnable (GL_LIGHT1);
glMaterialfv (GL_FRONT, GL_DIFFUSE, sun_diff);
glMaterialfv (GL_FRONT, GL_EMISSION, sun_emission);
glMaterialfv (GL_FRONT, GL_SPECULAR, sun_specular);
glMaterialfv (GL_FRONT, GL_SHININESS, sun_shininess);
/* plot the sun */
glBindTexture (GL_TEXTURE_2D, texture[1]);
glPushMatrix ();
glTranslatef (80000, 0, -150000);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
glRotatef(180, 0.0f, 0.0f, 1.0f);
glCallList (4);
glPopMatrix ();
glDisable (GL_LIGHT1);
glEnable (GL_LIGHT0);
glMaterialfv (GL_FRONT, GL_EMISSION, no_emission);
glMaterialfv (GL_FRONT, GL_DIFFUSE, planet_diff);
glMaterialfv (GL_FRONT, GL_SPECULAR, planet_spec);
/* plot the planet */
glMaterialfv (GL_FRONT, GL_DIFFUSE, planet_diff);
glBindTexture (GL_TEXTURE_2D, texture[0]);
/* turn on mipmapping */
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
// glDisable (GL_TEXTURE_2D); //temp
glPushMatrix ();
glTranslatef (-20000, 0, -200000);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
glRotatef(earth_rot, 0.0f, 0.0f, 1.0f);
glCallList (2);
glPopMatrix ();
glDisable (GL_TEXTURE_2D);
glMaterialfv (GL_FRONT, GL_DIFFUSE, atmos_diff);
glEnable(GL_BLEND);
/* planet atmosphere */
glBlendFunc (GL_SRC_ALPHA, GL_ONE);
glPushMatrix ();
glTranslatef (-20000, 0, -200000);
glCallList (3);
glPopMatrix ();
glDisable (GL_LIGHT0);
glMaterialfv (GL_FRONT, GL_DIFFUSE, sun_glow_diff);
glEnable (GL_LIGHT1);
/* sun shine */
glBlendFunc (GL_SRC_ALPHA, GL_ONE);
glPushMatrix ();
glColor4f(0.3f, 0.3f, 0.0f, 0.5f);
glTranslatef (80000, 0, -150000);
glCallList (5);
glPopMatrix ();
glMaterialfv (GL_FRONT, GL_DIFFUSE, sun_glow_diff2);
glPushMatrix ();
glColor4f(0.3f, 0.3f, 0.0f, 0.5f);
glTranslatef (80000, 0, -150000);
glCallList (6);
glPopMatrix ();
glDisable (GL_LIGHT1);
glDisable (GL_BLEND);
/** ships / object plotting **/
// glEnable (GL_LIGHT0);
glDisable (GL_LIGHTING); /* temp */
glEnable (GL_DEPTH_TEST);
glEnable (GL_TEXTURE_2D);
for (a = 0; a < 4; a++)
object_plot (&(ship[a]));
glDisable (GL_TEXTURE_2D);
glDisable (GL_DEPTH_TEST);
// glDisable (GL_LIGHT0);
// glDisable (GL_LIGHTING);
/** 2-d drawing **/
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
gluOrtho2D (0.0, (GLdouble) width, 0.0, (GLdouble) height);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity();
/* redraw the HUD */
hud_redraw ();
SDL_GL_SwapBuffers( );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment