從頭開始繪製一個圓錐體,從頭開始圓錐體
opengl協助庫glu裡有一個對象叫做二次幾何體,可以用來給球體圓錐體建模,然而在opengles中不能使用glu庫,那麼我們只能自己寫方法替代它了,上次給球體建了模,這次應該給圓錐體建模了.
圓錐體是平面上的一個圓以及它的所有切線和平面外一點確定的平面圍成的幾何體,圓被稱為底面,頂點被稱為尖端,定義聽上去有些複雜,那麼看圖:
嗯,懂了吧.其實它是一種特殊情況,如果尖端是一個圓面的話那麼它就是一個截頭圓錐體,頂面半徑為0的截頭圓錐體就是圓錐體了.
先來看下標頭檔是怎麼定義的吧:
class Cone {private:GLuint* vboId;GLuint vert,texcoord,norm;GLfloat* verts;GLfloat* texcoords;GLfloat* norms;int vertNum,topVertNum,sideVertNum;float radiusTop,radiusBottom,yHeight;//頂面半徑,底面半徑,高度void calculateSideNormal(float& x,float& y,float& z);//計算側面法線public:Cone(float rt,float rb,int m,float height);~Cone();void render();};
側面法線的計算方法,先取底面任意一點,求得圓心到該點的向量,然後沿著此向量把該截頭圓錐體劈開,
然後把紅色的向量沿著它的方向平移到圓周上的點,:
利用相似三角形即可求出紫色的法線N,具體代碼如下:
void Cone::calculateSideNormal(float& x,float& y,float& z) {y=(radiusBottom-radiusTop)/yHeight*sqrtf(x*x+z*z);if(x==0.0&&z==0.0)y=1.0;} 這邊的(x,y,z)就是法向量了,x和z都是圓周上那點的座標(x,z),y依靠相似三角形可以求出
接著是具體的建模代碼:
Cone::Cone(float rt,float rb,int m,float height) {radiusTop=rt;radiusBottom=rb;yHeight=height;vertNum=m*3*2+m*4;topVertNum=m*3*2;sideVertNum=m*4;verts=new GLfloat[vertNum*3];norms=new GLfloat[vertNum*3];texcoords=new GLfloat[vertNum*2];float stepAngZ=PI2/m;float angZ=0.0;int index=0;int indexNorm=0;int indexTex=0;for(int i=0;i<m;i++) {//bottomfloat x1=cos(angZ)*rb;float y1=0.0;float z1=sin(angZ)*rb;verts[index]=x1; index++;verts[index]=y1; index++;verts[index]=z1; index++;float nx1=0.0;float ny1=-1.0;float nz1=0.0;norms[indexNorm]=nx1; indexNorm++;norms[indexNorm]=ny1; indexNorm++;norms[indexNorm]=nz1; indexNorm++;float u1=angZ/PI2;float v1=1.0;texcoords[indexTex]=u1; indexTex++;texcoords[indexTex]=v1; indexTex++;float x2=cos(angZ+stepAngZ)*rb;float y2=0.0;float z2=sin(angZ+stepAngZ)*rb;verts[index]=x2; index++;verts[index]=y2; index++;verts[index]=z2; index++;norms[indexNorm]=nx1; indexNorm++;norms[indexNorm]=ny1; indexNorm++;norms[indexNorm]=nz1; indexNorm++;float u2=(angZ+stepAngZ)/PI2;float v2=1.0;texcoords[indexTex]=u2; indexTex++;texcoords[indexTex]=v2; indexTex++;float x3=0.0;float y3=0.0;float z3=0.0;verts[index]=x3; index++;verts[index]=y3; index++;verts[index]=z3; index++;norms[indexNorm]=nx1; indexNorm++;norms[indexNorm]=ny1; indexNorm++;norms[indexNorm]=nz1; indexNorm++;float u3=0.0;float v3=0.0;texcoords[indexTex]=u3; indexTex++;texcoords[indexTex]=v3; indexTex++;//topfloat x4=cos(angZ+stepAngZ)*rt;float y4=height;float z4=sin(angZ+stepAngZ)*rt;verts[index]=x4; index++;verts[index]=y4; index++;verts[index]=z4; index++;float nx4=0.0;float ny4=1.0;float nz4=0.0;norms[indexNorm]=nx4; indexNorm++;norms[indexNorm]=ny4; indexNorm++;norms[indexNorm]=nz4; indexNorm++;float u4=(angZ+stepAngZ)/PI2;float v4=1.0;texcoords[indexTex]=u4; indexTex++;texcoords[indexTex]=v4; indexTex++;float x5=cos(angZ)*rt;float y5=height;float z5=sin(angZ)*rt;verts[index]=x5; index++;verts[index]=y5; index++;verts[index]=z5; index++;norms[indexNorm]=nx4; indexNorm++;norms[indexNorm]=ny4; indexNorm++;norms[indexNorm]=nz4; indexNorm++;float u5=angZ/PI2;float v5=1.0;texcoords[indexTex]=u5; indexTex++;texcoords[indexTex]=v5; indexTex++;float x6=0.0;float y6=height;float z6=0.0;verts[index]=x6; index++;verts[index]=y6; index++;verts[index]=z6; index++;norms[indexNorm]=nx4; indexNorm++;norms[indexNorm]=ny4; indexNorm++;norms[indexNorm]=nz4; indexNorm++;float u6=0.0;float v6=0.0;texcoords[indexTex]=u6; indexTex++;texcoords[indexTex]=v6; indexTex++;angZ+=stepAngZ;}angZ=0.0;for(int i=0;i<m;i++) {//sidefloat x7=cos(angZ+stepAngZ)*rb;float y7=0.0;float z7=sin(angZ+stepAngZ)*rb;verts[index]=x7; index++;verts[index]=y7; index++;verts[index]=z7; index++;float nx7=x7;float ny7=0;float nz7=z7;calculateSideNormal(nx7,ny7,nz7);norms[indexNorm]=nx7; indexNorm++;norms[indexNorm]=ny7; indexNorm++;norms[indexNorm]=nz7; indexNorm++;float u7=(angZ+stepAngZ)/PI2;float v7=1.0;texcoords[indexTex]=u7; indexTex++;texcoords[indexTex]=v7; indexTex++;float x8=cos(angZ)*rb;float y8=0.0;float z8=sin(angZ)*rb;verts[index]=x8; index++;verts[index]=y8; index++;verts[index]=z8; index++;float nx8=x8;float ny8=0;float nz8=z8;calculateSideNormal(nx8,ny8,nz8);norms[indexNorm]=nx8; indexNorm++;norms[indexNorm]=ny8; indexNorm++;norms[indexNorm]=nz8; indexNorm++;float u8=angZ/PI2;float v8=1.0;texcoords[indexTex]=u8; indexTex++;texcoords[indexTex]=v8; indexTex++;float x10=cos(angZ)*rt;float y10=height;float z10=sin(angZ)*rt;verts[index]=x10; index++;verts[index]=y10; index++;verts[index]=z10; index++;float nx10=x10;float ny10=0;float nz10=z10;calculateSideNormal(nx10,ny10,nz10);norms[indexNorm]=nx10; indexNorm++;norms[indexNorm]=ny10; indexNorm++;norms[indexNorm]=nz10; indexNorm++;float u10=angZ/PI2*rt/rb;float v10=0.0;texcoords[indexTex]=u10; indexTex++;texcoords[indexTex]=v10; indexTex++;float x11=cos(angZ+stepAngZ)*rt;float y11=height;float z11=sin(angZ+stepAngZ)*rt;verts[index]=x11; index++;verts[index]=y11; index++;verts[index]=z11; index++;float nx11=x11;float ny11=0;float nz11=z11;calculateSideNormal(nx11,ny11,nz11);norms[indexNorm]=nx11; indexNorm++;norms[indexNorm]=ny11; indexNorm++;norms[indexNorm]=nz11; indexNorm++;float u11=(angZ+stepAngZ)/PI2*rt/rb;float v11=0.0;texcoords[indexTex]=u11; indexTex++;texcoords[indexTex]=v11; indexTex++;angZ+=stepAngZ;}vboId=new GLuint[3];vert=0;texcoord=1;norm=2;glGenBuffers(3,vboId);glBindBuffer(GL_ARRAY_BUFFER_ARB, vboId[vert]);glBufferData(GL_ARRAY_BUFFER_ARB, vertNum*3*sizeof(GLfloat),verts, GL_STATIC_DRAW_ARB);glBindBuffer(GL_ARRAY_BUFFER_ARB, vboId[texcoord]);glBufferData(GL_ARRAY_BUFFER_ARB, vertNum*2*sizeof(GLfloat),texcoords, GL_STATIC_DRAW_ARB);glBindBuffer(GL_ARRAY_BUFFER_ARB, vboId[norm]);glBufferData(GL_ARRAY_BUFFER_ARB, vertNum*3*sizeof(GLfloat),norms, GL_STATIC_DRAW_ARB);delete[] verts;delete[] texcoords;delete[] norms;}
和上次球體的代碼很像,紋理座標底面與頂面的變化程度一致以防貼圖分布不均勻.
這樣模型就建立完成了,渲染效果如下所示: