Last active
December 20, 2015 10:26
-
-
Save icebreaker/d1a3d744cd15cdeee660 to your computer and use it in GitHub Desktop.
(Open)GL Sans Triangles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| (Open)GL Sans Triangles. | |
| Mihail Szabolcs, Public Domain. | |
| Make: | |
| gcc notris.c -lglut -lGL -lGLU -funroll-loops -O3 -o notris | |
| Run: | |
| ./notris | |
| */ | |
| #include <GL/glut.h> | |
| #define WIDTH 800 | |
| #define HEIGHT 600 | |
| #define UNUSED(x) (void)(x) | |
| typedef int rect[4]; | |
| typedef float rgba[4]; | |
| static rect viewport = {0, }; | |
| void draw_rect(rect pos, rgba color) | |
| { | |
| glPushAttrib(GL_COLOR_BUFFER_BIT | GL_SCISSOR_BIT); | |
| glScissor(pos[0], viewport[3] - pos[1] - pos[3], pos[2], pos[3]); // invert Y | |
| glClearColor(color[0], color[1], color[2], color[3]); | |
| glClear(GL_COLOR_BUFFER_BIT); | |
| glPopAttrib(); | |
| } | |
| void draw(void) | |
| { | |
| int i, j; | |
| rect pos = { 0, 0, 100, 100 }; | |
| rgba color = { 0.0f, }; | |
| glClear(GL_COLOR_BUFFER_BIT); | |
| for(i=0; i<666; i++) | |
| { | |
| for(j=0; j<4; j++) | |
| { | |
| int c = rand() % 255; | |
| color[j] = c / 255.0f; | |
| } | |
| pos[0] = rand() % viewport[2]; | |
| pos[1] = rand() % viewport[3]; | |
| draw_rect(pos, color); | |
| } | |
| glutSwapBuffers(); | |
| } | |
| void resize(int w, int h) | |
| { | |
| glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
| glEnable(GL_SCISSOR_TEST); | |
| glViewport(0, 0, w, h); | |
| viewport[2] = w; | |
| viewport[3] = h; | |
| glutPostRedisplay(); | |
| } | |
| void keyboard(unsigned char key, int x, int y) | |
| { | |
| UNUSED(key); | |
| UNUSED(x); | |
| UNUSED(y); | |
| exit(0); | |
| } | |
| int main(int argc, char *argv[]) | |
| { | |
| srand(13666); | |
| glutInit(&argc, argv); | |
| glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); | |
| glutInitWindowSize(WIDTH, HEIGHT); | |
| glutCreateWindow("GL Sans Triangles"); | |
| glutDisplayFunc(draw); | |
| glutReshapeFunc(resize); | |
| glutIdleFunc(glutPostRedisplay); | |
| glutKeyboardFunc(keyboard); | |
| glutMainLoop(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
