Let me ask you an OpenGL man-making question

Asked 2 years ago, Updated 2 years ago, 92 views

How are you? I am a student studying OpenGL. I'm just using OpenGL to squeeze the sauce, but I can't do it with my humble skills, so I end up leaving a question at dawn. The source is given a notepad, which contains information about the apex of a person's appearance and information about faces.

The first picture in the picture above is the information about each vertex. 10113 is the number of vertices. The source of the human figure is made up of 10,113 vertices. The decimal places below that are written in three units, the coordinates of the vertices. The coordinates of the first vertex would be 91.459999 - 169.619995 -89.230003. So there are 1,0113 of them.This is the number zero vertex and it's stored in order.

202222 in the second picture is the number of faces. This figure is made up of 20222 sides. The numbers that have since been broken into three units are the numbers of the vertices. They're broken into three units, so these sides are triangles. The first side consists of the 16th, 20th, and 19th vertices.This is going to be number 0 and it's going to be saved in order.

All you have to do is get the information in this notebook using the file input/output function and draw it based on this vertex.

structureFileVertex // Vertex information read from the file
{
    float x;
    float y;
    float z;
    vector3d normal; // normal vector at that point
};

structureFileFace // Information on the side read from the file
{
    int fn1; // vertex of face 1
    intfn2; // vertex of face 2
    intfn3; // vertex of face 3
    vector3d normal; // normal vector on that plane
};

This is a structure that stores information read from notepad. Saved using dynamic assignments.

I've checked reading the information on the vertices and sides, so I won't upload the source that I read, judging that it's not a problem. But the problem is to draw it later, and before you draw it, you have to find the normal vectors for these vertices.

To get the normal vectors of these vertices, we're going to get the normal vectors of all the faces that are adjacent to these vertices, that is, the normal vectors of these vertices if we take the average.

for (inti = 0; i < numOfFace; i++) // Calculating the normal vector of each side in advance
    ff[i].normal = Face_Normal_Computing(ff[i]);

// Obtain the average of the normal vectors of adjacent faces at the corresponding vertex
for (inti = 0; i < numOfVertex; i++) // vertex-moving loop
{
    int n_count = 0;
    For (int j = 0; j < numOfFace; j++) // Face Check Loop
    {
        if (i == ff[j].fn1 | i == ff[j].fn2 | i == ff[j].fn3) // If the i-th vertex belongs to the j-th plane,
        {
            fv[i].normal.x += ff[j].normal.x;
            fv[i].normal.y += ff[j].normal.y;
            fv[i].normal.z += ff[j].normal.z;
            n_count++; // Count the number of adjacent faces
        }
    }
    fv[i].Normal.x /= n_count; // Find the mean of the normal vector
    fv[i].normal.y /= n_count; // Find the average of the normal vector
    fv[i].Normal.z /= n_count; // Find the mean of the normal vector
}

The Face_Normal_Computing function in the first for statement is a function that calculates the normal vector. For vertices on all sides, We calculated the normal vector and then calculated it in advance so that only the normal vector of the faces containing the vertices can be immediately inserted.

And then we'll look at the double for statement. i is the for statement for the vertex and j is the information for the face. So we start with i=0, which is vertex zero.

Check if this vertex is included from the 0th face, starting from the 0th vertex.                                       The 0 side consists of the 16th, 20th, and 19th vertices, so there is no 0th vertex.                                     The first side consists of 16th, 15th, and 0th vertices, so there are 0th vertices.

So now we're running the if statement, and we're adding the normal vector we calculated earlier to that vertex. Add it whenever it is included. If you exit the inner for statement, it means that the vertex does not contain any side, so if you divide the n_count, it becomes the real normal vector at that vertex.

voidDraw_Sphere(void) // Create faces with vertices created in Vertex_Generation, and draw phrases
{
    for (int i = 0; i < numOfVertex; i+=3)
    {
        glBegin(GL_POLYGON);
            glNormal3f(fv[i].x, fv[i].y, fv[i].z)z);
            glVertex3f(fv[i].x, fv[i].y, fv[i].z)z);
            glNormal3f(fv[i + 1].x, fv[i + 1].y, fv[i + 1].z)z);
            glVertex3f(fv[i + 1].x, fv[i + 1].y, fv[i + 1].z)z);
            glNormal3f(fv[i + 2].x, fv[i + 2].y, fv[i + 2].z)z);
            glVertex3f(fv[i + 2].x, fv[i + 2].y, fv[i + 2].z)z);
        glEnd();
    }
}

If you draw the vertices that you calculated using the function above, I expected it to come out It doesn't come out at all.I don't know what's wrong or what's wrong with logic... I'd appreciate your help. If you need the entire source, I will revise the text and upload it again. It's long, but I'd appreciate it if you could help me

opengl c

2022-09-22 20:10

1 Answers

glBegin(GL_TRIANGLES);
  glNormal3f(n1x, n1y, n1z);
  glVertex3f(v1x, v1y, v1z);
  glNormal3f(n2x, n2y, n2z);
  glVertex3f(v2x, v2y, v2z);
  glNormal3f(n3x, n3y, n3z);
  glVertex3f(v3x, v3y, v3z);
glEnd();

This is how much can be inferred by the code.


2022-09-22 20:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.