OpenGL: Difference between revisions

From LD Smith Games Workshop
Jump to navigation Jump to search
Line 110: Line 110:
Got the transparent PNG image to display correctly.  I changed the "format" parameter in the glTeximage2D (noted above) to "GL_RBGA".  However, the transparent pixels show up as white instead of the background color.
Got the transparent PNG image to display correctly.  I changed the "format" parameter in the glTeximage2D (noted above) to "GL_RBGA".  However, the transparent pixels show up as white instead of the background color.


[[File:Opengl tux05.jpg|400px]]
[[File:Opengl tux06.jpg|400px]]
 
The image now displays correctly with transparency!  I had to change the "internalformat" parameter for glTeximage2D from "3" to "GL_RBGA".
 
[[File:Opengl tux07.jpg|400px]]
 
Now I just need to write a standard method for generating textures, and I think I will be ready to integrate the code into the main game code.

Revision as of 05:38, 14 February 2010

I am attempting to integrate OpenGL into the code, so that the game uses hardware graphics acceleration. Hopefully, this will improve some of the framerate issues.

This will also allow me to use Fraps to make videos of the game to post to YouTube.

Below, I have documented the process I went through to enable OpenGL, including all of the issues I came across.

Get code to compile with a single line of OpenGL

I can compile a program using #include "SDL_opengl.h", but I am getting errors about missing functions when an OpenGL method is called.


I installed the OpenGL libraries in cygwin, so I'm not sure if those were necessary:

Opengl cygwin.jpg


Installing these files may help

http://www.libsdl.org/extras/win32/cygwin/


Found out that I was using the wrong parameters to compile. I should have been using this:

gcc test.c `sdl-config --libs --cflags` -lglut32 -lglu32 -lopengl32

Display a bitmap to the screen

I got a simple square BMP to display in OpenGL:

Opengl square.jpg

Added Tux BMP:

Opengl tux.jpg

Tux on an image background:

One of the vertices was off, which made the background skewed.

Opengl tuxbkg.jpg

Moving Block

Next Step is to get the player block moving

Opengl tux02.jpg

Pseudo Code:

 INTEGER playerX
 INTEGER playerY
 INTEGER playerVelocityX
 INTEGER playerVelocityY
 
 Setup_SDL
 Setup_OpenGL
 Load_Bitmaps
 
 GameLoop {
 
   While (User hasn't Quit) {
     IF (User presses up) THEN
       playerVelocityY = - PLAYER_SPEED
 
     IF (User presses down) THEN
       playerVelocityY = PLAYER_SPEED
 
     IF (User presses left) THEN
       playerVelocityX = - PLAYER_SPEED
 
     IF (User presses right) THEN
       playerVelocityX =  PLAYER_SPEED
 
     playerX = playerX + playerVelocityX
     playerY = playerY + playerVelocityY
 
     Render_Screen
   }
 }


Change BMP to PNG

Next step is to replace the BMP with a PNG

Some "gotchas"

  • Add "IMG_Init(IMG_INIT_PNG);" to the program's startup code
  • Replace "SDL_LoadBMP" with "IMG_Load"
  • Add -lSDL_image on the compile line (Makefile or go script)
  • Add required DLLs to the working directory (libpng12-0.dll, zlib1.dll, SDL_image.dll, etc)
  • If the PNG will not display, try passing a BMP to IMG_Load to see if it works

Got SDL and OpenGL to accept the PNG image with transparency, but there are display issues:

Opengl tux03.jpg

PNG with transparency disabled, and background color saved. Basic image is correct, but some discoloration.

Opengl tux04.jpg

Got the non-transparent PNG to display correctly. The problem was with the glTeximage2D line. The "format" parameter was set to "GL_BGR" when it should have been "GL_RBG".

Apparently, changing the format parameter to GL_RBG (from GL_BGR) will make BMPs appear discolored (cyan for yellow).

Opengl tux05.jpg

Another interesting note I found was that all textures MUST be a power of 2 (2, 4, 8, 16, 32, 64, 128, 256, ...) in order to work with OpenGL.


Got the transparent PNG image to display correctly. I changed the "format" parameter in the glTeximage2D (noted above) to "GL_RBGA". However, the transparent pixels show up as white instead of the background color.

Opengl tux06.jpg

The image now displays correctly with transparency! I had to change the "internalformat" parameter for glTeximage2D from "3" to "GL_RBGA".

Opengl tux07.jpg

Now I just need to write a standard method for generating textures, and I think I will be ready to integrate the code into the main game code.