Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

Gen 3d Texturing problem

$
0
0

@Joshua_deLorimier_TR wrote:

Hi I have been learning trying in 3d with the tutorial from Mastering OpenFrameWorks and with the Hubble Mesh tutorial. I thought I would try to try by hand a simple cube. I drew it correct. Added normals and there was a error. So I changed the vertex and got that to look correct. However I thought all you had to do was load a image and then match the verts to mesh.addTexCoord(ofPoint(100,100); So I did that then bound the image to the mesh but as you will see instead of wrapping the image around the whole mesh its glitch and stretched. If someone could explain to me why that happened I would really appreciate it. Also if you load a model and then draw it correctly as in the model example does it still run off of the gpu? I just used the code from Mastering openframeworks for the normals and it said it should be universal if the mesh is a tri-mesh.

Thanks again.



indent preformatted text by 4 spaces

include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){

mesh.setMode(OF_PRIMITIVE_TRIANGLES);
//ofDisableArbTex();


 ofPoint v0=ofPoint(-100,-100,100);
 ofPoint v1=ofPoint(100,-100,100);
 ofPoint v2=ofPoint(-100,100,100);
 ofPoint v3=ofPoint(100,100,100);
 ofPoint v4=ofPoint(-100,100,-100);
 ofPoint v5=ofPoint(-100,-100,-100);
 ofPoint v6=ofPoint(100,100,-100);
 ofPoint v7=ofPoint(100,-100,-100);

ofPoint t0 = ofPoint(-100,-100);
ofPoint t1 = ofPoint(100,-100);
ofPoint t2 = ofPoint(-100,100);
ofPoint t3 = ofPoint(100,100);
ofPoint t4 = ofPoint(-100,100);
ofPoint t5 = ofPoint(-100,-100);
ofPoint t6 = ofPoint(100,100);
ofPoint t7 = ofPoint(100,-100);

mesh.addVertex(v1);mesh.addVertex(v2);mesh.addVertex(v0);
mesh.addVertex(v3);mesh.addVertex(v2);mesh.addVertex(v1);

mesh.addVertex(v5);mesh.addVertex(v0);mesh.addVertex(v4);
mesh.addVertex(v4);mesh.addVertex(v0);mesh.addVertex(v2);

mesh.addVertex(v6);mesh.addVertex(v5);mesh.addVertex(v4);
mesh.addVertex(v7);mesh.addVertex(v5);mesh.addVertex(v6);

mesh.addVertex(v3);mesh.addVertex(v7);mesh.addVertex(v6);
mesh.addVertex(v1);mesh.addVertex(v7);mesh.addVertex(v3);

mesh.addVertex(v3);mesh.addVertex(v4);mesh.addVertex(v2);
mesh.addVertex(v6);mesh.addVertex(v4);mesh.addVertex(v3);

mesh.addVertex(v5);mesh.addVertex(v7);mesh.addVertex(v0);
mesh.addVertex(v0);mesh.addVertex(v7);mesh.addVertex(v1);



mesh.addTexCoord(t1);mesh.addTexCoord(t2);mesh.addTexCoord(t0);
mesh.addTexCoord(t3);mesh.addTexCoord(t2);mesh.addTexCoord(t1);

mesh.addTexCoord(t5);mesh.addTexCoord(t0);mesh.addTexCoord(t4);
mesh.addTexCoord(t4);mesh.addTexCoord(t0);mesh.addTexCoord(t2);

mesh.addTexCoord(t6);mesh.addTexCoord(t5);mesh.addTexCoord(t4);
mesh.addTexCoord(t7);mesh.addTexCoord(t5);mesh.addTexCoord(t6);

mesh.addTexCoord(t3);mesh.addTexCoord(t7);mesh.addTexCoord(t6);
mesh.addTexCoord(t1);mesh.addTexCoord(t7);mesh.addTexCoord(t3);

mesh.addTexCoord(t3);mesh.addTexCoord(t4);mesh.addTexCoord(t2);
mesh.addTexCoord(t6);mesh.addTexCoord(t4);mesh.addTexCoord(t3);

mesh.addTexCoord(t5);mesh.addTexCoord(t7);mesh.addTexCoord(t0);
mesh.addTexCoord(t0);mesh.addTexCoord(t7);mesh.addTexCoord(t1);



mesh.setupIndicesAuto();

setNormals(mesh);
light.enable();

image.loadImage("stars.png");

}

//--------------------------------------------------------------
void ofApp::update(){

}

//--------------------------------------------------------------
void ofApp::draw(){

float time = ofGetElapsedTimef();
ofEnableDepthTest();
ofBackground(0);
ofPushMatrix();

ofSetColor(255, 255, 255);
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotate(time*20,0,1,0);
//mesh.drawWireframe();
image.bind();
mesh.draw();
image.unbind();

ofPopMatrix();

}
void setNormals(ofMesh &mesh){

//number of vertices
int nV = mesh.getNumVertices();
//number of triangles
int nT = mesh.getNumIndices()/3;
vector<ofPoint> norm(nV); //array of normals

//scan for all triangles. for each triangle add its
//normal to the norms vector of triangles vertices

for(int t=0;t<nT;t++){
    //get indices of the triangle t
    int i1 = mesh.getIndex(3*t);
    int i2 = mesh.getIndex(3*t+1);
    int i3 = mesh.getIndex(3*t+2);

    //get vertices of the triangle
    const ofPoint &v1 = mesh.getVertex(i1);
    const ofPoint &v2 = mesh.getVertex(i2);
    const ofPoint &v3 = mesh.getVertex(i3);
    //compute the triangles normal
    ofPoint dir = ((v2-v1).crossed(v3-v1)).normalized();
    //accumulate it to norm array for i1, i2, i3
    norm[i1]+=dir;
    norm[i2]+=dir;
    norm[i3]+=dir;
}
//normalize the normals length
for(int i =0;i<nV;i++){
    norm[i].normalize();
}
//set normals to mesh
mesh.clearNormals();
mesh.addNormals(norm);

}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles