vendredi 24 juillet 2015

Creating cylinder vertex positions for a vbo

I've set up a pretty basic openGL context, (I'm using GLFW, GLM and GLEW as that may help to know) and have used it to generate a simple grid mesh with optimised indexing.

I use VBO's and VAO's, as well as a Vertex struct to draw my various meshes, and have drawn cubes & a grid, which I turned into a terrain with a heightmap. However, I'm now wanting to create a set of vertex positions that would draw a cylinder (essentially like a tunnel that I can look both at the inside and outside of, although I could always draw two seperate meshes once I have the drawing set) as I understand that gluCylinder and glu in general is bad practice in the modern graphics pipeline.

I'm generating a basic grid using something like this when my program starts:

for (int x = 0; x <= (int)channels; ++x)
{
    for (int z = 0; z <= (int)scans; ++z)
    {
        //TODO - this should be where we draw a cylinder for the pipeline
        glm::vec3 thisVertex = glm::vec3(x*length,0,-z*length); //basic XYZ for a grid for now

        pipe_data[x + (z * (scans + 1))].p = thisVertex;
        pipe_data[x + (z * (scans + 1))].globalUV = glm::vec2((float)x / scans, (float)z / channels); //global uv

    }
}

And after binding the buffers etc, I actually call to render each frame like this:

assert(scene_ != nullptr);

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
const float aspect_ratio = viewport[2] / (float)viewport[3];

std::shared_ptr<GLCamera> camera = scene_->getCamera();
glm::mat4 projection_xform = glm::perspective(camera->getFOV(),
                                              aspect_ratio,
                                              camera->getNearPlane(),
                                              camera->getFarPlane());
glm::vec3 camera_pos = camera->getPosition();
glm::vec3 camera_at = camera->getPosition() + camera->getDirection();
glm::vec3 world_up{ 0, 1, 0 };
glm::mat4 view_xform = glm::lookAt(camera_pos, camera_at, world_up);

glClearColor(0.f, 0.f, 0.75f, 0.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_FRONT_AND_BACK, shade_normals_ ? GL_FILL : GL_LINE);

glUseProgram(pipe_sp_);

GLuint shading_id = glGetUniformLocation(pipe_sp_, "use_normal");
glUniform1i(shading_id, shade_normals_);

glm::mat4 world_xform = glm::mat4(1);
glm::mat4 view_world_xform = view_xform * world_xform;

GLuint projection_xform_id = glGetUniformLocation(pipe_sp_, "projection_xform");
glUniformMatrix4fv(projection_xform_id, 1, GL_FALSE, glm::value_ptr(projection_xform));

GLuint view_world_xform_id = glGetUniformLocation(pipe_sp_, "view_world_xform");
glUniformMatrix4fv(view_world_xform_id, 1, GL_FALSE, glm::value_ptr(view_world_xform));

glBindVertexArray(pipe_mesh_.vao);
glDrawElements(GL_TRIANGLES, pipe_mesh_.element_count, GL_UNSIGNED_INT, 0);

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

So I've tested this with a grid and know it works, and I know where the math for working out the vertex positions should go, but I'm having a really hard time understanding how I would go about putting the vertices in place to generate a cylinder.

I assumed that if the cylinder didn't need to actually join at the end I could just draw a grid but modify the code slightly to 'bend' it round in a sort of pipe/toilet roll style, but again, I'm lost at how to calculate this.

Thanks to anyone who took the time to check through all this, I could really use some guidance!

Aucun commentaire:

Enregistrer un commentaire