Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Bagpuss

Textures won't display properly

This topic is 5731 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 am having problem loading a bitmap in as a texture. No Matter what bitmap I load in, all I get drawn on the screen is a blue square the size of my bitmap (Blue as that is the last colour that was selected). Anyone got any ideas as to things I can check / change to make this work ? (I have tried numerous bitmaps, from mono to 24 bit colour, none work) Bitmap loading and associating to a texture code
  
AUX_RGBImageRec * CTileEngine::LoadBmp(char *filename)
{

	FILE *file = NULL;

  if (!filename)
    return NULL;

  file = fopen(filename, "r");

  if (file)
  {
    fclose(file);
    return auxDIBImageLoad(filename);
  }

  return NULL;

}


int CTileEngine::LoadGLTextures()
{
	
	AUX_RGBImageRec *TextureImage[2];

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

  TextureImage[0] = this->LoadBmp("Green1.bmp");
  TextureImage[1] = this->LoadBmp("Brown1.bmp");

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


  
  //Associate the Bound Texture to a 2D Texture

  glBindTexture(GL_TEXTURE_2D, texture[0]);
  //Set the Texture Parameters

  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  
  //Load in the Texture Data from a Loaded Bitmap

  glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,
               TextureImage[0]->sizeY, 0, GL_RGB,
               GL_UNSIGNED_BYTE, TextureImage[0]->data);

  glBindTexture(GL_TEXTURE_2D, texture[1]);
  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[1]->sizeX,
               TextureImage[1]->sizeY, 0, GL_RGB,
               GL_UNSIGNED_BYTE, TextureImage[1]->data);

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

      free(TextureImage[t]);
    }
  }

  return(1);
}



  
Bitmap Drawing Routine
  

void CTileEngine::RenderScene()
{

	::glPushMatrix();

	int tile;
	
	glTranslatef(10.0f,10.0f,2.0f);

	glColor3d(0.0f,0.0f,255.0f);

	int a=0,b=0;

	for (int y = 0; y< m_intMapY; y++)
	{
		for (int x = 0;x< m_intMapX; x++)
		{
			tile = map[y][x];
			a = y*64;
			b = x * 64;

			glBindTexture(GL_TEXTURE_2D, texture[tile]);
			glBegin(GL_QUADS);

			glTexCoord2f(0.0f, 0.0f); glVertex3f(float(b), float(a), 0.0f);
			glTexCoord2f(1.0f, 0.0f); glVertex3f(float(b + 64), float(a), 0.0f);
			glTexCoord2f(1.0f, 1.0f); glVertex3f(float(b + 64), float(a + 64), 0.0f);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(float(b), float(a + 64), 0.0f);
			glEnd();

	   }
	}

	glTranslatef(60.0f,60.0f,1.0f);

	glColor3d(255.0f,255.0f,0.0f);
	glBegin(GL_LINES);
		glVertex2f(0.0f,0.0f);
		glVertex2f(50.0f,50.0f);
 	glEnd();

	::glPopMatrix();
}

  
And just in case, Palette loading routine
  
BOOL CTileEngine::SetupPixelFormat(
	PIXELFORMATDESCRIPTOR* pPFD)
{
	// default pixel format for a single-buffered,

	// OpenGL-supporting, hardware-accelerated, 

	// RGBA-mode format. Pass in a pointer to a different

	// pixel format if you want something else

    PIXELFORMATDESCRIPTOR pfd = 
        {
        sizeof(PIXELFORMATDESCRIPTOR),// size of this pfd

        1,                      // version number

        PFD_DRAW_TO_WINDOW |    // support window

          PFD_SUPPORT_OPENGL,   // support OpenGL

        PFD_TYPE_RGBA,          // RGBA type

        24,                     // 24-bit color depth

        0, 0, 0, 0, 0, 0,       // color bits ignored

        0,                      // no alpha buffer

        0,                      // shift bit ignored

        0,                      // no accumulation buffer

        0, 0, 0, 0,             // accum bits ignored

        16,                     // 16-bit z-buffer

        0,                      // no stencil buffer

        0,                      // no auxiliary buffer

        PFD_MAIN_PLANE,         // main layer

        0,                      // reserved

        0, 0, 0                 // layer masks ignored

        };

    int pixelformat;

	PIXELFORMATDESCRIPTOR* pPFDtoUse;
	
	// let the user override the default pixel format

	pPFDtoUse = (0 == pPFD)? &pfd : pPFD; 
 
    if ( 0 == (pixelformat = 
        ::ChoosePixelFormat( m_pDC->GetSafeHdc(), pPFDtoUse )) )
        {
		::AfxMessageBox("ChoosePixelFormat failed.");
		return FALSE;
        }

    if ( FALSE == ::SetPixelFormat( m_pDC->GetSafeHdc(),
        pixelformat, pPFDtoUse ) )
        {
		::AfxMessageBox("SetPixelFormat failed.");
        return FALSE;
        }

    return TRUE;
}



  
Whilst I am here, I wish my bitmaps to be transparent on the black colour, so I take it that somewhere I need to set up a variable for which colour is transparent ? tia, Bp.

Share this post


Link to post
Share on other sites
Advertisement
Do you load your texture after you have created the rendering context? Is the size of of the valid (must be power of two)?

As for a transparent color, you can allocate a new image with space for alpha channel, copy the original image to the new image, and set alpha to either 1 or 0 depending on the color of the pixel and what color you want to be transparent. Then use alpha test to reject transparent pixels.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!