Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


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: 477

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: 10042

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 slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis





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