Wednesday, August 17, 2022
HomeGame Developmentopengl - Why will not my legs rotate on the top as...

opengl – Why will not my legs rotate on the top as a substitute of the centre


Because the title suggests, I am battling with getting the legs of my mannequin right here to rotate correctly. They rotate within the center as a substitute of on the top the place the knee joint could be. To date I’ve tried translating them to 0.0, 0.0, 0.0 earlier than calling the glrotate perform on my physique elements however all that does is transfer the decrease leg additional away from the higher leg and hold the problem going. I can not determine this out.

Visible of Challenge

#embrace <glut.h>

float angle[4];
float LightAngle;
GLuint texture;

bool LowerFrontLegDown = true;
bool LowerBackLegDown = true;


GLfloat corners[8][3] = { {-0.5,0.5,-0.5},{0.5,0.5,-0.5},
                        {0.5,-0.5,-0.5},{-0.5,-0.5,-0.5},
                        {-0.5,0.5,0.5},{0.5,0.5,0.5},
                        {0.5,-0.5,0.5},{-0.5,-0.5,0.5} };

//Two Dimensional Array for the corners of the dice

GLfloat normals[][3] = { {0.0,0.0,1.0},
                                      {1.0,0.0,0.0},
                                      {0.0,-1.0,0.0},
                                      {0.0,1.0,0.0},
                                      {0.0,0.0,-1.0},
                                      {-1.0,0.0,0.0} };

//Regular maps for the dice to use lighting to it when this system runs

typedef struct materialStruct {
    GLfloat ambient[4];
    GLfloat diffuse[4];
    GLfloat specular[4];
    GLfloat shininess;
};

materialStruct redPlasticMaterial = {
       { 0.30, 0.00, 0.00, 1.00 },
       { 0.60, 0.00, 0.00, 1.00 },
       { 0.80, 0.60, 0.60, 1.00 },
       32.00 };

materialStruct* currentMaterial;



void setMaterial(materialStruct* supplies) {
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, materials->ambient);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, materials->diffuse);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, materials->specular);
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, materials->shininess);
}

//Defines a fabric that's then utilized to the mannequin

void drawFace(int a, int b, int c, int d) {
    glBegin(GL_POLYGON);
    glTexCoord2f(0.0, 0.0);
    glVertex3fv(corners[a]);
    glTexCoord2f(0.0, 1.0);
    glVertex3fv(corners[b]);
    glTexCoord2f(1.0, 1.0);
    glVertex3fv(corners[c]);
    glTexCoord2f(1.0, 0.0);
    glVertex3fv(corners[d]);
    glEnd();
} 

//Turns the corners from the 2 dimensional array into nook items for the mannequin, permitting faces to be drawn and in flip, permitting for the mannequin giraffe to be drawn

void ArrayCube() {
    glNormal3fv(normals[0]);
    drawFace(0, 3, 2, 1);
    glNormal3fv(normals[1]);
    drawFace(3, 0, 4, 7);
    glNormal3fv(normals[2]);
    drawFace(2, 3, 7, 6);
    glNormal3fv(normals[3]);
    drawFace(1, 2, 6, 5);
    glNormal3fv(normals[4]);
    drawFace(4, 5, 6, 7);
    glNormal3fv(normals[5]);
    drawFace(5, 4, 0, 1);
}
//Attracts the faces of the mannequin and creates a dice we will name later for the person elements of the mannequin.

void rotate() {
    angle[0] += 1.0;
    if (angle[0] > 360) angle[0] -= 360;

    if (LowerFrontLegDown) angle[1] -= 0.2;
    else angle[1] += 0.2;

    if (angle[1] < 315) LowerFrontLegDown = false;
    if (angle[1] > 360) LowerFrontLegDown = true;

    angle[0] += 1.0;
    if (angle[0] > 360) angle[0] -= 360;

    if (LowerBackLegDown) angle[1] -= 0.2;
    else angle[1] += 0.2;

    if (angle[1] < 315) LowerBackLegDown = false;
    if (angle[1] > 360) LowerBackLegDown = true;

    glutPostRedisplay();
}

void MainBody()
{
    glPushMatrix();
    glScalef(1.25, 0.25, 0.5);
    ArrayCube();
    glPopMatrix();
}

