Texture mapping a cube

Hi guys

I am having a few troubles trying to texture a cube. I am trying to wrap one texture around the cube but I dont understand how this can be achieved.

Attached is how it currently looks and the texture I want to wrap around the cube

``````

ofVec3f vertices[] = {
ofVec3f(+h,-h,+h), ofVec3f(+h,-h,-h), ofVec3f(+h,+h,-h), ofVec3f(+h,+h,+h),
ofVec3f(+h,+h,+h), ofVec3f(+h,+h,-h), ofVec3f(-h,+h,-h), ofVec3f(-h,+h,+h),
ofVec3f(+h,+h,+h), ofVec3f(-h,+h,+h), ofVec3f(-h,-h,+h), ofVec3f(+h,-h,+h),
ofVec3f(-h,-h,+h), ofVec3f(-h,+h,+h), ofVec3f(-h,+h,-h), ofVec3f(-h,-h,-h),
ofVec3f(-h,-h,+h), ofVec3f(-h,-h,-h), ofVec3f(+h,-h,-h), ofVec3f(+h,-h,+h),
ofVec3f(-h,-h,-h), ofVec3f(-h,+h,-h), ofVec3f(+h,+h,-h), ofVec3f(+h,-h,-h)
};

static ofVec3f normals[] = {
ofVec3f(+1,0,0), ofVec3f(+1,0,0), ofVec3f(+1,0,0), ofVec3f(+1,0,0),
ofVec3f(0,+1,0), ofVec3f(0,+1,0), ofVec3f(0,+1,0), ofVec3f(0,+1,0),
ofVec3f(0,0,+1), ofVec3f(0,0,+1), ofVec3f(0,0,+1), ofVec3f(0,0,+1),
ofVec3f(-1,0,0), ofVec3f(-1,0,0), ofVec3f(-1,0,0), ofVec3f(-1,0,0),
ofVec3f(0,-1,0), ofVec3f(0,-1,0), ofVec3f(0,-1,0), ofVec3f(0,-1,0),
ofVec3f(0,0,-1), ofVec3f(0,0,-1), ofVec3f(0,0,-1), ofVec3f(0,0,-1)
};

static ofVec2f tex[] = {
ofVec2f(1,0), ofVec2f(0,0), ofVec2f(0,1), ofVec2f(1,1),
ofVec2f(1,1), ofVec2f(1,0), ofVec2f(0,0), ofVec2f(0,1),
ofVec2f(0,1), ofVec2f(1,1), ofVec2f(1,0), ofVec2f(0,0),
ofVec2f(0,0), ofVec2f(0,1), ofVec2f(1,1), ofVec2f(1,0),
ofVec2f(0,0), ofVec2f(0,1), ofVec2f(1,1), ofVec2f(1,0),
ofVec2f(0,0), ofVec2f(0,1), ofVec2f(1,1), ofVec2f(1,0)
};

static ofIndexType indices[] = {
0,1,2, // right top left
0,2,3, // right bottom right
4,5,6, // bottom top right
4,6,7, // bottom bottom left
8,9,10, // back bottom right
8,10,11, // back top left
12,13,14, // left bottom right
12,14,15, // left top left
16,17,18, // ... etc
16,18,19,
20,21,22,
20,22,23
};
vertexData.setMode(OF_PRIMITIVE_TRIANGLES);

myTexture.bind();
vertexData.drawFaces();
myTexture.unbind();

``````

this technique is called CubeMapping , iit is used,for example, to make skybox i’ve made a little addon for this. look at: https://github.com/kalwalt/ofxSkyBox maybe this may help you in what you intend to do.

Thanks kalwalt. Thats what I was looking for but I have run into another problem.

What I what to do I have a grid of cubes, 4x4, 16 cubes in total.

I have managed to create the grid but now there is a problem with the shader. I get the error

``````

``````

Here is my code. It seems as soon as I have a vector of skyboxes the shaders break

``````

#include "ofMain.h"
#include "ofxSkyBox.h"

class CubeManager
{
public:

void                setup(int rows, int cols);
void                update();
void                draw();

vector<ofxSkyBox> cubes;
ofCamera            camera;

};

``````
``````

//--------------------------------------------------------------
void CubeManager::setup(int rows, int cols)
{
camera.setPosition(ofVec3f(280, 230, 1000));
camera.cacheMatrices(false);

int spacer = 20;

for(int i = 0; i < cols; i++)
{
for(int j = 0; j < rows; j++)
{
ofxSkyBox skybox;
skybox.setPos(j*(150+spacer), i*(150+spacer)); //I added this to ofxSkyBox to help position the boxes
skybox.setSize(150,150,150);//I added this to ofxSkyBox to set the size of the boxes
cubes.push_back(skybox);
}
}
}

//--------------------------------------------------------------
void CubeManager::update()
{
}

//--------------------------------------------------------------
void CubeManager::draw()
{
camera.begin();
for(int i = 0; i < cubes.size(); i++)
{
ofRotate(ofGetElapsedTimef()*i, 10, 50, 20 );
cubes[i].draw();

}
camera.end();
}

``````

![](http://forum.openframeworks.cc/uploads/default/2188/Screen shot 2012-02-16 at 3.41.05 PM.png)

Probably you have to do your class (maybe inherited from ofxSkyBox…or a new one) in which it has his method draw where you draw multiple box with only one call to the shader:

``````

void ofxMyClass::draw(int numBoxes, int rows, int cols) {

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glEnable(GL_TEXTURE_CUBE_MAP_ARB);

glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
//glDisable(GL_DEPTH_TEST);
glActiveTexture(GL_TEXTURE0);
cubeMap.bind();

glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();

for( int i = 0; i < numBoxes; i++){

//code to draw multiple boxes with numBoxes , rows and cols

}

glDisable(GL_TEXTURE_CUBE_MAP_ARB);

glDisable(GL_DEPTH_TEST);
glPopMatrix();

}

``````

Also i think you might use another shader instead of which inside of skybox.
Let me know if this suggestion is right or not.
regards