My tilemap is of an incorporeal nature...

Started by
5 comments, last by Benjamin Heath 19 years, 1 month ago
... because I can't see it. ;) I've been working on an OpenGL tiling demo this afternoon. I've loaded my texture images and I have the map in a good ol' 10x10 array. Everything works up to that point, but my drawing function just isn't drawing anything. Here is the function:
extern void
demo_tile_update (void)
{
  int tile;
  int x, y;

  for (y = 0; y < MAP_SIZEY; ++y)
    {
      for (x = 0; x < MAP_SIZEX; ++x)
	{
	  tile = map[y][x];
	  glBindTexture (GL_TEXTURE_2D, texture[tile]);
	  glBegin (GL_QUADS);
	  glTexCoord2f (0.0f, 0.0f);
	  glVertex3f ((float) x, (float) y, 0.0f);
	  glTexCoord2f (1.0f, 0.0f);
	  glVertex3f ((float) (x + 1), (float) (y), 0.0f);
	  glTexCoord2f (1.0f, 1.0f);
	  glVertex3f ((float) (x + 1), (float) (y + 1), 0.0f);
	  glTexCoord2f (0.0f, 1.0f);
	  glVertex3f ((float) (x), (float) (y + 1), 0.0f);
	  glEnd ();
	}
    }
}
I have a sneaking suspicion this has to do with coordinates. I should tell you that my images are 64x48 (that's 640/10 x 480/10), and that there obviously some globals in use here ('texture' and 'map'). This is also taken almost directly from the "Tiling in OpenGL" tutorial. What am I doing wrong here? Should I include the sizes of these images somewhere or what? All I see is a blank screen. As always, all help is appreciated. Take care, Ben PS: This function is also isolated into its own module along with an initialize and shutdown function, which are all called by another module that clears the screen after calling demo_tile_update(). I don't think there is anything in the architecture that should cause a problem here, but weirder things have happened indeed. [Edited by - Benjamin Heath on March 17, 2005 12:33:17 AM]
Advertisement
Quote:Original post by Benjamin Heath
What am I doing wrong here? Should I include the sizes of these images somewhere or what? All I see is a blank screen.


Try adding: glTranslate3f( 0, 0, -5 ); at the beginning of the function. I know I did something like this before and couldn't see anything because my camera wasn't looking [wink]
Okay, this is strange. First, thank you Drew because that's probably the problem (hehe). But now that I've added glTranslate3f, the linker yells at me that this is an implicit declaration and an undefined reference. It just didn't do that before with all those other gl* functions.

Basically there's a library or header not getting linked in somewhere, which is weird. Here's how I have it set up (I hate writing trivialities): This is written with Dev-C++. In the project settings, I'm linking with -lopengl32 and -lglu32. I'm also using SDL, and at the top of this very source file, I'm including "SDL.h" and "SDL_opengl.h". For all I know, this should work, but...
The proper function is glTranslatef. Though it comes in float and double varieties, glTranslate does not come in anything but a three parameter form.

Also; if your textures are 64*48, when you finally do see them, they will probably be solid white. 48 is not a power of two; OpenGL requires all textures to have power-of-two dimensions.

You might want to consider using Orthographic projection for this. Based on your choice of image dimensions (640/10,...) this is what you intended anyway. All you must do is replace your call to glPerspective with glOrthographic. You can then also drop the z value from your vertex coordinates (glVertex2f) and it should solve your camera problem too.
Quote:Original post by Deyja
The proper function is glTranslatef. Though it comes in float and double varieties, glTranslate does not come in anything but a three parameter form.


[embarrass] Thanks for that. I've been using OpenAL so much recently - most of the functions you can specify the parameter list if it takes 3 [lol]. Sorry for that Ben!
Thanks guys. There's still something wrong going on here, and I need to get a better understanding of GL to find out what it is. So instead of breaking my neck over this code, I think I'm just going to study Chapter 8 of the Red Book and see where that takes me.

Thanks again. [smile]
Okay, here's the demo module in its entirety. (Sorry for the lack of comments here. I assure you the other modules I have are well-commented.)

#include "demo_tile.h"#include "SDL.h"#include "SDL_image.h"#include "SDL_opengl.h"#define MAP_SIZEX 10#define MAP_SIZEY 10static GLuint texture[2] = { 0, 0 };static char map[MAP_SIZEY][MAP_SIZEX] = {  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},  {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},  {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},  {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},  {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},  {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},  {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},  {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},  {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},};static Booleanload_gl_texture (const char *filename, U32 texid){  SDL_Surface *temp = NULL;  SDL_Surface *bmp = NULL;  bmp = IMG_Load (filename);  if (bmp == NULL)    {      return FALSE;    }  temp = SDL_DisplayFormat (bmp);  SDL_FreeSurface (bmp);  SDL_LockSurface (temp);  glBindTexture (GL_TEXTURE_2D, texture[texid]);  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);  glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, temp->w, temp->h, 0, GL_RGB,		GL_UNSIGNED_BYTE, temp->pixels);  SDL_UnlockSurface (temp);  SDL_FreeSurface (temp);  return TRUE;}extern Booleandemo_tile_initialize (void){  glGenTextures (2, texture);  if (load_gl_texture ("tile0.bmp", 0) == FALSE)    {      return FALSE;    }  if (load_gl_texture ("tile1.bmp", 1) == FALSE)    {      return FALSE;    }  glTranslatef (0.0f, 0.0f, -5.0f);  return TRUE;}extern voiddemo_tile_shutdown (void){  glDeleteTextures (2, texture);}extern voiddemo_tile_update (void){  int tile;  int x, y;  for (y = 0; y < MAP_SIZEY; ++y)    {      for (x = 0; x < MAP_SIZEX; ++x)	{	  tile = map[y][x];	  glBindTexture (GL_TEXTURE_2D, texture[tile]);	  glBegin (GL_QUADS);	  glTexCoord2f (0.0f, 0.0f);	  glVertex2f ((float) (x), (float) (y));	  glTexCoord2f (1.0f, 0.0f);	  glVertex2f ((float) (x + 1), (float) (y));	  glTexCoord2f (1.0f, 1.0f);	  glVertex2f ((float) (x + 1), (float) (y + 1));	  glTexCoord2f (0.0f, 1.0f);	  glVertex2f ((float) (x), (float) (y + 1));	  glEnd ();	}    }}


SDL is initialized and OpenGL is set up in a higher module, which calls demo_tile_update() within its own update function, followed by SDL_GL_SwapBuffers().

I think the problem here lies in the static function load_gl_texture(). I don't think it's using the images properly after it loads them, which leads me to a question of pixel formatting, etc.

Also, on the subject of orthographic projection, how should I do that exactly? The Red Book (version 1.4) mentions only glOrtho() and gluOrtho2D(), but I don't understand how to use them. Some TLC in this area, please pretty please?

Thanks for the help so far. Anymore help is greatly appreciated.

This topic is closed to new replies.

Advertisement