Skip to content

Instantly share code, notes, and snippets.

@changemewtf
Created April 17, 2014 16:21
Show Gist options
  • Save changemewtf/10995685 to your computer and use it in GitHub Desktop.
Save changemewtf/10995685 to your computer and use it in GitHub Desktop.
#include "GLUT/glut.h"
#include "stdlib.h"
#define ESC 27
#define BLACK 0.0f, 0.0f, 0.0f, 0.0f
#define GREEN 0.0f, 1.0f, 0.0f
#define ORANGE 1.0f, 0.5f, 0.0f
#define RED 1.0f, 0.0f, 0.0f
#define YELLOW 1.0f, 1.0f, 0.0f
#define BLUE 0.0f, 0.0f, 1.0f
#define VIOLET 1.0f, 0.0f, 1.0f
int window;
float rotation = 0.0f;
void InitGL(int width, int height) {
glClearColor(BLACK);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
/* The main drawing function. */
void DrawGLScene() {
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f, 0.0f, -6.0f);
glLoadIdentity(); // make sure we're no longer rotated.
glTranslatef(1.5f, 0.0f, -7.0f);
glRotatef(rotation, 1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
// top
glColor3f(BLUE);
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
// bottom
glColor3f(ORANGE);
glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
// front
glColor3f(RED);
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
// back
glColor3f(YELLOW);
glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back)
glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back)
glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back)
glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back)
// left
glColor3f(GREEN);
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
// right
glColor3f(VIOLET);
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
glEnd();
rotation += 0.5f;
glutSwapBuffers();
}
void keyPressed(unsigned char key, int x, int y) {
if (key == ESC) {
glutDestroyWindow(window);
exit(0);
}
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutInitWindowPosition(300, 300);
window = glutCreateWindow("Cube");
glutDisplayFunc(&DrawGLScene);
glutIdleFunc(&DrawGLScene);
glutKeyboardFunc(&keyPressed);
InitGL(640, 480);
glutMainLoop();
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment