summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2011-07-26 21:12:33 -0500
committerKamil Kaminski <kamilkss@gmail.com>2011-07-26 21:12:33 -0500
commit077c0e5688cd488fb94f6e3439ea2365f8ac30ca (patch)
tree1e825b2ae159f45f5454e5c9bd553587c81dc083
parent7c65bff1fbb2bef8a6dc0b0cc5fd5cd639a7973a (diff)
downloadsdl-077c0e5688cd488fb94f6e3439ea2365f8ac30ca.tar.gz
sdl-077c0e5688cd488fb94f6e3439ea2365f8ac30ca.tar.bz2
sdl-077c0e5688cd488fb94f6e3439ea2365f8ac30ca.zip
disable AA, enable AF, cleanup OpenGL setupHEADmaster
-rw-r--r--sdl.c172
1 files changed, 117 insertions, 55 deletions
diff --git a/sdl.c b/sdl.c
index 2b421b5..e875a6e 100644
--- a/sdl.c
+++ b/sdl.c
@@ -1,9 +1,11 @@
/* sdl.c
*
- * @2011 Kamil Kaminski
+ * Kamil Kaminski
+ * kkaminsk.com
*
* Skeleton for SDL
*
+ *
*/
#include <SDL/SDL.h>
@@ -16,6 +18,7 @@
#include <math.h>
/* function prototypes */
+static void gltErrorCheck(void);
static void resize(int, int);
static void setup_opengl(void);
static void keys(SDL_keysym *, unsigned int *);
@@ -25,10 +28,27 @@ static void render(void);
/* global */
int program_running = 1;
-/* few arrays, they could make into the header at some point */
-GLfloat fNoLight[] = { 0.0f, 0.0f, 0.0f, 0.0f };
-GLfloat fLowLight[] = { 0.25f, 0.25f, 0.25f, 1.0f };
-GLfloat fBrightLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+/* 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)
{
@@ -55,11 +75,6 @@ static void resize(int w, int h)
static void setup_opengl(void)
{
- /* light values and coordinates */
- GLfloat whiteLight[] = { 0.05f, 0.05f, 0.05f, 1.0f };
- GLfloat sourceLight[] = { 0.75f, 0.75f, 0.75f, 1.0f };
- GLfloat lightPos[] = { -10.f, 5.0f, 5.0f, 1.0f };
-
/* setup fog */
glEnable(GL_FOG);
glFogfv(GL_FOG_COLOR, fLowLight); /* set fog color to match background */
@@ -67,38 +82,62 @@ static void setup_opengl(void)
glFogf(GL_FOG_END, 20.0f);
glFogi(GL_FOG_MODE, GL_LINEAR); /* fog equation */
- glEnable(GL_DEPTH_TEST); /* hidden surface removal */
- glFrontFace(GL_CCW); /* counter clock-wise polygons face out */
- glEnable(GL_CULL_FACE); /* do not calculate inside of a pyramid */
+ 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 */
- /* ambient RGBA intensity of the entire scene */
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, whiteLight);
- glLightfv(GL_LIGHT0, GL_AMBIENT, sourceLight);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, fBrightLight);
+ glLightfv(GL_LIGHT0, GL_AMBIENT, fMedLight);
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, fShinyLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, fBrightLight);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
-
- /* enable lighting */
- glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
-
- /* enable color tracking */
- glEnable(GL_COLOR_MATERIAL);
-
- /* set Material properties to follow glColor values */
+
+ /* 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);
- glMateriali(GL_FRONT, GL_SHININESS, 128);
- /* turn on anti aliasing for points, lines, and polygons */
+ /* 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);
- glEnable(GL_BLEND);
- glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
- glEnable(GL_LINE_SMOOTH);
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);
}
@@ -107,8 +146,11 @@ static void keys(SDL_keysym *keysym, unsigned int *keys_held)
{
switch (keysym->sym)
{
- case SDLK_ESCAPE: program_running = 0; break;
- default: break;
+ case SDLK_ESCAPE:
+ program_running = 0;
+ break;
+ default:
+ break;
}
}
@@ -125,21 +167,30 @@ static void process_events(void)
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;
+ 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;
}
}
}
@@ -152,6 +203,10 @@ static void render(void)
/* 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;
@@ -168,6 +223,10 @@ static void render(void)
}
glEnd();
+ glDisable(GL_LINE_SMOOTH);
+ glDisable(GL_BLEND);
+ glEnable(GL_MULTISAMPLE);
+
/* restore the matrix state */
glPopMatrix();
@@ -181,7 +240,7 @@ int main(int argc, char **argv)
if (SDL_Init(SDL_INIT_VIDEO) < 0 )
{
- fprintf(stderr, "unable to init SDL: %s\n", SDL_GetError());
+ fprintf(stderr, "SDL: unable to init, %s\n", SDL_GetError());
exit(-1);
}
atexit(SDL_Quit);
@@ -191,12 +250,12 @@ int main(int argc, char **argv)
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_DEPTH_SIZE, 16);
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, "unable to set video mode: %s\n", SDL_GetError());
+ fprintf(stderr, "SDL: unable to set video mode: %s\n", SDL_GetError());
exit(-1);
}
@@ -213,20 +272,21 @@ int main(int argc, char **argv)
GLenum glewerr = glewInit();
if (GLEW_OK != glewerr)
{
- fprintf(stderr, "error: %s\n", glewGetErrorString(glewerr));
- return -1;
+ fprintf(stderr, "GLEW error: %s\n", glewGetErrorString(glewerr));
+ exit(-1);
}
else
- fprintf(stdout, "status: using GLEW %s\n", glewGetString(GLEW_VERSION));
-
+ 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, "version: OpenGL %d.%d\n", major, minor);
+ fprintf(stdout, "GLEW: initialized OpenGL %d.%d\n", major, minor);
setup_opengl();
+ gltErrorCheck();
while (program_running)
{
@@ -234,6 +294,8 @@ int main(int argc, char **argv)
render();
}
+ gltErrorCheck();
+ SDL_FreeSurface(screen);
puts("bye!");
return 0;