Tindor
12-16-2006, 05:37 AM
Hi,
I'm writing a simple 2d program, but have problems rotating the primitives - for example my ellipse becomes a circle when the angle is 90 and at 180 it's an ellipse again. I'll attach 2 screenshots to illustrate the case.
Also here's how I draw the ellipse and rotate it:
void Ellipse::draw()
{ // glPushMatrix();
glLineWidth(this->line_width);
float xradius=(p2[0]-p1[0])/2;
float yradius=(p2[1]-p1[1])/2;
const float DEG2RAD = 3.14159/180;
glColor3f(color[0],color[1],color[2]);
glPushMatrix();
glTranslatef(p1[0],p1[1],0.0);
{ glTranslatef((p2[0]-p1[0])/2.0,(p2[1]-p1[1])/2.0,0.0);
glRotatef(rotationAngle,0,0,1);
glTranslatef(-(p2[0]-p1[0])/2.0,-(p2[1]-p1[1])/2.0,0.0);
}
if(scale!=1)
{
glScalef(scale,scale,scale);
}
glPolygonMode(GL_FRONT_AND_BACK, (isFilled)?GL_FILL:GL_LINE);
glBegin((isFilled)?GL_POLYGON:GL_LINE_LOOP);
for (int i=0; i < 360; i++)
{
float degInRad = i*DEG2RAD;
//glVertex2f(p1[0]+cos(degInRad)*xradius,p1[1]+sin(degInRad)*yradius);
glVertex2f(xradius+cos(degInRad)*xradius,yradius+s in(degInRad)*yradius);
}
cout << " is Filled: " << isFilled << endl;
glEnd();And here's how I initialize the GL widget:
void NeHeWidget::initializeGL()
{
glClearColor(1,1,1,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
}
void NeHeWidget::resizeGL( int width, int height )
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat x= (GLfloat) width/height;
glFrustum(-x,x,-1.0,1.0,1.0,1.0);
glMatrixMode(GL_MODELVIEW);
}http://geekbg.net/rotation_problem_pic1.png http://geekbg.net/rotation_problem_pic2.png
I'm writing a simple 2d program, but have problems rotating the primitives - for example my ellipse becomes a circle when the angle is 90 and at 180 it's an ellipse again. I'll attach 2 screenshots to illustrate the case.
Also here's how I draw the ellipse and rotate it:
void Ellipse::draw()
{ // glPushMatrix();
glLineWidth(this->line_width);
float xradius=(p2[0]-p1[0])/2;
float yradius=(p2[1]-p1[1])/2;
const float DEG2RAD = 3.14159/180;
glColor3f(color[0],color[1],color[2]);
glPushMatrix();
glTranslatef(p1[0],p1[1],0.0);
{ glTranslatef((p2[0]-p1[0])/2.0,(p2[1]-p1[1])/2.0,0.0);
glRotatef(rotationAngle,0,0,1);
glTranslatef(-(p2[0]-p1[0])/2.0,-(p2[1]-p1[1])/2.0,0.0);
}
if(scale!=1)
{
glScalef(scale,scale,scale);
}
glPolygonMode(GL_FRONT_AND_BACK, (isFilled)?GL_FILL:GL_LINE);
glBegin((isFilled)?GL_POLYGON:GL_LINE_LOOP);
for (int i=0; i < 360; i++)
{
float degInRad = i*DEG2RAD;
//glVertex2f(p1[0]+cos(degInRad)*xradius,p1[1]+sin(degInRad)*yradius);
glVertex2f(xradius+cos(degInRad)*xradius,yradius+s in(degInRad)*yradius);
}
cout << " is Filled: " << isFilled << endl;
glEnd();And here's how I initialize the GL widget:
void NeHeWidget::initializeGL()
{
glClearColor(1,1,1,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
}
void NeHeWidget::resizeGL( int width, int height )
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat x= (GLfloat) width/height;
glFrustum(-x,x,-1.0,1.0,1.0,1.0);
glMatrixMode(GL_MODELVIEW);
}http://geekbg.net/rotation_problem_pic1.png http://geekbg.net/rotation_problem_pic2.png