Jump to content
  • Advertisement
Sign in to follow this  
raptorstrike

OpenGL OpenGL texture coords have a problem with 1/3?

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

ok well I have the following code:
#ifndef _ANIMATED_OBJECT
#define _ANIMATED_OBJECT
#include <string>
#include "Object.h"
using namespace std;
class Animated_Object : public Object
{
  public:
  Animated_Object(float X, float Y, float Z, float Width, float Height, int nRow, int nFrame, string Texture) : Object(X,Y,Z,Width,Height, Texture)
  {
    Frame_Width = .3333333333;// 1/3
    Frame_Height = .2;
    Row = 1;
    Frame = 1;
  };
  virtual void Draw()
  {
     glPushMatrix();
      glBindTexture(GL_TEXTURE_2D, Image);
      glBegin(GL_QUADS);
        glTexCoord2f(Frame_Width ,0);
        glVertex3f(X + Width,Y,Z);
        glTexCoord2f(0,0);
        glVertex3f(X, Y, Z);
        glTexCoord2f(0,Frame_Height);
        glVertex3f(X, Y - Height, Z);
        glTexCoord2f(Frame_Width,Frame_Height);
        glVertex3f(X + Width,Y - Height, Z);
      glEnd();
    glPopMatrix();
  };
  float Frame_Width, Frame_Height;
  int Row, Frame;
};
#endif    


now if I make frame_width = 1/3 i get a black texture but if I make it .333333333 I get the texture like it is sapposed to be, Is there any reason for this? thanks for any explination [smile]

Share this post


Link to post
Share on other sites
Advertisement
Yep.. 1/3, in non-decimal form =0. The compiler assumes integers when not specified. Just type 1.0f/3.0f (both numbers seen as floats), and it should work.

Hope that solved it
~zix~

Share this post


Link to post
Share on other sites
Nothing to do with your question but try to use initializer lists in C++ to initialize class members rather than assigning in the constructor body like so:


class Animated_Object : public Object
{
public:
Animated_Object(float X, float Y, float Z, float Width, float Height, int nRow, int nFrame, string Texture) : Object(X,Y,Z,Width,Height, Texture),
Frame_Width(1.0f / 3.0f), Frame_Height(0.2f), Row(1), Frame(1) { }

...


Share this post


Link to post
Share on other sites
ok this has nothing to do with OpenGL just numbers in general. Wow thats very interesting, you learn somthing new everyday, I thought the f was just there for clarification.

Thanks alot [grin]

to the above poser. I am aware of this method of initialization but is there any real reason to do it like this? It seems like it might get harder to read if you were to do alot of initializing

Share this post


Link to post
Share on other sites
Quote:
Original post by zix99
Just type 1.0f/3.0f (both numbers seen as floats), and it should work.

Only one of the numbers need to be floating point to do a floating point division and return a float.

(1.f / 3) == (1 / 3.f) == (1.f / 3.f)

Share this post


Link to post
Share on other sites
Quote:
Original post by raptorstrike
to the above poser. I am aware of this method of initialization but is there any real reason to do it like this? It seems like it might get harder to read if you were to do alot of initializing
Much more info about that than I feel like posting can be found here.

Share this post


Link to post
Share on other sites
the f only works with opengl for as far as I know.
Maybe it should be pointed out that this is general typecasting.

float ( 1/3 ) = 1.0f/3 = (float)1 / (float) 3

etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kincaid
the f only works with opengl for as far as I know.
Maybe it should be pointed out that this is general typecasting.

float ( 1/3 ) = 1.0f/3 = (float)1 / (float) 3

What do you mean by the f only working in OpenGL? Do you mean the f in "1.f"? That is part of C and C++.

Also, float(1/3) == 0.f
The other two will yield correct results of one third.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
or you can just use 1/3.0 == 1.0/3.0 == 1/(float)(3)

Share this post


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

  • 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!