Skip to content

Instantly share code, notes, and snippets.

@satoruhiga
Created February 5, 2013 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satoruhiga/4714199 to your computer and use it in GitHub Desktop.
Save satoruhiga/4714199 to your computer and use it in GitHub Desktop.
PerspectivePoint
#include "testApp.h"
ofMesh points;
ofEasyCam cam;
bool use_perspective_point = true;
void enablePerspectivePoint(float size)
{
static GLfloat distance[] = { 0.0, 0.0, 1.0 };
glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, distance);
GLfloat m[16];
GLint viewport[4];
glGetFloatv(GL_PROJECTION_MATRIX, m);
glGetIntegerv(GL_VIEWPORT, viewport);
float aspect = (float)viewport[2] / viewport[3];
float a = (m[0] * aspect);
float fov = (atan(a) - M_PI_2) * -2;
float pixel_per_unit = fabs((float)viewport[3] / (2.0f * std::tan(fov * 0.5f)));
glPointSize(pixel_per_unit * size);
}
void disablePerspectivePoint(float size)
{
static GLfloat distance[] = { 1.0, 0.0, 0.0 };
glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, distance);
glPointSize(size);
}
//--------------------------------------------------------------
void testApp::setup()
{
ofSetFrameRate(60);
ofSetVerticalSync(true);
ofBackground(0);
for (int i = 0; i < 1000; i++)
{
ofVec3f p;
p.x = ofRandom(-500, 500);
p.y = ofRandom(-500, 500);
p.z = 0;
points.addVertex(p);
}
}
//--------------------------------------------------------------
void testApp::update()
{
}
//--------------------------------------------------------------
void testApp::draw()
{
ofEnableAlphaBlending();
glEnable(GL_POINT_SMOOTH);
if (use_perspective_point)
enablePerspectivePoint(10);
else
disablePerspectivePoint(10);
cam.begin();
points.drawVertices();
cam.end();
string str = string("PerspectivePoint: ") + (use_perspective_point ? "ENABLE" : "DISABLE");
ofDrawBitmapString(str, 10, 20);
}
//--------------------------------------------------------------
void testApp::keyPressed(int key)
{
use_perspective_point = !use_perspective_point;
}
//--------------------------------------------------------------
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)
{
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h)
{
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg)
{
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo)
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment