As you can see in the code below, there are four different calls for drawing a triangle:
ofDrawTriangle( 3 x ofVec3f)
ofDrawTriangle( 3 x ofVec2f)
ofDrawTriangle( 6 x float)
ofDrawTriangle( 9 x float)
When you call the one with 3 ofVec3f arguments, it will take their x, y and z values and use those to call the function again but with 9 floats. This tells to renderer to actually draw the triangle (ofGetCurrentRenderer()->drawTriangle ...
)
When you call it using 3 ofVec2f arguments, it will take their and y values and use those to call the function with 6 float arguments. Then that function will take the x and y arguments and add 0.0f as the z value to call the function with 9 float arguments, which will draw the triangle. This means there is one extra function call when you use ofVec2f. In theory this might make it slower, but I think the difference will be extremely small and you won’t notice it in practice.
Note: the code below doesn’t actually use ofVec2f or ofVec3f but the new glm::vec2 and glm::vec3, but the idea is still the same.
//----------------------------------------------------------
void ofDrawTriangle(const glm::vec3 & p1, const glm::vec3 & p2, const glm::vec3 & p3){
ofDrawTriangle(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z, p3.x, p3.y, p3.z);
}
//----------------------------------------------------------
void ofDrawTriangle(const glm::vec2 & p1, const glm::vec2 & p2, const glm::vec2 & p3){
ofDrawTriangle(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
}
//----------------------------------------------------------
void ofDrawTriangle(float x1,float y1,float x2,float y2,float x3, float y3){
ofDrawTriangle(x1, y1, 0.0f, x2, y2, 0.0f, x3, y3, 0.0f);
}
//----------------------------------------------------------
void ofDrawTriangle(float x1,float y1,float z1,float x2,float y2,float z2,float x3, float y3,float z3){
ofGetCurrentRenderer()->drawTriangle(x1,y1,z1,x2,y2,z2,x3,y3,z3);
}
I actually wonder why the function call with the ofVec2f arguments doesn’t directly add the 0.0f’s for the z-values and call the one with 9 floats directly, instead of going through the extra step.
Update:
Here is a simple test script. Turns out ofVec3f is indeed slightly faster, but not a whole lot.
Update 2:
I changed it a bit so only the actual drawing time is measured, not the generation of the random numbers.
void ofApp::draw(){
ofVec2f point1_2f;
ofVec2f point2_2f;
ofVec2f point3_2f;
ofVec3f point1_3f;
ofVec3f point2_3f;
ofVec3f point3_3f;
float startTime;
float totalTime = 0;
for(int i=0;i<100000;i++){
point1_2f.set(ofRandom(0, 500), ofRandom(0, 500));
point2_2f.set(ofRandom(0, 500), ofRandom(0, 500));
point3_2f.set(ofRandom(0, 500), ofRandom(0, 500));
startTime = ofGetElapsedTimeMicros();
ofDrawTriangle(point1_2f, point2_2f, point3_2f);
totalTime += ofGetElapsedTimeMicros() - startTime;
}
ofLog() << "drawing with ofVec2f took an average of " << (totalTime/100000) << " microseconds";
totalTime = 0;
for(int i=0;i<100000;i++){
point1_3f.set(ofRandom(0, 500), ofRandom(0, 500), ofRandom(0, 500));
point2_3f.set(ofRandom(0, 500), ofRandom(0, 500), ofRandom(0, 500));
point3_3f.set(ofRandom(0, 500), ofRandom(0, 500), ofRandom(0, 500));
startTime = ofGetElapsedTimeMicros();
ofDrawTriangle(point1_3f, point2_3f, point3_3f);
totalTime += ofGetElapsedTimeMicros() - startTime;
}
ofLog() << "drawing with ofVec3f took an average of " << (totalTime/100000) << " microseconds";
ofExit();
}