Strange vertical lines when drawing ofMesh of points

Hello,

I’m iteratively drawing an ofMesh of points. But for some strange reason, multiple vertical lines are left blank. Even though the mesh is always completely different at each iteration, the lines always show in the same places, regardless of how many times I run the program.

mesh.setMode(OF_PRIMITIVE_POINTS);
  mesh.clear();
  mesh.addVertices(positions);
  ofSetColor(color);
  ofFill();
  mesh.draw();

Here are some examples of the problem (this is the result of iteratively drawing hundreds of different (evolving) meshes):

These are with Retina (High Resolution Capable) ON:

And this one is with Retina OFF:

The lines are always in the same places, even though they slightly vary depending on retina being on/off.

Any clue as to why this may be happening is most welcome.

I was drawing the points with a loop of ofDrawRectangle but found that using ofMesh is 50% faster so it would be great to be able to get rid of these glitches.

Thanks in advance,
Nuno

Hey what happens if you call mesh.drawVertices() instead of mesh.draw()? Is the draw any different? Also if you just draw the vertices you’ll just get the points, regardless of what the mode of the mesh is.

1 Like

I didn’t know about drawVertices(). Unfortunately the same happens. Actually, much worse. This time I have lots and lots of vertical lines:

Maybe I should submit this as a bug?

Can you post a small code example / test case to show this issue?

Hi @zach,

Here it is:

void ofApp::setup(){
  ofSetBackgroundAuto(false);
  ofDisableAntiAliasing();  // <----------- this is the reason
  ofSetBackgroundColor(255);
  ofSetColor(ofColor::black,10);
}

void ofApp::update(){
  mesh.addVertex({ofGetFrameNum(),500, 0});
}

void ofApp::draw() {
  mesh.drawVertices();
}

This is what I get:

While reducing the code to make it as simple as possible I found that the offending line is ofDisableAntiAliasing()! I do need anti aliasing to be off though. And still don’t understand why this happens when anti aliasing is off. Any clues o what I may be doing wrong?

Btw, the same happens with both ofMesh and ofVboMesh.

Thanks!
Nuno

Can you post the full code (h / cpp)
Thx

Oh, sorry, here it is:

ofApp.h:

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{
private:
  ofMesh mesh;
public:
  void setup();
  void update();
  void draw();

  void keyPressed(int key);
  void keyReleased(int key);
  void mouseMoved(int x, int y );
  void mouseDragged(int x, int y, int button);
  void mousePressed(int x, int y, int button);
  void mouseReleased(int x, int y, int button);
  void mouseEntered(int x, int y);
  void mouseExited(int x, int y);
  void windowResized(int w, int h);
  void dragEvent(ofDragInfo dragInfo);
  void gotMessage(ofMessage msg);
};

ofApp.cpp:

#include "ofApp.h"

void ofApp::setup(){
  ofSetBackgroundAuto(false);
  ofDisableAntiAliasing();
  ofSetBackgroundColor(255);
  ofSetColor(ofColor::black,10);
}

void ofApp::update(){
  mesh.addVertex({ofGetFrameNum(),500, 0});
}

void ofApp::draw() {
  mesh.drawVertices();
}

void ofApp::keyPressed(int key) {}
void ofApp::keyReleased(int key){}
void ofApp::mouseMoved(int x, int y ){}
void ofApp::mouseDragged(int x, int y, int button){}
void ofApp::mousePressed(int x, int y, int button){}
void ofApp::mouseReleased(int x, int y, int button){}
void ofApp::mouseEntered(int x, int y){}
void ofApp::mouseExited(int x, int y){}
void ofApp::windowResized(int w, int h){}
void ofApp::gotMessage(ofMessage msg){}
void ofApp::dragEvent(ofDragInfo dragInfo){}

so I am not 100% sure what you are trying to achieve – you are creating degenerate triangles with this mesh – also you didn’t set a mesh type, which seems like it’s not clear what you want. but since you are drawing just the points (drawVertices is drawing dots where the vertex positions are), I think the white lines is a bit of a rounding issue – when you map floating point numbers to integers, it can sometimes be a little weird. also mapping float to pixel positions, I’ve found sometimes if I do this:

mesh.addVertex({ofGetFrameNum()+0.5,500, 0});

it helps, when I do this here, the white lines go away… here’s a cleaner example where you can see that removing the 0.5 has white lines, while keeping it there doesn’t

void ofApp::setup(){
  ofSetBackgroundAuto(false);
  ofDisableAntiAliasing();
  ofSetBackgroundColor(255);
  ofSetColor(ofColor::black,10);
  mesh.setMode(OF_PRIMITIVE_LINES);
}

void ofApp::update(){
  mesh.addVertex({ofGetFrameNum()+0.5,ofRandom(450,500), 0});
  mesh.addVertex({ofGetFrameNum()+0.5,ofRandom(550,600), 0});
}

void ofApp::draw() {
  mesh.drawVertices();
}
1 Like

ps: I believe adding 0.5 is putting the vertex in the center of the pixel, ie, 4.5 is in the center of pixel 4, where as 4.0000001 is on the right side and 4.9999 is on the left side. The issue with this code is that saying float x = 4.0 is not entirely accurate, 4.0 could actually be 3.9999999999 in terms of how a float is represented in memory. I think the white lines are actually values where the real float is slightly less than the value you set it to and when rounded to an integer pixels rounds down, thus skipping that pixel …

1 Like

Thank you so much @zach! Your suggestion solved my problem! And your explanation makes it perfectly clear. I wouldn’t have thought of it.

As to what I am trying to do: I just want to draw thousands of individual pixels per frame. I am currently doing a loop at the vector and using ofDrawRectangle() but found that using a ofMesh to print them all at the same time is much faster. Unlike the simplified example I provided, I’m doing mesh.clear() on every frame.

Regarding not defining the mesh type, if you look at my first post, I was setting it to OF_PRIMITIVE_POINTS and using mesh.draw() but then @TimChi [suggested] that, since I only wanted to draw the points, I could use drawVertices() and, if so, I didn’t really need to set the mesh type.

Thank you so much so solving my problem and helping me learn a bit more of OpenFrameworks and OpenGL!

Actually I ended up taking a slightly different approach: since my drawing class already receives a vector of points which I directly add to the mesh, instead of adjusting the individual vectors, I tried to do ofTranslate(0.5f, 0f) before drawing the mesh and it also worked!

Cheers!
Nuno