GPWiki.org
GPWiki.org
It is currently Mon May 20, 2013 4:51 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Thu Oct 04, 2012 6:04 pm 
Rookie

Joined: Thu Oct 04, 2012 5:45 pm
Posts: 3
I have recently started a project with SDL involving 2d graphics. I am fairly new to graphics programming, but I feel that I have a good grip on the subject. I was wondering if anyone could answer a few questions for me though:

first of all, I have a recurring problem with loading texture. I have my texture load function set up like this:

Code:
GLuint loadTexture (const std::string&fileName) //Load texture function
{
   SDL_Surface *image = IMG_Load(fileName.c_str() );

   SDL_DisplayFormatAlpha(image);

   unsigned object(0);

   glGenTextures(1, &object);

   glBindTexture(GL_TEXTURE_2D, object); //Bind to an object

   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

   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_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);

   SDL_FreeSurface(image);

   return object;
}


I followed a tutorial to build this function, but I do not know what most of it does. If anyone could explain it to me in detail I would appreciate it. My error, however, involves execution of my program as a whole. Every time I call this function, I get an access violation error if the dimensions of my .PNG file are not a power of 2, and are not 128 pixels or more for each dimension. This is a problem, because my objects that I am binding the textures to are not above 128 pixels each, and I always have a horrible skewing effect. I tried resizing the glTexCoord for my object, but the skewing remains. Does anyone know a reliable and efficient workaround or fix for this?

My next issue is with collision. I have a very shoddy collision detection function that I want to replace with something better and more comprehensive, but I don't know where to start. If anyone could direct me to a good tutorial on collision that would be all I would need for now.

Finally, I was wondering if anyone could show me how to move the camera during movement, like a side-scrolling function, but from a top down perspective. I have no idea where to start in that regard, and a detailed example would be wonderful.

If anyone has the time to answer at least one my questions, I would appreciate it greatly.

Jake.


Top
 Profile  
 
PostPosted: Thu Oct 04, 2012 7:20 pm 
Bibliotherapist
User avatar

Joined: Wed Nov 03, 2004 1:28 pm
Posts: 6704
Location: Lincoln, Englandshire
Hi SixString,

OpenGL textures must be in powers of two, not really sure why, probably some old optimisation lost in the mists of time. You can use an extension to allow non-power of two image sizes, but that's overcomplicating things at this stage. I'm not sure why you're having issues below 128px, haven't come across that before.

Which bits of the code are you struggling with?

The glGenTexture call allocates a unique 'handle' that the texture will be attached to.

The GL_TEXTURE_MIN/MAG_FILTER parameters set the filtering mode for smoothing the texture when it is rendered bigger or smaller than the original size.GL_LINEAR produces nicer results, but is slower than GL_NEAREST.

The GL_TEXTURE_WRAP parameter set the behaviour of the texture mapping when you go past the edge of the texure. IIRC, CLAMP_TO_EDGE stops the texture wrapping around back to the start.

The glTexImage call actually does the work of producing a texture from the image data and attaching it to the handle.

As far as mapping goes, it's kind of hard to explain without resorting to drawings, but think of 0.0f, 0.0f as the bottom left of the image and 1.0f, 1.0f as the top right. If you only want to map two thirds of the image onto a quad, you'd use 0.0f,0.0f - 0.66f, 0.66f as the texture coords.

If that doesn't make sense, post back and I'll try to make it clearer.

_________________
10 PRINT "Bad Monkey ";
20 GOTO 10


Top
 Profile  
 
PostPosted: Thu Oct 04, 2012 8:12 pm 
Double Guru
User avatar

Joined: Fri Aug 12, 2005 8:58 am
Posts: 2009
Location: LA, CA
Collision detection is a huge subject. If you are making a top down game/side scroller then you can probably get away with rectangles or sphere for collisions. When an object moves check it again all other collision shapes and see if it collides, if it collides you want to remove the movement. That is very a basic model to start with. To make it more playable you will want to move the object as close as possible without colliding, only check collisions around the player instead of everything, allow partial movement such as jumping forward against a wall will let you move upwards but not forwards, support slopes, etc. You might just want to use a physics library such as bullet, chipmunk physics or box2d.

Cameras are another big topic. A simple camera would center on the player so when the player moves the camera moves as well. Just google "2d camera".

_________________
My Development Blog | My Website | My Current 3d Engine


Top
 Profile  
 
PostPosted: Fri Oct 05, 2012 10:52 pm 
Rookie

Joined: Thu Oct 04, 2012 5:45 pm
Posts: 3
Ah, well I assumed i was delving into broad topics when I asked those other two questions, but I understand what you mean. I also got my images to start looking a little better with my TexCoords. My next issue is with my texture layers. I want my textures to be rendered in the right order, but I can't really find a way to do this. I know its probably simple, but I can't get anything to work. Do you guys know how to do this?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group