3 Replies - 386 Views - Last Post: 11 February 2014 - 12:46 AM Rate Topic: -----

#1 LunaRebirth  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 125
  • Joined: 21-March 13

OpenGL Cube, SDL Sprite. Texturing. C++

Posted 08 February 2014 - 10:32 PM

Sorry for the title, didn't quite know how to word this, lol.

What I'm trying to do is..
I've got a 432x384 image with a bunch of 16x16 sprites for tiles (Like an editor).
So the first 16x16 sprite is grass and the next is a wall, so on and so-fourth.

I've got my OpenGL cube that has texturing added, so I can add images to the cube.
Currently I have a 16x16 image of just the grass, then another 16x16 image of just the wall, in which I can place on all 6 sides of a cube.

Rather than having 2 separate images, one grass one wall, I want to have a big image of all kinds of sprites.

Here's my current code..
int wall;
int grass;
int loadTexture(std::string fileName) {
	SDL_Surface *image=IMG_Load(fileName.c_str());
	
	SDL_Rect rect;
    rect.x = 15;
    rect.y = 15;
    SDL_BlitSurface(image,NULL,screen,&rect);
    
	SDL_DisplayFormatAlpha(image);
	unsigned object(0);
	glGenTextures(1,&object);
	glBindTexture(GL_TEXTURE_2D,object);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,image->w,image->h,0,GL_RGB,GL_UNSIGNED_BYTE,image->pixels);
	SDL_FreeSurface(image);
	return object;
}

void drawCube(float size, float OBJ, float x, float y, float z)
{
	if (OBJ == 0) {
		glBindTexture(GL_TEXTURE_2D,wall); //The 16x16 wall image
	} else if (OBJ == 1) {
		glBindTexture(GL_TEXTURE_2D,grass); //The 16x16 grass image
	}
	glBegin(GL_QUADS);
		// front face
		glTexCoord2f(1,1); glVertex3f(size/2,size/2,size/2);
		glTexCoord2f(0,1); glVertex3f(-size/2,size/2,size/2);
		glTexCoord2f(0,0); glVertex3f(-size/2,-size/2,size/2);
		glTexCoord2f(1,0); glVertex3f(size/2,-size/2,size/2);
		// left face
//etc...

This works! But only for the 16x16 image.
I cannot use my giant image for 16x16 sprites from the image.
Here's what I've TRIED to change to get working.
I failed, and got a really strange mixture of pixels from the 432x384 image.
int fullImage; //432x384 image I was talking about
int loadTexture(std::string fileName) {
	SDL_Surface *image=IMG_Load(fileName.c_str());
	
	SDL_Rect rect;
    rect.x = 15;
    rect.y = 15;
    SDL_BlitSurface(image,NULL,screen,&rect);
    
	SDL_DisplayFormatAlpha(image);
	unsigned object(0);
	glGenTextures(1,&object);
	glBindTexture(GL_TEXTURE_2D,object);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,16,16,0,GL_RGB,GL_UNSIGNED_BYTE,image->pixels);
     //Changed above, image->w and image->h is now 16. 
     //Figured by changing this, It would give the 16x16 sprites to set cube texture to.
     //Without doing this, I still get strange results.
	SDL_FreeSurface(image);
	return object;
}

void drawCube(float size, float OBJ, float x, float y, float z)
{
        glBindTexture(GL_TEXTURE_2D,fullImage); //The 432x384 image
	glBegin(GL_QUADS);
		// front face
		glTexCoord2f(1.0/432.0,1.0/432.0); glVertex3f(size/2,size/2,size/2);
		glTexCoord2f(0,1.0/432.0); glVertex3f(-size/2,size/2,size/2);
		glTexCoord2f(0,0); glVertex3f(-size/2,-size/2,size/2);
		glTexCoord2f(1.0/432.0,0); glVertex3f(size/2,-size/2,size/2);
             //I thought that changing 1 (Which is the whole image)
             //To part of the image, it would work.
             //In this case, I should be getting 0x0 from the image
             //with a 16x16 width and height.
             //Does not work
		// left face
//etc...


Sorry for the long notes in there, Just wanted to make sure I didn't leave out any details on my issue.

Thanks in advance!

EDIT:
Loading the texture 'fullImage' to the cube by doing:
int fullImage;

fullImage = loadTexture("FilePath");

Yes, this works.

This post has been edited by LunaRebirth: 08 February 2014 - 10:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: OpenGL Cube, SDL Sprite. Texturing. C++

#2 LunaRebirth  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: OpenGL Cube, SDL Sprite. Texturing. C++

Posted 09 February 2014 - 08:24 PM

Sorry, bump :(
Still unsolved and can't find anything online.
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1307
  • View blog
  • Posts: 4,496
  • Joined: 19-February 09

Re: OpenGL Cube, SDL Sprite. Texturing. C++

Posted 10 February 2014 - 11:29 PM

The large images are often called sprite sheets or tile sheets. Did you include the width and height in the rect.

Clip Rendering and Sprite Sheets
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 762
  • View blog
  • Posts: 2,218
  • Joined: 20-March 10

Re: OpenGL Cube, SDL Sprite. Texturing. C++

Posted 11 February 2014 - 12:46 AM

You have added and alpha channel with

SDL_DisplayFormatAlpha(image);

but you are using GL_RGB instead of GL_RGBA also,

you are saying the 432 by 384 image is 16 by 16 not sure that will work.

better to get an sdl_image from that 432 by 384 map that is 16 by 16 and

pass that to glTexImage2D.

Regards

Snoopy.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1