Not so much “gotchas” as “got-me”s, probably. It’s been so long since I’ve done any significant OpenGL coding that I keep forgetting little things that result in a blank screen or a white texture :) Anyway, I thought I’d record them here to remind myself.
Note that I’m currently using OpenGL for 2D drawing.
-
Set
glTexParameter
forGL_TEXTURE_MIN_FILTER
to something likeGL_LINEAR
before callingglTexture2D
. The default value forGL_TEXTURE_MIN_FILTER
isGL_NEAREST_MIPMAP_LINEAR
, and if you’re not using mipmaps, the texture won’t draw! -
I forgot that
glOrtho
manipulates the current matrix. Which means that if you don’t callglLoadIdentity
beforehand you might not be getting the projection matrix you were hoping for! - This one was particularly boneheaded :P Make sure that the type parameter in
glTexCoordPointer
is actually the same type as the array! As in, if you pass in a pointer to an array of floats, make sure that the type parameter is set toGL_FLOAT
. I blame this one on copy/pasting without paying enough attention.
Well that’s enough embarrassment for today. And I also just found this article: Avoiding 16 Common OpenGL Pitfalls, which looks useful…
EDIT: Just needed to mention another oversight that had me scratching my head for a while when things weren’t rendering as expected. Note the difference between the GL constants GL_TEXTURE
and GL_TEXTURE_2D
! Use the former for the parameter to glMatrixMode
. Use the latter for enabling/disable texturing with glEnable/glDisable
!