draw grid of nxn cubes

Hi all,

This is a very simple question, and yet I have been the last 3 days trying to figure it out.
How do I construct a n x n grid of cubes? I have seen several Processing examples but I did not get results.

Here is my attempt

class cubeClass::cubeClass cubo[_ARRAYSIZE];  
void testApp::drawCubes(void){  
	//cube positions  
	for (int i=0; i<_ARRAYSIZE; i++) //_ARRAYSIZE: NR ROWS  
		for(int j=0; j<_ARRAYSIZE; j++) //_ARRAYSIZE: NR COLUMNS  
		cubo[i].size = 100.0f;  
		cubo[i].xPos = (i*cubo[i].size+cubo[i].size/2)+((_ARRAYSIZE/2)*cubo[i].size);  
		cubo[i].yPos = (j*cubo[i].size + cubo[i].size/2)+((_ARRAYSIZE/2)*cubo[i].size);;  
		cout << i <<"," <<j << "\n";  

Where xPos and yPos will be used as positions in glTranslatef() (I want the grid to appear centered in the window):

void cubeClass::draw() {  
	   glTranslatef(xPos, yPos, 0);  

Any clues?


What happens if you dont translate? are you using a camera?

Here is how I did it, whit no camera:

// Setup Boxes  
mBoxSize = 80.0f;  
float numBoxesInX = ofGetWidth() / mBoxSize;  
float numBoxesInY = ofGetHeight() / mBoxSize;  
mNumBoxes = numBoxesInX * numBoxesInY;  
for ( int i = 0; i < numBoxesInX; i++ ) {  
	for (int j = 0; j < numBoxesInY; j++ ) {  
		float x = i * mBoxSize + mBoxSize * 0.5f;  
		float y = j * mBoxSize + mBoxSize * 0.5f;  
		float z = -mBoxSize * 0.5f;  
		Box box;  
		box.mSize = mBoxSize;  
		box.mPosition = ofVec3f( x, y, z ); // set position for this box  
		mBoxes.push_back( box );  

This will fill the screen with boxes like a m x n matrix, this can be change by adjusting numBoxes(X/Y), hope that make sense.


  • rS

Thanks for the answer nardove!
I am trying your coude but still don´t get any results. I am not using cameras, but I want this n x n cube grid to appear in the middle of my screen, instead of occupying the whole screen. Cubes have a pre-defined size.

I will keep you posted about my results soon.

Beatriz :mrgreen:

I got it working. I realized that the “problem” was that OF windows are set up in such a way that the coordinate 0,0 is situated at the top left corner of the window, while in OpenGL the 0,0 coordinate is right in the center of the window. I will open a separate post about this.

Here the code:

#include "testApp.h"  
#define numBoxesInX 5  
#define numBoxesInY 5  
#define numBoxes numBoxesInX * numBoxesInY  
float cubeSize = 30.0f;  
float offset = 10.0f;  
struct coords {  
    float x,y,z;  
    float s; //size  
    int r,g,b;  
struct coords p [numBoxesInX] [numBoxesInY]; //ARRAY OF STRUCTS  
void testApp::setup(){  
    ofBackground(256, 256, 256);  
    //setup cube positions  
    for(int i=0; i<numBoxesInX; i++){  
        for (int j=0; j<numBoxesInY; j++)  
            p[i][j].r = ofRandom(0,255);  
            p[i][j].g = ofRandom(0,255);  
            p[i][j].b = ofRandom(0,255);  
            p[i][j].x = i*(cubeSize+offset)+(ofGetWidth()/2-cubeSize*(numBoxesInX*0.5));  
            p[i][j].y = j*(cubeSize+offset)+(ofGetHeight()/2-cubeSize*(numBoxesInY*0.5));  
            p[i][j].z = 0.0f;  
void testApp::update(){  
void testApp::draw(){  
    for(int i=0; i<numBoxesInX; i++){  
        for (int j=0; j<numBoxesInY; j++)  
        ofSetColor(p[i][j].r, p[i][j].g, p[i][j].b);  

Thanks again to nardove for the help!
Beatriz :mrgreen: