matzar
matzar

Reputation: 305

Cylinder drawn with triangles in OpenGL

It's drawing a cylinder with stacks and edges but the problem is that stacks are connected to one point instead of a new one. Maybe a picture will show it better: Cylinder

And here's how I'm rendering the side because disks are rendered separately:

for (int i = 1; i <= height; ++i) {
        for (int j = 0; j < edges; ++j) {
            glBegin(GL_TRIANGLES); {
                // 0 bottom 
                glVertex3f(x + radius * cos(theta + interval), y , z + radius * sin(theta + interval));
                // 1 bottom
                glVertex3f(x + radius * cos(theta), y + y_value * i, z + radius * sin(theta));
                // 2 top
                glVertex3f(x + radius * cos(theta), y + y_value * i, z + radius * sin(theta));
                // 2 top
                glVertex3f(x + radius * cos(theta), y + y_value * i, z + radius * sin(theta));
                // 3 top
                glVertex3f(x + radius * cos(theta + interval), y + y_value * i, z + radius * sin(theta + interval));
                // 0 bottom 
                glVertex3f(x + radius * cos(theta + interval), y , z + radius * sin(theta + interval));
            } glEnd();
            theta += interval;
        }
        theta = 0.0;
    }

I've been trying to solve it for days and I run out of ideas. Do you know what I am doing wrong?

UPDATE: I've changed it to be render with quads using ybungalobill advice. Now I'm struggling with UV mapping. And hopefully once this part is solved it'll be easy enough to convert into triangles. That's what I have now: enter image description here

And that's the code I'm using for UV mapping:

u = 0.0,
v = 0.0,
u_inter = 1.0 / edges,
v_inter = 1.0 / y_value;  // (y_value = height / edges)

    for (int i = 1; i <= height; ++i) {
        for (int j = 0; j < edges; ++j) {
            glBegin(GL_QUAD_STRIP); {
                // 0 bottom 
                glTexCoord2f(u, v);
                // 1 bottom
                glTexCoord2f(u + u_inter, v);
                // 2 top
                glTexCoord2f(u + u_inter, v + v_inter);
                // 3 top
                glTexCoord2f(u, v + v_inter);
            } glEnd();
            theta += interval;
            u += u_inter;
        }
        v += v_inter;
        theta = 0.0;
    }

Upvotes: 3

Views: 2346

Answers (1)

Yakov Galka
Yakov Galka

Reputation: 72479

float y0 = y + y_value * (i-1);
float y1 = y + y_value * i;
// 0 bottom 
glVertex3f(x + radius * cos(theta + interval), y0, z + radius * sin(theta + interval));
// 1 bottom
glVertex3f(x + radius * cos(theta), y0, z + radius * sin(theta));
// 2 top
glVertex3f(x + radius * cos(theta), y1, z + radius * sin(theta));
// 2 top
glVertex3f(x + radius * cos(theta), y1, z + radius * sin(theta));
// 3 top
glVertex3f(x + radius * cos(theta + interval), y1, z + radius * sin(theta + interval));
// 0 bottom 
glVertex3f(x + radius * cos(theta + interval), y0, z + radius * sin(theta + interval));

Upvotes: 2

Related Questions