first cut are removing immediate mode GL_LINES

This commit is contained in:
ZappoMan 2015-01-05 15:46:48 -08:00
parent c2d45df541
commit a20fa1242d
8 changed files with 234 additions and 48 deletions

View file

@ -34,6 +34,8 @@
using namespace std;
void renderWorldBox() {
GeometryCache::SharedPointer geometryCache = DependencyManager::get<GeometryCache>();
// Show edge of world
float red[] = {1, 0, 0};
float green[] = {0, 1, 0};
@ -42,22 +44,17 @@ void renderWorldBox() {
glDisable(GL_LIGHTING);
glLineWidth(1.0);
glBegin(GL_LINES);
glColor3fv(red);
glVertex3f(0, 0, 0);
glVertex3f(TREE_SCALE, 0, 0);
geometryCache->renderLine(glm::vec3(0, 0, 0), glm::vec3(TREE_SCALE, 0, 0));
glColor3fv(green);
glVertex3f(0, 0, 0);
glVertex3f(0, TREE_SCALE, 0);
geometryCache->renderLine(glm::vec3(0, 0, 0), glm::vec3(0, TREE_SCALE, 0));
glColor3fv(blue);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, TREE_SCALE);
geometryCache->renderLine(glm::vec3(0, 0, 0), glm::vec3(0, 0, TREE_SCALE));
glColor3fv(gray);
glVertex3f(0, 0, TREE_SCALE);
glVertex3f(TREE_SCALE, 0, TREE_SCALE);
glVertex3f(TREE_SCALE, 0, TREE_SCALE);
glVertex3f(TREE_SCALE, 0, 0);
glEnd();
geometryCache->renderLine(glm::vec3(0, 0, TREE_SCALE), glm::vec3(TREE_SCALE, 0, TREE_SCALE));
geometryCache->renderLine(glm::vec3(TREE_SCALE, 0, TREE_SCALE), glm::vec3(TREE_SCALE, 0, 0));
// Draw meter markers along the 3 axis to help with measuring things
const float MARKER_DISTANCE = 1.0f;
const float MARKER_RADIUS = 0.05f;
@ -65,7 +62,6 @@ void renderWorldBox() {
glPushMatrix();
glTranslatef(MARKER_DISTANCE, 0, 0);
glColor3fv(red);
GeometryCache::SharedPointer geometryCache = DependencyManager::get<GeometryCache>();
geometryCache->renderSphere(MARKER_RADIUS, 10, 10);
glPopMatrix();
glPushMatrix();
@ -138,30 +134,6 @@ void renderBevelCornersRect(int x, int y, int width, int height, int bevelDistan
DependencyManager::get<GeometryCache>()->renderBevelCornersRect(x, y, width, height, bevelDistance);
}
void renderOrientationDirections(glm::vec3 position, const glm::quat& orientation, float size) {
glm::vec3 pRight = position + orientation * IDENTITY_RIGHT * size;
glm::vec3 pUp = position + orientation * IDENTITY_UP * size;
glm::vec3 pFront = position + orientation * IDENTITY_FRONT * size;
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINE_STRIP);
glVertex3f(position.x, position.y, position.z);
glVertex3f(pRight.x, pRight.y, pRight.z);
glEnd();
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_LINE_STRIP);
glVertex3f(position.x, position.y, position.z);
glVertex3f(pUp.x, pUp.y, pUp.z);
glEnd();
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_LINE_STRIP);
glVertex3f(position.x, position.y, position.z);
glVertex3f(pFront.x, pFront.y, pFront.z);
glEnd();
}
// Do some basic timing tests and report the results
void runTimingTests() {
// How long does it take to make a call to get the time?

View file

@ -27,9 +27,6 @@ void drawText(int x, int y, float scale, float radians, int mono,
void renderCollisionOverlay(int width, int height, float magnitude, float red = 0, float blue = 0, float green = 0);
void renderOrientationDirections( glm::vec3 position, const glm::quat& orientation, float size );
void renderBevelCornersRect(int x, int y, int width, int height, int bevelDistance);
void runTimingTests();

View file

@ -319,7 +319,7 @@ void SkeletonModel::renderJointConstraints(int jointIndex) {
return;
}
const FBXGeometry& geometry = _geometry->getFBXGeometry();
const float BASE_DIRECTION_SIZE = 300.0f;
const float BASE_DIRECTION_SIZE = 0.3f;
float directionSize = BASE_DIRECTION_SIZE * extractUniformScale(_scale);
glLineWidth(3.0f);
do {
@ -362,7 +362,7 @@ void SkeletonModel::renderJointConstraints(int jointIndex) {
}
glPopMatrix();
renderOrientationDirections(position, _rotation * jointState.getRotation(), directionSize);
renderOrientationDirections(jointIndex, position, _rotation * jointState.getRotation(), directionSize);
jointIndex = joint.parentIndex;
} while (jointIndex != -1 && geometry.joints.at(jointIndex).isFree);
@ -370,6 +370,34 @@ void SkeletonModel::renderJointConstraints(int jointIndex) {
glLineWidth(1.0f);
}
void SkeletonModel::renderOrientationDirections(int jointIndex, glm::vec3 position, const glm::quat& orientation, float size) {
GeometryCache::SharedPointer geometryCache = DependencyManager::get<GeometryCache>();
if (!_jointOrientationLines.contains(jointIndex)) {
OrientationLineIDs jointLineIDs;
jointLineIDs._up = geometryCache->allocateID();
jointLineIDs._front = geometryCache->allocateID();
jointLineIDs._right = geometryCache->allocateID();
_jointOrientationLines[jointIndex] = jointLineIDs;
}
OrientationLineIDs& jointLineIDs = _jointOrientationLines[jointIndex];
glm::vec3 pRight = position + orientation * IDENTITY_RIGHT * size;
glm::vec3 pUp = position + orientation * IDENTITY_UP * size;
glm::vec3 pFront = position + orientation * IDENTITY_FRONT * size;
glColor3f(1.0f, 0.0f, 0.0f);
geometryCache->renderLine(position, pRight, jointLineIDs._right);
glColor3f(0.0f, 1.0f, 0.0f);
geometryCache->renderLine(position, pUp, jointLineIDs._up);
glColor3f(0.0f, 0.0f, 1.0f);
geometryCache->renderLine(position, pFront, jointLineIDs._front);
}
void SkeletonModel::setHandPosition(int jointIndex, const glm::vec3& position, const glm::quat& rotation) {
// this algorithm is from sample code from sixense
const FBXGeometry& geometry = _geometry->getFBXGeometry();

View file

@ -144,6 +144,14 @@ protected:
private:
void renderJointConstraints(int jointIndex);
void renderOrientationDirections(int jointIndex, glm::vec3 position, const glm::quat& orientation, float size);
struct OrientationLineIDs {
int _up;
int _front;
int _right;
};
QHash<int, OrientationLineIDs> _jointOrientationLines;
/// \param jointIndex index of joint in model
/// \param position position of joint in model-frame

View file

@ -12,6 +12,7 @@
#include "InterfaceConfig.h"
#include <GlowEffect.h>
#include <GeometryCache.h>
#include "Line3DOverlay.h"
@ -21,7 +22,8 @@ Line3DOverlay::Line3DOverlay() {
Line3DOverlay::Line3DOverlay(const Line3DOverlay* line3DOverlay) :
Base3DOverlay(line3DOverlay),
_end(line3DOverlay->_end)
_end(line3DOverlay->_end),
_geometryCacheID(DependencyManager::get<GeometryCache>()->allocateID())
{
}
@ -59,10 +61,7 @@ void Line3DOverlay::render(RenderArgs* args) {
if (getIsDashedLine()) {
drawDashedLine(_position, _end);
} else {
glBegin(GL_LINES);
glVertex3f(_start.x, _start.y, _start.z);
glVertex3f(_end.x, _end.y, _end.z);
glEnd();
DependencyManager::get<GeometryCache>()->renderLine(_start, _end, _geometryCacheID);
}
glEnable(GL_LIGHTING);

View file

@ -38,6 +38,7 @@ public:
protected:
glm::vec3 _start;
glm::vec3 _end;
int _geometryCacheID;
};

View file

@ -765,7 +765,6 @@ void GeometryCache::renderBevelCornersRect(int x, int y, int width, int height,
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void GeometryCache::renderQuad(const glm::vec2& minCorner, const glm::vec2& maxCorner, int id) {
@ -1153,6 +1152,176 @@ void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottom
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void GeometryCache::renderLine(const glm::vec3& p1, const glm::vec3& p2, int id) {
bool registeredLine = (id != UNKNOWN_ID);
Vec3Pair key(p1, p2);
VerticesIndices& vbo = registeredLine ? _registeredLine3DVBOs[id] : _line3DVBOs[key];
// if this is a registered quad, and we have buffers, then check to see if the geometry changed and rebuild if needed
if (registeredLine && vbo.first != 0) {
Vec3Pair& lastKey = _lastRegisteredLine3D[id];
if (lastKey != key) {
glDeleteBuffers(1, &vbo.first);
glDeleteBuffers(1, &vbo.second);
vbo.first = vbo.second = 0;
#if 1 // def WANT_DEBUG
qDebug() << "renderLine() 3D ... RELEASING REGISTERED line";
#endif // def WANT_DEBUG
}
#if 1 // def WANT_DEBUG
else {
qDebug() << "renderLine() 3D ... REUSING PREVIOUSLY REGISTERED line";
}
#endif // def WANT_DEBUG
}
const int FLOATS_PER_VERTEX = 3;
const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat);
const int vertices = 2;
const int indices = 2;
if (vbo.first == 0) {
_lastRegisteredLine3D[id] = key;
int vertexPoints = vertices * FLOATS_PER_VERTEX;
GLfloat* vertexData = new GLfloat[vertexPoints]; // only vertices, no normals because we're a 2D quad
GLfloat* vertex = vertexData;
static GLubyte cannonicalIndices[indices] = {0, 1};
int vertexPoint = 0;
// p1
vertex[vertexPoint++] = p1.x;
vertex[vertexPoint++] = p1.y;
vertex[vertexPoint++] = p1.z;
// p2
vertex[vertexPoint++] = p2.x;
vertex[vertexPoint++] = p2.y;
vertex[vertexPoint++] = p2.z;
glGenBuffers(1, &vbo.first);
glBindBuffer(GL_ARRAY_BUFFER, vbo.first);
glBufferData(GL_ARRAY_BUFFER, vertices * NUM_BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW);
delete[] vertexData;
GLushort* indexData = new GLushort[indices];
GLushort* index = indexData;
for (int i = 0; i < indices; i++) {
index[i] = cannonicalIndices[i];
}
glGenBuffers(1, &vbo.second);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * NUM_BYTES_PER_INDEX, indexData, GL_STATIC_DRAW);
delete[] indexData;
#if 1 // def WANT_DEBUG
if (id == UNKNOWN_ID) {
qDebug() << "new renderLine() 3D VBO made -- _line3DVBOs.size():" << _line3DVBOs.size();
} else {
qDebug() << "new registered renderLine() 3D VBO made -- _registeredLine3DVBOs.size():" << _registeredLine3DVBOs.size();
}
#endif
} else {
glBindBuffer(GL_ARRAY_BUFFER, vbo.first);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second);
}
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(FLOATS_PER_VERTEX, GL_FLOAT, FLOATS_PER_VERTEX * sizeof(float), 0);
glDrawRangeElementsEXT(GL_LINES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void GeometryCache::renderLine(const glm::vec2& p1, const glm::vec2& p2, int id) {
bool registeredLine = (id != UNKNOWN_ID);
Vec2Pair key(p1, p2);
VerticesIndices& vbo = registeredLine ? _registeredLine2DVBOs[id] : _line2DVBOs[key];
// if this is a registered quad, and we have buffers, then check to see if the geometry changed and rebuild if needed
if (registeredLine && vbo.first != 0) {
Vec2Pair& lastKey = _lastRegisteredLine2D[id];
if (lastKey != key) {
glDeleteBuffers(1, &vbo.first);
glDeleteBuffers(1, &vbo.second);
vbo.first = vbo.second = 0;
#if 1 // def WANT_DEBUG
qDebug() << "renderLine() 2D... RELEASING REGISTERED line";
#endif // def WANT_DEBUG
}
#if 1 // def WANT_DEBUG
else {
qDebug() << "renderLine() 2D... REUSING PREVIOUSLY REGISTERED line";
}
#endif // def WANT_DEBUG
}
const int FLOATS_PER_VERTEX = 2;
const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat);
const int vertices = 2;
const int indices = 2;
if (vbo.first == 0) {
_lastRegisteredLine2D[id] = key;
int vertexPoints = vertices * FLOATS_PER_VERTEX;
GLfloat* vertexData = new GLfloat[vertexPoints]; // only vertices, no normals because we're a 2D quad
GLfloat* vertex = vertexData;
static GLubyte cannonicalIndices[indices] = {0, 1};
int vertexPoint = 0;
// p1
vertex[vertexPoint++] = p1.x;
vertex[vertexPoint++] = p1.y;
// p2
vertex[vertexPoint++] = p2.x;
vertex[vertexPoint++] = p2.y;
glGenBuffers(1, &vbo.first);
glBindBuffer(GL_ARRAY_BUFFER, vbo.first);
glBufferData(GL_ARRAY_BUFFER, vertices * NUM_BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW);
delete[] vertexData;
GLushort* indexData = new GLushort[indices];
GLushort* index = indexData;
for (int i = 0; i < indices; i++) {
index[i] = cannonicalIndices[i];
}
glGenBuffers(1, &vbo.second);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * NUM_BYTES_PER_INDEX, indexData, GL_STATIC_DRAW);
delete[] indexData;
#if 1 // def WANT_DEBUG
if (id == UNKNOWN_ID) {
qDebug() << "new renderLine() 2D VBO made -- _line2DVBOs.size():" << _line2DVBOs.size();
} else {
qDebug() << "new registered renderLine() 2D VBO made -- _registeredLine2DVBOs.size():" << _registeredLine2DVBOs.size();
}
#endif
} else {
glBindBuffer(GL_ARRAY_BUFFER, vbo.first);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second);
}
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(FLOATS_PER_VERTEX, GL_FLOAT, FLOATS_PER_VERTEX * sizeof(float), 0);
glDrawRangeElementsEXT(GL_LINES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
QSharedPointer<NetworkGeometry> GeometryCache::getGeometry(const QUrl& url, const QUrl& fallback, bool delayLoad) {
return getResource(url, fallback, delayLoad).staticCast<NetworkGeometry>();
}

View file

@ -109,6 +109,10 @@ public:
const glm::vec2& texCoordTopLeft, const glm::vec2& texCoordBottomLeft,
const glm::vec2& texCoordBottomRight, const glm::vec2& texCoordTopRight, int id = UNKNOWN_ID);
void renderLine(const glm::vec3& p1, const glm::vec3& p2, int id = UNKNOWN_ID);
void renderLine(const glm::vec2& p1, const glm::vec2& p2, int id = UNKNOWN_ID);
/// Loads geometry from the specified URL.
/// \param fallback a fallback URL to load if the desired one is unavailable
/// \param delayLoad if true, don't load the geometry immediately; wait until load is first requested
@ -149,6 +153,14 @@ private:
QHash<Vec3Pair, VerticesIndices> _rectVBOs;
QHash<int, VerticesIndices> _registeredRectVBOs;
QHash<int, Vec3Pair> _lastRegisteredLine3D;
QHash<Vec3Pair, VerticesIndices> _line3DVBOs;
QHash<int, VerticesIndices> _registeredLine3DVBOs;
QHash<int, Vec2Pair> _lastRegisteredLine2D;
QHash<Vec2Pair, VerticesIndices> _line2DVBOs;
QHash<int, VerticesIndices> _registeredLine2DVBOs;
QHash<IntPair, QOpenGLBuffer> _gridBuffers;