Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
259 lines (208 sloc) 7.33 KB
#include "ofxCubeMap.h"
static const ofxCubeMapOptions defaultCubeOptions = {
100.0f,
true,
true,
true,
true,
true,
true,
false
};
ofxCubeMap::ofxCubeMap(){
init(defaultCubeOptions);
}
ofxCubeMap::ofxCubeMap(float _side){
options = defaultCubeOptions;
options.side = _side;
init(options);
}
ofxCubeMap::ofxCubeMap(ofxCubeMapOptions opt){
init(opt);
}
void ofxCubeMap::init(ofxCubeMapOptions opt){
options = opt;
width.set("width", options.side, 1.0f, options.side*2.0f);
height.set("height", options.side, 1.0f, options.side*2.0f);
deep.set("deep", options.side, 1.0f, options.side*2.0f);
params.add(width, height, deep);
regenerateMesh(width, height, deep);
}
void ofxCubeMap::regenerateMesh(const float _width, const float _height, const float _deep){
width.set(_width);
height.set(_height);
deep.set(_deep);
mesh.clear();
mesh.setMode(OF_PRIMITIVE_TRIANGLES);
float posZ = (float)_deep/float(2.0f);
float negZ = -posZ;
float posX = (float)_width/float(2.0f);
float negX = -posX;
float posY = (float)_height/float(2.0f);
float negY = -posY;
float vdata[36][3] = {
//back
{posX, negY, negZ}, {negX, negY, negZ}, {negX, posY, negZ},
{negX, posY, negZ}, {posX, posY, negZ}, {posX, negY, negZ},
// left
{negX, negY, negZ}, {negX, negY, posZ}, {negX, posY, posZ},
{negX, posY, posZ}, {negX, posY, negZ}, {negX, negY, negZ},
// right
{posX, negY, posZ}, {posX, negY, negZ}, {posX, posY, negZ},
{posX, posY, negZ}, {posX, posY, posZ}, {posX, negY, posZ},
// front
{negX, negY, posZ}, {posX, negY, posZ}, {posX, posY, posZ},
{posX, posY, posZ}, {negX, posY, posZ}, {negX, negY, posZ},
// top
{negX, posY, posZ}, {posX, posY, posZ}, {posX, posY, negZ},
{posX, posY, negZ}, {negX, posY, negZ}, {negX, posY, posZ},
// bottom
{negX, negY, negZ}, {posX, negY, negZ}, {posX, negY, posZ},
{posX, negY, posZ}, {negX, negY, posZ}, {negX, negY, negZ},
};
float imageWidth = float(_deep)*float(2) + _width*float(2);
float imageHeight = float(_deep)*float(2) + _height;
float frontU1 = ofMap(imageWidth-_width, 0.0f, imageWidth, 0.0f, 1.0f);
float frontV2 = ofMap(_deep, 0.0f, imageHeight, 0.0f, 1.0f);
float frontU2 = 1.0f;
float frontV1 = ofMap(_deep+_height, 0.0f, imageHeight, 0.0f, 1.0f);
float leftU1 = 0.0f;
float leftV2 = frontV2;
float leftU2 = ofMap(_deep, 0.0f, imageWidth, 0.0f, 1.0f);
float leftV1 = frontV1;
float rightU2 = ofMap(_deep*2.0f+_width, 0.0f, imageWidth, 0.0f, 1.0f);
float rightV1 = frontV1;
float rightU1 = ofMap(_deep+_width, 0.0f, imageWidth, 0.0f, 1.0f);
float rightV2 = ofMap(_deep, 0.0f, imageHeight, 0.0f, 1.0f);
float backU2 = rightU1;
float backV1 = frontV1;
float backU1 = ofMap(_width, 0.0f, imageWidth, 0.0f, 1.0f);
float backV2 = frontV2;
float topU2 = backU2;
float topV1 = backV2;
float topU1 = backU1;
float topV2 = 0.0f;
float bottomU2 = backU2;
float bottomV1 = 1.0f;
float bottomU1 = backU1;
float bottomV2 = backV1;
float vcoord[36][2] = {
// back
{frontU1, frontV1},{frontU2, frontV1},{frontU2, frontV2},
{frontU2, frontV2},{frontU1, frontV2},{frontU1, frontV1},
// left
{leftU1, leftV1},{leftU2, leftV1},{leftU2, leftV2},
{leftU2, leftV2},{leftU1, leftV2},{leftU1, leftV1},
// right
{rightU1, rightV1},{rightU2, rightV1},{rightU2, rightV2},
{rightU2, rightV2},{rightU1, rightV2},{rightU1, rightV1},
// front
{backU1, backV1},{backU2, backV1},{backU2, backV2},
{backU2, backV2},{backU1, backV2},{backU1, backV1},
// top
{topU1, topV1},{topU2, topV1},{topU2, topV2},
{topU2, topV2},{topU1, topV2},{topU1, topV1},
// bottom
{bottomU1, bottomV1},{bottomU2, bottomV1},{bottomU2, bottomV2},
{bottomU2, bottomV2},{bottomU1, bottomV2},{bottomU1, bottomV1},
};
static int indices[12][3] = {
// back
{0, 1, 2},{3, 4, 5},
// left
{6, 7, 8},{9, 10, 11},
//right
{12, 13, 14}, {15, 16, 17},
//front
{18, 19, 20}, {21, 22, 23},
//top
{24, 25, 26}, {27, 28, 29},
//bottom
{30, 31, 32}, {33, 34, 35},
};
for (int i = 0; i<36; ++i) {
mesh.addVertex(ofVec3f(vdata[i][0], vdata[i][1], vdata[i][2]));
mesh.addTexCoord(glm::vec2(vcoord[i][0], vcoord[i][1]));
}
// Add indices
if(options.back){
mesh.addIndex(indices[0][0]);
mesh.addIndex(indices[0][1]);
mesh.addIndex(indices[0][2]);
mesh.addIndex(indices[1][0]);
mesh.addIndex(indices[1][1]);
mesh.addIndex(indices[1][2]);
}
if(options.left){
mesh.addIndex(indices[2][0]);
mesh.addIndex(indices[2][1]);
mesh.addIndex(indices[2][2]);
mesh.addIndex(indices[3][0]);
mesh.addIndex(indices[3][1]);
mesh.addIndex(indices[3][2]);
}
if(options.right){
mesh.addIndex(indices[4][0]);
mesh.addIndex(indices[4][1]);
mesh.addIndex(indices[4][2]);
mesh.addIndex(indices[5][0]);
mesh.addIndex(indices[5][1]);
mesh.addIndex(indices[5][2]);
}
if(options.front){
mesh.addIndex(indices[6][0]);
mesh.addIndex(indices[6][1]);
mesh.addIndex(indices[6][2]);
mesh.addIndex(indices[7][0]);
mesh.addIndex(indices[7][1]);
mesh.addIndex(indices[7][2]);
}
if(options.top){
mesh.addIndex(indices[8][0]);
mesh.addIndex(indices[8][1]);
mesh.addIndex(indices[8][2]);
mesh.addIndex(indices[9][0]);
mesh.addIndex(indices[9][1]);
mesh.addIndex(indices[9][2]);
}
if(options.bottom){
mesh.addIndex(indices[10][0]);
mesh.addIndex(indices[10][1]);
mesh.addIndex(indices[10][2]);
mesh.addIndex(indices[11][0]);
mesh.addIndex(indices[11][1]);
mesh.addIndex(indices[11][2]);
}
// Add normals
if (options.flipNormals) {
calcNormals(mesh,true, true);
} else {
calcNormals(mesh,true, false);
}
mesh.enableNormals();
};
void ofxCubeMap::calcNormals( ofMesh &mesh, bool bNormalize=true, bool flipNormals=false ){
for( int i=0; i < mesh.getVertices().size(); i++ ) mesh.addNormal(glm::vec3(0.f,0.f,0.f));
for( int i=0; i < mesh.getIndices().size(); i+=3 ){
const int ia = mesh.getIndices()[i];
const int ib = mesh.getIndices()[i+1];
const int ic = mesh.getIndices()[i+2];
glm::vec3 e1 = mesh.getVertices()[ia] - mesh.getVertices()[ib];
glm::vec3 e2 = mesh.getVertices()[ic] - mesh.getVertices()[ib];
glm::vec3 no;
if (flipNormals) {
no = glm::cross(e1,e2);
}else{
no = glm::cross(e2,e1);
}
// depending on your clockwise / winding order, you might want to reverse the e2 / e1 above if your normals are flipped.
mesh.getNormals()[ia] += no;
mesh.getNormals()[ib] += no;
mesh.getNormals()[ic] += no;
}
if (bNormalize) {
for(int i=0; i < mesh.getNormals().size(); i++ ) {
mesh.getNormals()[i] = glm::normalize(mesh.getNormals()[i]);
}
}
};