From 0022c92ba75f3c1ca80d18a4064076a7685c0213 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Fri, 2 May 2014 18:01:35 +0200 Subject: [PATCH] Add texture and texture coordinates to triangle surface --- example/src/ofApp.cpp | 5 ++++- example/src/ofApp.h | 1 + src/ofxBaseSurface.cpp | 7 ++++++- src/ofxBaseSurface.h | 1 + src/ofxTriangleSurface.cpp | 21 ++++++++++++++++++--- src/ofxTriangleSurface.h | 2 +- 6 files changed, 31 insertions(+), 6 deletions(-) diff --git a/example/src/ofApp.cpp b/example/src/ofApp.cpp index 85c08d1..3fb042d 100644 --- a/example/src/ofApp.cpp +++ b/example/src/ofApp.cpp @@ -2,8 +2,11 @@ void ofApp::setup() { + // Load test pattern image + image.loadImage("TestPatternInvert.jpg"); + // Create a triangle surface - triangleSurface.setup( ofVec2f(30,40), ofVec2f(500,500), ofVec2f(0,600) ); + triangleSurface.setup( ofVec2f(30,40), ofVec2f(500,500), ofVec2f(0,600), ofVec2f(0,0), ofVec2f(1,1), ofVec2f(0,1), &image.getTextureReference() ); } void ofApp::update() diff --git a/example/src/ofApp.h b/example/src/ofApp.h index 10a9a84..1edd863 100644 --- a/example/src/ofApp.h +++ b/example/src/ofApp.h @@ -17,6 +17,7 @@ public: void mouseDragged(int x, int y, int button); ofxTriangleSurface triangleSurface; + ofImage image; }; #endif \ No newline at end of file diff --git a/src/ofxBaseSurface.cpp b/src/ofxBaseSurface.cpp index 3f73333..be62503 100644 --- a/src/ofxBaseSurface.cpp +++ b/src/ofxBaseSurface.cpp @@ -1 +1,6 @@ -#include "ofxBaseSurface.h" \ No newline at end of file +#include "ofxBaseSurface.h" + +ofxBaseSurface::ofxBaseSurface() +{ + ofEnableNormalizedTexCoords(); +} \ No newline at end of file diff --git a/src/ofxBaseSurface.h b/src/ofxBaseSurface.h index def7efe..f54517a 100644 --- a/src/ofxBaseSurface.h +++ b/src/ofxBaseSurface.h @@ -6,6 +6,7 @@ class ofxBaseSurface { public: + ofxBaseSurface(); virtual void setup(){}; virtual void draw(){}; diff --git a/src/ofxTriangleSurface.cpp b/src/ofxTriangleSurface.cpp index e769651..e2907c3 100644 --- a/src/ofxTriangleSurface.cpp +++ b/src/ofxTriangleSurface.cpp @@ -18,21 +18,36 @@ void ofxTriangleSurface::setup() ofVec2f p2 = ofVec2f(ofVec2f(0, ofGetHeight())); ofVec2f p3 = ofVec2f(ofGetWidth(), ofGetHeight()); - setup( p1, p2, p3 ); + // Create 3 point for the texture coordinates + ofVec2f t1 = ofVec2f(0.5f, 0); + ofVec2f t2 = ofVec2f(0, 1.0f); + ofVec2f t3 = ofVec2f(1, 1.0f); + + setup( p1, p2, p3, t1, t2, t3, texture ); } -void ofxTriangleSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3 ) +void ofxTriangleSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr ) { - // Clear previous vertices if any + // Assign texture + texture = texturePtr; + + // Clear mesh mesh.clear(); // Create a surface with the points mesh.addVertex( p1 ); mesh.addVertex( p2 ); mesh.addVertex( p3 ); + + // Add texture coordinates + mesh.addTexCoord(t1); + mesh.addTexCoord(t2); + mesh.addTexCoord(t3); } void ofxTriangleSurface::draw() { + texture->bind(); mesh.draw(); + texture->unbind(); } \ No newline at end of file diff --git a/src/ofxTriangleSurface.h b/src/ofxTriangleSurface.h index b34aa00..7c19f64 100644 --- a/src/ofxTriangleSurface.h +++ b/src/ofxTriangleSurface.h @@ -11,7 +11,7 @@ public: ~ofxTriangleSurface(); void setup(); - void setup( ofVec2f p1, ofVec2f p2, ofVec2f p3 ); + void setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr ); void draw(); };