{"title":"void DrawModelTriangles() {\n\tassert(collapse_map.size());\n\trenderpolycount=0;\n\tfor (unsigned int i = 0; i<tri.size(); i++) {\n\t\tint p0= Map(tri[i].v[0],render_num);\n\t\tint p1= Map(tri[i].v[1],render_num);\n\t\tint p2= Map(tri[i].v[2],render_num);\n\t\t// note: serious optimization opportunity here,\n\t\t// by sorting the triangles the following &quot;continue&quot; \n\t\t// could have been made into a &quot;break&quot; statement.\n\t\tif(p0==p1 || p1==p2 || p2==p0) continue;\n\t\trenderpolycount++;\n\t\t// if we are not currenly morphing between 2 levels of detail\n\t\t// (i.e. if morph=1.0) then q0,q1, and q2 are not necessary.\n\t\tint q0= Map(p0,(int)(render_numlodbase));\n\t\tint q1= Map(p1,(int)(render_numlodbase));\n\t\tint q2= Map(p2,(int)(render_num*lodbase));\n\t\tfloat3 v0, v1, v2;\n\t\tv0 = vert[p0]morph + vert[q0](1-morph);\n\t\tv1 = vert[p1]morph + vert[q1](1-morph);\n\t\tv2 = vert[p2]morph + vert[q2](1-morph);\n\t\tglBegin(GL_POLYGON);\n\t\t\t// the purpose of the demo is to show polygons\n\t\t\t// therefore just use 1 face normal (flat shading)\n\t\t\tfloat3 nrml = cross(v1-v0,v2-v1); \n\t\t\tif(0<length(nrml)) {\n\t\t\t\tglNormal3fv(normalize(nrml));\n\t\t\t}\n\t\t\tglVertex3fv(v0);\n\t\t\tglVertex3fv(v1);\n\t\t\tglVertex3fv(v2);\n\t\tglEnd();\n\t}\n}\n解析代码内容:这段代码是一个绘制模型三角形的函数。函数首先使用断言确保折叠映射表(collapse_map)不为空。然后使用一个循环遍历所有的三角形(tri)。\n\n在循环中,函数首先获取三角形的三个顶点的索引(p0、p1、p2)。如果任何两个顶点的索引相等,说明这个三角形是一个线段或者点,不需要绘制,所以使用continue语句跳过。\n\n如果三个顶点的索引都不相等,说明这是一个有效的三角形,renderpolycount加一。然后根据当前的细节级别(render_num)和形态(morph)计算出对应的顶点位置(v0、v1、v2)。\n\n接下来使用OpenGL的glBegin和glEnd函数开始和结束绘制多边形的过程。在这个过程中,将顶点的位置和法线传递给OpenGL进行绘制。\n\n最后,绘制完成后,函数返回。\n


原文地址: https://www.cveoy.top/t/topic/pFVa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录