# Adding vertex to ofMesh resulting in flat surface

Hello there!

I’m trying to create a surface in 3 dimensions. I have a formula that plots all the points of my surface and when I draw the points by themselves, I get the end result that I’m looking for, but adding these points to ofMesh always generates a flat surface when drawing the vertices.

My point generation looks like this (it generates a Coons patch):

`````` for(double i = 0; i <= size; i++){
for(double j = 0; j <= size; j++){
double s = i / (double)size;
double t = j / (double)size;
points.push_back(((1 - t) * s4->getPointX(i) + t * s2->getPointX(i)) +
((1 - s) * s1->getPointZ(j) + s * s3->getPointZ(j)) -
((1 - s) * (1 - t) * p1 +
(1 - s) * t * p2 +
s * (1 - t) * p4 +
s * t * p3));
}
}
``````

And if I try to draw all the points individually I get this:

However, if I add the points as vertices and ask ofMesh to draw Vertices, I only get a flat surface:

``````int size = 200;
mesh = ofMesh::plane(size, size, size, size, OF_PRIMITIVE_TRIANGLES);
for(int i = 0; i < size; i++){
mesh.setVertex(i, points[i]);
}
``````

Result:

Asking it to draw wireframe is even weirder:

The shape sticks to one side of the surface and then does a weird flip to where the vertices of the 1st image are. I tried changing the order of how my points are added but it doesn’t seem to have much effect

Anyone has any idea why all my vertices are on a flat surface rather than following those that I can draw outside of ofMesh?

That’s super weird. My guess is either A) you’re trying to hijack the vertices created using OF_PRIMITIVE_TRIANGLES or B) you’re filling your mesh with a primitive plane then trying to change them later

A) OF_PRIMITIVE_TRIANGLES mode assembles triangles by taking vertices in groups of 3. That means a simple plane of 4 corners (clockwise from top left corner 0, 1, 2 and 3) will take 6 vertices, with triangle 1 being corners 0-1-2 and triangle 2 being corners 2-3-0. You can either add all your points as you are and connect them with the proper indices like on the ofMesh example page or you can add points in the correct order so that the vertices are in the proper order to assemble triangles 0, 1, 2 then 3, 4, 5, etc. I recommend the former since you already have all your points arranged point by point, line by line, like the vertices in the example above.

B) Another (possible related) issue is you are borrowing the internal structure of a plane primitive then trying to reset the vertices but leaving the internal index structure as a plane. So the points you are giving it don’t line up with the vertex ordering the indices are expecting to make a plane.

Your best bet is to use addVertex() to put points in a fresh ofMesh then assemble the indices as in the link above. Shouldn’t be too hard since you already know the number of rows and columns. Try that out and let us know how it goes.

I’m not entirely sure what the issue was although what you said helped me fix my issue.

The weird wireframe effect was just because I was modifying vertices from a mesh instantiated as a plane so I just removed the code.
The weird thing is that while this works without issues:

``````for(double i = 0; i <= size; i++){
for(double j = 0; j <= size; j++){
double s = i / (double)size;
double t = j / (double)size;
ofPoint tmp = ((1 - t) * s4->getPointX(i) + t * s2->getPointX(i)) +
((1 - s) * s1->getPointZ(j) + s * s3->getPointZ(j)) -
((1 - s) * (1 - t) * p1 +
(1 - s) * t * p2 +
s * (1 - t) * p4 +
s * t * p3);
points.push_back(tmp);
mesh.addVertex(tmp); // make a new vertex
}
}
``````

This wont:

``````for(double i = 0; i <= size; i++){
for(double j = 0; j <= size; j++){
double s = i / (double)size;
double t = j / (double)size;
points.push_back(((1 - t) * s4->getPointX(i) + t * s2->getPointX(i)) +
((1 - s) * s1->getPointZ(j) + s * s3->getPointZ(j)) -
((1 - s) * (1 - t) * p1 +
(1 - s) * t * p2 +
s * (1 - t) * p4 +
s * t * p3));
}
}

for(int i = 0; i < size; i++){
}
``````

Although, if I had more time I could probably find out why given that the vertices look like this with the second chunk of code:

Annyways, ty for the help it works now

Glad it works! The issue with the second chunk of code is probably that

``````for(int i = 0; i < size; i++){
``````for(int i = 0; i < points.size(); i++){