0
\$\begingroup\$

currently I re-write my codes and change them to make they are better.

I have vertex struct like this:

struct SVertex
{
    float X, Y, Z;
};

struct STexCoord
{
    float U, V;
};

And I re-wrote it like this

struct SVertex
{
    int Flag, BoneID;
    float Pos[3];
    float UV[2];
};

When it comes to the render function, I just need to change it from:

glVertexPointer(3, GL_FLOAT, 0, &(Mesh[i].Vertex[0]));
glTexCoordPointer(2, GL_FLOAT, 0, &(Mesh[i].TexCoord[0]));

to:

glVertexPointer(3, GL_FLOAT, sizeof (SVertex), &(Mesh[i].Vertex[0].Pos));
glTexCoordPointer(2, GL_FLOAT, sizeof (SVertex), &(Mesh[i].Vertex[0].UV));

The problem comes from the triangle struct. I re-wrote the Triangle from

struct STriangle
{
    int X, Y, Z;
};

to

struct STriangle
{
    int Flag, Group;
    int V[3], N[3];
};

and I changed the glDrawElements from:

glDrawElements(GL_TRIANGLES, Mesh[i].TriangleTotal*3, GL_UNSIGNED_INT, &(Mesh[i].Triangle[0]));

to

glDrawElements(GL_TRIANGLES, Mesh[i].TriangleTotal*3, GL_UNSIGNED_INT, &(Mesh[i].Triangle[0].V));

And I got messed vertex with the new triangle struct. If I use the old ones, it is correct. How to pass the new triangle struct to the glDrawElements?

\$\endgroup\$
  • \$\begingroup\$ For a start you should avoid using deprecated features such as glVertexPointer and glTexCoordPointer. Please refer to this OpenGL tutorial to see how to do modern OpenGL properly. \$\endgroup\$ – Soapy Jun 17 '15 at 10:36
  • \$\begingroup\$ Thanks, I'd like to learn how Vertex Array works before moving on to VBO \$\endgroup\$ – DavidJr Jun 17 '15 at 11:32
1
\$\begingroup\$

EDIT: There is actually no support for what you're trying to do. There is no equivalent function to glVertexPointer in OpenGL for the indexes. I earlier suggested glIndexPointer but it uses an array of colors, not indexes (a very misleading name, and my bad for posting it).

Unfortunately you will have to supply a contiguous array of indexes, or find another way to arrange your data in the structures. I searched for a while and stumbled upon this question in stackoverflow which is similar to what you are looking for, https://stackoverflow.com/questions/19057864/in-opengl-how-do-you-specify-indices-with-a-stride.

You should consider moving into modern OpenGL and start using VBOs. It's never too early to learn VBOs, it's probably best if you start learning VBOs sooner than later.

Timur's answer and Soapy's comment already have links to using VBOs and modern Open GL, do check them out.

\$\endgroup\$
  • \$\begingroup\$ -1 For suggesting the use of deprecated OpenGL features. \$\endgroup\$ – Soapy Jun 17 '15 at 10:37
  • 2
    \$\begingroup\$ @Soapy Fixed, I refer that in my answer now. The OP himself is using deprecated OpenGL features and I didn't question whether or not he is doing it because he chose to do so or doesn't know better. Nor did you. So I pointed out a deprecated solution to answer his question. \$\endgroup\$ – aslg Jun 17 '15 at 11:15
  • \$\begingroup\$ Thanks aslg, Yes I'd like to try to do animation. It's still too early to learn VBO, I'd like to learn the Vertex Array before moving to VBO. Btw, I did try your code but that makes the app crashed. Any solution? Thanks for your help. \$\endgroup\$ – DavidJr Jun 17 '15 at 11:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.