[CGSG] SVN Commit r425 - user/meta/gfx
meta at metalab.at
meta at metalab.at
Sun Apr 20 05:44:53 CEST 2008
Author: meta
Date: 2008-04-20 05:44:52 +0200 (Sun, 20 Apr 2008)
New Revision: 425
Added:
user/meta/gfx/space.cpp
Log:
virtual space
Added: user/meta/gfx/space.cpp
===================================================================
--- user/meta/gfx/space.cpp (rev 0)
+++ user/meta/gfx/space.cpp 2008-04-20 03:44:52 UTC (rev 425)
@@ -0,0 +1,225 @@
+/*!
+ 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"
+#include "Grid.h"
+
+
+Grid *g;
+GLfloat rotation[4] = {0.0f, 0.0f, 0.0f, 0.0f};
+
+float timecounter = 0;
+void animate(unsigned int step)
+{
+ timecounter += 0.001*step;
+}
+
+void display(int width, int height)
+{
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+
+ glColor4f(0.05,0.2,0.9, 0.4);
+
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+
+ glTranslatef(0, 0, -5.0);
+ glRotatef(rotation[0], rotation[1], rotation[2], rotation[3]);
+
+ glPushMatrix();
+ glTranslatef(0., 3. , 0.);
+ glScalef(100,100,100);
+ glRotatef(90, 1., 0., 0.);
+ g->draw();
+ glPopMatrix();
+
+
+ glColor4f(0.9,0.05,0.2, 0.4);
+
+ glPushMatrix();
+ glTranslatef(0., -3. , 0.);
+ glScalef(100,100,100);
+ glRotatef(90, 1., 0., 0.);
+ g->draw();
+ 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.0f, 0.0f);
+
+ //glClearDepth(1.0);
+ //glDepthFunc(GL_LESS);
+ //glEnable(GL_DEPTH_TEST);
+
+ glPolygonMode(GL_FRONT, GL_LINE);
+ glPolygonMode(GL_BACK, GL_LINE);
+
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE);
+
+ glShadeModel(GL_SMOOTH);
+
+ glLineWidth(1.5);
+
+ g = new Grid(false, false);
+ g->setup(200,200);
+
+ 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