• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[Development] Rotate image using Opengl

Skilled Illusionist
Joined
Jun 22, 2017
Messages
363
Reaction score
561
Hope this helps!

myheart - [Development] Rotate image using Opengl - RaGEZONE Forums


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:
Newbie Spellweaver
Joined
Feb 12, 2012
Messages
60
Reaction score
2
Hi, very interesting, you can share complete source or not?
 
Skilled Illusionist
Joined
Aug 5, 2008
Messages
377
Reaction score
33
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?
 
Skilled Illusionist
Joined
Jun 22, 2017
Messages
363
Reaction score
561
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)

79jJlyx - [Development] Rotate image using Opengl - RaGEZONE Forums
 

Attachments

You must be registered for see attachments list
Experienced Elementalist
Joined
May 4, 2017
Messages
219
Reaction score
318
Cool. I remember I’ve seen this in rotating lottery feature somewhere :) ...
 
Last edited:
Skilled Illusionist
Joined
Jun 22, 2017
Messages
363
Reaction score
561
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;
 
Newbie Spellweaver
Joined
Feb 12, 2012
Messages
60
Reaction score
2
Hope this helps!

myheart - [Development] Rotate image using Opengl - RaGEZONE Forums


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!
 
Back
Top