diff options
author | Kyle K <kylek389@gmail.com> | 2011-07-13 21:45:55 -0500 |
---|---|---|
committer | Kamil Kaminski <kamilkss@gmail.com> | 2011-07-13 21:45:55 -0500 |
commit | f2190d3118dc1c58891793c934ae224d3d355e3c (patch) | |
tree | 5f8ca3d197d3b3f478a4139b59ed284a50de9a4f /sdl | |
parent | 6ad248c86e45933f9c61a7ce9f0546429d41c97e (diff) | |
download | GLPyramid-f2190d3118dc1c58891793c934ae224d3d355e3c.tar.gz GLPyramid-f2190d3118dc1c58891793c934ae224d3d355e3c.tar.bz2 GLPyramid-f2190d3118dc1c58891793c934ae224d3d355e3c.zip |
sdl: tweak opengl state
Diffstat (limited to 'sdl')
-rw-r--r-- | sdl/GL_notes.txt | 6 | ||||
-rw-r--r-- | sdl/gldraw.c | 8 | ||||
-rw-r--r-- | sdl/platform.c | 81 | ||||
-rw-r--r-- | sdl/platform.h | 14 | ||||
-rw-r--r-- | sdl/pyramid.c | 26 |
5 files changed, 92 insertions, 43 deletions
diff --git a/sdl/GL_notes.txt b/sdl/GL_notes.txt index 23795e9..ac51035 100644 --- a/sdl/GL_notes.txt +++ b/sdl/GL_notes.txt @@ -2,6 +2,9 @@ Performance - Depth Testing, aka Z-buffer - Culling: don't render what you can't see +Ideas +- Use scissor test or bind texture for HUD display? + Terms - Winding: by default, primitives that follow in CCW fashion are front facing @@ -36,7 +39,8 @@ Terms - Dithering: simulate displaying wider range of colors on on systems with small amount of colors - Texture Filtering: process of calculating color fragments from a stretched or - shrunken texture map + shrunken texture map + - Fragments: color values placed in the color buffer OBJ Loader - glGenTextures() diff --git a/sdl/gldraw.c b/sdl/gldraw.c index ed455e0..5430009 100644 --- a/sdl/gldraw.c +++ b/sdl/gldraw.c @@ -188,6 +188,10 @@ void glDrawTriangle(void) void glDrawGround(void) { + glDisable(GL_MULTISAMPLE); + glEnable(GL_BLEND); + glEnable(GL_LINE_SMOOTH); + GLfloat fExtent = 20.0f; GLfloat fStep = 0.5f; GLfloat y = -0.4f; @@ -203,6 +207,10 @@ void glDrawGround(void) glVertex3f(-fExtent, y, iLine); } glEnd(); + + glDisable(GL_LINE_SMOOTH); + glDisable(GL_BLEND); + glEnable(GL_MULTISAMPLE); } void glDrawUnitAxes(void) diff --git a/sdl/platform.c b/sdl/platform.c index 270a938..677b5b8 100644 --- a/sdl/platform.c +++ b/sdl/platform.c @@ -2,6 +2,7 @@ * * Platform * + * Notes: Assumes multisampling is always enabled * */ @@ -11,15 +12,14 @@ #include "platform.h" /* few light arrays */ -GLfloat fNoLight[] = { 0.0f, 0.0f, 0.0f, 0.0f }; -GLfloat fLowLight[] = { 0.25f, 0.25f, 0.25f, 1.0f }; -GLfloat fMedLight[] = { 0.50f, 0.50f, 0.50f, 1.0f }; -GLfloat fBrightLight[] = { 1.0f, 1.0f, 1.0f, 1.0f }; +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 */ -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 }; +const GLfloat lightPos[] = { -10.f, 5.0f, 5.0f, 1.0f }; /* variables that should be already defined and declared for us by main program */ extern const unsigned int xres_w; @@ -38,47 +38,74 @@ 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 */ + 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 */ + /* 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, fMedLight); + 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); - /* 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); + + if (glIsEnabled(GL_MULTISAMPLE) == GL_FALSE) + { + fprintf(stderr, "OpenGL error: multisampling cannot be enabled!\n"); + exit(-1); + } + + /* turn off AA forever, it enables blending globally, so depth test goes out + * of the window, blended objects should be rendered in back to front order, + * also, following lines are ignored by OpenGL if multisampling is enabled */ +#if 0 + /* turn on anti aliasing for points and lines */ 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); +#endif - /* screws up snowman, this has been replaced by superior multisampling */ - //glEnable(GL_POLYGON_SMOOTH); - //glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); + /* 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); - /* multisampling for polygons, conflicts with *polygon* anti-aliasing */ - glEnable(GL_MULTISAMPLE); + /* XXX: anisotropic filtering */ + +#if 0 + /* screws up snowman, this has been replaced by superior multisampling */ + glEnable(GL_POLYGON_SMOOTH); + glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); +#endif /* how OpenGL combines the colors from texels with the color of the underlying * geometry is controlled by the texture environment mode */ @@ -88,8 +115,11 @@ void setup_opengl(void) * that specifies coordinates, ORLY? */ glEnable(GL_TEXTURE_2D); - /* don't know about this, but why not */ + /* specular highlights for textured items */ glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR); + /* glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_COLOR_SINGLE); */ + /* only if lighting is disabled */ + /* glEnable(GL_COLOR_SUM); */ /* gray background */ glClearColor(0.5f, 0.5f, 0.5f, 1.0f); @@ -127,7 +157,7 @@ SDL_Surface *setup_sdl_video(int w, int h, int bpp, unsigned int flags) return srfc; } -void setup_sdl(void) +SDL_Surface *setup_sdl(void) { SDL_Surface *screen; const SDL_VideoInfo* info = NULL; @@ -194,6 +224,7 @@ void setup_sdl(void) SDL_PushEvent(&resizeEvent); /* save the surf pointer in platform */ + return screen; } void setup_glew(void) @@ -213,6 +244,6 @@ void setup_glew(void) GLint minor; glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MINOR_VERSION, &minor); - fprintf(stdout, "GLEW: detected OpenGL %d.%d\n", major, minor); + fprintf(stdout, "GLEW: initialized OpenGL %d.%d\n", major, minor); } diff --git a/sdl/platform.h b/sdl/platform.h index 8be6052..09028d1 100644 --- a/sdl/platform.h +++ b/sdl/platform.h @@ -1,18 +1,16 @@ #ifndef _PLATFORM_H_ #define _PLATFORM_H_ -extern GLfloat fNoLight[]; -extern GLfloat fLowLight[]; -extern GLfloat fBrightLight[]; - -extern GLfloat whiteLight[]; -extern GLfloat sourceLight[]; -extern GLfloat lightPos[]; +extern const GLfloat fNoLight[]; +extern const GLfloat fLowLight[]; +extern const GLfloat fShinyLight[]; +extern const GLfloat fBrightLight[]; +extern const GLfloat lightPos[]; /* function prototypes */ void setup_opengl(void); SDL_Surface *setup_sdl_video(int, int, int, unsigned int); -void setup_sdl(void); +SDL_Surface *setup_sdl(void); void setup_glew(void); #endif diff --git a/sdl/pyramid.c b/sdl/pyramid.c index 4fb2b6a..42c7a22 100644 --- a/sdl/pyramid.c +++ b/sdl/pyramid.c @@ -61,6 +61,9 @@ static struct GLfloat xrot; GLfloat yrot; + /* SDL surface, our screen */ + SDL_Surface *screen; + SDL_TimerID timer_id; SDL_Cursor *my_cursor; } p; @@ -173,20 +176,20 @@ static void platform_init(void) p.ground_list = glGenLists(2); p.triangle_list = p.ground_list + 1; - /* a ground with grass drawn using magneta lines */ - glNewList(p.ground_list, GL_COMPILE); - //glBindTexture(GL_TEXTURE_2D, p.textures[1]); - glColor3ub(255, 0, 255); - glDrawGround(); - glEndList(); - /* a triangle with a texture */ glNewList(p.triangle_list, GL_COMPILE); glBindTexture(GL_TEXTURE_2D, p.textures[0]); + /* glBindTexture(GL_TEXTURE_2D, p.textures[1]); */ glColor3f(1.0f, 1.0f, 1.0f); glDrawTriangle(); glEndList(); + /* a ground drawn using magneta lines */ + glNewList(p.ground_list, GL_COMPILE); + glColor3ub(255, 0, 255); + glDrawGround(); + glEndList(); + /* add a simple timer / callback function */ p.timer_id = SDL_AddTimer(5000, sdlTimerCallback, &(p.camera)); @@ -199,6 +202,8 @@ static void platform_destroy(void) { glDeleteTextures(2, p.textures); glDeleteLists(p.ground_list, 2); + + SDL_FreeSurface(p.screen); } static void render(void) @@ -210,7 +215,6 @@ static void render(void) glPushMatrix(); /* apply camera transform, and draw the ground */ glframe_apply_camera_transform(&p.camera, 1); - glCallList(p.ground_list); glPushMatrix(); @@ -225,7 +229,10 @@ static void render(void) /* draw a snowman */ glTranslatef(0.0f, 0.0f, -7.0f); + + glDisable(GL_TEXTURE_2D); glDrawSnowman(); + glEnable(GL_TEXTURE_2D); /* restore the matrix state */ glPopMatrix(); @@ -236,11 +243,12 @@ static void render(void) int main(int argc, char **argv) { - setup_sdl(); + p.screen = setup_sdl(); setup_glew(); setup_opengl(); platform_init(); gltErrorCheck(); + gltOpenGLInfo(); unsigned int startclock; while (program_running) |