# glRotatef() Without Calling It

I’m rendering a bunch of quads all at different rotations and translations. I’m trying to do all of this in one call the glBegin(GL_QUADS) and glEnd(). Since glRotatef() and glTranslatef() cannot be called within a glBegin() and glEnd() I was hoping to do all the math myself. Since I’m not 100% with matrices, I figured I’d do trigonometry. Here is what I have so far:

``````void draw()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

double size=200.0;
glPushMatrix();
glBegin(GL_QUADS);

for(unsigned int ii=0;ii<world.size();++ii)
{
if(ii%2==0)
{
glColor3f(1.0,0.0,0.0);
}
else
{
glColor3f(0.0,1.0,0.0);
}

double xrad=world[ii].xr*(3.14159265/180.0);
double yrad=world[ii].yr*(3.14159265/180.0);
double zrad=world[ii].zr*(3.14159265/180.0);
glVertex3f(world[ii].x+size*cos(zrad)-size*sin(zrad),world[ii].y+size*cos(zrad)+size*sin(zrad),-world[ii].z);
glVertex3f(world[ii].x+size*cos(zrad)+size*sin(zrad),world[ii].y-size*cos(zrad)+size*sin(zrad),-world[ii].z);
glVertex3f(world[ii].x-size*cos(zrad)+size*sin(zrad),world[ii].y-size*cos(zrad)-size*sin(zrad),-world[ii].z);
glVertex3f(world[ii].x-size*cos(zrad)-size*sin(zrad),world[ii].y+size*cos(zrad)-size*sin(zrad),-world[ii].z);
}

glEnd();
glPopMatrix();
glutSwapBuffers();
}
``````

That works of one rotation(Z axis). I’ve been trying to implement the two other axes, but I keep getting weird results. So I was hoping someone had done something like this before, however searches on this forum and Google haven’t turned up anything. So if someone could help me with the trig that would be awesome! I figured it wouldn’t be that hard, for the Y axis I would do the same thing I did with the Z axis but instead of doing anything to the Y’s I’d do that part to the Z’s (and change from zrad to yrad) and do the same for X. But again I keep getting very strange results. After a few days of this I figured I’d ask for help, it’s getting hard to stay sane!!!

Does this chapter help you?
http://glprogramming.com/red/appendixf.html#name2

Good luck!

So I’m not 100% sure how to implement a matrix system, should I create my own matrix and vector classes and do all that stuff myself? Like so:

``````//Exception Header
#include <stdexcept>

//Mat4 Class Declaration
class Mat4
{
public:
//Constructor - Default
Mat4()
{
for(int yy=0;yy<4;++yy)
{
for(int xx=0;xx<4;++xx)
{
_matrix[yy][xx]=0.0;
}
}
}

//Constructor - One Argument
Mat4(const double matrix[4][4])
{
for(int yy=0;yy<4;++yy)
{
for(int xx=0;xx<4;++xx)
{
_matrix[yy][xx]=matrix[yy][xx];
}
}
}

//Constructor - Copy
Mat4(const Mat4& copy)
{
for(int yy=0;yy<4;++yy)
{
for(int xx=0;xx<4;++xx)
{
_matrix[yy][xx]=copy._matrix[yy][xx];
}
}
}

//Operator - Assignment
Mat4& operator=(const Mat4& rhs)
{
if(this!=&rhs)
{
for(int yy=0;yy<4;++yy)
{
for(int xx=0;xx<4;++xx)
{
_matrix[yy][xx]=rhs._matrix[yy][xx];
}
}
}

return *this;
}

double get(const int xx,const int yy) const
{
if(xx<0||xx>3||yy<0||yy>3)
{
throw std::runtime_error("Matrix(get) - Invalid position!!!");
}

return _matrix[yy][xx];
}

private:
//Member Variables
double _matrix[4][4];
};

//Vec4 Class Declaration
class Vec4
{
public:
//Member Variables
double x;
double y;
double z;
double c;

//Constructor - Default and 4 Argument
Vec4(const double xx=0.0,const double yy=0.0,const double zz=0.0,const double cc=0.0):
x(xx),y(yy),z(zz),c(cc)
{}

//Operator - Multiplication
Vec4 operator*(const Mat4& rhs)
{
double arrTemp[4]={x,y,z,c};
double arrAns[4]={0.0,0.0,0.0,0.0};

for(int xx=0;xx<4;++xx)
{
double ans=0;

for(int yy=0;yy<4;++yy)
{
ans+=arrTemp[yy]*rhs.get(xx,yy);
}

arrAns[xx]=ans;
}

return Vec4(arrAns[0],arrAns[1],arrAns[2],arrAns[3]);
}
};
``````

Then create a matrix (based on rotation, scale, or translation), then a vector based on my position, and finally do the multiplication to figure out the coordinates?

So I’m not 100% sure how to implement a matrix system, should I create my own matrix and vector classes and do all that stuff myself?

Why? What benefit would that serve? There are plenty of good math libraries out there.

No need to reinvent the wheel.

So far what I posted above works (I haven’t had time to test in OpenGL, but using simple inputs in a console window and checking the results seems to work). The only real benefit I had from making the classes myself was re-teaching myself how matrices work. A little later I’ll implement this into my project to verify everything above is working right.

No need to reinvent the wheel.

I agree, however making your own class to handle this stuff helps you get a good understanding of how matrix transformations work…at least it did for me.