Jump to content

  • Log In with Google      Sign In   
  • Create Account

Problems with storing fractions in variables


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 The Magical Pot   Members   -  Reputation: 178

Like
0Likes
Like

Posted 10 May 2013 - 12:13 PM

Hello!

 

I'm using OpenGL to render things in my game, but I'm haveing some problems. OpenGL uses decimals/fractions between 0 and 1 to locate pixels in a given texture. (for example, if I want to locate the 8th pixel on the x-axis in an image that's 32 pixels wide, I would type 'x=8/32' instead of 'x=8')

The problem is that I can't put fractions into float variables, (which would be convenient in my code) I can only use decimals. I can't write 'float x = 8/32' because the result of 8/32 isn't 0.25, but when I write 'float x = 0.25' it works perfectly.

 

This is a snippet that demonstrates how I render:

 

 

glBegin( GL_QUADS );
       glTexCoord2f( 0.f, 0.f );    glVertex2f(    0.f,   0.f );
       glTexCoord2f( 8/32, 0.f );    glVertex2f( 8.f,   0.f );
       glTexCoord2f( 8/32, 8/32 ); glVertex2f( 8.f, 8.f );
       glTexCoord2f( 0.f, 8/32 ); glVertex2f( 0.f, 8.f );
glEnd();

 

I just wanted to ask this question before I start a suboptimal solution. Am I missing something or do I have to work around it? Am I using the wrong type of variables to store fractions? Is it the 'glTexCoord2f' function that's the problem?

 

Thanks.


Edited by The Magical Pot, 10 May 2013 - 12:46 PM.


Sponsor:

#2 HermanssoN   Members   -  Reputation: 148

Like
4Likes
Like

Posted 10 May 2013 - 12:23 PM

Hi.

If you write this:
 

float number = 1/4;

We know that 1/4 = 0.25, but we if use integers when dividing the reuslt will also be an integer. Integers can not hold decimal number, so it will round it to a whole number. e.g 0.

If you type:
 

float number = 1.0f / 4.0f;

now the operation will return a decimal number.

to sum it up:
 

float f1 = 1/4; // f1 is now 0
float f2 = 1.0f/4.0f; //f2 is now 0.25



Edited by HermanssoN, 10 May 2013 - 12:25 PM.


#3 Brother Bob   Moderators   -  Reputation: 8632

Like
0Likes
Like

Posted 10 May 2013 - 12:24 PM

The reason it appears to not work is that "8/32" is integer division because both operands are integers, and the result of integer division is an integer. You need to ensure that at least one operand of the division has a floating point type to get a floating point division. For example: 8.0/32, 8/32.0, 8.0/32.0, static_cast<float>(8)/32, or whatever method you want, as long as at least one operand is not an integer.

 

But you can also use the texture matrix.

 

glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glScale(1.0/32, 1.0/32, 1);

Edited by Brother Bob, 10 May 2013 - 12:25 PM.


#4 The Magical Pot   Members   -  Reputation: 178

Like
0Likes
Like

Posted 10 May 2013 - 12:47 PM

Ok, that worked. Thank you!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS