Jump to content
  • Advertisement
Sign in to follow this  
cur_image

OpenGL glDrawArrays and glDrawElements

This topic is 2556 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have a job interview the next monday and I'm reviewing some concepts of OpenGL. I have a doubt about two questions that i found on internet and I don't know how to solve them.
Question 1:
[font="Verdana, Arial, Helvetica, sans-serif"]Quote: "You have this code:

GLfloat coords[4][12] = {
{ /* coords of an object (a quad) */ },
{ /* ... */ },
{ /* ... */ },
{ /* ... */ }
};

GLfloat colors[4][12] = {
{ /* colors of vertices of first object */ },
{ /* ... */ },
{ /* ... */ },
{ /* ... */ }
};

Matrix4 matrices[4] = { /* ... */ };

for (int i = 0; i < 4; ++i)
{
glPushMatrix();
glLoadMatrixf(matrices.pointer());
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, coords);
glColorPointer(3, GL_FLOAT, 0, colors);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glPopMatrix();
}

Optimize it :
Note, Matrix4 class is used as to represent a 4x4 matrix. Its methods can be used to perform matrix operations and data manipulation."
[/font]
[font="Verdana, Arial, Helvetica, sans-serif"]
[/font]
[font="Verdana, Arial, Helvetica, sans-serif"]I think the way to optimize it its using glDrawElements adding an index array. not?[/font]
[font="Verdana, Arial, Helvetica, sans-serif"]
[/font]
[font="Verdana, Arial, Helvetica, sans-serif"]Question 2:[/font]
[font="Verdana, Arial, Helvetica, sans-serif"]Quote: "The following code

GLint vertices[9][2] = {{0, 0}, {2, 1}, {1, 2}, {2, 0}, {0, 1}, {1, 0}, {0, 2}, {1, 1}, {2, 2}};
GLubyte indices[8][3] = {{5, 1, 7}, {4, 2, 6}, {7, 1, 8}, {0, 7, 4}, {5, 3, 1}, {7, 8, 2}, {0, 5, 7}, {4, 7, 2}};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_INT, 0, &vertices[0][0]);
glDrawElements(GL_TRIANGLES, 24, GL_UNSIGNED_BYTE, &indices[0][0]);

draws this:
(0,2) (1,2) (2,2)
+-------+-------+
| /| /|
| / | / |
| / | / |
| / | / |
| / | / |
| / | / |
|/ (1,1)|/ |
(0,1) +-------+-------+ (2,1)
| /| /|
| / | / |
| / | / |
| / | / |
| / | / |
| / | / |
|/ |/ |
+-------+-------+
(0,0) (1,0) (2,0)
a. Describe two problems with this rendering procedure, and why they are problems.
[/font][font="Verdana, Arial, Helvetica, sans-serif"]b. How can the code be improved?"[/font]
[font="Verdana, Arial, Helvetica, sans-serif"]
[/font]
[font=Verdana, Arial, Helvetica, sans-serif][size=2]Not idea of the answer for this question. [/font]
[font=Verdana, Arial, Helvetica, sans-serif][size=2]
[/font]
[font=Verdana, Arial, Helvetica, sans-serif][size=2]P[size=2]lease help me to figure out with this two questions.[size=2]Thanks.[/font]


[font="Verdana, Arial, Helvetica, sans-serif"]
[/font]


[font="Verdana, Arial, Helvetica, sans-serif"] [/font]

Share this post


Link to post
Share on other sites
Advertisement
Now that it's Monday, here's the most obvious place to start with number 1....

Matrix4 matrices[4] = { /* ... */ };

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, coords);
glColorPointer(3, GL_FLOAT, 0, colors);

glPushMatrix();

for (int i = 0; i < 4; ++i)
{
glLoadMatrixf(matrices.pointer());
glDrawArrays(GL_QUADS, 0, 4);
}

glPopMatrix();

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!