# My triangle wont render!

I am trying to render a triangle that rotates on its axis ( in this case the Centre, which is marked by a white spot ). The triangle appears at first but when i press space to start the triangle rotating it disappears. Below is my display code:

Centre();
gl.glPushMatrix();
if ( bRotating == true ) {
gl.glTranslated(centreX, centreY, 1);
gl.glRotated(degRot,0,0,1);
gl.glTranslated(-centreX, -centreY, 1);
}
gl.glColor3f (0.0f, 1.0f, 0.0f);
DrawTriangle();
gl.glPopMatrix();

DrawWhiteSpot();
if ( degRot == 360 ) {
degRot = 0;
}
else degRot++;

Here is my triangle drawing code:

public void DrawTriangle() {
gl.glBegin(GL_TRIANGLES);
gl.glColor3f (1.0f, 0.0f, 0.0f);
gl.glVertex2d (vertex[0],vertex[1]);
gl.glColor3f (0.0f, 1.0f, 0.0f);
gl.glVertex2d (vertex[2],vertex[3]);
gl.glColor3f (0.0f, 0.0f, 1.0f);
gl.glVertex2d (vertex[4],vertex[5]);
gl.glEnd();
}

I’ve been stuck on this problem for ages and would really appreciate some help. The person who helped me last time mentioned something about putting a matrix identity on the end of my drawing method. I’m not sure what this means. Please help me, i’m getting upset with this problem now.

Thanks alot for your help, hopefully i’ll be able to return the favour when i’ve mastered this OpenGL.

Why are you using translate if you only want to rotate the triangle. I think from what I see you are translating the triangle out of the screen, you could post a little more code like the routine that you move the triangle.(centreX, centreY)

Originally posted by thebarslider:
[b]I am trying to render a triangle that rotates on its axis ( in this case the Centre, which is marked by a white spot ). The triangle appears at first but when i press space to start the triangle rotating it disappears. Below is my display code:

Centre();
gl.glPushMatrix();
if ( bRotating == true ) {
gl.glTranslated(centreX, centreY, 1);
gl.glRotated(degRot,0,0,1);
gl.glTranslated(-centreX, -centreY, 1);
}
gl.glColor3f (0.0f, 1.0f, 0.0f);
DrawTriangle();
gl.glPopMatrix();

DrawWhiteSpot();
if ( degRot == 360 ) {
degRot = 0;
}
else degRot++;

Here is my triangle drawing code:

public void DrawTriangle() {
gl.glBegin(GL_TRIANGLES);
gl.glColor3f (1.0f, 0.0f, 0.0f);
gl.glVertex2d (vertex[0],vertex[1]);
gl.glColor3f (0.0f, 1.0f, 0.0f);
gl.glVertex2d (vertex[2],vertex[3]);
gl.glColor3f (0.0f, 0.0f, 1.0f);
gl.glVertex2d (vertex[4],vertex[5]);
gl.glEnd();
}

I’ve been stuck on this problem for ages and would really appreciate some help. The person who helped me last time mentioned something about putting a matrix identity on the end of my drawing method. I’m not sure what this means. Please help me, i’m getting upset with this problem now.

Thanks alot for your help, hopefully i’ll be able to return the favour when i’ve mastered this OpenGL.[/b]

i also think you’ve got a glTranslate call you don’t need as mentioned above, however some further advice is to get the triangle spinning in one location first before you start adding user events

void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) //clear color and depth

glMatrixMode(GL_MODELVIEW) //make sure we are in modelview

glPushMatrix();
glTranslatef(x,y,z);
glRotatef(yrot, x, 1, z);
drawTriangle();
glPopMatrix();

yrot += 1.0

Swapbuffer();
}

glMatrixMode and glLoadIdentity may not be necessary if you have it in your Init(), OnSize(), or Resize() function the point is to make sure the modelview matrix is the current matrix and then clear it or you will be combining all transforms from previous frames

glPushMatrix and glPopMatrix() may not be necessary if this is the only object your rendering

Hi,

he doesn’t need the glTranslate() is not exactly right. What he seems to want to do is translating the triangle to the origin, then rotate it and translate it back to where it was before -> local rotation.

But it has to be:
if ( bRotating == true ) {
gl.glTranslated(-centreX, -centreY, 0); <- move to origin
gl.glRotated(degRot,0,0,1);
gl.glTranslated(centreX, centreY, 0); <- move to previous position
}

Originally posted by mphanke:
[b]Hi,

he doesn’t need the glTranslate() is not exactly right. What he seems to want to do is translating the triangle to the origin, then rotate it and translate it back to where it was before -> local rotation.

But it has to be:
if ( bRotating == true ) {
gl.glTranslated(-centreX, -centreY, 0); <- move to origin
gl.glRotated(degRot,0,0,1);
gl.glTranslated(centreX, centreY, 0); <- move to previous position
}[/b]

I think He had better doing:

// Initialization
glMatrixMode(GL_PROJECTION);
gluPerspective();
glMatrixMode(GL_MODELVIEW);

// The render routine
glPushMatrix();
glRotate();
glTraslate();
glPopMatrix();

I think It should work.

Hope It works

Originally posted by thebarslider:
[b]

if ( bRotating == true ) {
gl.glTranslated(centreX, centreY, 1);
gl.glRotated(degRot,0,0,1);
gl.glTranslated(-centreX, -centreY, 1);
}

[/b]

Just a guess but, the third parameter of the glTranslate is setting Z = 1. That is probably behind the camera.