|
楼主 |
发表于 2010-11-30 11:46:50
|
显示全部楼层
这是右边圆柱(osg::cylinder)的绘制过程
void DrawShapeVisitor::drawCylinderBody(unsigned int numSegments, float radius, float height)
{
const float angleDelta = 2.0f*osg:I/(float)numSegments;
const float texCoordDelta = 1.0f/(float)numSegments;
const float r = radius;
const float h = height;
float basez = -h*0.5f;
float topz = h*0.5f;
float angle = 0.0f;
float texCoord = 0.0f;
bool drawFrontFace = _hints ? _hints->getCreateFrontFace() : true;
bool drawBackFace = _hints ? _hints->getCreateBackFace() : false;
// The only difference between the font & back face loops is that the
// normals are inverted and the order of the vertex pairs is reversed.
// The code is mostly duplicated in order to hoist the back/front face
// test out of the loop for efficiency
GLBeginEndAdapter& gl = _state.getGLBeginEndAdapter();
gl.Begin(GL_QUAD_STRIP);
if (drawFrontFace) {
for(unsigned int bodyi=0;
bodyi<numSegments;
++bodyi,angle+=angleDelta,texCoord+=texCoordDelta)
{
float c = cosf(angle);
float s = sinf(angle);
gl.Normal3f(c,s,0.0f);
gl.TexCoord2f(texCoord,1.0f);
gl.Vertex3f(c*r,s*r,topz);
gl.TexCoord2f(texCoord,0.0f);
gl.Vertex3f(c*r,s*r,basez);
}
// do last point by hand to ensure no round off errors.
gl.Normal3f(1.0f,0.0f,0.0f);
gl.TexCoord2f(1.0f,1.0f);
gl.Vertex3f(r,0.0f,topz);
gl.TexCoord2f(1.0f,0.0f);
gl.Vertex3f(r,0.0f,basez);
}
if (drawBackFace) {
for(unsigned int bodyi=0;
bodyi<numSegments;
++bodyi,angle+=angleDelta,texCoord+=texCoordDelta)
{
float c = cosf(angle);
float s = sinf(angle);
gl.Normal3f(-c,-s,0.0f);
gl.TexCoord2f(texCoord,0.0f);
gl.Vertex3f(c*r,s*r,basez);
gl.TexCoord2f(texCoord,1.0f);
gl.Vertex3f(c*r,s*r,topz);
}
// do last point by hand to ensure no round off errors.
gl.Normal3f(-1.0f,0.0f,0.0f);
gl.TexCoord2f(1.0f,0.0f);
gl.Vertex3f(r,0.0f,basez);
gl.TexCoord2f(1.0f,1.0f);
gl.Vertex3f(r,0.0f,topz);
}
gl.End();
} |
|