gpt4 book ai didi

c - 纹理映射到在 OpenGL 中使用 GL_QUAD_STRIP 制作的圆柱体

转载 作者:太空宇宙 更新时间:2023-11-04 07:33:21 24 4
gpt4 key购买 nike

所以我在 openGL 中有一个圆柱体,我不是使用 gluCylinder 创建的,而是使用分段的 GL_QUAD_STRIP 来给人一种圆管的印象。我想得到一个纹理图像来环绕圆柱体,但不知道如何;当涉及到 OpenGL 中的纹理时,我仍然是一个新手,所以也许我不像我想的那样理解纹理。这是我绘制圆柱体并尝试对其进行纹理化的代码:

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++;
}

最佳答案

如果您将纹理映射过程想象成将一张纸包裹在圆柱体周围(因此圆柱体的高度 = 纸张的高度,圆柱体的周长 = 纸张的宽度),您需要做的就是生成随角度改变 Y(或 X)的纹理坐标(但这将从 0-1 变化,而角度从 0 变化到 Pi)。另一个纹理坐标 X(或 Y)将为 0 和 1,因此它映射到该轴上的完整范围。

关于c - 纹理映射到在 OpenGL 中使用 GL_QUAD_STRIP 制作的圆柱体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11252165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com