void LowerNeck()
{
    glPushMatrix();
    glTranslatef(0.5, 0.25, 0);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void UpperNeck()
{
    glPushMatrix();
    glTranslatef(0.5, 0.75, 0);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void Head()
{
    glPushMatrix();
    glRotatef(90, 0.0, 0.0,1.0);
    glTranslatef(1, -0.6, 0);
    glScalef(0.1, 0.4, 0.15);
    ArrayCube();
    glPopMatrix();
}

void RightHorn()
{
    glPushMatrix();
    glRotatef(0, 0.0, 0.0,1);
    glTranslatef(0.5, 1.15, 0.035);
    glScalef(0.05, 0.15, 0.05);
    ArrayCube();
    glPopMatrix();
}

void LeftHorn()
{
    glPushMatrix();
    glRotatef(0, 0.0, 0.0, 1);
    glTranslatef(0.5, 1.15, -0.035);
    glScalef(0.05, 0.15, 0.05);
    ArrayCube();
    glPopMatrix();
}

void FrontUpperRightLeg()
{
    glPushMatrix();
    glTranslatef(0.5, -0.35, 0.15);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void FrontLowerRightLeg()
{
    glPushMatrix();
    glTranslatef(0.5, -0.85, 0.15);
    glRotatef(angle[1], 0.0, 0.0, 1.0);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void FrontUpperLeftLeg()
{
    glPushMatrix();
    glTranslatef(0.5, -0.35, -0.15);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void FrontLowerLeftLeg()
{
    glPushMatrix();
    glTranslatef(0.5, -0.85, -0.15);
    glRotatef(angle[1], 0.0, 0.0, 1.0);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();

    glPopMatrix();
}

void BackUpperRightLeg()
{
    glPushMatrix();
    glTranslatef(-0.5, -0.35, -0.15);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void BackLowerRightLeg()
{
    glPushMatrix();
    glTranslatef(-0.5, -0.85, -0.15);
    glRotatef(angle[1], 0.0, 0.0, 1.0);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void BackUpperLeftLeg()
{
    glPushMatrix();
    glTranslatef(-0.5, -0.35, 0.15);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void BackLowerLeftLeg()
{
    glPushMatrix();
    glTranslatef(-0.5, -0.85, 0.15);
    glRotatef(angle[1], 0.0, 0.0, 1.0);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void Tail()
{
    glPushMatrix();
    glTranslatef(-0.65, -0.25, 0);
    glScalef(0.05, 0.75, 0.05);
    ArrayCube();
    glPopMatrix();
}

//Every of the under features attracts a person a part of the entire mannequin and locations these elements the place they should go as soon as this system runs

void DrawGiraffe()
{
    MainBody();
    LowerNeck();
    UpperNeck();
    Head();
    RightHorn();
    LeftHorn();
    FrontUpperRightLeg();
    FrontLowerRightLeg(); 
    FrontUpperLeftLeg();
    FrontLowerLeftLeg();
    BackUpperRightLeg();
    BackLowerRightLeg();
    BackUpperLeftLeg();
    BackLowerLeftLeg();
    Tail();

}

//Calls the above features to render the ultimate mannequin

//The rotate perform permits the digital camera to rotate across the mannequin

void show()  GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.6, 0.6, 0.6, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

    DrawGiraffe();
    glutSwapBuffers();



void init() {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    GLubyte picture[64][64][3];
    int i, j, r, c;
    for (i = 0; i < 64; i++) {
        for (j = 0; j < 64; j++) {
            c = ((((i & 0x8) == 0) ^ ((j & 0x8)) == 0)) * 255;
            picture[i][j][0] = (GLubyte)c;
            picture[i][j][1] = (GLubyte)c;
            picture[i][j][2] = (GLubyte)c;
        }
    }
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, picture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    currentMaterial = &redPlasticMaterial;
    setMaterial(currentMaterial);
    glColor3f(1.0, 1.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 2.5);
    GLfloat light_pos[] = {2.0,2.0,2.0, 1.0};
    glLightfv(GL_LIGHT0, GL_POSITION, light_pos);     
}

int most important(int argc, char** argv) 
 GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("Giraffe");
    glutDisplayFunc(show);
    glutIdleFunc(rotate);
    init();
    glutMainLoop();

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments