• Advertisement
Sign in to follow this  

Trying to draw area from image fails.

This topic is 707 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

Hey Community,

 

I am trying to draw an area of a texture.

Drawing a texture on the normal way works fine with:

glTexCoord2f(0.0, 1.0); glVertex2f(_at.x, _at.y + dim.h);
glTexCoord2f(0.0, 0.0); glVertex2f(_at.x, _at.y);
glTexCoord2f(1.0, 0.0); glVertex2f(_at.x + dim.w, _at.y);
glTexCoord2f(1.0, 1.0); glVertex2f(_at.x + dim.w, _at.y + dim.h);

Now the area drawing code which fails:  (width should not change, only the height orientation within the parent texture)

int n = dim.h / _odim.h;
int t = 3;

float topborder    = (1 / n)	  * (t - 1);
float bottomborder = 1 - ((1 / n) * (n - t));


glTexCoord2f(0.0, bottomborder); glVertex2f(_at.x, _at.y + _odim.h);  // bottom left
glTexCoord2f(0.0, topborder); glVertex2f(_at.x, _at.y); // top left 
glTexCoord2f(1.0, topborder); glVertex2f(_at.x + _odim.w, _at.y); // top right
glTexCoord2f(1.0, bottomborder); glVertex2f(_at.x + _odim.w, _at.y + _odim.h); // bottom right	

dim is the parent texture, means the "big" one,  _odim is the area.

 

I divided the height of the original by the height of the region to get the number n which represents the ammount of the squares inside the main

texture.   (important to understand : http://imgur.com/Ln1Blzh    )

Then I randomly chose 3 as the the 3'rd square inside of the texture I want see. (Numerated first 7 in the picture for debug purpose)

 

Based on the glTexCoord2f  which reaches from 0.0f to 1.0f  I tought that I need to    divide 1 by the ammount of squares possible for selection.

 

-> Topborder results out of the selected square - 1   times  1/n  

-> Bottomborder is   1 minus  1/n times  the ammount of squares minus the chosen one

 

And the  glVertex2f  are by common sized on the _odim.

 

 

Well then, what am I doing wrong to get this result:

 

http://img.prntscr.com/img?url=http://i.imgur.com/JgNC7e5.png

 

I realy expected something like this:

http://img.prntscr.com/img?url=http://i.imgur.com/KFYG3br.png

 

 

I hope to find an answer here, since I realy dont know. 

 

,greetings

Edited by GAFO

Share this post


Link to post
Share on other sites
Advertisement
Integer division: 1 / 3 is 0.

Consider using float literals wherever you expect to have a non-integer result:


float topborder    = (1.f / n)	  * (t - 1);
float bottomborder = 1 - ((1.f / n) * (n - t));

Share this post


Link to post
Share on other sites

float topborder    = (1 / n)	  * (t - 1);
float bottomborder = 1 - ((1 / n) * (n - t));

-> Topborder results out of the selected square - 1   times  1/n  

-> Bottomborder is   1 minus  1/n times  the ammount of squares minus the chosen one

 

Let's think about this really simply.

We'll pretend you have a texture that is 8 units tall (I'm using "units" because I don't want to bring pixels into this). Each image you want to display is 1 unit big. There's no width - we only care about height.

If I want to draw image #1, the top is 0/8, and the bottom is 1/8. The next pair is 1/8 and 2/8. The next is 2/8 and 3/8. The next is 3/8 and 4/8. Do you see a pattern here?

(1/n) * (t - 1) ... looks ok
(t - 1) / n ... that looks a little better

1 - (1/n) * (n - t) ... oh dear, let's see what we can do to check this.
(n/n) - (1/n) * (n - t)
(n/n) - (n - t) / n
(n/n)  + (t - n) / n
(n + t - n) / n
t / n ... that checks out, too.

I guess it's just the integer math stuff mentioned above. You still might want to try to keep the math simple, if possible.

Edit: Note, it's common to save a reciprocal like (1/n) to a variable, and multiply by that instead of dividing by n. However, I don't write the math that way on paper (or type it like that into a forum).

Edited by Pink Horror

Share this post


Link to post
Share on other sites
int raw_n = dim.h / _odim.h;
int raw_t = 2;

float n = (float)raw_n;
float t = (float)raw_t;
	
float topborder = (t - 1.f) / n;
float bottomborder = t / n;

glTexCoord2f(0.0, bottomborder); glVertex2f(_at.x, _at.y + _odim.h);  // bottom left
glTexCoord2f(0.0, topborder); glVertex2f(_at.x, _at.y); // top left 
glTexCoord2f(1.0, topborder); glVertex2f(_at.x + _odim.w, _at.y); // top right
glTexCoord2f(1.0, bottomborder); glVertex2f(_at.x + _odim.w, _at.y + _odim.h); // bottom right	

Thats the working result. Gues I was thinking way to complicated but well yea it works now.

Thank you guys :)

Share this post


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

  • Advertisement