Jump to content

  • Log In with Google      Sign In   
  • Create Account

14 years ago on June 15th Gamedev.net was first launched! We want to thank all of you for being part of our community and hope the best years are ahead of us. Happy birthday Gamedev.net!

explaining a piece of code


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 AhmedCoeia   Members   -  Reputation: 127

Like
0Likes
Like

Posted 21 May 2012 - 02:45 PM

The following code is a calculation of a fish that is drawn by OpenGL, I would like someone to explain to me please.


GoldfishGeometry::GoldfishGeometry() :
m_BodySegments(50),
m_CircleSegments(25),
m_StripSegments(50)
{
m_InnerCurve.p0 = Vec2f(0.0f, 0.0f);
m_InnerCurve.p1 = Vec2f(0.8f, 0.1f);
m_InnerCurve.p2 = Vec2f(0.2f, 0.9f);
m_InnerCurve.p3 = Vec2f(0.0f, 1.0f);

m_OuterCurve.p0 = Vec2f(0.0f, 0.0f);
m_OuterCurve.p1 = Vec2f(0.8f, 0.1f);
m_OuterCurve.p2 = Vec2f(1.0f, 0.5f);
m_OuterCurve.p3 = Vec2f(1.0f, 1.0f);

m_EndCurve.p0 = Vec2f(0.0f, 0.0f);
m_EndCurve.p1 = Vec2f(5.0f, 0.1f);
m_EndCurve.p2 = Vec2f(5.0f, 0.5f);
m_EndCurve.p3 = Vec2f(0.0f, 1.0f);

std::vector< float > cylVerts;
cylVerts.reserve(((int)m_CircleSegments+1)*3*2*(int)m_BodySegments);

std::vector< float > cylNormals;
cylNormals.reserve(((int)m_CircleSegments+1)*3*2*(int)m_BodySegments);

std::vector< float > cylTexcoords;
cylTexcoords.reserve(((int)m_CircleSegments+1)*2*2*(int)m_BodySegments);

float x, y0, y1, z, u, v, angle;
float hStep = 1.0f / m_BodySegments;
float angleStep = ( 360.0f/(float)(m_CircleSegments) ) * 3.14159265f / 180.0f;
float texcoordStep = 1.0f/(float)(m_CircleSegments);

unsigned int hOffsetVerts = ((int)m_CircleSegments + 1)*3;
unsigned int hOffsetTex = ((int)m_CircleSegments + 1)*2;

for ( unsigned int h=0; h<(int)m_BodySegments; ++h )
{
for (unsigned int c=0; c<=(int)m_CircleSegments; ++c )
{
unsigned int offsetVerts = c*3;
unsigned int offsetTex = c*2;

angle = c*angleStep;

x = sin(angle);
z = cos(angle);
y0 = h*hStep; y1 = (h+1)*hStep;
u = c*texcoordStep;

cylVerts.push_back(x);
cylVerts.push_back(y0);
cylVerts.push_back(z);

cylNormals.push_back(x);
cylNormals.push_back(0.0f);
cylNormals.push_back(z);

cylTexcoords.push_back(u);
cylTexcoords.push_back(y0);

cylVerts.push_back(x);
cylVerts.push_back(y1);
cylVerts.push_back(z);

cylNormals.push_back(x);
cylNormals.push_back(0.0f);
cylNormals.push_back(z);

cylTexcoords.push_back(u);
cylTexcoords.push_back(y1);
}
}

std::vector< float > stripVerts;
std::vector< float > stripNormals;
std::vector< float > stripTexcoords;

stripVerts.reserve(((int)m_StripSegments)*3*2*(int)m_BodySegments);
stripNormals.reserve(((int)m_StripSegments)*3*2*(int)m_BodySegments);
stripTexcoords.reserve(((int)m_StripSegments)*2*2*(int)m_BodySegments);

float stripStep = 1.0f/(float)(m_StripSegments);

for ( unsigned int h=0; h<(int)m_BodySegments; ++h )
{
for (unsigned int v=0; v<(int)m_StripSegments; ++v )
{
u = v * stripStep;
y0 = h*hStep; y1 = (h+1)*hStep;
x = v * stripStep;

stripVerts.push_back(x);
stripVerts.push_back(y0);
stripVerts.push_back(0.0f);

stripNormals.push_back(0.0f);
stripNormals.push_back(0.0f);
stripNormals.push_back(1.0f);

stripTexcoords.push_back(u);
stripTexcoords.push_back(y0);

stripVerts.push_back(x);
stripVerts.push_back(y1);
stripVerts.push_back(0.0f);

stripNormals.push_back(0.0f);
stripNormals.push_back(0.0f);
stripNormals.push_back(1.0f);

stripTexcoords.push_back(u);
stripTexcoords.push_back(y1);
}
}

glGenVertexArrays(2, &m_VAO[0]);
glGenBuffers(6, &m_VBO[0]);

glBindVertexArray(m_VAO[0]);

glBindBuffer(GL_ARRAY_BUFFER, m_VBO[0]);
glBufferData(GL_ARRAY_BUFFER, ((int)m_CircleSegments+1)*3*2*((int)m_BodySegments)*sizeof(float), (const GLvoid *)&cylVerts[0], GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray((GLuint)0);

glBindBuffer(GL_ARRAY_BUFFER, m_VBO[1]);
glBufferData(GL_ARRAY_BUFFER, ((int)m_CircleSegments+1)*2*2*((int)m_BodySegments)*sizeof(float), (const GLvoid *)&cylTexcoords[0], GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray((GLuint)1);

glBindBuffer(GL_ARRAY_BUFFER, m_VBO[2]);
glBufferData(GL_ARRAY_BUFFER, ((int)m_CircleSegments+1)*3*2*((int)m_BodySegments)*sizeof(float), (const GLvoid *)&cylNormals[0], GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray((GLuint)2);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

glBindVertexArray(m_VAO[1]);

glBindBuffer(GL_ARRAY_BUFFER, m_VBO[3]);
glBufferData(GL_ARRAY_BUFFER, ((int)m_StripSegments)*3*2*((int)m_BodySegments)*sizeof(float), (const GLvoid *)&stripVerts[0], GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray((GLuint)0);

glBindBuffer(GL_ARRAY_BUFFER, m_VBO[4]);
glBufferData(GL_ARRAY_BUFFER, ((int)m_StripSegments)*2*2*((int)m_BodySegments)*sizeof(float), (const GLvoid *)&stripTexcoords[0], GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray((GLuint)1);

glBindBuffer(GL_ARRAY_BUFFER, m_VBO[5]);
glBufferData(GL_ARRAY_BUFFER, ((int)m_StripSegments)*3*2*((int)m_BodySegments)*sizeof(float), (const GLvoid *)&stripNormals[0], GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray((GLuint)2);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);


}

Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 3861

Like
3Likes
Like

Posted 21 May 2012 - 03:19 PM

It looks like this code is supposed to create a fish from some geometry (by starting with a few default lines and "shaping" the fish by pushing vertices back and forth to approximate a goldfish's outline, using a sinuosidal pattern), and then displays it via OpenGL (apparently just using solid lines). In immediate mode.

Talk about reusability...

"The best comment is a deleted comment."
website · blog





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS