Skip to content

Instantly share code, notes, and snippets.

@m0r13
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save m0r13/76325cadb115c4fc88c4 to your computer and use it in GitHub Desktop.

Select an option

Save m0r13/76325cadb115c4fc88c4 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glu.h>
bool fullscreen = false;
int windowX, windowY, windowWidth, windowHeight, cursor;
float angle = 0;
float color = 0;
bool colorMode = true;
void setup() {
glMatrixMode(GL_PROJECTION);
glClearColor(0, 0, 0, 0);
}
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-1.0, 1.0, -1.0*(GLfloat)h/(GLfloat)w,
1.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0);
else
glOrtho(-1.0*(GLfloat)w/(GLfloat)h,
1.0*(GLfloat)w/(GLfloat)h, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyPressed(unsigned char key, int x, int y) {
if (key == 'f' || (fullscreen && key == 27)) {
fullscreen = !fullscreen;
if (fullscreen) {
windowX = glutGet(GLUT_WINDOW_X);
windowY = glutGet(GLUT_WINDOW_Y);
windowWidth = glutGet(GLUT_WINDOW_WIDTH);
windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
glutFullScreen();
glutSetCursor(GLUT_CURSOR_NONE);
} else {
glutPositionWindow(windowX, windowY);
glutReshapeWindow(windowWidth, windowHeight);
glutSetCursor(GLUT_CURSOR_INHERIT);
}
}
}
void animate(int value) {
angle += 7;
if (colorMode)
color += 0.01;
else
color -= 0.01;
if (color > 1) {
colorMode = false;
} else if (color < 0) {
colorMode = true;
}
glutPostRedisplay();
glutTimerFunc(25, animate, 0);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRotatef(0.5 * angle, 0, 0, 1);
glRotatef(-0.2 * angle, 1, 1, 0);
glTranslatef(-0.5, 0, 0);
glRotatef(angle, 0, 0, 1);
glRotatef(-0.2 * angle, 1, 0, 0);
glRotatef(0.4 * angle, 0, 1, 0);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex2f(0, 0.5); // vertex 1
glColor3f(0, 1, 0);
glVertex2f(-0.5f, -0.25f); // vertex 2
glColor3f(0, 0, 1);
glVertex2f(0.5f, -0.25f); // vertex 3
glEnd();
glRotatef(-0.4 * angle, 0, 1, 0);
glRotatef(0.2 * angle, 1, 0, 0);
glRotatef(-angle, 0, 0, 1);
glTranslatef(0.5, 0, 0);
glTranslatef(0.5, 0, 0);
glRotatef(-angle, 0, 0, 1);
glColor3f(color, 1 - color, 0.5 + 0.5*color);
glutWireTeapot(0.25);
glRotatef(angle, 0, 0, 1);
glTranslatef(-0.5, 0, 0);
glLoadIdentity();
glutSwapBuffers();
}
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(800, 800);
glutCreateWindow("TeapotTriangle");
setup();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyPressed);
glutTimerFunc(1, animate, 0);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment