opengl绘制曲面柱体的代码,和绘制圆柱体的一半的代码
时间: 2024-02-21 14:56:49 浏览: 15
绘制曲面柱体的代码:
```c
// 绘制曲面柱体
void drawSurfaceCylinder(float height, float radius, int slices, int stacks) {
float x = 0.0f, y = 0.0f, z = 0.0f;
float theta = 0.0f, phi = 0.0f;
float theta_step = 2 * M_PI / slices;
float phi_step = M_PI / stacks;
float texture_step_s = 1.0f / slices;
float texture_step_t = 1.0f / stacks;
float texture_s = 1.0f, texture_t = 1.0f;
float texture_s_step = -2.0f / slices;
float texture_t_step = -1.0f / stacks;
float radius_step = radius / stacks;
float radius_curr = radius;
glBegin(GL_TRIANGLE_STRIP);
for (int j = 0; j <= stacks; j++) {
x = radius_curr;
z = 0.0f;
for (int i = 0; i <= slices; i++) {
glTexCoord2f(texture_s, texture_t);
glNormal3f(x / radius_curr, y / radius_curr, z / radius_curr);
glVertex3f(x, y, z);
x = radius_curr * cos(theta);
z = radius_curr * sin(theta);
glTexCoord2f(texture_s, texture_t - texture_step_t);
glNormal3f(x / radius_curr, y / radius_curr, z / radius_curr);
glVertex3f(x, y + height / stacks, z);
theta += theta_step;
texture_s += texture_step_s;
}
y += height / stacks;
radius_curr -= radius_step;
phi += phi_step;
theta = 0.0f;
texture_s = 1.0f;
texture_t += texture_t_step;
}
glEnd();
}
```
绘制圆柱体一半的代码:
```c
// 绘制圆柱体一半
void drawHalfCylinder(float height, float radius, int slices) {
float x = 0.0f, y = 0.0f, z = 0.0f;
float theta = 0.0f;
float theta_step = 2 * M_PI / slices;
glBegin(GL_TRIANGLE_STRIP);
for (int i = 0; i <= slices; i++) {
x = radius * cos(theta);
z = radius * sin(theta);
glNormal3f(x / radius, y / radius, z / radius);
glVertex3f(x, y, z);
glNormal3f(x / radius, y / radius, z / radius);
glVertex3f(x, y + height, z);
theta += theta_step;
}
glEnd();
}
```
这两个函数都需要在程序中包含math.h和GL/glut.h头文件。