Disabling indices on ofMesh

Hi,

I only recently started using OF on a regular basis. So it’s very possible that I’m misunderstanding how the indices of ofMesh work internally. I understand how to write/generate my own meshes and they are drawn as expected. But in one case I wanted to change the mode of my ofMesh instance which also required to disable the indices. My code looks somewhat like this:

// ofApp.h
#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp {
    ofMesh m_mesh;
    bool m_clear_indices;
public:
    void setup();
    void update();
    void draw();
    void keyPressed(int key);
};
// ofApp.cpp
#include "ofApp.h"

#define MESH_WIDTH 300
#define MESH_HEIGHT 300

void ofApp::setup() {
    m_mesh.setMode(OF_PRIMITIVE_TRIANGLES);
    
    m_mesh.addVertex(ofVec3f(0.f, 0.f, 0.f));
    m_mesh.addVertex(ofVec3f(MESH_WIDTH, 0.f, 0.f));
    m_mesh.addVertex(ofVec3f(MESH_WIDTH, MESH_HEIGHT, 0.f));
    m_mesh.addVertex(ofVec3f(0.f, MESH_HEIGHT, 0.f));
    
    m_mesh.enableColors();
    m_mesh.addColors({
        ofColor(255, 0, 0),
        ofColor(0, 0, 255),
        ofColor(255, 127, 0),
        ofColor(255, 255, 0)
    });
    
    m_mesh.enableIndices();
    m_mesh.addIndices({
        0, 1, 3,
        1, 2, 3
    });
    
    m_clear_indices = true;
}

void ofApp::update() {
    // do something funky with the mesh...
}

void ofApp::draw() {
    string info = "";
    info += "MODE: OF_PRIMITIVE_TRIANGLES";
    info += "\nINDICES ENABLED: TRUE";
    info += "\nINDICES CLEARED: FALSE";
    
    ofPushMatrix();
    ofTranslate(50.f, 50.f);
    ofSetColor(33);
    ofDrawBitmapString(info, 0.f, -35.f);
    ofSetColor(255);
    m_mesh.draw();
    ofPopMatrix();
    
    m_mesh.setMode(OF_PRIMITIVE_LINE_LOOP);
    m_mesh.disableIndices();
    // I need to clear the indices, simply disabling them doesn't work
    if (m_clear_indices) m_mesh.clearIndices();
    
    info = "";
    info += "MODE: OF_PRIMITIVE_LINE_LOOP";
    info += "\nINDICES ENABLED: FALSE";
    info += "\nINDICES CLEARED: ";
    info += m_clear_indices ? "TRUE" : "FALSE";
    
    ofPushMatrix();
    ofTranslate(400.f, 50.f);
    ofSetColor(33);
    ofDrawBitmapString(info, 0.f, -35.f);
    ofSetColor(255);
    m_mesh.draw();
    ofPopMatrix();
    
    m_mesh.setMode(OF_PRIMITIVE_TRIANGLES);
    m_mesh.enableIndices();
    // since I cleared the indices, I need to redefine them here
    if (m_clear_indices) {
        m_mesh.addIndices({
            0, 1, 3,
            1, 2, 3
        });
    }
}

void ofApp::keyPressed(int key) {
    m_clear_indices = !m_clear_indices;
}

So the point that confuses me is that I have to clear the indices although I already disabled them. Ideally I’d like to keep the indices and simply turn them on and off. But there’s probably a very good reason why it is this way. Unfortunately I wasn’t able to find an explanation for this behavior yet.

Can someone explain this to me? Thanks.

that actually sounds like a bug, can you report it on github

Done: https://github.com/openframeworks/openFrameworks/issues/5234.

I also updated the code above. Here are two screenshots from the app.

Actual behavior

Expected behavior

Category for this thread is also updated accordingly.