Can anyone help me applying a texture to circular ofMesh?
Here is the code that I have so far:
ofApp.h:
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
//____________________________________
ofImage texture;
ofMesh mesh;
};
ofApp.cpp:
#include "ofApp.h"
void ofApp::setup(){
texture.loadImage("tex.png");
texture.getTextureReference().setTextureMinMagFilter(GL_NEAREST, GL_NEAREST);
texture.getTextureReference().setTextureWrap(GL_REPEAT, GL_REPEAT);
mesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
int nPts = 20;
int r = 100;
ofVec2f center=ofVec2f(ofGetWidth()/2, ofGetHeight()/2);
float scale = r / (float)texture.getWidth();
for (int i=0; i<nPts; i++) {
float n = ofMap(i, 0, nPts-1, 0.0, TWO_PI);
float x = sin(n);
float y = cos(n);
mesh.addVertex(center);
mesh.addVertex(ofPoint(center.x + (x * r), center.y + (y * r)));
mesh.addTexCoord(ofPoint(0, 0));
mesh.addTexCoord(ofPoint(x * scale, y * scale));
}
}
//--------------------------------------------------------------
void ofApp::draw(){
ofSetColor(255);
texture.bind();
mesh.draw();
texture.unbind();
ofSetColor(0);
mesh.drawWireframe();
}
here is the output I have so far, what am I doing wrong? I suspect it has to do with “mesh.addTexCoord()”
Can you share which command/correction renders the texture correctly?
I agree with @amnon suggestions, but if I use you code and just add ofDisableArbTex(), it’s enough to render it correctly. I would like to understand why you get that error.
@hubris@jftf
The thing is that if you combine a) the wrong center texture coordinates with b) the wrong, unmapped texture coordinates for each vertex but c) you do use REPEAT, it might also give a result that appears correct visually.
However, if you remove the REPEAT in this scenario, you will probably notice that it all falls apart. The code I posted above, will behave correctly, even when REPEAT is removed (given a sufficiently visible scale). So while I am a supporter of the “whatever works” philosophy, I would not choose that route here, because it may lead to bugs later on.
Absolutely @amnon , thanks a lot for the help and clarifications.
I just came across with another problem. I would like to use a shape instead so I can use curveVertex do draw the shape! I just can’t figure out how to add the texture coordinates to the shape, Is this possible to map a texture to something like:
I copy-pasted your code, and just added ofDisableArbTex(). Didn’t change anything, so OF_PRIMITIVE_TRIANGLE_STRIP.
I’m on Mac OS 10.10.2, OF 0.8.4 and my GPU is an AMD Radeon HD 6750M. (I know this GPU as some issues with point sprites.)
@amnon that’s exactly my point, I know how to do it properly but my question is: why does it work either way? Even if I set other wrap mode, it still works. Something related to this GPU that ignores/corrects negative values?
(I know the issue is solved, it’s just pure curiosity. )
@jftf
You can’t add texture coordinates to code using of[Begin/End]Shape and ofCurveVertex. As a workaround however, you could draw such a complex shape to an ofFbo and use that ofFbo as a mask. You could implement this basic principle in several different ways manually or you could use one of the available mask ofxAddons.
@hubris
Ah right, that’s pretty strange. I guess a self-correcting GPU is a bit of a blessing in disguise.
Hi @arturo, I’m trying to implement your method! I got the texture on the shape, But it works more like a mask over the texture. If i move the shape around I can see that the texture is fixed in the background!
if you change the texture coordinate according to the movement of the shape they’ll move too. once you have the correct texture coordinates in the first frame i guess, you’ll need to copy them and reuse them every frame. or somehow reverse the transformation.
I don’t know why you are doing that in update, in the long run you want to change the shape? Even so, you can either subtract the translation or do something like this… Set always the same center and then: