Use of geom:: enumerations

hello all,

I am in the middle of what must be one of the the steep parts of the learning curve with cinder and opengl: the beginning; sorry if this question is too basic :blush:.

the forums and samples have been extremely helpful, but sometimes it seems the simplest questions are hard to find answers to.

my question is about how one would use the primitive enums for geom:: …for example, I see that there are various shapes that can be supplied by geom for use in a batch. I am assuming the basic shapes are made from triangle strips? how does something like triangle fan fit into this? I have learned that you could supply an array of vertices and draw them using a triangle fan or whatever method separately but how do the primitives fit into geom and batch and how would you use the enums to change them?

I am often going back and forth between learning the raw opengl concepts and then figuring out how cinder is wrapping them up :no_mouth:. it is an absolutely fantastic interface but I can’t always connect the dots. thanks so much for any help and any advice for learning would be hugely appreciated. learning these things is my favorite creative outlet from my work (chemistry grad school, whew!)

1 Like

Good question!

If I’m changing the draw enum from TRIANGLES/TRIANGLE_STRIP/TRIANGLE_FAN, I am usually also providing my own gl::Vbo containing the relevant vertex data rather than using one of the provided geom::Source types. I haven’t yet found a need to change the enum used by one of the provided sources.

When doing custom shapes, I like to follow a pattern something like:

// Define some custom vertex type.
struct Vertex {
  vec3 position;
  vec2 uv;
  float custom_attribute; // name something meaningful to your app
};

// Create a collection of vertices.
vector<Vertex> vertices;
// Assume you fill the vertices with something meaningful.
// Here: a circle.
for (auto i = 0; i < 32; i += 1) {
  auto t = i / 31.0f;
  auto pos = vec3(cos(t), sin(t), 0.0f);
  auto uv = vec2(pos);
  vertices.push_back(Vertex{pos, uv, randFloat()});
}

// Upload your vertex data to the GPU.
auto buffer = gl::Vbo::create(GL_ARRAY_BUFFER, vertices);
// Create a layout so we can tell the GPU what our vertex data is.
geom::BufferLayout layout;
layout.append(geom::POSITION, geom::FLOAT, 3, sizeof(Vertex), offsetof(Vertex, position));
layout.append(geom::TEX_COORD_0, geom::FLOAT, 2, sizeof(Vertex), offsetof(Vertex, uv));
layout.append(geom::CUSTOM_0, geom::FLOAT, 1, sizeof(Vertex), offsetof(Vertex, custom_attribute));
// Create a mesh that contains the data and meaning of data.
// Here, you can pick what triangle-generating approach to use.
auto mesh = gl::VboMesh::create(vertices.size(), GL_TRIANGLE_FAN, {{layout, buffer}});

// Load a shader that uses your custom attribute.
auto glsl = gl::GlslProg::create(vs, fs);
// Create a batch that associates shader variables with the vertex data.
auto batch = gl::Batch::create(mesh, glsl, {{geom::CUSTOM_0, "CustomShaderVariableName"}});

Whew. It can be a bit long, but we basically need to layout our data in C++ then tell the GPU how our data is laid out, then tell the GPU what shader variables should use the different parts of the data in our layout. Not so bad, just need to tell the two computers (CPU and GPU) what they’re dealing with.

2 Likes

thanks so much! that makes sense. your code is very illustrative and that helps a bunch!

Hi,

in case you’re still wondering: the geom namespace also contains classes that allow you to create geometric primitives like a cube, a cylinder and even a teapot. All of them are constructed using either GL_TRIANGLES or GL_TRIANGLE_STRIP, as far as I can remember, whatever is more appropriate for the shape. The class makes sure the vertex and index buffers contain compatible data, with the correct polygon winding. Normals and tangents can also be calculated if required.

1 Like

thank you Paul! I have been playing with some of the supplied primitives to work with learning basic lighting since the normals are supplied with ciNormal.

I will make a new post about that if I can’t get it figured out. you guys are super helpful and I really appreciate the work you put in answering questions so often.

1 Like