2012-06-28 133 views
0

所以我在openGL中創建了一個圓柱體,我創建的圓柱體不是使用gluCylinder,而是使用分段的GL_QUAD_STRIP給出圓管的印象。我想得到一個紋理圖像來包裹整個圓柱體,但不知道如何;在OpenGL紋理方面,我仍然是一個新手,所以我可能不理解像我以前那樣的紋理。下面是我有畫缸,並嘗試紋理它的代碼:在OpenGL中使用GL_QUAD_STRIP製作圓柱體的紋理貼圖

float x,z,angle;   // Used to calculate cylinder wall 
float height = 75.0f;  // Height of the cylinder 
float diameter = 10.0f;  // Diameter of the cylinder, or more specifically, 
float normal[3],corners[2][3]; // Storeage for vertex calculations 
float numSides = 100.0f; 
float step; 
float sideLength; 
float perimeter;   // Not perimeter of ideal circular cylinder 
          // but actual perimeter of what is drawn 
int whichSide;    // Used to keep track of which side you are on during the loop 

step = (GL_PI/numSides/2.0f); // Approximate the cylinder wall with 
           // some number of flat segments 
sideLength = 
    2.0f*diameter*(float)sin(GL_PI/(2.0f*numSides)); // Calculate the length of each side 

perimeter = numSides * sideLength; 

printf("There are %f sides on the small cylinder\nthat are each %f units 
    long.\nThe dimensions of the texture must be\nclose to %f x %f\n\n\n", numSides, 
    sideLength, perimeter, height); 



// Set material color for head of screw 
glColor3f(1.0f, 1.0f, 1.0f); 

// Assemble the wall as 100 quadrilaterals formed by 
// placing adjoining Quads together 
glFrontFace(GL_CCW); 
glBegin(GL_QUAD_STRIP); 


for(angle = (2.0f*GL_PI); angle > 0.0f; angle -= step) 
{ 
    // Calculate x and y position of the first vertex 
    x = diameter*(float)sin(angle); 
    z = diameter*(float)cos(angle); 

    // Get the coordinate for this point and extrude the 
    // length of the cylinder. 
    corners[0][0] = x; 
    corners[0][1] = -height/2.0f; 
    corners[0][2] = z; 

    corners[1][0] = x; 
    corners[1][1] = height/2.0f; 
    corners[1][2] = z; 

    // Instead of using real normal to actual flat section 
    // Use what the normal would be if the surface was really 
    // curved. Since the cylinder goes up the Y axis, the normal 
    // points from the Y axis out directly through each vertex. 
    // Therefore we can use the vertex as the normal, as long as 
    // we reduce it to unit length first and assume the y component 
    // to be zero 
    normal[0] = corners[1][0]; 
    normal[1] = 0.0f; 
    normal[2] = corners[1][2]; 




//@@@@@#####*****TEXTURING DONE HERE *****#####@@@@@// 

    // Reduce to length of one and specify for this point 
    ReduceToUnit(normal); 
    glNormal3fv(normal); 
    glTexCoord2f(((float)whichSide)/numSides,0.0f); 
    glVertex3fv(corners[0]); 
    glTexCoord2f(((float)whichSide)/numSides,1.0f); 
    glVertex3fv(corners[1]); 

    whichSide++; 
} 

回答

3

如果您覺得紋理映射過程的包裝紙一張紙缸(氣缸,這樣高度圍繞=的高度紙張和圓柱周長=紙張的寬度),所有您需要做的就是生成texcoords,它隨着角度改變Y(或X)(但是這將從0-1變爲0,而角度從0變到Pi)。另一個紋理座標X(或Y)將爲0和1,因此它映射到該軸上的全部範圍。

+0

工作,謝謝一堆! – 23ChrisChen

相關問題