/* sdl.c * * Kamil Kaminski * kkaminsk.com * * Skeleton for SDL * * */ #include #include #include #include #include #include #include #include /* function prototypes */ static void gltErrorCheck(void); static void resize(int, int); static void setup_opengl(void); static void keys(SDL_keysym *, unsigned int *); static void process_events(void); static void render(void); /* global */ int program_running = 1; /* few light arrays */ const GLfloat fNoLight[] = { 0.0f, 0.0f, 0.0f, 1.0f }; const GLfloat fLowLight[] = { 0.25f, 0.25f, 0.25f, 1.0f }; const GLfloat fMedLight[] = { 0.50f, 0.50f, 0.50f, 1.0f }; const GLfloat fShinyLight[] = { 0.70f, 0.70f, 0.70f, 1.0f }; const GLfloat fBrightLight[] = { 1.0f, 1.0f, 1.0f, 1.0f }; /* light values and coordinates */ const GLfloat lightPos[] = { -10.f, 5.0f, 5.0f, 1.0f }; static void gltErrorCheck(void) { GLenum err_code; const GLubyte *err_str; while ((err_code = glGetError()) != GL_NO_ERROR) { err_str = gluErrorString(err_code); fprintf(stderr, "OpenGL error: %s\n", err_str); } } static void resize(int w, int h) { printf("window: resizing to %dx%d\n", w, h); GLfloat fAspect = (GLfloat) w / (GLfloat) h; if (h == 0) h = 1; glViewport(0, 0, w, h); /* reset coordinate system */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); /* produce the perspective projection */ /* void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar) */ gluPerspective(40.0f, fAspect, 1.0, 40.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* this needs to be ran again, glut does it for you I suppose */ SDL_SetVideoMode(w, h, 32, SDL_OPENGL | SDL_RESIZABLE); } static void setup_opengl(void) { /* setup fog */ glEnable(GL_FOG); glFogfv(GL_FOG_COLOR, fLowLight); /* set fog color to match background */ glFogf(GL_FOG_START, 4.0f); glFogf(GL_FOG_END, 20.0f); glFogi(GL_FOG_MODE, GL_LINEAR); /* fog equation */ glEnable(GL_DEPTH_TEST); /* hidden surface removal, aka Z-buffer */ glFrontFace(GL_CCW); /* counter clock-wise polygons face out */ glEnable(GL_CULL_FACE); /* do not calculate inside of a pyramid */ /* enable lighting, primitives now need to define color properties */ glEnable(GL_LIGHTING); /* global illumination, ambient RGBA intensity of the entire scene */ glLightModelfv(GL_LIGHT_MODEL_AMBIENT, fLowLight); /* setup and enable light 0 */ glLightfv(GL_LIGHT0, GL_AMBIENT, fMedLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, fShinyLight); glLightfv(GL_LIGHT0, GL_SPECULAR, fBrightLight); glLightfv(GL_LIGHT0, GL_POSITION, lightPos); glEnable(GL_LIGHT0); /* set material properties to follow all glColor values */ /* all primitives specified after the glMaterial call are affected by the * last values set, until another call to glMaterial is made */ glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); /* enable color tracking, should be called after glColorMaterial */ glEnable(GL_COLOR_MATERIAL); /* set globally, sets specular reflection to a white color, * this call follows all subsequent primitives */ glMaterialfv(GL_FRONT, GL_SPECULAR, fBrightLight); /* default (0), by increasing this value you reduce the size and increase * the focus of the specular highlight, causing a shiny spot to appear */ glMateriali(GL_FRONT, GL_SHININESS, 64); /* multisampling for polygons, conflicts with *polygon* anti-aliasing */ /* enabled by default, that's what man page says, hmmm! */ glEnable(GL_MULTISAMPLE); /* AA is disabled, but we will keep the blending function and hints though */ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); /* anisotropic filtering */ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 8); /* how OpenGL combines the colors from texels with the color of the underlying * geometry is controlled by the texture environment mode */ glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); /* once texture is loaded and enabled, it will applied to every primitive * that specifies coordinates, ORLY? */ glEnable(GL_TEXTURE_2D); /* specular highlights for textured items */ glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR); /* gray background */ glClearColor(0.5f, 0.5f, 0.5f, 1.0f); } static void keys(SDL_keysym *keysym, unsigned int *keys_held) { switch (keysym->sym) { case SDLK_ESCAPE: program_running = 0; break; default: break; } } /* process SDL events */ static void process_events(void) { SDL_Event event; unsigned static int keys_held[323]; SDLKey sym; while (SDL_PollEvent(&event)) { sym = event.key.keysym.sym; switch (event.type) { case SDL_KEYUP: { /* reset the key to 0 */ keys_held[sym] = 0; break; } case SDL_KEYDOWN: { keys_held[sym] = 1; keys(&event.key.keysym, keys_held); break; } case SDL_VIDEORESIZE: { resize(event.resize.w, event.resize.h); break; } case SDL_QUIT: { program_running = 0; break; } default: break; } } } static void render(void) { /* clear the window with current clearing color */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* save the matrix state and do the rotations */ glPushMatrix(); glDisable(GL_MULTISAMPLE); glEnable(GL_BLEND); glEnable(GL_LINE_SMOOTH); GLfloat fExtent = 20.0f; GLfloat fStep = 0.5f; GLfloat y = -0.4f; GLfloat iLine; glLineWidth(1.0f); glBegin(GL_LINES); for (iLine = -fExtent; iLine <= fExtent; iLine += fStep) { glVertex3f(iLine, y, fExtent); glVertex3f(iLine, y, -fExtent); glVertex3f(fExtent, y, iLine); glVertex3f(-fExtent, y, iLine); } glEnd(); glDisable(GL_LINE_SMOOTH); glDisable(GL_BLEND); glEnable(GL_MULTISAMPLE); /* restore the matrix state */ glPopMatrix(); /* buffer swap */ SDL_GL_SwapBuffers(); } int main(int argc, char **argv) { SDL_Surface *screen; if (SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "SDL: unable to init, %s\n", SDL_GetError()); exit(-1); } atexit(SDL_Quit); SDL_WM_SetCaption("SDL", NULL); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); if ((screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL | SDL_RESIZABLE)) == NULL) { fprintf(stderr, "SDL: unable to set video mode: %s\n", SDL_GetError()); exit(-1); } SDL_EnableUNICODE(1); /* SDL doesn't trigger off a ResizeEvent at startup, but as we need this * for OpenGL, we do this ourselves */ SDL_Event resizeEvent; resizeEvent.type = SDL_VIDEORESIZE; resizeEvent.resize.w = 640; resizeEvent.resize.h = 480; SDL_PushEvent(&resizeEvent); /* initalize glew */ GLenum glewerr = glewInit(); if (GLEW_OK != glewerr) { fprintf(stderr, "GLEW error: %s\n", glewGetErrorString(glewerr)); exit(-1); } else fprintf(stdout, "GLEW: using version %s\n", glewGetString(GLEW_VERSION)); /* display OpenGL version */ GLint major; GLint minor; glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MINOR_VERSION, &minor); fprintf(stdout, "GLEW: initialized OpenGL %d.%d\n", major, minor); setup_opengl(); gltErrorCheck(); while (program_running) { process_events(); render(); } gltErrorCheck(); SDL_FreeSurface(screen); puts("bye!"); return 0; }