[CGSG] SVN Commit r421 - user/kintel/opengl/effects
kintel at metalab.at
kintel at metalab.at
Sun Apr 20 04:42:44 CEST 2008
Author: kintel
Date: 2008-04-20 04:42:43 +0200 (Sun, 20 Apr 2008)
New Revision: 421
Added:
user/kintel/opengl/effects/maxbill.cpp
Log:
Max Bill art recreation
Added: user/kintel/opengl/effects/maxbill.cpp
===================================================================
--- user/kintel/opengl/effects/maxbill.cpp (rev 0)
+++ user/kintel/opengl/effects/maxbill.cpp 2008-04-20 02:42:43 UTC (rev 421)
@@ -0,0 +1,319 @@
+#include <math.h>
+#include <stdio.h>
+
+// Common OpenGL includes
+#include <GL/glew.h>
+#ifdef __APPLE__
+ #include <OpenGL/gl.h>
+ #include <OpenGL/glu.h>
+#else
+ #include <GL/gl.h>
+ #include <GL/glu.h>
+#endif
+#include <SDL.h>
+
+// Shader utils
+#include "shader.h"
+
+// Camera control:
+#include "camera.h"
+GLfloat rotation[4] = {0.0f, 0.0f, 0.0f, 0.0f};
+
+// Colors
+#include "colors.h"
+
+float timecounter = 0;
+void animate(unsigned int step)
+{
+ timecounter += 0.001*step;
+}
+
+void drawCube()
+{
+ glBegin(GL_QUADS);
+ // front
+ glNormal3f(0, 0, 1);
+ glVertex3f(-1, -1, 1);
+ glVertex3f(1, -1, 1);
+ glVertex3f(1, 1, 1);
+ glVertex3f(-1, 1, 1);
+
+ // back
+ glNormal3f(0, 0, -1);
+ glVertex3f(-1, -1, -1);
+ glVertex3f(-1, 1, -1);
+ glVertex3f( 1, 1, -1);
+ glVertex3f( 1, -1, -1);
+
+ // left
+ glNormal3f(-1, 0, 0);
+ glVertex3f(-1, -1, 1);
+ glVertex3f(-1, 1, 1);
+ glVertex3f(-1, 1, -1);
+ glVertex3f(-1, -1, -1);
+
+ // right
+ glNormal3f(1, 0, 0);
+ glVertex3f(1, -1, 1);
+ glVertex3f(1, -1, -1);
+ glVertex3f(1, 1, -1);
+ glVertex3f(1, 1, 1);
+
+ // top
+ glNormal3f(0, 1, 0);
+ glVertex3f(-1, 1, 1);
+ glVertex3f(1, 1, 1);
+ glVertex3f(1, 1, -1);
+ glVertex3f(-1, 1, -1);
+
+ // bottom
+ glNormal3f(0, -1, 0);
+ glVertex3f(1, -1, 1);
+ glVertex3f(-1, -1, 1);
+ glVertex3f(-1, -1, -1);
+ glVertex3f(1, -1, -1);
+
+ glEnd();
+}
+
+void drawNGon(int n, float r)
+{
+ glBegin(GL_POLYGON);
+ float a = 0;
+ for (int i=0;i<n;i++) {
+ float x = r*cos(a);
+ float y = r*sin(a);
+ a += 2*M_PI/n;
+ glVertex3f(x, y, 0.0f);
+ }
+ glEnd();
+}
+
+typedef float Vert[2];
+
+void display(int width, int height)
+{
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+
+ glTranslatef(0, 0, -5.0);
+ glRotatef(rotation[0], rotation[1], rotation[2], rotation[3]);
+
+ bool first = true;
+ float l = 1.0f;
+ Vert c = {0.0f, 0.0f};
+ Vert *verts = new Vert[2*20];
+ Vert norm;
+ for (int n=10;n>=3;n--) {
+ float r,g,b;
+ float hue = 1.0f*(n%6)/5 + 0.6;
+ hue = hue - floor(hue);
+ hsv2rgb(hue, 0.8f, 1.0f, r, g, b);
+ glColor3f(r, g, b);
+
+ float beta = M_PI-2*M_PI/n;
+ float alpha = beta/2;
+ float rc = l/(2*cos(alpha));
+ float ri = rc*sin(alpha);
+
+ if (!first) {
+ Vert edgevec = {verts[1][0] - verts[0][0], verts[1][1] - verts[0][1]};
+ Vert m = {0.5f*(verts[0][0] + verts[1][0]),
+ 0.5f*(verts[0][1] + verts[1][1])};
+ norm[0] = edgevec[1];
+ norm[1] = -edgevec[0];
+ c[0] = m[0] - norm[0]*ri/sqrt(norm[0]*norm[0]+norm[1]*norm[1]);
+ c[1] = m[1] - norm[1]*ri/sqrt(norm[0]*norm[0]+norm[1]*norm[1]);
+ }
+
+ float starta = atan2(norm[1], norm[0]) + (M_PI-2*alpha)/2;
+
+// printf("%d: b = %.2f, rc = %.2f, ri = %.2f\n", n, beta, rc, ri);
+// printf(" n = %.2f, %.2f, c = %.2f, %.2f\n", norm[0], norm[1], c[0], c[1]);
+// printf(" starta = %.2f\n", starta);
+
+
+ glLineWidth(2.0f);
+ glBegin(GL_LINE_LOOP);
+ // glBegin(GL_POLYGON);
+ float a = 0;
+ for (int i=0;i<n;i++) {
+ verts[i][0] = c[0] + rc*cos(starta+a);
+ verts[i][1] = c[1] + rc*sin(starta+a);
+ a += 2*M_PI/n;
+ glVertex3f(verts[i][0], verts[i][1], 0.0f);
+ }
+ glEnd();
+
+
+// glPointSize(2.0f);
+// glBegin(GL_POINTS);
+// glVertex3f(c[0], c[1], 0.0f);
+// glEnd();
+
+
+ if (first) first = false;
+ }
+}
+
+void resizeGL(int w, int h)
+{
+ // SDL_SetVideoMode(w, h, 16, SDL_OPENGL | SDL_RESIZABLE);
+ glViewport(0, 0, w, h);
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ gluPerspective(45.0f, 1.0f*w/h, 0.1f, 1000.0f);
+}
+
+void initGL(int w, int h)
+{
+ glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
+ glClearDepth(1.0);
+ glDepthFunc(GL_LEQUAL);
+ // glEnable(GL_DEPTH_TEST);
+
+ glShadeModel(GL_SMOOTH);
+ glCullFace(GL_BACK);
+ glEnable(GL_CULL_FACE);
+ glFrontFace(GL_CCW);
+
+// glEnable(GL_LIGHTING);
+// glEnable(GL_LIGHT0);
+// GLfloat lightpos[] = { 10.0, 10.0, 10.0, 1.0 };
+// glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
+// GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
+// glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
+// GLfloat mat_ambient[] = { 0.0, 0.3, 0.3, 1.0 };
+// glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
+// GLfloat mat_diffuse[] = { 0.3, 1.0, 0.8, 1.0 };
+// glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
+// glMaterialf(GL_FRONT, GL_SHININESS, 30.0);
+
+ resizeGL(w,h);
+}
+
+void printUsage(char *name)
+{
+ fprintf(stderr, "Usage: %s [options]\n\n", name);
+ fprintf(stderr, "Options:\n");
+ fprintf(stderr, " -f Run in fullscreen mode\n");
+ fprintf(stderr, " -m <samples=4> Enable multisample\n");
+ exit(1);
+}
+
+int main(int argc, char ** argv)
+{
+ bool fullscreen = false;
+ int multisample = 0;
+ for (int i=1;i<argc;i++) {
+ if (!strcmp(argv[i], "-f")) {
+ fullscreen = true;
+ }
+ else if (!strcmp(argv[i], "-m")) {
+ if (i==argc-1 || argv[i+1][0] == '-') {
+ multisample = 4;
+ }
+ else if (i<argc-1) {
+ multisample = atol(argv[++i]);
+ if (multisample < 0) {
+ fprintf(stderr, "%s: Illegal value option: '%s %s'\n\n",
+ argv[0], argv[i-1], argv[i]);
+ printUsage(argv[0]);
+ }
+ }
+ }
+ else if (argv[i][0] == '-') {
+ printUsage(argv[0]);
+ }
+ else {
+ printUsage(argv[0]);
+ }
+ }
+
+ if (SDL_Init(SDL_INIT_VIDEO) < 0) {
+ fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
+ return -1;
+ }
+
+ if (multisample > 0) {
+ SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
+ SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, multisample);
+ }
+
+ int width = 512, height = 512;
+ Uint32 flags = SDL_OPENGL;
+ if (fullscreen) {
+ const SDL_VideoInfo *info = SDL_GetVideoInfo();
+ width = info->current_w;
+ height = info->current_h;
+ flags |= SDL_FULLSCREEN;
+ }
+ // else flags |= SDL_RESIZABLE;
+ SDL_Surface *surface;
+ if (!(surface = SDL_SetVideoMode(width, height, 0, flags))) {
+ fprintf(stderr, "Unable to set SDL video mode: %s\n", SDL_GetError());
+ SDL_Quit();
+ return -1;
+ }
+ if ((surface->flags & flags) != flags) {
+ fprintf(stderr, "Warning: Flags couldn't be set: %x\n", (surface->flags&flags)^flags);
+ }
+
+ SDL_WM_SetCaption("OpenGL demo", NULL);
+
+ glewInit();
+ initGL(width, height);
+ if (multisample > 0) glEnable(GL_MULTISAMPLE);
+
+ unsigned int now, prev = SDL_GetTicks();
+ bool done = false;
+ while (!done) {
+ display(width, height);
+ SDL_Event event;
+ while (SDL_PollEvent(&event)) {
+ switch (event.type) {
+ case SDL_QUIT:
+ done = true;
+ break;
+// case SDL_VIDEORESIZE:
+// width = event.resize.w;
+// height = event.resize.h;
+// resizeGL(width, height);
+// break;
+ case SDL_MOUSEBUTTONDOWN:
+ if (event.button.button == 1) {
+ trackballInit(event.button.x, height-event.button.y, width, height);
+ }
+ break;
+ case SDL_MOUSEMOTION:
+ if (event.motion.state & SDL_BUTTON(1)) {
+ trackballRotate(event.motion.x, height-event.motion.y, rotation);
+ }
+ break;
+ case SDL_KEYDOWN:
+ switch (event.key.keysym.sym) {
+ case SDLK_ESCAPE:
+ done = true;
+ break;
+ }
+ break;
+ case SDL_KEYUP:
+ switch (event.key.keysym.sym) {
+ }
+ break;
+ }
+ }
+ now = SDL_GetTicks();
+ animate(now-prev);
+ prev = now;
+
+ SDL_GL_SwapBuffers();
+ }
+
+ SDL_Quit();
+
+ return 0;
+}
More information about the CGSG
mailing list