[CGSG] SVN Commit r424 - user/kintel/opengl/effects

kintel at metalab.at kintel at metalab.at
Sun Apr 20 05:41:27 CEST 2008


Author: kintel
Date: 2008-04-20 05:41:27 +0200 (Sun, 20 Apr 2008)
New Revision: 424

Added:
   user/kintel/opengl/effects/circles.cpp
Modified:
   user/kintel/opengl/effects/Makefile
Log:
Testing circles and blending

Modified: user/kintel/opengl/effects/Makefile
===================================================================
--- user/kintel/opengl/effects/Makefile	2008-04-20 03:20:12 UTC (rev 423)
+++ user/kintel/opengl/effects/Makefile	2008-04-20 03:41:27 UTC (rev 424)
@@ -23,3 +23,4 @@
 texturedisplacement:texturedisplacement.cpp Texture.cpp Grid.cpp
 moebius:moebius.cpp Texture.cpp Grid.cpp colors.cpp
 maxbill:maxbill.cpp colors.cpp
+circles:circles.cpp

Added: user/kintel/opengl/effects/circles.cpp
===================================================================
--- user/kintel/opengl/effects/circles.cpp	                        (rev 0)
+++ user/kintel/opengl/effects/circles.cpp	2008-04-20 03:41:27 UTC (rev 424)
@@ -0,0 +1,258 @@
+/*!
+  SDL OpenGL empty shell program including some typical utility functions.
+
+  FIXME:
+  o Resizing is buggy: Figure out SDL limitations
+*/
+
+#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};
+
+float timecounter = 0;
+void animate(unsigned int step)
+{
+  timecounter += 0.001*step;
+}
+
+void drawCircle(float r)
+{
+  glBegin(GL_QUADS);
+
+  int segments = 128;
+  float t = 0.04;
+  for (int i=0;i<segments;i++) {
+    float a = i*2*M_PI/segments;
+    float a2 = (i+1)%segments*2*M_PI/segments;
+    glColor3f(0.0f, 0.0f, 0.3f);
+    glVertex3f((r-t)*cos(a), (r-t)*sin(a), 0.0f);
+    glColor3f(0.2f, 0.7f, 1.0f);
+    glVertex3f(r*cos(a), r*sin(a), 0.0f);
+    glVertex3f(r*cos(a2), r*sin(a2), 0.0f);
+    glColor3f(0.0f, 0.0f, 0.3f);
+    glVertex3f((r-t)*cos(a2), (r-t)*sin(a2), 0.0f);
+
+    glColor3f(0.2f, 0.7f, 1.0f);
+    glVertex3f((r)*cos(a), (r)*sin(a), 0.0f);
+    glColor3f(0.0f, 0.0f, 0.3f);
+    glVertex3f((r+t)*cos(a), (r+t)*sin(a), 0.0f);
+    glVertex3f((r+t)*cos(a2), (r+t)*sin(a2), 0.0f);
+    glColor3f(0.2f, 0.7f, 1.0f);
+    glVertex3f((r)*cos(a2), (r)*sin(a2), 0.0f);
+  }
+
+  glEnd();
+}
+
+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]);
+
+  srandom(0);
+  glColor3f(1,0,0);
+  for (int i=0;i<100;i++) {
+    glPushMatrix();
+    glTranslatef(cos(timecounter+1.0*i/30)+ cos(timecounter*i/30),
+                 sin(timecounter+1.0*i/30)+ sin(timecounter*i/30),
+                  0);
+//     glTranslatef(2.5*(sin(0.3*timecounter+1.0*i/20)+sin(1.0*i/200+0.8)), 
+//                  1.0*sin(0.2*timecounter+0.2*i/20)+0.91*cos(1.0*i*200), 
+//                  0);
+ //    glTranslatef(sin(timecounter) + 0.3*sin(3*timecounter) + 0.4*sin(1.0*timecounter*i/50)+0.3*sin(1.0*i*timecounter*1.5/50+0.8), 
+//                  sin(2*timecounter+0.4) + 0.2*sin(1.5*timecounter + 0.1) + 0.5*sin(3.0*i*timecounter/50)+0.91*cos(1.0*i*timecounter/50), 
+//                   0);
+    //    drawCircle(1.0*(random()%20)/30);
+        drawCircle(0.2);
+        //     drawCircle(1.0-0.01*i);
+    glPopMatrix();
+  }
+}
+
+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.3f, 0.0f);
+
+  glClearDepth(1.0);
+//   glDepthFunc(GL_LESS);
+//   glEnable(GL_DEPTH_TEST);
+
+  glShadeModel(GL_SMOOTH);
+  glCullFace(GL_BACK);
+  glEnable(GL_CULL_FACE);
+  glFrontFace(GL_CCW);
+
+  glEnable(GL_BLEND);
+  //  glBlendEquation(GL_FUNC_BLEND);
+  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
+
+
+
+//   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