Skip to content

Instantly share code, notes, and snippets.

@davidwparker
Created December 26, 2012 21:08
Show Gist options
  • Save davidwparker/4383192 to your computer and use it in GitHub Desktop.
Save davidwparker/4383192 to your computer and use it in GitHub Desktop.
OpenGL Screencast 21: Skybox
#include "screencasts.h"
/*
* initGlobals()
* ------
* Initializes the global variables.
*/
void initGlobals(void)
{
/* WINDOW */
windowHeight=DEF_WINDOW_HEIGHT;
windowWidth=DEF_WINDOW_WIDTH;
windowPosHeight=DEF_WINDOW_POS_H;
windowPosWidth=DEF_WINDOW_POS_W;
/* PROJECTION */
dim=DEF_DIM;
th=DEF_TH;
ph=DEF_PH;
fov=DEF_FOV;
asp=DEF_ASP;
ecX=DEF_ECX;
ecY=DEF_ECY;
ecZ=DEF_ECZ;
}
/*
* initSkybox
* ------
* initializes all of our textures for the skybox background
*/
void initSkybox(void)
{
/*
SKY_FRONT 0
SKY_RIGHT 1
SKY_LEFT 2
SKY_BACK 3
SKY_UP 4
SKY_DOWN 5
*/
skybox[SKY_FRONT] = loadTexBMP("txStormydays_front.bmp");
skybox[SKY_RIGHT] = loadTexBMP("txStormydays_right.bmp");
skybox[SKY_LEFT] = loadTexBMP("txStormydays_left.bmp");
skybox[SKY_BACK] = loadTexBMP("txStormydays_back.bmp");
skybox[SKY_UP] = loadTexBMP("txStormydays_up.bmp");
skybox[SKY_DOWN] = loadTexBMP("txStormydays_down.bmp");
}
void windowKey(unsigned char key,int x,int y)
{
/* Exit on ESC */
if (key == 27) exit(0);
/* Change field of view angle */
else if (key == '-' && key>1) fov--;
else if (key == '+' && key<179) fov++;
redisplayAll();
}
void windowSpecial(int key,int x,int y)
{
/* Right/Left - rotate */
if (key == GLUT_KEY_RIGHT) th += 5;
else if (key == GLUT_KEY_LEFT) th -= 5;
/* Up/Down - elevation */
else if (key == GLUT_KEY_UP) ph += 5;
else if (key == GLUT_KEY_DOWN) ph -= 5;
/* Keep angles at +/- 360 degrees */
th %= 360;
ph %= 360;
redisplayAll();
}
/*
* main()
* ----
* Start up GLUT and tell it what to do
*/
int main(int argc,char* argv[])
{
initGlobals();
/* screencast specific variables */
windowName = "OpenGL screenscasts 21: Skybox";
screencastID = 21;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(windowWidth,windowHeight);
glutInitWindowPosition(windowPosWidth,windowPosHeight);
glutCreateWindow(windowName);
glutDisplayFunc(display);
glutReshapeFunc(displayReshape);
glutKeyboardFunc(windowKey);
glutSpecialFunc(windowSpecial);
initSkybox();
redisplayAll();
glutMainLoop();
return 0;
}
#include "screencasts.h"
/*
* displayInit()
* -------
* Initializes display
*/
void displayInit(void)
{
glClearColor(1,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glLoadIdentity();
}
/*
* displayEye()
* ------
* Set the eye position
*/
void displayEye(void)
{
double Ex = -2*dim*Sin(th)*Cos(ph);
double Ey = +2*dim *Sin(ph);
double Ez = +2*dim*Cos(th)*Cos(ph);
/* camera/eye position, aim of camera lens, up-vector */
gluLookAt(Ex+ecX,Ey,Ez+ecZ , ecX,ecY,ecZ , 0,Cos(ph),0);
}
/*
* displayReshape()
* ------
* GLUT calls this routine when the window is resized
*/
void displayReshape(int width,int height)
{
asp = (height>0) ? (double)width/height : 1;
glViewport(0,0, width,height);
displayProject(fov,asp,dim);
}
/*
* displayProject()
* ------
* Sets the projection
*/
void displayProject(double fov, double asp, double dim)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov,asp,dim/16,16*dim);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/*
* display()
* ------
* Display the scene
*/
void display(void)
{
/* setup functions */
displayInit();
displayEye();
/* Draw Scene */
drawScene();
/* Flush, SwapBuffers, and sanity check */
glFlush();
glutSwapBuffers();
errCheck("display sanity check");
}
/*
* redisplayAll()
* ------
* This is called whenever we need to draw the display
*/
void redisplayAll(void)
{
displayProject(fov,asp,dim);
glutPostRedisplay();
}
/*
* drawScene()
* ------
* Draw the entire Scene
*/
void drawScene()
{
drawSkybox(3.5*dim);
}
/*
* drawSkybox(double D)
* ------
* Draws the skybox around the entire screen
*/
void drawSkybox(double D)
{
glColor3fv(white);
glEnable(GL_TEXTURE_2D);
/* Sides */
glBindTexture(GL_TEXTURE_2D,skybox[SKY_RIGHT]);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(-D,-D,-D);
glTexCoord2f(1,0); glVertex3f(+D,-D,-D);
glTexCoord2f(1,1); glVertex3f(+D,+D,-D);
glTexCoord2f(0,1); glVertex3f(-D,+D,-D);
glEnd();
glBindTexture(GL_TEXTURE_2D,skybox[SKY_FRONT]);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(+D,-D,-D);
glTexCoord2f(1,0); glVertex3f(+D,-D,+D);
glTexCoord2f(1,1); glVertex3f(+D,+D,+D);
glTexCoord2f(0,1); glVertex3f(+D,+D,-D);
glEnd();
glBindTexture(GL_TEXTURE_2D,skybox[SKY_LEFT]);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(+D,-D,+D);
glTexCoord2f(1,0); glVertex3f(-D,-D,+D);
glTexCoord2f(1,1); glVertex3f(-D,+D,+D);
glTexCoord2f(0,1); glVertex3f(+D,+D,+D);
glEnd();
glBindTexture(GL_TEXTURE_2D,skybox[SKY_BACK]);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(-D,-D,+D);
glTexCoord2f(1,0); glVertex3f(-D,-D,-D);
glTexCoord2f(1,1); glVertex3f(-D,+D,-D);
glTexCoord2f(0,1); glVertex3f(-D,+D,+D);
glEnd();
/* Top and Bottom */
glBindTexture(GL_TEXTURE_2D,skybox[SKY_UP]);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(-D,+D,-D);
glTexCoord2f(1,0); glVertex3f(+D,+D,-D);
glTexCoord2f(1,1); glVertex3f(+D,+D,+D);
glTexCoord2f(0,1); glVertex3f(-D,+D,+D);
glEnd();
glBindTexture(GL_TEXTURE_2D,skybox[SKY_DOWN]);
glBegin(GL_QUADS);
glTexCoord2f(1,1); glVertex3f(+D,-D,-D);
glTexCoord2f(0,1); glVertex3f(-D,-D,-D);
glTexCoord2f(0,0); glVertex3f(-D,-D,+D);
glTexCoord2f(1,0); glVertex3f(+D,-D,+D);
glEnd();
glDisable(GL_TEXTURE_2D);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment