Jump to content
  • Advertisement
Sign in to follow this  
Omelas0469

OpenGL Texture Mapping Issue

This topic is 3690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've created a map for a pacman game I'm making in C++ using 8 bmps as tiles. I'm basically trying to texture map each bmp to a quad to make a 2D map as explained here: http://www.gamedev.net/reference/articles/article1256.asp When I run my code, all I see is a white square (20x20) so I'm guessing the texture mapping isn't working... Here's the code:
#include <windows.h>	
#include <gl/gl.h>		    
#include <gl/glu.h>			
#include <gl/glaux.h> // For texture mapping	
#include <GL/glut.h>

#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>

const int MAP_WIDTH = 20;
const int MAP_HEIGHT = 20;

GLuint texture[9];

int pacmap[MAP_WIDTH][MAP_HEIGHT] = {
	{ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6}, 
	{ 6, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 4, 4, 4, 4, 4, 4, 4, 4, 6}, 
	{ 6, 4, 6, 6, 6, 4, 6, 6, 4, 6, 6, 4, 6, 6, 4, 6, 6, 6, 4, 6}, 
	{ 6, 4, 4, 4, 4, 4, 6, 6, 4, 4, 4, 4, 6, 6, 4, 4, 4, 4, 4, 6}, 
	{ 6, 4, 6, 6, 6, 4, 6, 4, 4, 6, 6, 4, 4, 6, 4, 6, 6, 6, 4, 6}, 
	{ 6, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 4, 4, 4, 4, 4, 4, 4, 6}, 
	{ 6, 6, 6, 6, 4, 6, 6, 4, 6, 6, 6, 6, 4, 6, 6, 4, 6, 6, 6, 6}, 
	{ 6, 6, 6, 6, 4, 6, 4, 4, 4, 4, 4, 4, 4, 4, 6, 4, 6, 6, 6, 6}, 
	{ 6, 6, 6, 6, 4, 6, 4, 6, 6, 6, 3, 6, 6, 4, 6, 4, 6, 6, 6, 6}, 
	{ 4, 4, 4, 4, 4, 4, 4, 6, 6, 3, 3, 3, 6, 4, 4, 4, 4, 4, 4, 4}, 
	{ 6, 6, 6, 6, 4, 6, 4, 6, 6, 6, 6, 6, 6, 4, 6, 4, 6, 6, 6, 6}, 
	{ 6, 6, 6, 6, 4, 6, 4, 4, 4, 4, 4, 4, 4, 4, 6, 4, 6, 6, 6, 6}, 
	{ 6, 6, 6, 6, 4, 4, 4, 6, 6, 6, 4, 6, 6, 4, 4, 4, 6, 6, 6, 6}, 
	{ 6, 4, 4, 4, 4, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 4, 4, 4, 4, 6}, 
	{ 6, 4, 6, 6, 4, 4, 4, 6, 6, 6, 4, 6, 6, 6, 4, 4, 6, 6, 4, 6}, 
	{ 6, 4, 6, 6, 6, 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 4, 6}, 
	{ 6, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 4, 6, 6, 4, 4, 4, 4, 4, 6}, 
	{ 6, 4, 6, 6, 6, 6, 4, 6, 6, 6, 6, 4, 6, 6, 4, 6, 6, 6, 4, 6}, 
	{ 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6}, 
	{ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6}
};

AUX_RGBImageRec *LoadBMP(char *Filename)			
{
	FILE *File=NULL;

	if (!Filename)return NULL;						
	File=fopen(Filename,"r");						
	if (File)										
	{
		fclose(File);								
		return auxDIBImageLoad(Filename);			
	}
	return NULL;									
}

int load_gl_textures()
{
  AUX_RGBImageRec *TextureImage[9];

  memset(TextureImage,0,sizeof(void *)*1);

  TextureImage[0] = LoadBMP("000.bmp");
  TextureImage[1] = LoadBMP("001.bmp");
  TextureImage[2] = LoadBMP("002.bmp");
  TextureImage[3] = LoadBMP("003.bmp");
  TextureImage[4] = LoadBMP("004.bmp");
  TextureImage[5] = LoadBMP("005.bmp");
  TextureImage[6] = LoadBMP("006.bmp");
  TextureImage[7] = LoadBMP("007.bmp");
  TextureImage[8] = LoadBMP("008.bmp");

  glGenTextures(9, &texture[0]); // We are generating 9 textures

  for(int i = 0; i < 9; ++i)
  {
	glBindTexture(GL_TEXTURE_2D, texture);
	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, 3, TextureImage->sizeX,
					TextureImage->sizeY, 0, GL_RGB,
					GL_UNSIGNED_BYTE, TextureImage->data);
  }

  for (int t = 0; t < 9; t++)
  {
    if (TextureImage[t])
    {
      if (TextureImage[t]->data)
      {
        free(TextureImage[t]->data);
      }

      free(TextureImage[t]);
    }
  }

  return(1);
}

void draw_tiles()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glClearColor(0.09, 0.9, 1.0, 1.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity(); //set current matrix to identity

  glPushMatrix(); 
  //glRotatef(180.0, 1.0, 0.0, 0.0);
  int tile;
  for (int y = 0; y < MAP_HEIGHT; y++)
  {
    for (int x = 0; x < MAP_WIDTH; x++)
    {
		tile = pacmap[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();
    }
  }
  glPopMatrix(); 
  glFlush();
}


int main(int argc, char **argv) {  // called when application starts
  glutInit(&argc, argv);            // initialize GLUT
  glutInitDisplayMode(GLUT_RGBA);   // set RGBA display mode
  glutInitWindowPosition(100, 100); // self explaining
  glutInitWindowSize(600, 600);
  glutCreateWindow("Pacman");    // create the window "OpenGL Transformations"
  glutDisplayFunc(draw_tiles);     // set our rendering function
  glutMainLoop();               // process events and wait until exit
  return 0;
}

Of course, "000.bmp"-"008.bmp" are all located in the same directory as the file you see above... Any ideas? [Edited by - Omelas0469 on November 3, 2008 2:31:57 AM]

Share this post


Link to post
Share on other sites
Advertisement
I think that texturing isn't enabled: I'm missing a
glEnable(GL_TEXTURE_2D);
invocation somewhere. See e.g. here for a description.


BTW: OP, please wrap such big code snippets not in [ code ] but in [ source ] tags. Thanks.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!