OpenGL immediate mode: Replacement for gluCylinder

This commit is contained in:
Clément Foucault 2017-02-12 19:19:35 +01:00
parent 1634cef2b3
commit 9c35907ca2
2 changed files with 65 additions and 0 deletions

View File

@ -137,6 +137,20 @@ void imm_draw_line(unsigned pos, float x1, float y1, float x2, float y2);
*/
void imm_cpack(unsigned int x);
/**
* Draw a cylinder. Replacement for gluCylinder.
* _warning_ : Slow, better use it only if you no other choices.
*
* \param pos The vertex attribute number for position.
* \param nor The vertex attribute number for normal.
* \param base Specifies the radius of the cylinder at z = 0.
* \param top Specifies the radius of the cylinder at z = height.
* \param height Specifies the height of the cylinder.
* \param slices Specifies the number of subdivisions around the z axis.
* \param stacks Specifies the number of subdivisions along the z axis.
*/
void imm_cylinder(unsigned int pos, unsigned int nor, float base, float top, float height, int slices, int stacks);
/**
* Returns a float value as obtained by glGetFloatv.
* The param must cause only one value to be gotten from GL.

View File

@ -251,6 +251,57 @@ void imm_cpack(unsigned int x)
(((x) >> 16) & 0xFF));
}
void imm_cylinder(unsigned int pos, unsigned int nor, float base, float top, float height, int slices, int stacks)
{
immBegin(GL_TRIANGLES, 6 * slices * stacks);
for (int i = 0; i < slices; ++i) {
const float angle1 = 2 * M_PI * ((float)i / (float)slices);
const float angle2 = 2 * M_PI * ((float)(i+1) / (float)slices);
const float cos1 = cosf(angle1);
const float sin1 = sinf(angle1);
const float cos2 = cosf(angle2);
const float sin2 = sinf(angle2);
for (int j = 0; j < stacks; ++j) {
float fac1 = (float)j / (float)stacks;
float fac2 = (float)(j+1) / (float)stacks;
float r1 = base * (1.f - fac1) + top * fac1;
float r2 = base * (1.f - fac2) + top * fac2;
float h1 = height * ((float)j / (float)stacks);
float h2 = height * ((float)(j+1) / (float)stacks);
float v1[3] = {r1 * cos2, r1 * sin2, h1};
float v2[3] = {r2 * cos2, r2 * sin2, h2};
float v3[3] = {r2 * cos1, r2 * sin1, h2};
float v4[3] = {r1 * cos1, r1 * sin1, h1};
float n1[3], n2[3];
/* calc normals */
sub_v3_v3v3(n1, v2, v1);
normalize_v3(n1);
n1[0] = cos1; n1[1] = sin1; n1[2] = 1-n1[2];
sub_v3_v3v3(n2, v3, v4);
normalize_v3(n2);
n2[0] = cos2; n2[1] = sin2; n2[2] = 1-n2[2];
/* first tri */
immAttrib3fv(nor, n2);
immVertex3fv(pos, v1);
immVertex3fv(pos, v2);
immAttrib3fv(nor, n1);
immVertex3fv(pos, v3);
/* second tri */
immVertex3fv(pos, v3);
immVertex3fv(pos, v4);
immAttrib3fv(nor, n2);
immVertex3fv(pos, v1);
}
}
immEnd();
}
float glaGetOneFloat(int param)
{
GLfloat v;