antialised shapes

I’ve been reading about antialiased shapes and there seem to bee severa ways to achieve so but these seem to be a bit expensive.
I came out with a different idea that’s quite simple indeed.
It would be really nice to, eventually, have antialised shapes natively in OF.

I’d love to see some income from the comunity.
Best!
testApp.h

  
#pragma once  
  
#include "ofMain.h"  
  
class testApp : 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);  
      
    ofVec2f points [6];  
    int 	cur_drag;  
    float feather;  
    bool bAntiAlias;  
};  
  

testApp.cpp

  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
    points[0].set(400, 400);  
    points[1].set(600, 400);  
    points[2].set(600, 650);  
      
    points[3] = points[0];  
    points[4] = points[1];  
    points[5] = points[2];  
    cur_drag=-1;  
      
    ofEnableAlphaBlending();  
    feather =5;  
    bAntiAlias = true;  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
      
    for (int i = 0; i<3; i++) {  
        ofVec2f p1, p2;  
        int c = (i+1)%3;  
        p1 = points[i] - points[c];  
        p2 = points[(i+2)%3] - points[c];  
        points[c+3].set((max(p1.x,p2.x) - min(p1.x,p2.x))*0.5f + min(p1.x,p2.x), (max(p1.y,p2.y) - min(p1.y,p2.y))*0.5f + min(p1.y,p2.y));  
        points[c+3].normalize();  
        points[c+3] *= -feather;  
        points[c+3] += points[c];  
    }  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
    if(bAntiAlias){     
    for (int i = 0; i < 3; i++) {  
        int c = (i+1)%3;  
        glBegin(GL_TRIANGLES);  
        glColor4f(1, 0, 0, 1);  
        glVertex2f(points[i].x, points[i].y);  
        glVertex2f(points[c].x, points[c].y);  
        glColor4f(1, 0, 0, 0);      
        glVertex2f(points[i+3].x, points[i+3].y);  
          
        glColor4f(1, 0, 0, 1);  
        glVertex2f(points[c].x, points[c].y);  
        glColor4f(1, 0, 0, 0);      
        glVertex2f(points[i+3].x, points[i+3].y);  
        glVertex2f(points[c+3].x, points[c+3].y);  
        glEnd();  
    }  
}  
    glBegin(GL_TRIANGLES);  
    glColor4f(1, 0, 0, 1);  
    glVertex2f(points[0].x, points[0].y);  
    glVertex2f(points[1].x, points[1].y);  
    glVertex2f(points[2].x, points[2].y);  
    glEnd();  
      
    ofSetColor(0);  
    for (int  i = 3; i <6; i++) {  
        ofCircle(points[i].x, points[i].y, 3);  
    }  
      
    ofPushStyle();  
    ofSetColor(0);  
    ofDrawBitmapString("feather: " + ofToString(feather), 100, ofGetHeight() - 100);  
    ofPopStyle();  
}  
  
//--------------------------------------------------------------  
void testApp::keyPressed(int key){  
    if (key == OF_KEY_UP) {  
        feather *= 1.2;  
    }else if (key == OF_KEY_DOWN) {  
        feather *=0.8;  
          
        if (feather < 1.0f) {  
            feather = 1.0f;  
        }  
    }else if(key == 'a'){  
        bAntiAlias ^= true;  
    }  
}  
//--------------------------------------------------------------  
void testApp::keyReleased(int key){}  
//--------------------------------------------------------------  
void testApp::mouseMoved(int x, int y){}  
//--------------------------------------------------------------  
void testApp::mouseDragged(int x, int y, int button){  
    if ( cur_drag >=0 && cur_drag< 3){  
        points[cur_drag].x=x;  
        points[cur_drag].y=y;  
    }  
}  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button){  
	for ( int i=0; i<3; i++)  
	{  
		const short ALW=10;  
		short dx=x-points[i].x;  
		short dy=y-points[i].y;  
		if ( -ALW<dx && dx<ALW)  
            if ( -ALW<dy && dy<ALW)  
            {  
                cur_drag=i;  
                return;  
            }  
	}  
	cur_drag=-1;  
}  
//--------------------------------------------------------------  
void testApp::mouseReleased(int x, int y, int button){}    
}  

hm, what is window.setGlutDisplayString(“rgba double samples>=4 depth”); technically doing ? seems to be faster

or in cocoa // NSOpenGLPixelFormatAttribute attribs[] =
{
kCGLPFAAccelerated,
kCGLPFANoRecovery,
kCGLPFADoubleBuffer,
kCGLPFAAlphaSize, 8,
kCGLPFAColorSize, 24,
kCGLPFADepthSize, 16,
kCGLPFAMultisample,
kCGLPFASampleBuffers, 1,
kCGLPFASamples, 4,
0
};
how does this samples-thing work?

greetings ?

Uhh,
I tried using multisampling before but it didn’t work but now that I did some google-ing I found out the problem. Indeed I just needed what you mention.
Is quite easy indeed. Much more than what I previously posted.

window.setGlutDisplayString(“rgba double samples>=4 depth”);

sets the rendering destination (window) to use multisampling, with 4 or more samples (if available).
The samples values determines how many times each pixel is sampled at a different position (at subpixel coordinates) then these samples are averaged to get the final pixel color.

Just don’t know why this is not done by default in OF.

Check this out:
main.cpp (this is not OF’s default main.cpp so make sure you copy it, otherwise it wont work).

  
  
#include "testApp.h"  
#include "ofAppGlutWindow.h"  
  
//--------------------------------------------------------------  
int main(){  
	ofAppGlutWindow window; // create a window  
	// set width, height, mode (OF_WINDOW or OF_FULLSCREEN)  
    window.setGlutDisplayString("rgba double samples>=4 depth");  
	ofSetupOpenGL(&window, 1024, 768, OF_WINDOW);  
	ofRunApp(new testApp()); // start the app  
}  
  
  

testApp.h

  
  
#pragma once  
  
#include "ofMain.h"  
  
class testApp : 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);  
    bool bMulti;  
};  
  

testApp.cpp

  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
    bMulti = true;  
    string str = ofToString(glGetString(GL_EXTENSIONS));  
    vector<string> exts = ofSplitString(str, " ");  
    string search = "GL_ARB_multisample";  
    if(ofFind(exts, search) < exts.size()){  
        ofLog(OF_LOG_NOTICE, "ARB multisampling is supported by your system");  
    }else{  
        ofLog(OF_LOG_NOTICE, "ARB multisampling is NOT supported by your system");  
    }  
      
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
    if(bMulti){  
    glEnable(GL_MULTISAMPLE_ARB);  
    }  
    ofCircle(ofGetWidth()*.5, ofGetHeight()*.5, 200);      
    if(bMulti){  
    glDisable(GL_MULTISAMPLE_ARB);  
    }  
    ofDrawBitmapStringHighlight("FPS: " + ofToString(ofGetFrameRate()), 50, ofGetHeight() -100 );  
}  
  
//--------------------------------------------------------------  
void testApp::keyPressed(int key){  
    bMulti ^= true;  
}  
  
//--------------------------------------------------------------  
void testApp::keyReleased(int key){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseMoved(int x, int y){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseDragged(int x, int y, int button){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseReleased(int x, int y, int button){  
  
}