1
\$\begingroup\$

I'm new to OpenGL and don't really know how to rotate a terrain. For my graphics class, I have a project I need to do that is making a tire using OpenGL and work that we done in class (my framework is one of the labs that we did that introduced us to heightmaps and terrains).

I have this tire tread terrain (see picture) that I made and how I'm going to make the tire is by making lots of these tire terrains and rotate them to make the tire shape. I'm sure there is better ways to accomplish this but my professor recommended I do this. So I was wondering how I can rotate it.

The code below is my code for the terrain that is shown in the picture. Thank you in advance for the help!

void TerrainScene()
{
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    //gluLookAt(0,g.moveup,g.movedown,  0,0,0,  0,1,0);
    //gluLookAt(g.movedown-50,8,g.moveup,  g.movedown-50,-2,g.moveup-40,  0,1,0);
    gluLookAt(g.movedown-50,70,g.moveup,  g.movedown-50,-2,g.moveup-40,  0,1,0);
    glLightfv(GL_LIGHT0, GL_POSITION, g.lightPosition);
    glColor3f(1.0f, 1.0f, 0.0f);
    glBegin(GL_TRIANGLES);
    glNormal3f(1.0f, 1.0f, 0.0f);
    float x = -25.0;
    float z = -25.0;
    float inc = 0.2;
    glColor3f(0.8f, 0.8f, 0.8f);
    for (int i = 0; i < 256; i++)
    {
        for (int j = 0; j < 256; j++)
        {
            float scale = 5.0;
            int tmp1 = (int)*(img[0].data + (i*img[0].width*3 + j*3));
            int tmp2 = (int)*(img[0].data + ((i+1)*img[0].width*3 + j*3));
            int tmp3 = (int)*(img[0].data + (i*img[0].width*3 + (j+1)*3));
            int tmp4 = (int)*(img[0].data + ((i+1)*img[0].width*3 + (j+1)*3));

            float e1 = -10.0 + ((float)tmp1 / 255.0) * scale;
            float e2 = -10.0 + ((float)tmp2 / 255.0) * scale;
            float e3 = -10.0 + ((float)tmp3 / 255.0) * scale;
            float e4 = -10.0 + ((float)tmp4 / 255.0) * scale;

            unsigned char rgb[3];
            getTerrainColor(i,j,1,rgb);
            glColor3ubv(rgb);
            Vec v1, v2, v3, norm;
            vecMake(x,      e2, z+inc, v1);
            vecMake(x+inc,  e4, z+inc, v2);
            vecMake(x,      e1, z,     v3);
            getTriangleNormal(v1,v2,v3,norm);
            glNormal3fv(norm);
            glVertex3f(x,     e2, z+inc);
            glVertex3f(x+inc, e4, z+inc);
            glVertex3f(x,     e1, z); 
            vecMake(x+inc,  e4, z+inc, v1);
            vecMake(x+inc,  e3, z,     v2);
            vecMake(x,      e1, z,     v3);
            getTriangleNormal(v1,v2,v3,norm);
            glNormal3fv(norm);

            glVertex3f(x+inc, e4, z+inc);
            glVertex3f(x+inc, e3, z); 
            glVertex3f(x,     e1, z); 
            x += inc;
        }
        x = -25.0f;
        z += inc;
    }   
    glEnd();
}


image of my tire tread terrain

\$\endgroup\$
  • 1
    \$\begingroup\$ How do you draw your terrain currently? What does the code look like? Also, I'm a little alarmed by your prof's suggested method, but I'll assume it makes sense for the particular lesson you're working on. \$\endgroup\$ – DMGregory Nov 19 '19 at 2:05
  • 1
    \$\begingroup\$ @DMGregory Hi, thank you for replying! I have edited my comment and added in my code for my terrain scene. \$\endgroup\$ – Raúl Nov 19 '19 at 2:45
  • \$\begingroup\$ You could iterate pieces and use glPushMatrix and glPopMatrix around your outer loop. for every piece you would set glRotate and glTranslate and then draw it just like you do now. \$\endgroup\$ – Jonas Beck Nov 19 '19 at 16:48
  • \$\begingroup\$ @JonasBeck Thank you for replying! I tried adding glPushMatrix and glPopMatrix on my outer loop but it doesn't seem to do anything. maybe i'm not adding my glRotate and glTranslate in the right spot... Can you clarify what you mean by "for every piece"? Are you talking about the inner loop or certain parts in my inner loop? \$\endgroup\$ – Raúl Nov 21 '19 at 19:14
  • \$\begingroup\$ @JonasBeck I've also never used glPushMatrix and glPopMatrix before so it could be that i'm not even doing that properly. \$\endgroup\$ – Raúl Nov 21 '19 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.