[Development] Rotate image using Opengl

Results 1 to 11 of 11
  1. #1
    0x4D696E68 myheart is offline
    MemberRank
    Jun 2017 Join Date
    354Posts

    [Development] Rotate image using Opengl

    Hope this helps!



    code:
    Code:
    class Pos2D
    {
    public:
        Pos2D(float x= 0.0f, float y = 0.0f)
        {
      this->X = x;
      this->Y = y;
        }
        float X;
        float Y;
    
    public:
        void Set(float x, float y)
        {
      this->X = x;
      this->Y = y;
        }
    };
    
    class Vec3D
    {
    public:
        Vec3D(float x = 0.0f, float y = 0.0f, float z = 0.0f)
        {
      this->X = x;
      this->Y = y;
      this->Z = z;
        }
        float X;
        float Y;
        float Z;
    
    public:
       void SetVertexCoord(Vec3D *Pos, Vec3D *a2)
        {
      this->X = a2->Y * Pos->Y + a2->Z * Pos->Z + Pos->X * a2->X;
      this->Y = a2[1].Y * Pos->X + a2[1].Z * Pos->Y + a2[2].X * Pos->Z;
      this->Z = a2[2].Z * Pos->X + a2[3].X * Pos->Y + a2[3].Y * Pos->Z;
        }
    
        void SetVertex(Vec3D *lpVertex)
        {
      float PosZ = this->Z * 0.01745329238474369f;
      float v3 = sin(PosZ);
      float v4 = cos(PosZ);
      float PosY = this->Y * 0.01745329238474369f;
      float v6 = sin(PosY);
      float v7 = cos(PosY);
      float PosX = this->X * 0.01745329238474369f;
      float v9 = sin(PosX);
      float v10 = cos(PosX);
    
      lpVertex->X = v7 * v4;
      lpVertex[1].Y = v7 * v3;
      lpVertex[2].Z = -v6;
    
      lpVertex->Y = v9 * v6 * v4 + -v3 * v10;
      lpVertex[1].Z = v9 * v6 * v3 + v10 * v4;
      lpVertex[3].X = v9 * v7;
    
      lpVertex->Z = v10 * v6 * v4 + -v9 * -v3;
      lpVertex[2].X = v10 * v6 * v3 + -v9 * v4;
      lpVertex[3].Y = v10 * v7;
    
      lpVertex[1].X = 0.0;
      lpVertex[2].Y = 0.0;
      lpVertex[3].Z = 0.0;
        }
    
        void Set(float R, float G, float B)
        {
      this->X = R;
      this->Y = G;
      this->Z = B;
        }
    
        Vec3D * Get()
        {
      return this;
        }
    };
    Code:
    void CTexture::RotateImage(int ID, float PosX, float PosY, float Width, float Height, float Angle, float StartX, float StartY, float ScaleX, float ScaleY, float Alpha)
    {
        Pos2D TextureCoord[4];
        Vec3D Vertex3f1[4];
        Vec3D VertexCoord[4];
        Vec3D VertexSize[4];
    
        PosX = WINDOW_WIDTH * PosX / 640.0f;
        PosY = WINDOW_HEIGHT * PosY / 480.0f;
        Width = WINDOW_WIDTH * Width / 640.0f;
        Height = WINDOW_HEIGHT * Height / 480.0f;
        float PosYHeight = WINDOW_HEIGHT - PosY;
    
        glPushMatrix();
    
        this->BindTexture(ID);
    
        VertexSize[0].Set(-Width * 0.5f, Height * 0.5f, 0.0);
        VertexSize[1].Set(-Width * 0.5f, -Height * 0.5f, 0.0);
        VertexSize[2].Set(Width * 0.5f, -Height * 0.5f, 0.0);
        VertexSize[3].Set(Width * 0.5f, Height * 0.5f, 0.0);
    
       Vec3D VecPos3f(0.0, 0.0, Angle);
        VecPos3f.SetVertex(Vertex3f1);    
        TextureCoord[0].Set(StartX, StartY);
        TextureCoord[1].Set(StartX, (StartY + ScaleY));
        TextureCoord[2].Set((StartX + ScaleX), (StartY + ScaleY));
        TextureCoord[3].Set((StartX + ScaleX), StartY);
        
        glBegin(GL_TRIANGLE_FAN);
    
        if (Alpha > 0.0)
      glColor4f(1.0f, 1.0f, 1.0f, Alpha);
    
        for (int i = 0; i < 4; i++)
        {
      glTexCoord2f(TextureCoord[i].X, TextureCoord[i].Y);
      VertexCoord[i].SetVertexCoord(&VertexSize[i], Vertex3f1);
      glVertex2f((VertexCoord[i].X + PosX), (PosYHeight - VertexCoord[i].Y)); //PosYHeight + VertexCoord[i].Y : Used for DrawText
        }
    
        if (Alpha > 0.0)
      glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    
        glEnd();
        glPopMatrix();
    }
    Last edited by myheart; 22-08-18 at 01:03 PM. Reason: Code changed | Fixed | Last fix


  2. #2

    re: [Development] Rotate image using Opengl

    interesting, for all client versions?

  3. #3
    0x4D696E68 myheart is offline
    MemberRank
    Jun 2017 Join Date
    354Posts

    Re: [Development] Rotate image using Opengl

    Quote Originally Posted by KarLi View Post
    interesting, for all client versions?
    yes , for all client version

    - - - Updated - - -

    first post updated!

  4. #4
    Member NaveMu is offline
    MemberRank
    Feb 2012 Join Date
    61Posts

    Re: [Development] Rotate image using Opengl

    Hi, very interesting, you can share complete source or not?

  5. #5
    c# Programmer martinx09 is offline
    MemberRank
    Aug 2008 Join Date
    391Posts

    Re: [Development] Rotate image using Opengl

    How to use?

    Code:
    RotateImage(int ID, float PosX, float PosY, float Width, float Height, float Angle, float StartX, float StartY, float ScaleX, float ScaleY, float Alpha)
    I assume ID would be the texture's ID, then.. what's the difference between PosX/PosY and StartX/StartY? Same for Width/Height and ScaleX/ScaleY.. what's the difference?

  6. #6
    0x4D696E68 myheart is offline
    MemberRank
    Jun 2017 Join Date
    354Posts

    Re: [Development] Rotate image using Opengl

    Quote Originally Posted by martinx09 View Post
    How to use?

    Code:
    RotateImage(int ID, float PosX, float PosY, float Width, float Height, float Angle, float StartX, float StartY, float ScaleX, float ScaleY, float Alpha)
    I assume ID would be the texture's ID, then.. what's the difference between PosX/PosY and StartX/StartY? Same for Width/Height and ScaleX/ScaleY.. what's the difference?
    Usage: RotateImage(TEXTURE_ID, 100.0, 100.0, 80.0, 80.0, 180.0, 0.0, 0.0, 1.0, 1.0, 1.0)


  7. #7
    Account Upgraded | Title Enabled! solarismu is offline
    MemberRank
    May 2017 Join Date
    219Posts

    Re: [Development] Rotate image using Opengl

    Cool. I remember I’ve seen this in rotating lottery feature somewhere :) ...
    Last edited by solarismu; 22-08-18 at 03:09 PM.

  8. #8
    Apprentice Sweng is offline
    MemberRank
    Jun 2009 Join Date
    23Posts

    Re: [Development] Rotate image using Opengl

    this->BindTexture(ID); - share source?

  9. #9
    Member NaveMu is offline
    MemberRank
    Feb 2012 Join Date
    61Posts

    Re: [Development] Rotate image using Opengl

    BindTexture(ID), can you share this?

  10. #10
    0x4D696E68 myheart is offline
    MemberRank
    Jun 2017 Join Date
    354Posts

    Re: [Development] Rotate image using Opengl

    Quote Originally Posted by NaveMu View Post
    BindTexture(ID), can you share this?
    Code:
    bool CTexture::BindTexture(const unsigned int ID)
    {
    	if (this->m_TextureID.find(ID) != this->m_TextureID.end())
    	{
      glBindTexture(GL_TEXTURE_2D, this->m_TextureID[ID]);
    
      return true;
    	}
    
    	return false;
    }
    you can load .jpg using freeimage library
    or .tga using this:
    Code:
    typedef struct
    {
    /* +0*/  unsigned char  identsize;          // size of ID field that follows 18 byte header (0 usually)
    /* +1*/  unsigned char  colourmaptype;      // type of colour map 0=none, 1=has palette
    /* +2*/  unsigned char  imagetype;          // type of image 0=none,1=indexed,2=rgb,3=grey,+8=rle packed
    /* +3*/  short colourmapstart;     // first colour map entry in palette
    /* +5*/  short colourmaplength;    // number of colours in palette
    /* +7*/  unsigned char  colourmapbits;      // number of bits per palette entry 15,16,24,32
    /* +8*/  short xstart;             // image x origin
    /* +10*/	short ystart;             // image y origin
    /* +12*/	short width;              // image width in pixels
    /* +14*/	short height;             // image height in pixels
    /* +16*/	unsigned char  bits;               // image bits per pixel 8,16,24,32
    /* +17*/	unsigned char  descriptor;         // image descriptor bits (vh flip bits)
    /* +18*/	unsigned char *data;
    } TGA_HEADER;

  11. #11
    Member NaveMu is offline
    MemberRank
    Feb 2012 Join Date
    61Posts

    Re: [Development] Rotate image using Opengl

    Quote Originally Posted by myheart View Post
    Hope this helps!



    code:
    Code:
    class Pos2D
    {
    public:
        Pos2D(float x= 0.0f, float y = 0.0f)
        {
      this->X = x;
      this->Y = y;
        }
        float X;
        float Y;
    
    public:
        void Set(float x, float y)
        {
      this->X = x;
      this->Y = y;
        }
    };
    
    class Vec3D
    {
    public:
        Vec3D(float x = 0.0f, float y = 0.0f, float z = 0.0f)
        {
      this->X = x;
      this->Y = y;
      this->Z = z;
        }
        float X;
        float Y;
        float Z;
    
    public:
       void SetVertexCoord(Vec3D *Pos, Vec3D *a2)
        {
      this->X = a2->Y * Pos->Y + a2->Z * Pos->Z + Pos->X * a2->X;
      this->Y = a2[1].Y * Pos->X + a2[1].Z * Pos->Y + a2[2].X * Pos->Z;
      this->Z = a2[2].Z * Pos->X + a2[3].X * Pos->Y + a2[3].Y * Pos->Z;
        }
    
        void SetVertex(Vec3D *lpVertex)
        {
      float PosZ = this->Z * 0.01745329238474369f;
      float v3 = sin(PosZ);
      float v4 = cos(PosZ);
      float PosY = this->Y * 0.01745329238474369f;
      float v6 = sin(PosY);
      float v7 = cos(PosY);
      float PosX = this->X * 0.01745329238474369f;
      float v9 = sin(PosX);
      float v10 = cos(PosX);
    
      lpVertex->X = v7 * v4;
      lpVertex[1].Y = v7 * v3;
      lpVertex[2].Z = -v6;
    
      lpVertex->Y = v9 * v6 * v4 + -v3 * v10;
      lpVertex[1].Z = v9 * v6 * v3 + v10 * v4;
      lpVertex[3].X = v9 * v7;
    
      lpVertex->Z = v10 * v6 * v4 + -v9 * -v3;
      lpVertex[2].X = v10 * v6 * v3 + -v9 * v4;
      lpVertex[3].Y = v10 * v7;
    
      lpVertex[1].X = 0.0;
      lpVertex[2].Y = 0.0;
      lpVertex[3].Z = 0.0;
        }
    
        void Set(float R, float G, float B)
        {
      this->X = R;
      this->Y = G;
      this->Z = B;
        }
    
        Vec3D * Get()
        {
      return this;
        }
    };
    Code:
    void CTexture::RotateImage(int ID, float PosX, float PosY, float Width, float Height, float Angle, float StartX, float StartY, float ScaleX, float ScaleY, float Alpha)
    {
        Pos2D TextureCoord[4];
        Vec3D Vertex3f1[4];
        Vec3D VertexCoord[4];
        Vec3D VertexSize[4];
    
        PosX = WINDOW_WIDTH * PosX / 640.0f;
        PosY = WINDOW_HEIGHT * PosY / 480.0f;
        Width = WINDOW_WIDTH * Width / 640.0f;
        Height = WINDOW_HEIGHT * Height / 480.0f;
        float PosYHeight = WINDOW_HEIGHT - PosY;
    
        glPushMatrix();
    
        this->BindTexture(ID);
    
        VertexSize[0].Set(-Width * 0.5f, Height * 0.5f, 0.0);
        VertexSize[1].Set(-Width * 0.5f, -Height * 0.5f, 0.0);
        VertexSize[2].Set(Width * 0.5f, -Height * 0.5f, 0.0);
        VertexSize[3].Set(Width * 0.5f, Height * 0.5f, 0.0);
    
       Vec3D VecPos3f(0.0, 0.0, Angle);
        VecPos3f.SetVertex(Vertex3f1);    
        TextureCoord[0].Set(StartX, StartY);
        TextureCoord[1].Set(StartX, (StartY + ScaleY));
        TextureCoord[2].Set((StartX + ScaleX), (StartY + ScaleY));
        TextureCoord[3].Set((StartX + ScaleX), StartY);
        
        glBegin(GL_TRIANGLE_FAN);
    
        if (Alpha > 0.0)
      glColor4f(1.0f, 1.0f, 1.0f, Alpha);
    
        for (int i = 0; i < 4; i++)
        {
      glTexCoord2f(TextureCoord[i].X, TextureCoord[i].Y);
      VertexCoord[i].SetVertexCoord(&VertexSize[i], Vertex3f1);
      glVertex2f((VertexCoord[i].X + PosX), (PosYHeight - VertexCoord[i].Y)); //PosYHeight + VertexCoord[i].Y : Used for DrawText
        }
    
        if (Alpha > 0.0)
      glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    
        glEnd();
        glPopMatrix();
    }
    Bro, could you share with us all this complete source? I'm tempted to do this but I can not, I know you've helped a lot by posting part of it, if not ask too much could you post it complete? I believe it would help many people just like me beginners in OpenGL!



Advertisement