Skip to content

Instantly share code, notes, and snippets.

@adonaldson
Created February 14, 2010 01:08
Show Gist options
  • Save adonaldson/303773 to your computer and use it in GitHub Desktop.
Save adonaldson/303773 to your computer and use it in GitHub Desktop.
// This is bloody ugly, but it works! Hooray!
// One of the early NEHE tutorials, compiled using:
// gcc -framework GLUT -framework OpenGL triangle_square_colour_rotate.c -o triangle_square_colour_rotate -Wall
#include <stdio.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h> /* glut.h includes gl.h and glu.h*/
#endif
GLvoid InitGL(GLvoid);
GLvoid DrawGLScene(GLvoid);
GLvoid ReSizeGLScene(int width, int height);
GLvoid DoIdle(GLvoid);
GLfloat triangle_angle = 0.0f;
GLfloat quad_angle = 0.0f;
GLvoid ReSizeGLScene(int width, int height)
{
if (height == 0) {
height = 1;
}
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
GLvoid ProcessKeys(unsigned char key, int x, int y) {
if (key == 'q')
exit(0);
if (key == 'f')
glutPostRedisplay();
}
GLvoid DoIdle(GLvoid) {
glutPostRedisplay();
}
GLvoid InitGL(GLvoid)
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
GLvoid DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f, 0.0f, -6.0f);
glRotatef(triangle_angle, 0.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f,-1.0f, 0.0f);
glEnd();
glLoadIdentity();
glTranslatef(1.5f, 0.0f, -6.0f);
glRotatef(quad_angle, 1.0f, 0.0f, 0.0f);
glColor3f(0.5f, 0.5f, 1.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
triangle_angle += 0.2f;
quad_angle -= 0.15f;
glFlush();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutInitWindowPosition(200, 200);
glutCreateWindow(argv[0]);
glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);
glutKeyboardFunc(ProcessKeys);
glutIdleFunc(DoIdle);
InitGL();
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment