first cut at adding colors to GeometryCache and supporting batch internally

This commit is contained in:
ZappoMan 2015-01-08 15:52:33 -08:00
parent 25b9b50468
commit a03c2b79fa
7 changed files with 138 additions and 96 deletions

View file

@ -36,22 +36,18 @@ void renderWorldBox() {
GeometryCache::SharedPointer geometryCache = DependencyManager::get<GeometryCache>(); GeometryCache::SharedPointer geometryCache = DependencyManager::get<GeometryCache>();
// Show edge of world // Show edge of world
float red[] = {1, 0, 0}; glm::vec3 red(1.0f, 0.0f, 0.0f);
float green[] = {0, 1, 0}; glm::vec3 green(0.0f, 1.0f, 0.0f);
float blue[] = {0, 0, 1}; glm::vec3 blue(0.0f, 0.0f, 1.0f);
float gray[] = {0.5, 0.5, 0.5}; glm::vec3 grey(0.5f, 0.5f, 0.5f);
glDisable(GL_LIGHTING); glDisable(GL_LIGHTING);
glLineWidth(1.0); glLineWidth(1.0);
glColor3fv(red); geometryCache->renderLine(glm::vec3(0, 0, 0), glm::vec3(TREE_SCALE, 0, 0), red);
geometryCache->renderLine(glm::vec3(0, 0, 0), glm::vec3(TREE_SCALE, 0, 0)); geometryCache->renderLine(glm::vec3(0, 0, 0), glm::vec3(0, TREE_SCALE, 0), green);
glColor3fv(green); geometryCache->renderLine(glm::vec3(0, 0, 0), glm::vec3(0, 0, TREE_SCALE), blue);
geometryCache->renderLine(glm::vec3(0, 0, 0), glm::vec3(0, TREE_SCALE, 0)); geometryCache->renderLine(glm::vec3(0, 0, TREE_SCALE), glm::vec3(TREE_SCALE, 0, TREE_SCALE), grey);
glColor3fv(blue); geometryCache->renderLine(glm::vec3(TREE_SCALE, 0, TREE_SCALE), glm::vec3(TREE_SCALE, 0, 0), grey);
geometryCache->renderLine(glm::vec3(0, 0, 0), glm::vec3(0, 0, TREE_SCALE));
glColor3fv(gray);
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 // Draw meter markers along the 3 axis to help with measuring things
@ -60,21 +56,21 @@ void renderWorldBox() {
glEnable(GL_LIGHTING); glEnable(GL_LIGHTING);
glPushMatrix(); glPushMatrix();
glTranslatef(MARKER_DISTANCE, 0, 0); glTranslatef(MARKER_DISTANCE, 0, 0);
glColor3fv(red); glColor3f(red.x, red.y, red.z);
geometryCache->renderSphere(MARKER_RADIUS, 10, 10); geometryCache->renderSphere(MARKER_RADIUS, 10, 10);
glPopMatrix(); glPopMatrix();
glPushMatrix(); glPushMatrix();
glTranslatef(0, MARKER_DISTANCE, 0); glTranslatef(0, MARKER_DISTANCE, 0);
glColor3fv(green); glColor3f(green.x, green.y, green.z);
geometryCache->renderSphere(MARKER_RADIUS, 10, 10); geometryCache->renderSphere(MARKER_RADIUS, 10, 10);
glPopMatrix(); glPopMatrix();
glPushMatrix(); glPushMatrix();
glTranslatef(0, 0, MARKER_DISTANCE); glTranslatef(0, 0, MARKER_DISTANCE);
glColor3fv(blue); glColor3f(blue.x, blue.y, blue.z);
geometryCache->renderSphere(MARKER_RADIUS, 10, 10); geometryCache->renderSphere(MARKER_RADIUS, 10, 10);
glPopMatrix(); glPopMatrix();
glPushMatrix(); glPushMatrix();
glColor3fv(gray); glColor3f(grey.x, grey.y, grey.z);
glTranslatef(MARKER_DISTANCE, 0, MARKER_DISTANCE); glTranslatef(MARKER_DISTANCE, 0, MARKER_DISTANCE);
geometryCache->renderSphere(MARKER_RADIUS, 10, 10); geometryCache->renderSphere(MARKER_RADIUS, 10, 10);
glPopMatrix(); glPopMatrix();

View file

@ -301,8 +301,7 @@ void Avatar::render(const glm::vec3& cameraPosition, RenderMode renderMode, bool
glm::vec3 axis = glm::axis(rotation); glm::vec3 axis = glm::axis(rotation);
glRotatef(angle, axis.x, axis.y, axis.z); glRotatef(angle, axis.x, axis.y, axis.z);
glColor3f(laserColor.x, laserColor.y, laserColor.z); geometryCache->renderLine(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, laserLength, 0.0f), laserColor);
geometryCache->renderLine(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, laserLength, 0.0f));
} glPopMatrix(); } glPopMatrix();
} }
@ -327,8 +326,7 @@ void Avatar::render(const glm::vec3& cameraPosition, RenderMode renderMode, bool
float angle = glm::degrees(glm::angle(rotation)); float angle = glm::degrees(glm::angle(rotation));
glm::vec3 axis = glm::axis(rotation); glm::vec3 axis = glm::axis(rotation);
glRotatef(angle, axis.x, axis.y, axis.z); glRotatef(angle, axis.x, axis.y, axis.z);
glColor3f(laserColor.x, laserColor.y, laserColor.z); geometryCache->renderLine(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, laserLength, 0.0f), laserColor);
geometryCache->renderLine(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, laserLength, 0.0f));
} glPopMatrix(); } glPopMatrix();
} }

View file

@ -345,11 +345,10 @@ void Head::renderLookatVectors(glm::vec3 leftEyePosition, glm::vec3 rightEyePosi
glLineWidth(2.0); glLineWidth(2.0);
// TODO: implement support for lines with gradient colors glm::vec4 startColor(0.2f, 0.2f, 0.2f, 1.0f);
// glColor4f(0.2f, 0.2f, 0.2f, 1.0f); --> to --> glColor4f(1.0f, 1.0f, 1.0f, 0.0f); glm::vec4 endColor(1.0f, 1.0f, 1.0f, 0.0f);
glColor4f(0.5f, 0.5f, 0.5f, 0.5f); geometryCache->renderLine(leftEyePosition, lookatPosition, startColor, endColor, _leftEyeLookAtID);
geometryCache->renderLine(leftEyePosition, lookatPosition, _leftEyeLookAtID); geometryCache->renderLine(rightEyePosition, lookatPosition, startColor, endColor, _rightEyeLookAtID);
geometryCache->renderLine(rightEyePosition, lookatPosition, _rightEyeLookAtID);
DependencyManager::get<GlowEffect>()->end(); DependencyManager::get<GlowEffect>()->end();
} }

View file

@ -396,14 +396,14 @@ void SkeletonModel::renderOrientationDirections(int jointIndex, glm::vec3 positi
glm::vec3 pUp = position + orientation * IDENTITY_UP * size; glm::vec3 pUp = position + orientation * IDENTITY_UP * size;
glm::vec3 pFront = position + orientation * IDENTITY_FRONT * size; glm::vec3 pFront = position + orientation * IDENTITY_FRONT * size;
glColor3f(1.0f, 0.0f, 0.0f); glm::vec3 red(1.0f, 0.0f, 0.0f);
geometryCache->renderLine(position, pRight, jointLineIDs._right); geometryCache->renderLine(position, pRight, red, jointLineIDs._right);
glColor3f(0.0f, 1.0f, 0.0f); glm::vec3 green(0.0f, 1.0f, 0.0f);
geometryCache->renderLine(position, pUp, jointLineIDs._up); geometryCache->renderLine(position, pUp, green, jointLineIDs._up);
glColor3f(0.0f, 0.0f, 1.0f); glm::vec3 blue(0.0f, 0.0f, 1.0f);
geometryCache->renderLine(position, pFront, jointLineIDs._front); geometryCache->renderLine(position, pFront, blue, jointLineIDs._front);
} }

View file

@ -50,6 +50,7 @@ void Line3DOverlay::render(RenderArgs* args) {
xColor color = getColor(); xColor color = getColor();
const float MAX_COLOR = 255.0f; const float MAX_COLOR = 255.0f;
glColor4f(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); glColor4f(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha);
glm::vec4 colorv4(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha);
glm::vec3 position = getPosition(); glm::vec3 position = getPosition();
glm::quat rotation = getRotation(); glm::quat rotation = getRotation();
@ -61,7 +62,7 @@ void Line3DOverlay::render(RenderArgs* args) {
if (getIsDashedLine()) { if (getIsDashedLine()) {
DependencyManager::get<GeometryCache>()->renderDashedLine(_position, _end, _geometryCacheID); DependencyManager::get<GeometryCache>()->renderDashedLine(_position, _end, _geometryCacheID);
} else { } else {
DependencyManager::get<GeometryCache>()->renderLine(_start, _end, _geometryCacheID); DependencyManager::get<GeometryCache>()->renderLine(_start, _end, colorv4, _geometryCacheID);
} }
glEnable(GL_LIGHTING); glEnable(GL_LIGHTING);

View file

@ -18,6 +18,9 @@
#include <QRunnable> #include <QRunnable>
#include <QThreadPool> #include <QThreadPool>
#include <gpu/Batch.h>
#include <gpu/GLBackend.h>
#include <SharedUtil.h> #include <SharedUtil.h>
#include "TextureCache.h" #include "TextureCache.h"
@ -1397,18 +1400,30 @@ void GeometryCache::renderDashedLine(const glm::vec3& start, const glm::vec3& en
details.buffer.release(); details.buffer.release();
} }
void GeometryCache::renderLine(const glm::vec3& p1, const glm::vec3& p2, int id) { void GeometryCache::renderLine(const glm::vec3& p1, const glm::vec3& p2,
bool registeredLine = (id != UNKNOWN_ID); const glm::vec4& color1, const glm::vec4& color2, int id) {
bool registered = (id != UNKNOWN_ID);
Vec3Pair key(p1, p2); Vec3Pair key(p1, p2);
VerticesIndices& vbo = registeredLine ? _registeredLine3DVBOs[id] : _line3DVBOs[key]; BatchItemDetails& details = registered ? _registeredLine3DVBOs[id] : _line3DVBOs[key];
int compactColor1 = ((int(color1.x * 255.0f) & 0xFF)) |
((int(color1.y * 255.0f) & 0xFF) << 8) |
((int(color1.z * 255.0f) & 0xFF) << 16) |
((int(color1.w * 255.0f) & 0xFF) << 24);
int compactColor2 = ((int(color2.x * 255.0f) & 0xFF)) |
((int(color2.y * 255.0f) & 0xFF) << 8) |
((int(color2.z * 255.0f) & 0xFF) << 16) |
((int(color2.w * 255.0f) & 0xFF) << 24);
// if this is a registered quad, and we have buffers, then check to see if the geometry changed and rebuild if needed // 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) { if (registered && details.isCreated) {
Vec3Pair& lastKey = _lastRegisteredLine3D[id]; Vec3Pair& lastKey = _lastRegisteredLine3D[id];
if (lastKey != key) { if (lastKey != key) {
glDeleteBuffers(1, &vbo.first); details.clear();
glDeleteBuffers(1, &vbo.second); _lastRegisteredLine3D[id] = key;
vbo.first = vbo.second = 0;
#ifdef WANT_DEBUG #ifdef WANT_DEBUG
qDebug() << "renderLine() 3D ... RELEASING REGISTERED line"; qDebug() << "renderLine() 3D ... RELEASING REGISTERED line";
#endif // def WANT_DEBUG #endif // def WANT_DEBUG
@ -1421,45 +1436,37 @@ void GeometryCache::renderLine(const glm::vec3& p1, const glm::vec3& p2, int id)
} }
const int FLOATS_PER_VERTEX = 3; const int FLOATS_PER_VERTEX = 3;
const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat);
const int vertices = 2; const int vertices = 2;
const int indices = 2; if (!details.isCreated) {
if (vbo.first == 0) {
_lastRegisteredLine3D[id] = key;
int vertexPoints = vertices * FLOATS_PER_VERTEX; details.isCreated = true;
GLfloat* vertexData = new GLfloat[vertexPoints]; // only vertices, no normals because we're a 2D quad details.vertices = vertices;
GLfloat* vertex = vertexData; details.vertexSize = FLOATS_PER_VERTEX;
static GLubyte cannonicalIndices[indices] = {0, 1};
int vertexPoint = 0; gpu::BufferPointer verticesBuffer(new gpu::Buffer());
gpu::BufferPointer colorBuffer(new gpu::Buffer());
gpu::Stream::FormatPointer streamFormat(new gpu::Stream::Format());
gpu::BufferStreamPointer stream(new gpu::BufferStream());
// p1 details.verticesBuffer = verticesBuffer;
vertex[vertexPoint++] = p1.x; details.colorBuffer = colorBuffer;
vertex[vertexPoint++] = p1.y; details.streamFormat = streamFormat;
vertex[vertexPoint++] = p1.z; details.stream = stream;
// p2 details.streamFormat->setAttribute(gpu::Stream::POSITION, 0, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::POS_XYZ), 0);
vertex[vertexPoint++] = p2.x; details.streamFormat->setAttribute(gpu::Stream::COLOR, 1, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA));
vertex[vertexPoint++] = p2.y;
vertex[vertexPoint++] = p2.z; details.stream->addBuffer(details.verticesBuffer, 0, details.streamFormat->getChannels().at(0)._stride);
details.stream->addBuffer(details.colorBuffer, 0, details.streamFormat->getChannels().at(1)._stride);
glGenBuffers(1, &vbo.first); float vertexBuffer[vertices * FLOATS_PER_VERTEX] = { p1.x, p1.y, p1.z, p2.x, p2.y, p2.z };
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]; const int NUM_COLOR_SCALARS = 2;
GLushort* index = indexData; int colors[NUM_COLOR_SCALARS] = { compactColor1, compactColor2 };
for (int i = 0; i < indices; i++) {
index[i] = cannonicalIndices[i];
}
glGenBuffers(1, &vbo.second); details.verticesBuffer->append(sizeof(vertexBuffer), (gpu::Buffer::Byte*) vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); details.colorBuffer->append(sizeof(colors), (gpu::Buffer::Byte*) colors);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * NUM_BYTES_PER_INDEX, indexData, GL_STATIC_DRAW);
delete[] indexData;
#ifdef WANT_DEBUG #ifdef WANT_DEBUG
if (id == UNKNOWN_ID) { if (id == UNKNOWN_ID) {
@ -1468,18 +1475,20 @@ void GeometryCache::renderLine(const glm::vec3& p1, const glm::vec3& p2, int id)
qDebug() << "new registered renderLine() 3D VBO made -- _registeredLine3DVBOs.size():" << _registeredLine3DVBOs.size(); qDebug() << "new registered renderLine() 3D VBO made -- _registeredLine3DVBOs.size():" << _registeredLine3DVBOs.size();
} }
#endif #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);
gpu::Batch batch;
// this is what it takes to render a quad
batch.setInputFormat(details.streamFormat);
batch.setInputStream(0, *details.stream);
batch.draw(gpu::LINES, 2, 0);
gpu::GLBackend::renderBatch(batch);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
} }
void GeometryCache::renderLine(const glm::vec2& p1, const glm::vec2& p2, int id) { void GeometryCache::renderLine(const glm::vec2& p1, const glm::vec2& p2, int id) {

View file

@ -111,7 +111,20 @@ public:
const glm::vec2& texCoordBottomRight, const glm::vec2& texCoordTopRight, int id = UNKNOWN_ID); 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::vec3& p1, const glm::vec3& p2, const glm::vec3& color, int id = UNKNOWN_ID)
{ renderLine(p1, p2, color, color, id); }
void renderLine(const glm::vec3& p1, const glm::vec3& p2,
const glm::vec3& color1, const glm::vec3& color2, int id = UNKNOWN_ID)
{ renderLine(p1, p2, glm::vec4(color1, 1.0f), glm::vec4(color2, 1.0f), id); }
void renderLine(const glm::vec3& p1, const glm::vec3& p2,
const glm::vec4& color, int id = UNKNOWN_ID)
{ renderLine(p1, p2, color, color, id); }
void renderLine(const glm::vec3& p1, const glm::vec3& p2,
const glm::vec4& color1, const glm::vec4& color2, int id = UNKNOWN_ID);
void renderDashedLine(const glm::vec3& start, const glm::vec3& end, int id = UNKNOWN_ID); void renderDashedLine(const glm::vec3& start, const glm::vec3& end, int id = UNKNOWN_ID);
void renderLine(const glm::vec2& p1, const glm::vec2& p2, int id = UNKNOWN_ID); void renderLine(const glm::vec2& p1, const glm::vec2& p2, int id = UNKNOWN_ID);
@ -135,6 +148,39 @@ private:
typedef QPair<int, int> IntPair; typedef QPair<int, int> IntPair;
typedef QPair<GLuint, GLuint> VerticesIndices; typedef QPair<GLuint, GLuint> VerticesIndices;
struct BufferDetails {
QOpenGLBuffer buffer;
int vertices;
int vertexSize;
};
class BatchItemDetails {
public:
gpu::BufferPointer verticesBuffer;
gpu::BufferPointer colorBuffer;
gpu::Stream::FormatPointer streamFormat;
gpu::BufferStreamPointer stream;
int vertices;
int vertexSize;
bool isCreated;
BatchItemDetails() :
verticesBuffer(NULL),
colorBuffer(NULL),
streamFormat(NULL),
stream(NULL),
vertices(0),
vertexSize(0),
isCreated(false)
{
}
void clear() {
// TODO: add the proper de-allocation of the gpu items
isCreated = false;
}
};
QHash<IntPair, VerticesIndices> _hemisphereVBOs; QHash<IntPair, VerticesIndices> _hemisphereVBOs;
QHash<IntPair, VerticesIndices> _sphereVBOs; QHash<IntPair, VerticesIndices> _sphereVBOs;
@ -160,27 +206,20 @@ private:
QHash<int, VerticesIndices> _registeredRectVBOs; QHash<int, VerticesIndices> _registeredRectVBOs;
QHash<int, Vec3Pair> _lastRegisteredLine3D; QHash<int, Vec3Pair> _lastRegisteredLine3D;
QHash<Vec3Pair, VerticesIndices> _line3DVBOs; QHash<Vec3Pair, BatchItemDetails> _line3DVBOs;
QHash<int, VerticesIndices> _registeredLine3DVBOs; QHash<int, BatchItemDetails> _registeredLine3DVBOs;
QHash<int, Vec2Pair> _lastRegisteredLine2D; QHash<int, Vec2Pair> _lastRegisteredLine2D;
QHash<Vec2Pair, VerticesIndices> _line2DVBOs; QHash<Vec2Pair, VerticesIndices> _line2DVBOs;
QHash<int, VerticesIndices> _registeredLine2DVBOs; QHash<int, VerticesIndices> _registeredLine2DVBOs;
struct BufferDetails {
QOpenGLBuffer buffer;
int vertices;
int vertexSize;
};
QHash<int, BufferDetails> _registeredVertices; QHash<int, BufferDetails> _registeredVertices;
QHash<int, Vec3Pair> _lastRegisteredDashedLines; QHash<int, Vec3Pair> _lastRegisteredDashedLines;
QHash<Vec3Pair, BufferDetails> _dashedLines; QHash<Vec3Pair, BufferDetails> _dashedLines;
QHash<int, BufferDetails> _registeredDashedLines; QHash<int, BufferDetails> _registeredDashedLines;
QHash<IntPair, BufferDetails> _colors;
QHash<IntPair, QOpenGLBuffer> _gridBuffers; QHash<IntPair, QOpenGLBuffer> _gridBuffers;
QHash<int, QOpenGLBuffer> _registeredAlternateGridBuffers; QHash<int, QOpenGLBuffer> _registeredAlternateGridBuffers;