RGB cube with ofMesh and ofVbo

This kind of freaky example shows how to draw a RGB cube using ofMesh and ofVbo. A mesh is created by calling addVertex and addColor. The mesh will be send to the graphics card by calling vbo. The binary string contains the corners of the cubes and the colors associated with them. You can divide the string in chunks of three to extract the vertex and color values.

    111 means vertex(1,1,1) and color(1,1,1)
    010 means vertex(-1,1,-1) and color(0,1,0)

The example also shows you how to subtract one 'char' from another to convert from a ascii value to a number:  '0'='0' = 0 and '1'-'0'=1.

testApp.h

#pragma once

#include "ofMain.h"

class testApp : public ofBaseApp{
public:
    void setup();
    void draw();
    
    ofVbo vbo;
    ofEasyCam camera;
};

testApp.cpp

#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup() {
    ofBackground(0);
    glEnable(GL_DEPTH_TEST);

    ofMesh mesh;
        
    char c[] = "011111101001111110100101110010000100"
               "010011001000010110111011000100101001";
    
    for (int i=0,x,y,z; i<72; i+=3) {
        
        x = c[i+0]-'0';
        y = c[i+1]-'0'; 
        z = c[i+2]-'0'; 
        
        mesh.addColor(ofFloatColor(x,y,z));
        mesh.addVertex(ofVec3f(x*2-1,y*2-1,z*2-1));
    }
    
    vbo.setMesh(mesh, GL_STATIC_DRAW);
 }

//--------------------------------------------------------------
void testApp::draw(){
    camera.begin();
    ofScale(100,100,100);
    vbo.draw(GL_QUADS,0,24);
}

Comments