Problem rendering multiple objects in Opengl Legacy (GLFW)

Asked

Viewed 44 times

1

I am trying to render a circle and a rectangle, and both will have independent motion (Remake of Pong), but the problem is that the glTranslatef() of one is influencing the other, if one goes to the right, the other goes too. Putting to print the values of variables in the console, everything seems to be right.

glColor3f(0.0, 1.0, 0.0);
glRotatef(0.0, 0.0, 0.0, 0.0);
glScalef(1.0, 1.0, 0.0);
glTranslatef(bola.x, bola.y, 0);
CriarBola(0.5, 24);

glRotatef(0.0, 0.0, 0.0, 0.0);
glScalef(1.0, 1.0, 0.0);
glTranslatef(13, player.pos, 0);
CriarQuadrado(player.larg, player.alt);

I believe that the error is not coming from the functions below, but I will put them:

//Função de criar o retangulo
void CriarQuadrado(float larg, float alt)
{
    glColor3f(0.0, 0.0, 1.0);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glBegin(GL_QUADS);
    glVertex3f(-larg, alt, 0.0);
    glVertex3f(-larg,-alt, 0.0);
    glVertex3f(larg, -alt, 0.0);
    glVertex3f(larg, alt, 0.0);
    glEnd();
}
//Função de criar o círculo, que é feito a partir de triângulos
void CriarTriangulo(float v1[], float v2[], float v3[])
{
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glBegin(GL_TRIANGLES);
    glVertex3fv(v1);
    glVertex3fv(v2);
    glVertex3fv(v3);
    glEnd();
}

void CriarBola(float raio, float faces)
{
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    float ang = 2 * PI / faces;
    for (short x = 0; x < faces; x++) {
        float v1[3]{ raio * std::cos((x + 1) * ang), raio * std::sin((x + 1) * ang), 0 };
        float v2[3]{ 0, 0, 0 };
        float v3[3]{ raio * std::cos(x * ang), raio * std::sin(x * ang), 0 };
        CriarTriangulo(v1, v2, v3);
    }
}

I will leave all the code of this project in the repl.it

  • 1

    you are using a version of Opengl that already makes but decade that was marked as deprecated, look for Modern Opengl

1 answer

1


I still can’t comment so go this way. It will, hypothetically speaking since I haven’t dealt with Opengl yet. I would be more suspicious, from your comment that glTranslatef() would be to rotate the global coordinates which would affect all transformations before rendering. Normally you would pass the object or I would modify the structure that makes up the coordinates the object and and call to render.

Thinking about it, I took a look at the doc from Khronos Group:
https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml

There in the doc has this reference that I will paste the below:
" If the Matrix mode is either GL_MODELVIEW or GL_PROJECTION, all Objects Drawn after a call to glTranslate are Translated. Use glPushMatrix and glPopMatrix to save and Restore the untranslated coordinate system. "

Basically if you do not isolate the cohorts you change in the matrix will affect all later created objects.

See if this doesn’t help, it reminds a lot of a push pop operation on Assembly.
https://www.khronos.org/opengl/wiki/General_OpenGL:_Transformations
The top has a link that shows a little how to use

And below a video:
https://www.youtube.com/watch?v=WYD6zDiI_3A

If this solves a feedback, and puts how to solve and how it works, it would be interesting to know.

I believe that with this you can solve.

  • 1

    And really, if it works or another solution shares there, I will not have time to test the code, but if you share and I or someone else needs in the future, you will already have a material available.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.