diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 5a60be39a1..33ddad3c2b 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2799,8 +2799,7 @@ void Application::displaySide(Camera& theCamera, bool selfAvatarOnly, RenderArgs if (!selfAvatarOnly) { // draw a red sphere float originSphereRadius = 0.05f; - glColor3f(1,0,0); - DependencyManager::get()->renderSphere(originSphereRadius, 15, 15); + DependencyManager::get()->renderSphere(originSphereRadius, 15, 15, glm::vec4(1.0f, 0.0f, 0.0f, 1.0f)); // also, metavoxels if (Menu::getInstance()->isOptionChecked(MenuOption::Metavoxels)) { diff --git a/interface/src/Environment.cpp b/interface/src/Environment.cpp index 645bbbdc4f..5cc132bb7f 100644 --- a/interface/src/Environment.cpp +++ b/interface/src/Environment.cpp @@ -263,7 +263,7 @@ void Environment::renderAtmosphere(Camera& camera, const EnvironmentData& data) glDepthMask(GL_FALSE); glDisable(GL_DEPTH_TEST); - DependencyManager::get()->renderSphere(1.0f, 100, 50); //Draw a unit sphere + DependencyManager::get()->renderSphere(1.0f, 100, 50, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); //Draw a unit sphere glDepthMask(GL_TRUE); program->release(); diff --git a/interface/src/MetavoxelSystem.cpp b/interface/src/MetavoxelSystem.cpp index 80c7c3f41f..fc3aef89ea 100644 --- a/interface/src/MetavoxelSystem.cpp +++ b/interface/src/MetavoxelSystem.cpp @@ -1251,7 +1251,6 @@ SphereRenderer::SphereRenderer() { void SphereRenderer::render(const MetavoxelLOD& lod, bool contained, bool cursor) { Sphere* sphere = static_cast(_spanner); const QColor& color = sphere->getColor(); - glColor4f(color.redF(), color.greenF(), color.blueF(), color.alphaF()); glPushMatrix(); const glm::vec3& translation = sphere->getTranslation(); @@ -1260,7 +1259,8 @@ void SphereRenderer::render(const MetavoxelLOD& lod, bool contained, bool cursor glm::vec3 axis = glm::axis(rotation); glRotatef(glm::degrees(glm::angle(rotation)), axis.x, axis.y, axis.z); - DependencyManager::get()->renderSolidSphere(sphere->getScale(), 32, 32); + DependencyManager::get()->renderSolidSphere(sphere->getScale(), 32, 32, + glm::vec4(color.redF(), color.greenF(), color.blueF(), color.alphaF())); glPopMatrix(); } @@ -1271,7 +1271,6 @@ CuboidRenderer::CuboidRenderer() { void CuboidRenderer::render(const MetavoxelLOD& lod, bool contained, bool cursor) { Cuboid* cuboid = static_cast(_spanner); const QColor& color = cuboid->getColor(); - glColor4f(color.redF(), color.greenF(), color.blueF(), color.alphaF()); glPushMatrix(); const glm::vec3& translation = cuboid->getTranslation(); @@ -1281,7 +1280,8 @@ void CuboidRenderer::render(const MetavoxelLOD& lod, bool contained, bool cursor glRotatef(glm::degrees(glm::angle(rotation)), axis.x, axis.y, axis.z); glScalef(1.0f, cuboid->getAspectY(), cuboid->getAspectZ()); - DependencyManager::get()->renderSolidCube(cuboid->getScale() * 2.0f); + DependencyManager::get()->renderSolidCube(cuboid->getScale() * 2.0f, + glm::vec4(color.redF(), color.greenF(), color.blueF(), color.alphaF())); glPopMatrix(); } diff --git a/interface/src/Util.cpp b/interface/src/Util.cpp index 79ea6b6924..e2ec29ecdc 100644 --- a/interface/src/Util.cpp +++ b/interface/src/Util.cpp @@ -36,22 +36,18 @@ void renderWorldBox() { auto geometryCache = DependencyManager::get(); // Show edge of world - float red[] = {1, 0, 0}; - float green[] = {0, 1, 0}; - float blue[] = {0, 0, 1}; - float gray[] = {0.5, 0.5, 0.5}; + glm::vec3 red(1.0f, 0.0f, 0.0f); + glm::vec3 green(0.0f, 1.0f, 0.0f); + glm::vec3 blue(0.0f, 0.0f, 1.0f); + glm::vec3 grey(0.5f, 0.5f, 0.5f); glDisable(GL_LIGHTING); glLineWidth(1.0); - glColor3fv(red); - geometryCache->renderLine(glm::vec3(0, 0, 0), glm::vec3(TREE_SCALE, 0, 0)); - glColor3fv(green); - geometryCache->renderLine(glm::vec3(0, 0, 0), glm::vec3(0, TREE_SCALE, 0)); - glColor3fv(blue); - 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)); + geometryCache->renderLine(glm::vec3(0, 0, 0), glm::vec3(TREE_SCALE, 0, 0), red); + geometryCache->renderLine(glm::vec3(0, 0, 0), glm::vec3(0, TREE_SCALE, 0), green); + geometryCache->renderLine(glm::vec3(0, 0, 0), glm::vec3(0, 0, TREE_SCALE), blue); + geometryCache->renderLine(glm::vec3(0, 0, TREE_SCALE), glm::vec3(TREE_SCALE, 0, TREE_SCALE), grey); + geometryCache->renderLine(glm::vec3(TREE_SCALE, 0, TREE_SCALE), glm::vec3(TREE_SCALE, 0, 0), grey); // Draw meter markers along the 3 axis to help with measuring things @@ -60,23 +56,19 @@ void renderWorldBox() { glEnable(GL_LIGHTING); glPushMatrix(); glTranslatef(MARKER_DISTANCE, 0, 0); - glColor3fv(red); - geometryCache->renderSphere(MARKER_RADIUS, 10, 10); + geometryCache->renderSphere(MARKER_RADIUS, 10, 10, red); glPopMatrix(); glPushMatrix(); glTranslatef(0, MARKER_DISTANCE, 0); - glColor3fv(green); - geometryCache->renderSphere(MARKER_RADIUS, 10, 10); + geometryCache->renderSphere(MARKER_RADIUS, 10, 10, green); glPopMatrix(); glPushMatrix(); glTranslatef(0, 0, MARKER_DISTANCE); - glColor3fv(blue); - geometryCache->renderSphere(MARKER_RADIUS, 10, 10); + geometryCache->renderSphere(MARKER_RADIUS, 10, 10, blue); glPopMatrix(); glPushMatrix(); - glColor3fv(gray); glTranslatef(MARKER_DISTANCE, 0, MARKER_DISTANCE); - geometryCache->renderSphere(MARKER_RADIUS, 10, 10); + geometryCache->renderSphere(MARKER_RADIUS, 10, 10, grey); glPopMatrix(); } @@ -114,25 +106,23 @@ void drawText(int x, int y, float scale, float radians, int mono, // glPushMatrix(); glTranslatef(static_cast(x), static_cast(y), 0.0f); - glColor3fv(color); + + glRotated(double(radians * DEGREES_PER_RADIAN), 0.0, 0.0, 1.0); glScalef(scale / 0.1f, scale / 0.1f, 1.0f); - textRenderer(mono)->draw(0, 0, string); + + glm::vec4 colorV4 = {color[0], color[1], color[3], 1.0f }; + textRenderer(mono)->draw(0, 0, string, colorV4); glPopMatrix(); } void renderCollisionOverlay(int width, int height, float magnitude, float red, float blue, float green) { const float MIN_VISIBLE_COLLISION = 0.01f; if (magnitude > MIN_VISIBLE_COLLISION) { - glColor4f(red, blue, green, magnitude); - DependencyManager::get()->renderQuad(0, 0, width, height); + DependencyManager::get()->renderQuad(0, 0, width, height, glm::vec4(red, blue, green, magnitude)); } } -void renderBevelCornersRect(int x, int y, int width, int height, int bevelDistance) { - DependencyManager::get()->renderBevelCornersRect(x, y, width, height, bevelDistance); -} - // Do some basic timing tests and report the results void runTimingTests() { // How long does it take to make a call to get the time? diff --git a/interface/src/Util.h b/interface/src/Util.h index 524216f527..419b776706 100644 --- a/interface/src/Util.h +++ b/interface/src/Util.h @@ -27,8 +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 renderBevelCornersRect(int x, int y, int width, int height, int bevelDistance); - void runTimingTests(); bool rayIntersectsSphere(const glm::vec3& rayStarting, const glm::vec3& rayNormalizedDirection, diff --git a/interface/src/audio/AudioIOStatsRenderer.cpp b/interface/src/audio/AudioIOStatsRenderer.cpp index 066b5b51e5..782010bd00 100644 --- a/interface/src/audio/AudioIOStatsRenderer.cpp +++ b/interface/src/audio/AudioIOStatsRenderer.cpp @@ -50,14 +50,12 @@ void AudioIOStatsRenderer::render(const float* color, int width, int height) { int statsHeight = STATS_HEIGHT_PER_LINE * lines; - static const float backgroundColor[4] = { 0.2f, 0.2f, 0.2f, 0.6f }; + static const glm::vec4 backgroundColor = { 0.2f, 0.2f, 0.2f, 0.6f }; int x = std::max((width - (int)STATS_WIDTH) / 2, 0); int y = std::max((height - CENTERED_BACKGROUND_HEIGHT) / 2, 0); int w = STATS_WIDTH; int h = statsHeight; - glColor4fv(backgroundColor); - DependencyManager::get()->renderQuad(x, y, w, h); - glColor4f(1, 1, 1, 1); + DependencyManager::get()->renderQuad(x, y, w, h, backgroundColor); int horizontalOffset = x + 5; int verticalOffset = y; diff --git a/interface/src/audio/AudioScope.cpp b/interface/src/audio/AudioScope.cpp index 3034e64b6e..6ad3a60cba 100644 --- a/interface/src/audio/AudioScope.cpp +++ b/interface/src/audio/AudioScope.cpp @@ -111,11 +111,11 @@ void AudioScope::render(int width, int height) { return; } - static const float backgroundColor[4] = { 0.4f, 0.4f, 0.4f, 0.6f }; - static const float gridColor[4] = { 0.7f, 0.7f, 0.7f, 1.0f }; - static const float inputColor[4] = { 0.3f, 1.0f, 0.3f, 1.0f }; - static const float outputLeftColor[4] = { 1.0f, 0.3f, 0.3f, 1.0f }; - static const float outputRightColor[4] = { 0.3f, 0.3f, 1.0f, 1.0f }; + static const glm::vec4 backgroundColor = { 0.4f, 0.4f, 0.4f, 0.6f }; + static const glm::vec4 gridColor = { 0.7f, 0.7f, 0.7f, 1.0f }; + static const glm::vec4 inputColor = { 0.3f, 1.0f, 0.3f, 1.0f }; + static const glm::vec4 outputLeftColor = { 1.0f, 0.3f, 0.3f, 1.0f }; + static const glm::vec4 outputRightColor = { 0.3f, 0.3f, 1.0f, 1.0f }; static const int gridRows = 2; int gridCols = _framesPerScope; @@ -132,21 +132,15 @@ void AudioScope::render(int width, int height) { renderLineStrip(_outputRightD, outputRightColor, x, y, _samplesPerScope, _scopeOutputOffset, _scopeOutputRight); } -void AudioScope::renderBackground(const float* color, int x, int y, int width, int height) { - glColor4fv(color); - DependencyManager::get()->renderQuad(x, y, width, height); - glColor4f(1, 1, 1, 1); +void AudioScope::renderBackground(const glm::vec4& color, int x, int y, int width, int height) { + DependencyManager::get()->renderQuad(x, y, width, height, color); } -void AudioScope::renderGrid(const float* color, int x, int y, int width, int height, int rows, int cols) { - glColor4fv(color); - DependencyManager::get()->renderGrid(x, y, width, height, rows, cols, _audioScopeGrid); - glColor4f(1, 1, 1, 1); +void AudioScope::renderGrid(const glm::vec4& color, int x, int y, int width, int height, int rows, int cols) { + DependencyManager::get()->renderGrid(x, y, width, height, rows, cols, color, _audioScopeGrid); } -void AudioScope::renderLineStrip(int id, const float* color, int x, int y, int n, int offset, const QByteArray* byteArray) { - - glColor4fv(color); +void AudioScope::renderLineStrip(int id, const glm::vec4& color, int x, int y, int n, int offset, const QByteArray* byteArray) { int16_t sample; int16_t* samples = ((int16_t*) byteArray->data()) + offset; @@ -200,10 +194,8 @@ void AudioScope::renderLineStrip(int id, const float* color, int x, int y, int n } - geometryCache->updateVertices(id, points); - geometryCache->renderVertices(GL_LINE_STRIP, id); - - glColor4f(1, 1, 1, 1); + geometryCache->updateVertices(id, points, color); + geometryCache->renderVertices(gpu::LINE_STRIP, id); } int AudioScope::addBufferToScope(QByteArray* byteArray, int frameOffset, const int16_t* source, int sourceSamplesPerChannel, diff --git a/interface/src/audio/AudioScope.h b/interface/src/audio/AudioScope.h index 1d92412d8c..df902fe7cd 100644 --- a/interface/src/audio/AudioScope.h +++ b/interface/src/audio/AudioScope.h @@ -12,6 +12,8 @@ #ifndef hifi_AudioScope_h #define hifi_AudioScope_h +#include + #include #include @@ -46,9 +48,9 @@ private slots: private: // Audio scope methods for rendering - static void renderBackground(const float* color, int x, int y, int width, int height); - void renderGrid(const float* color, int x, int y, int width, int height, int rows, int cols); - void renderLineStrip(int id, const float* color, int x, int y, int n, int offset, const QByteArray* byteArray); + static void renderBackground(const glm::vec4& color, int x, int y, int width, int height); + void renderGrid(const glm::vec4& color, int x, int y, int width, int height, int rows, int cols); + void renderLineStrip(int id, const glm::vec4& color, int x, int y, int n, int offset, const QByteArray* byteArray); // Audio scope methods for data acquisition int addBufferToScope(QByteArray* byteArray, int frameOffset, const int16_t* source, int sourceSamples, diff --git a/interface/src/audio/AudioToolBox.cpp b/interface/src/audio/AudioToolBox.cpp index 844f4be30f..8c0fd13ca0 100644 --- a/interface/src/audio/AudioToolBox.cpp +++ b/interface/src/audio/AudioToolBox.cpp @@ -62,17 +62,19 @@ void AudioToolBox::render(int x, int y, bool boxed) { glBindTexture(GL_TEXTURE_2D, _boxTextureId); + glm::vec4 quadColor; + if (isClipping) { - glColor3f(1.0f, 0.0f, 0.0f); + quadColor = glm::vec4(1.0f, 0.0f, 0.0f, 1.0f); } else { - glColor3f(0.41f, 0.41f, 0.41f); + quadColor = glm::vec4(0.41f, 0.41f, 0.41f, 1.0f); } glm::vec2 topLeft(boxBounds.left(), boxBounds.top()); glm::vec2 bottomRight(boxBounds.right(), boxBounds.bottom()); glm::vec2 texCoordTopLeft(1,1); glm::vec2 texCoordBottomRight(0,0); - DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, quadColor); } float iconColor = 1.0f; @@ -98,14 +100,14 @@ void AudioToolBox::render(int x, int y, bool boxed) { iconColor = PULSE_MIN + (PULSE_MAX - PULSE_MIN) * pulseFactor; } - glColor3f(iconColor, iconColor, iconColor); + glm::vec4 quadColor(iconColor, iconColor, iconColor, 1.0f); glm::vec2 topLeft(_iconBounds.left(), _iconBounds.top()); glm::vec2 bottomRight(_iconBounds.right(), _iconBounds.bottom()); glm::vec2 texCoordTopLeft(1,1); glm::vec2 texCoordBottomRight(0,0); - DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, quadColor); glDisable(GL_TEXTURE_2D); } \ No newline at end of file diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index 2c50295edf..727f5f5d7c 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -302,8 +302,7 @@ void Avatar::render(const glm::vec3& cameraPosition, RenderMode renderMode, bool glm::vec3 axis = glm::axis(rotation); 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)); + geometryCache->renderLine(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, laserLength, 0.0f), laserColor); } glPopMatrix(); } @@ -328,8 +327,7 @@ void Avatar::render(const glm::vec3& cameraPosition, RenderMode renderMode, bool float angle = glm::degrees(glm::angle(rotation)); glm::vec3 axis = glm::axis(rotation); 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)); + geometryCache->renderLine(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, laserLength, 0.0f), laserColor); } glPopMatrix(); } @@ -405,15 +403,14 @@ void Avatar::render(const glm::vec3& cameraPosition, RenderMode renderMode, bool if (_isLookAtTarget && Menu::getInstance()->isOptionChecked(MenuOption::RenderFocusIndicator)) { const float LOOK_AT_INDICATOR_RADIUS = 0.03f; const float LOOK_AT_INDICATOR_OFFSET = 0.22f; - const float LOOK_AT_INDICATOR_COLOR[] = { 0.8f, 0.0f, 0.0f, 0.75f }; + const glm::vec4 LOOK_AT_INDICATOR_COLOR = { 0.8f, 0.0f, 0.0f, 0.75f }; glPushMatrix(); - glColor4fv(LOOK_AT_INDICATOR_COLOR); if (_displayName.isEmpty() || _displayNameAlpha == 0.0f) { glTranslatef(_position.x, getDisplayNamePosition().y, _position.z); } else { glTranslatef(_position.x, getDisplayNamePosition().y + LOOK_AT_INDICATOR_OFFSET, _position.z); } - DependencyManager::get()->renderSphere(LOOK_AT_INDICATOR_RADIUS, 15, 15); + DependencyManager::get()->renderSphere(LOOK_AT_INDICATOR_RADIUS, 15, 15, LOOK_AT_INDICATOR_COLOR); glPopMatrix(); } } @@ -437,11 +434,11 @@ void Avatar::render(const glm::vec3& cameraPosition, RenderMode renderMode, bool if (renderMode == NORMAL_RENDER_MODE && (sphereRadius > MIN_SPHERE_SIZE) && (angle < MAX_SPHERE_ANGLE) && (angle > MIN_SPHERE_ANGLE)) { - glColor4f(SPHERE_COLOR[0], SPHERE_COLOR[1], SPHERE_COLOR[2], 1.0f - angle / MAX_SPHERE_ANGLE); glPushMatrix(); glTranslatef(_position.x, _position.y, _position.z); glScalef(height, height, height); - DependencyManager::get()->renderSphere(sphereRadius, 15, 15); + DependencyManager::get()->renderSphere(sphereRadius, 15, 15, + glm::vec4(SPHERE_COLOR[0], SPHERE_COLOR[1], SPHERE_COLOR[2], 1.0f - angle / MAX_SPHERE_ANGLE)); glPopMatrix(); } @@ -572,15 +569,12 @@ void Avatar::renderBillboard() { float size = getBillboardSize(); glScalef(size, size, size); - glColor3f(1.0f, 1.0f, 1.0f); - glm::vec2 topLeft(-1.0f, -1.0f); glm::vec2 bottomRight(1.0f, 1.0f); glm::vec2 texCoordTopLeft(0.0f, 0.0f); glm::vec2 texCoordBottomRight(1.0f, 1.0f); - DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); - + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); glPopMatrix(); @@ -706,15 +700,15 @@ void Avatar::renderDisplayName() { glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(1.0f, 1.0f); - glColor4f(0.2f, 0.2f, 0.2f, _displayNameAlpha * DISPLAYNAME_BACKGROUND_ALPHA / DISPLAYNAME_ALPHA); - renderBevelCornersRect(left, bottom, right - left, top - bottom, 3); + DependencyManager::get()->renderBevelCornersRect(left, bottom, right - left, top - bottom, 3, + glm::vec4(0.2f, 0.2f, 0.2f, _displayNameAlpha * DISPLAYNAME_BACKGROUND_ALPHA / DISPLAYNAME_ALPHA)); - glColor4f(0.93f, 0.93f, 0.93f, _displayNameAlpha); + glm::vec4 color(0.93f, 0.93f, 0.93f, _displayNameAlpha); QByteArray ba = _displayName.toLocal8Bit(); const char* text = ba.data(); glDisable(GL_POLYGON_OFFSET_FILL); - textRenderer(DISPLAYNAME)->draw(text_x, text_y, text); + textRenderer(DISPLAYNAME)->draw(text_x, text_y, text, color); glPopMatrix(); @@ -949,7 +943,8 @@ int Avatar::parseDataAtOffset(const QByteArray& packet, int offset) { int Avatar::_jointConesID = GeometryCache::UNKNOWN_ID; // render a makeshift cone section that serves as a body part connecting joint spheres -void Avatar::renderJointConnectingCone(glm::vec3 position1, glm::vec3 position2, float radius1, float radius2) { +void Avatar::renderJointConnectingCone(glm::vec3 position1, glm::vec3 position2, + float radius1, float radius2, const glm::vec4& color) { auto geometryCache = DependencyManager::get(); @@ -994,8 +989,8 @@ void Avatar::renderJointConnectingCone(glm::vec3 position1, glm::vec3 position2, // TODO: this is really inefficient constantly recreating these vertices buffers. It would be // better if the avatars cached these buffers for each of the joints they are rendering - geometryCache->updateVertices(_jointConesID, points); - geometryCache->renderVertices(GL_TRIANGLES, _jointConesID); + geometryCache->updateVertices(_jointConesID, points, color); + geometryCache->renderVertices(gpu::TRIANGLES, _jointConesID); } } diff --git a/interface/src/avatar/Avatar.h b/interface/src/avatar/Avatar.h index f975e8dac3..6d1a21af94 100644 --- a/interface/src/avatar/Avatar.h +++ b/interface/src/avatar/Avatar.h @@ -133,7 +133,8 @@ public: virtual int parseDataAtOffset(const QByteArray& packet, int offset); - static void renderJointConnectingCone(glm::vec3 position1, glm::vec3 position2, float radius1, float radius2); + static void renderJointConnectingCone(glm::vec3 position1, glm::vec3 position2, + float radius1, float radius2, const glm::vec4& color); virtual void applyCollision(const glm::vec3& contactPoint, const glm::vec3& penetration) { } diff --git a/interface/src/avatar/Hand.cpp b/interface/src/avatar/Hand.cpp index b477f606d7..55ba1701e0 100644 --- a/interface/src/avatar/Hand.cpp +++ b/interface/src/avatar/Hand.cpp @@ -115,8 +115,7 @@ void Hand::render(bool isMine, Model::RenderMode renderMode) { glm::vec3 position = palm.getPosition(); glPushMatrix(); glTranslatef(position.x, position.y, position.z); - glColor3f(0.0f, 1.0f, 0.0f); - DependencyManager::get()->renderSphere(PALM_COLLISION_RADIUS * _owningAvatar->getScale(), 10, 10); + DependencyManager::get()->renderSphere(PALM_COLLISION_RADIUS * _owningAvatar->getScale(), 10, 10, glm::vec3(0.0f, 1.0f, 0.0f)); glPopMatrix(); } } @@ -132,7 +131,6 @@ void Hand::render(bool isMine, Model::RenderMode renderMode) { void Hand::renderHandTargets(bool isMine) { glPushMatrix(); - MyAvatar* myAvatar = Application::getInstance()->getAvatar(); const float avatarScale = Application::getInstance()->getAvatar()->getScale(); const float alpha = 1.0f; @@ -152,8 +150,7 @@ void Hand::renderHandTargets(bool isMine) { glTranslatef(targetPosition.x, targetPosition.y, targetPosition.z); const float collisionRadius = 0.05f; - glColor4f(0.5f,0.5f,0.5f, alpha); - DependencyManager::get()->renderSphere(collisionRadius, 10, 10, false); + DependencyManager::get()->renderSphere(collisionRadius, 10, 10, glm::vec4(0.5f,0.5f,0.5f, alpha), false); glPopMatrix(); } } @@ -167,17 +164,17 @@ void Hand::renderHandTargets(bool isMine) { for (size_t i = 0; i < getNumPalms(); ++i) { PalmData& palm = getPalms()[i]; if (palm.isActive()) { - glColor4f(handColor.r, handColor.g, handColor.b, alpha); glm::vec3 tip = palm.getTipPosition(); glm::vec3 root = palm.getPosition(); - Avatar::renderJointConnectingCone(root, tip, PALM_FINGER_ROD_RADIUS, PALM_FINGER_ROD_RADIUS); + Avatar::renderJointConnectingCone(root, tip, PALM_FINGER_ROD_RADIUS, PALM_FINGER_ROD_RADIUS, glm::vec4(handColor.r, handColor.g, handColor.b, alpha)); + // Render sphere at palm/finger root glm::vec3 offsetFromPalm = root + palm.getNormal() * PALM_DISK_THICKNESS; - Avatar::renderJointConnectingCone(root, offsetFromPalm, PALM_DISK_RADIUS, 0.0f); + Avatar::renderJointConnectingCone(root, offsetFromPalm, PALM_DISK_RADIUS, 0.0f, glm::vec4(handColor.r, handColor.g, handColor.b, alpha)); glPushMatrix(); glTranslatef(root.x, root.y, root.z); - DependencyManager::get()->renderSphere(PALM_BALL_RADIUS, 20.0f, 20.0f); + DependencyManager::get()->renderSphere(PALM_BALL_RADIUS, 20.0f, 20.0f, glm::vec4(handColor.r, handColor.g, handColor.b, alpha)); glPopMatrix(); } } diff --git a/interface/src/avatar/Head.cpp b/interface/src/avatar/Head.cpp index 161c73b7f9..afc1346ad8 100644 --- a/interface/src/avatar/Head.cpp +++ b/interface/src/avatar/Head.cpp @@ -344,11 +344,10 @@ void Head::renderLookatVectors(glm::vec3 leftEyePosition, glm::vec3 rightEyePosi glLineWidth(2.0); - // TODO: implement support for lines with gradient colors - // glColor4f(0.2f, 0.2f, 0.2f, 1.0f); --> to --> glColor4f(1.0f, 1.0f, 1.0f, 0.0f); - glColor4f(0.5f, 0.5f, 0.5f, 0.5f); - geometryCache->renderLine(leftEyePosition, lookatPosition, _leftEyeLookAtID); - geometryCache->renderLine(rightEyePosition, lookatPosition, _rightEyeLookAtID); + glm::vec4 startColor(0.2f, 0.2f, 0.2f, 1.0f); + glm::vec4 endColor(1.0f, 1.0f, 1.0f, 0.0f); + geometryCache->renderLine(leftEyePosition, lookatPosition, startColor, endColor, _leftEyeLookAtID); + geometryCache->renderLine(rightEyePosition, lookatPosition, startColor, endColor, _rightEyeLookAtID); DependencyManager::get()->end(); } diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 4ea7c3214e..28c6f6a965 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -383,17 +383,15 @@ void MyAvatar::renderDebugBodyPoints() { // Torso Sphere position = torsoPosition; glPushMatrix(); - glColor4f(0, 1, 0, .5f); glTranslatef(position.x, position.y, position.z); - DependencyManager::get()->renderSphere(0.2f, 10.0f, 10.0f); + DependencyManager::get()->renderSphere(0.2f, 10.0f, 10.0f, glm::vec4(0, 1, 0, .5f)); glPopMatrix(); // Head Sphere position = headPosition; glPushMatrix(); - glColor4f(0, 1, 0, .5f); glTranslatef(position.x, position.y, position.z); - DependencyManager::get()->renderSphere(0.15f, 10.0f, 10.0f); + DependencyManager::get()->renderSphere(0.15f, 10.0f, 10.0f, glm::vec4(0, 1, 0, .5f)); glPopMatrix(); } @@ -1950,14 +1948,13 @@ void MyAvatar::renderLaserPointers() { for (size_t i = 0; i < getHand()->getNumPalms(); ++i) { PalmData& palm = getHand()->getPalms()[i]; if (palm.isActive()) { - glColor4f(0, 1, 1, 1); glm::vec3 tip = getLaserPointerTipPosition(&palm); glm::vec3 root = palm.getPosition(); //Scale the root vector with the avatar scale scaleVectorRelativeToPosition(root); - Avatar::renderJointConnectingCone(root, tip, PALM_TIP_ROD_RADIUS, PALM_TIP_ROD_RADIUS); + Avatar::renderJointConnectingCone(root, tip, PALM_TIP_ROD_RADIUS, PALM_TIP_ROD_RADIUS, glm::vec4(0, 1, 1, 1)); } } } diff --git a/interface/src/avatar/SkeletonModel.cpp b/interface/src/avatar/SkeletonModel.cpp index f6f7c65101..296ff6fee9 100644 --- a/interface/src/avatar/SkeletonModel.cpp +++ b/interface/src/avatar/SkeletonModel.cpp @@ -354,7 +354,7 @@ void SkeletonModel::renderJointConstraints(int jointIndex) { } else { otherAxis.x = 1.0f; } - glColor4f(otherAxis.r, otherAxis.g, otherAxis.b, 0.75f); + glm::vec4 color(otherAxis.r, otherAxis.g, otherAxis.b, 0.75f); QVector points; points << glm::vec3(0.0f, 0.0f, 0.0f); @@ -366,8 +366,8 @@ void SkeletonModel::renderJointConstraints(int jointIndex) { } // TODO: this is really inefficient constantly recreating these vertices buffers. It would be // better if the skeleton model cached these buffers for each of the joints they are rendering - geometryCache->updateVertices(_triangleFanID, points); - geometryCache->renderVertices(GL_TRIANGLE_FAN, _triangleFanID); + geometryCache->updateVertices(_triangleFanID, points, color); + geometryCache->renderVertices(gpu::TRIANGLE_FAN, _triangleFanID); } glPopMatrix(); @@ -396,14 +396,14 @@ void SkeletonModel::renderOrientationDirections(int jointIndex, glm::vec3 positi 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); + glm::vec3 red(1.0f, 0.0f, 0.0f); + geometryCache->renderLine(position, pRight, red, jointLineIDs._right); - glColor3f(0.0f, 1.0f, 0.0f); - geometryCache->renderLine(position, pUp, jointLineIDs._up); + glm::vec3 green(0.0f, 1.0f, 0.0f); + geometryCache->renderLine(position, pUp, green, jointLineIDs._up); - glColor3f(0.0f, 0.0f, 1.0f); - geometryCache->renderLine(position, pFront, jointLineIDs._front); + glm::vec3 blue(0.0f, 0.0f, 1.0f); + geometryCache->renderLine(position, pFront, blue, jointLineIDs._front); } @@ -601,10 +601,8 @@ void SkeletonModel::renderRagdoll() { glm::vec3 position = points[i]._position - simulationTranslation; glTranslatef(position.x, position.y, position.z); // draw each point as a yellow hexagon with black border - glColor4f(0.0f, 0.0f, 0.0f, alpha); - geometryCache->renderSphere(radius2, BALL_SUBDIVISIONS, BALL_SUBDIVISIONS); - glColor4f(1.0f, 1.0f, 0.0f, alpha); - geometryCache->renderSphere(radius1, BALL_SUBDIVISIONS, BALL_SUBDIVISIONS); + geometryCache->renderSphere(radius2, BALL_SUBDIVISIONS, BALL_SUBDIVISIONS, glm::vec4(0.0f, 0.0f, 0.0f, alpha)); + geometryCache->renderSphere(radius1, BALL_SUBDIVISIONS, BALL_SUBDIVISIONS, glm::vec4(1.0f, 1.0f, 0.0f, alpha)); glPopMatrix(); } glPopMatrix(); @@ -953,9 +951,8 @@ void SkeletonModel::renderBoundingCollisionShapes(float alpha) { _boundingShape.getEndPoint(endPoint); endPoint = endPoint - _translation; glTranslatef(endPoint.x, endPoint.y, endPoint.z); - glColor4f(0.6f, 0.6f, 0.8f, alpha); auto geometryCache = DependencyManager::get(); - geometryCache->renderSphere(_boundingShape.getRadius(), BALL_SUBDIVISIONS, BALL_SUBDIVISIONS); + geometryCache->renderSphere(_boundingShape.getRadius(), BALL_SUBDIVISIONS, BALL_SUBDIVISIONS, glm::vec4(0.6f, 0.6f, 0.8f, alpha)); // draw a yellow sphere at the capsule startpoint glm::vec3 startPoint; @@ -963,13 +960,11 @@ void SkeletonModel::renderBoundingCollisionShapes(float alpha) { startPoint = startPoint - _translation; glm::vec3 axis = endPoint - startPoint; glTranslatef(-axis.x, -axis.y, -axis.z); - glColor4f(0.8f, 0.8f, 0.6f, alpha); - geometryCache->renderSphere(_boundingShape.getRadius(), BALL_SUBDIVISIONS, BALL_SUBDIVISIONS); + geometryCache->renderSphere(_boundingShape.getRadius(), BALL_SUBDIVISIONS, BALL_SUBDIVISIONS, glm::vec4(0.8f, 0.8f, 0.6f, alpha)); // draw a green cylinder between the two points glm::vec3 origin(0.0f); - glColor4f(0.6f, 0.8f, 0.6f, alpha); - Avatar::renderJointConnectingCone( origin, axis, _boundingShape.getRadius(), _boundingShape.getRadius()); + Avatar::renderJointConnectingCone( origin, axis, _boundingShape.getRadius(), _boundingShape.getRadius(), glm::vec4(0.6f, 0.8f, 0.6f, alpha)); glPopMatrix(); } @@ -998,8 +993,7 @@ void SkeletonModel::renderJointCollisionShapes(float alpha) { glm::vec3 position = shape->getTranslation() - simulationTranslation; glTranslatef(position.x, position.y, position.z); // draw a grey sphere at shape position - glColor4f(0.75f, 0.75f, 0.75f, alpha); - geometryCache->renderSphere(shape->getBoundingRadius(), BALL_SUBDIVISIONS, BALL_SUBDIVISIONS); + geometryCache->renderSphere(shape->getBoundingRadius(), BALL_SUBDIVISIONS, BALL_SUBDIVISIONS, glm::vec4(0.75f, 0.75f, 0.75f, alpha)); } else if (shape->getType() == CAPSULE_SHAPE) { CapsuleShape* capsule = static_cast(shape); @@ -1008,8 +1002,7 @@ void SkeletonModel::renderJointCollisionShapes(float alpha) { capsule->getEndPoint(endPoint); endPoint = endPoint - simulationTranslation; glTranslatef(endPoint.x, endPoint.y, endPoint.z); - glColor4f(0.6f, 0.6f, 0.8f, alpha); - geometryCache->renderSphere(capsule->getRadius(), BALL_SUBDIVISIONS, BALL_SUBDIVISIONS); + geometryCache->renderSphere(capsule->getRadius(), BALL_SUBDIVISIONS, BALL_SUBDIVISIONS, glm::vec4(0.6f, 0.6f, 0.8f, alpha)); // draw a yellow sphere at the capsule startpoint glm::vec3 startPoint; @@ -1017,13 +1010,11 @@ void SkeletonModel::renderJointCollisionShapes(float alpha) { startPoint = startPoint - simulationTranslation; glm::vec3 axis = endPoint - startPoint; glTranslatef(-axis.x, -axis.y, -axis.z); - glColor4f(0.8f, 0.8f, 0.6f, alpha); - geometryCache->renderSphere(capsule->getRadius(), BALL_SUBDIVISIONS, BALL_SUBDIVISIONS); + geometryCache->renderSphere(capsule->getRadius(), BALL_SUBDIVISIONS, BALL_SUBDIVISIONS, glm::vec4(0.8f, 0.8f, 0.6f, alpha)); // draw a green cylinder between the two points glm::vec3 origin(0.0f); - glColor4f(0.6f, 0.8f, 0.6f, alpha); - Avatar::renderJointConnectingCone( origin, axis, capsule->getRadius(), capsule->getRadius()); + Avatar::renderJointConnectingCone( origin, axis, capsule->getRadius(), capsule->getRadius(), glm::vec4(0.6f, 0.8f, 0.6f, alpha)); } glPopMatrix(); } diff --git a/interface/src/devices/PrioVR.cpp b/interface/src/devices/PrioVR.cpp index 78b802ac9c..56b2587b0d 100644 --- a/interface/src/devices/PrioVR.cpp +++ b/interface/src/devices/PrioVR.cpp @@ -218,6 +218,6 @@ void PrioVR::renderCalibrationCountdown() { auto glCanvas = DependencyManager::get(); textRenderer->draw((glCanvas->width() - textRenderer->computeWidth(text.constData())) / 2, glCanvas->height() / 2, - text); + text, glm::vec4(1,1,1,1)); #endif } diff --git a/interface/src/octree/OctreeFade.cpp b/interface/src/octree/OctreeFade.cpp index 2e2e45eb90..917bb67124 100644 --- a/interface/src/octree/OctreeFade.cpp +++ b/interface/src/octree/OctreeFade.cpp @@ -43,12 +43,11 @@ void OctreeFade::render() { glDisable(GL_LIGHTING); glPushMatrix(); glScalef(TREE_SCALE, TREE_SCALE, TREE_SCALE); - glColor4f(red, green, blue, opacity); glTranslatef(voxelDetails.x + voxelDetails.s * 0.5f, voxelDetails.y + voxelDetails.s * 0.5f, voxelDetails.z + voxelDetails.s * 0.5f); glLineWidth(1.0f); - DependencyManager::get()->renderSolidCube(voxelDetails.s); + DependencyManager::get()->renderSolidCube(voxelDetails.s, glm::vec4(red, green, blue, opacity)); glLineWidth(1.0f); glPopMatrix(); glEnable(GL_LIGHTING); diff --git a/interface/src/ui/ApplicationOverlay.cpp b/interface/src/ui/ApplicationOverlay.cpp index 7031db4498..9f5f6e21e5 100644 --- a/interface/src/ui/ApplicationOverlay.cpp +++ b/interface/src/ui/ApplicationOverlay.cpp @@ -118,10 +118,16 @@ void ApplicationOverlay::renderReticle(glm::quat orientation, float alpha) { glm::vec3 topRight = getPoint(-reticleSize / 2.0f, -reticleSize / 2.0f); glm::vec3 bottomLeft = getPoint(reticleSize / 2.0f, reticleSize / 2.0f); glm::vec3 bottomRight = getPoint(-reticleSize / 2.0f, reticleSize / 2.0f); - glColor4f(RETICLE_COLOR[0], RETICLE_COLOR[1], RETICLE_COLOR[2], alpha); + + // TODO: this version of renderQuad() needs to take a color + glm::vec4 reticleColor = { RETICLE_COLOR[0], RETICLE_COLOR[1], RETICLE_COLOR[2], alpha }; + + + DependencyManager::get()->renderQuad(topLeft, bottomLeft, bottomRight, topRight, glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f), - glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f), _reticleQuad); + glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f), + reticleColor, _reticleQuad); } glPopMatrix(); } @@ -246,13 +252,13 @@ void ApplicationOverlay::displayOverlayTexture() { glDisable(GL_LIGHTING); glEnable(GL_BLEND); - glColor4f(1.0f, 1.0f, 1.0f, _alpha); glm::vec2 topLeft(0.0f, 0.0f); glm::vec2 bottomRight(glCanvas->getDeviceWidth(), glCanvas->getDeviceHeight()); glm::vec2 texCoordTopLeft(0.0f, 1.0f); glm::vec2 texCoordBottomRight(1.0f, 0.0f); - DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, + glm::vec4(1.0f, 1.0f, 1.0f, _alpha)); } glPopMatrix(); @@ -315,8 +321,6 @@ void ApplicationOverlay::displayOverlayTextureOculus(Camera& whichCamera) { glDepthMask(GL_FALSE); glDisable(GL_ALPHA_TEST); - glColor4f(1.0f, 1.0f, 1.0f, _alpha); - static float textureFOV = 0.0f, textureAspectRatio = 1.0f; if (textureFOV != _textureFov || textureAspectRatio != _textureAspectRatio) { @@ -375,7 +379,7 @@ void ApplicationOverlay::displayOverlayTexture3DTV(Camera& whichCamera, float as glTranslatef(pos.x, pos.y, pos.z); glRotatef(glm::degrees(glm::angle(rot)), axis.x, axis.y, axis.z); - glColor4f(1.0f, 1.0f, 1.0f, _alpha); + glm::vec4 overlayColor = {1.0f, 1.0f, 1.0f, _alpha}; //Render const GLfloat distance = 1.0f; @@ -394,7 +398,8 @@ void ApplicationOverlay::displayOverlayTexture3DTV(Camera& whichCamera, float as glm::vec3(x + quadWidth, y, -distance), glm::vec3(x, y, -distance), glm::vec2(0.0f, 1.0f), glm::vec2(1.0f, 1.0f), - glm::vec2(1.0f, 0.0f), glm::vec2(0.0f, 0.0f)); + glm::vec2(1.0f, 0.0f), glm::vec2(0.0f, 0.0f), + overlayColor); auto glCanvas = DependencyManager::get(); if (_crosshairTexture == 0) { @@ -410,7 +415,7 @@ void ApplicationOverlay::displayOverlayTexture3DTV(Camera& whichCamera, float as const float mouseX = (application->getMouseX() / (float)glCanvas->width()) * quadWidth; const float mouseY = (1.0 - (application->getMouseY() / (float)glCanvas->height())) * quadHeight; - glColor3f(RETICLE_COLOR[0], RETICLE_COLOR[1], RETICLE_COLOR[2]); + glm::vec4 reticleColor = { RETICLE_COLOR[0], RETICLE_COLOR[1], RETICLE_COLOR[2], 1.0f }; DependencyManager::get()->renderQuad(glm::vec3(x + mouseX, y + mouseY, -distance), glm::vec3(x + mouseX + reticleSize, y + mouseY, -distance), @@ -418,7 +423,7 @@ void ApplicationOverlay::displayOverlayTexture3DTV(Camera& whichCamera, float as glm::vec3(x + mouseX, y + mouseY - reticleSize, -distance), glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f), glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f), - _reticleQuad); + reticleColor, _reticleQuad); glEnable(GL_DEPTH_TEST); @@ -430,8 +435,6 @@ void ApplicationOverlay::displayOverlayTexture3DTV(Camera& whichCamera, float as glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE); glEnable(GL_LIGHTING); - - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); } void ApplicationOverlay::computeOculusPickRay(float x, float y, glm::vec3& origin, glm::vec3& direction) const { @@ -674,14 +677,13 @@ void ApplicationOverlay::renderControllerPointers() { mouseY += reticleSize / 2.0f; - glColor3f(RETICLE_COLOR[0], RETICLE_COLOR[1], RETICLE_COLOR[2]); - glm::vec2 topLeft(mouseX, mouseY); glm::vec2 bottomRight(mouseX + reticleSize, mouseY - reticleSize); glm::vec2 texCoordTopLeft(0.0f, 0.0f); glm::vec2 texCoordBottomRight(1.0f, 1.0f); - DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, + glm::vec4(RETICLE_COLOR[0], RETICLE_COLOR[1], RETICLE_COLOR[2], 1.0f)); } } @@ -757,7 +759,7 @@ void ApplicationOverlay::renderMagnifier(glm::vec2 magPos, float sizeMult, bool border << bottomRight; border << topRight; border << topLeft; - geometryCache->updateVertices(_magnifierBorder, border); + geometryCache->updateVertices(_magnifierBorder, border, glm::vec4(1.0f, 0.0f, 0.0f, _alpha)); _previousMagnifierBottomLeft = bottomLeft; _previousMagnifierBottomRight = bottomRight; @@ -770,18 +772,17 @@ void ApplicationOverlay::renderMagnifier(glm::vec2 magPos, float sizeMult, bool glDisable(GL_TEXTURE_2D); glLineWidth(1.0f); //Outer Line - glColor4f(1.0f, 0.0f, 0.0f, _alpha); - geometryCache->renderVertices(GL_LINE_STRIP, _magnifierBorder); + geometryCache->renderVertices(gpu::LINE_STRIP, _magnifierBorder); glEnable(GL_TEXTURE_2D); } - glColor4f(1.0f, 1.0f, 1.0f, _alpha); + glm::vec4 magnifierColor = { 1.0f, 1.0f, 1.0f, _alpha }; DependencyManager::get()->renderQuad(bottomLeft, bottomRight, topRight, topLeft, glm::vec2(magnifyULeft, magnifyVBottom), glm::vec2(magnifyURight, magnifyVBottom), glm::vec2(magnifyURight, magnifyVTop), glm::vec2(magnifyULeft, magnifyVTop), - _magnifierQuad); + magnifierColor, _magnifierQuad); } glPopMatrix(); } @@ -810,9 +811,9 @@ void ApplicationOverlay::renderAudioMeter() { audioMeterY = AUDIO_METER_GAP + MUTE_ICON_PADDING; } - const float AUDIO_METER_BLUE[] = { 0.0, 0.0, 1.0 }; - const float AUDIO_METER_GREEN[] = { 0.0, 1.0, 0.0 }; - const float AUDIO_METER_RED[] = { 1.0, 0.0, 0.0 }; + const glm::vec4 AUDIO_METER_BLUE = { 0.0, 0.0, 1.0, 1.0 }; + const glm::vec4 AUDIO_METER_GREEN = { 0.0, 1.0, 0.0, 1.0 }; + const glm::vec4 AUDIO_METER_RED = { 1.0, 0.0, 0.0, 1.0 }; const float AUDIO_GREEN_START = 0.25 * AUDIO_METER_SCALE_WIDTH; const float AUDIO_RED_START = 0.80 * AUDIO_METER_SCALE_WIDTH; const float CLIPPING_INDICATOR_TIME = 1.0f; @@ -847,59 +848,56 @@ void ApplicationOverlay::renderAudioMeter() { DependencyManager::get()->render(glCanvas->width(), glCanvas->height()); DependencyManager::get()->render(WHITE_TEXT, glCanvas->width(), glCanvas->height()); - if (isClipping) { - glColor3f(1, 0, 0); - } else { - glColor3f(0.475f, 0.475f, 0.475f); - } - audioMeterY += AUDIO_METER_HEIGHT; - glColor3f(0, 0, 0); // Draw audio meter background Quad - DependencyManager::get()->renderQuad(AUDIO_METER_X, audioMeterY, AUDIO_METER_WIDTH, AUDIO_METER_HEIGHT); + DependencyManager::get()->renderQuad(AUDIO_METER_X, audioMeterY, AUDIO_METER_WIDTH, AUDIO_METER_HEIGHT, + glm::vec4(0, 0, 0, 1)); if (audioLevel > AUDIO_RED_START) { + glm::vec4 quadColor; if (!isClipping) { - glColor3fv(AUDIO_METER_RED); + quadColor = AUDIO_METER_RED; } else { - glColor3f(1, 1, 1); + quadColor = glm::vec4(1, 1, 1, 1); } // Draw Red Quad DependencyManager::get()->renderQuad(AUDIO_METER_X + AUDIO_METER_INSET + AUDIO_RED_START, audioMeterY + AUDIO_METER_INSET, audioLevel - AUDIO_RED_START, - AUDIO_METER_HEIGHT - AUDIO_METER_INSET, + AUDIO_METER_HEIGHT - AUDIO_METER_INSET, quadColor, _audioRedQuad); audioLevel = AUDIO_RED_START; } if (audioLevel > AUDIO_GREEN_START) { + glm::vec4 quadColor; if (!isClipping) { - glColor3fv(AUDIO_METER_GREEN); + quadColor = AUDIO_METER_GREEN; } else { - glColor3f(1, 1, 1); + quadColor = glm::vec4(1, 1, 1, 1); } // Draw Green Quad DependencyManager::get()->renderQuad(AUDIO_METER_X + AUDIO_METER_INSET + AUDIO_GREEN_START, audioMeterY + AUDIO_METER_INSET, audioLevel - AUDIO_GREEN_START, - AUDIO_METER_HEIGHT - AUDIO_METER_INSET, + AUDIO_METER_HEIGHT - AUDIO_METER_INSET, quadColor, _audioGreenQuad); audioLevel = AUDIO_GREEN_START; } // Draw Blue Quad + glm::vec4 quadColor; if (!isClipping) { - glColor3fv(AUDIO_METER_BLUE); + quadColor = AUDIO_METER_BLUE; } else { - glColor3f(1, 1, 1); + quadColor = glm::vec4(1, 1, 1, 1); } // Draw Blue (low level) quad DependencyManager::get()->renderQuad(AUDIO_METER_X + AUDIO_METER_INSET, audioMeterY + AUDIO_METER_INSET, - audioLevel, AUDIO_METER_HEIGHT - AUDIO_METER_INSET, + audioLevel, AUDIO_METER_HEIGHT - AUDIO_METER_INSET, quadColor, _audioBlueQuad); } @@ -955,23 +953,24 @@ void ApplicationOverlay::renderDomainConnectionStatusBorder() { int height = glCanvas->height(); if (width != _previousBorderWidth || height != _previousBorderHeight) { + glm::vec4 color(CONNECTION_STATUS_BORDER_COLOR[0], + CONNECTION_STATUS_BORDER_COLOR[1], + CONNECTION_STATUS_BORDER_COLOR[2], 1.0f); + QVector border; border << glm::vec2(0, 0); border << glm::vec2(0, height); border << glm::vec2(width, height); border << glm::vec2(width, 0); border << glm::vec2(0, 0); - geometryCache->updateVertices(_domainStatusBorder, border); + geometryCache->updateVertices(_domainStatusBorder, border, color); _previousBorderWidth = width; _previousBorderHeight = height; } - glColor3f(CONNECTION_STATUS_BORDER_COLOR[0], - CONNECTION_STATUS_BORDER_COLOR[1], - CONNECTION_STATUS_BORDER_COLOR[2]); glLineWidth(CONNECTION_STATUS_BORDER_LINE_WIDTH); - geometryCache->renderVertices(GL_LINE_STRIP, _domainStatusBorder); + geometryCache->renderVertices(gpu::LINE_STRIP, _domainStatusBorder); } } diff --git a/interface/src/ui/BandwidthMeter.cpp b/interface/src/ui/BandwidthMeter.cpp index 69cd20d9a2..f1bcb2f0c9 100644 --- a/interface/src/ui/BandwidthMeter.cpp +++ b/interface/src/ui/BandwidthMeter.cpp @@ -86,20 +86,21 @@ void BandwidthMeter::Stream::updateValue(double amount) { glm::clamp(dt / _msToAverage, 0.0, 1.0)); } -void BandwidthMeter::setColorRGBA(unsigned c) { +glm::vec4 BandwidthMeter::getColorRGBA(unsigned c) { - glColor4ub(GLubyte( c >> 24), - GLubyte((c >> 16) & 0xff), - GLubyte((c >> 8) & 0xff), - GLubyte( c & 0xff)); + float r = (c >> 24) / 255.0f; + float g = ((c >> 16) & 0xff) / 255.0f; + float b = ((c >> 8) & 0xff) / 255.0f; + float a = (c & 0xff) / 255.0f; + return glm::vec4(r,g,b,a); } -void BandwidthMeter::renderBox(int x, int y, int w, int h) { - DependencyManager::get()->renderQuad(x, y, w, h); +void BandwidthMeter::renderBox(int x, int y, int w, int h, unsigned c) { + DependencyManager::get()->renderQuad(x, y, w, h, getColorRGBA(c)); } -void BandwidthMeter::renderVerticalLine(int x, int y, int h) { - DependencyManager::get()->renderLine(glm::vec2(x, y), glm::vec2(x, y + h)); +void BandwidthMeter::renderVerticalLine(int x, int y, int h, unsigned c) { + DependencyManager::get()->renderLine(glm::vec2(x, y), glm::vec2(x, y + h), getColorRGBA(c)); } inline int BandwidthMeter::centered(int subject, int object) { @@ -155,15 +156,16 @@ void BandwidthMeter::render(int screenWidth, int screenHeight) { glTranslatef((float)barX, (float)y, 0.0f); // Render captions - setColorRGBA(COLOR_TEXT); - _textRenderer->draw(barWidth + SPACING_LEFT_CAPTION_UNIT, textYcenteredLine, CAPTION_UNIT); - _textRenderer->draw(-labelWidthIn - SPACING_RIGHT_CAPTION_IN_OUT, textYupperLine, CAPTION_IN); - _textRenderer->draw(-labelWidthOut - SPACING_RIGHT_CAPTION_IN_OUT, textYlowerLine, CAPTION_OUT); + glm::vec4 textColor = getColorRGBA(COLOR_TEXT); + _textRenderer->draw(barWidth + SPACING_LEFT_CAPTION_UNIT, textYcenteredLine, CAPTION_UNIT, textColor); + _textRenderer->draw(-labelWidthIn - SPACING_RIGHT_CAPTION_IN_OUT, textYupperLine, CAPTION_IN, textColor); + _textRenderer->draw(-labelWidthOut - SPACING_RIGHT_CAPTION_IN_OUT, textYlowerLine, CAPTION_OUT, textColor); // Render vertical lines for the frame - setColorRGBA(COLOR_FRAME); - renderVerticalLine(0, 0, h); - renderVerticalLine(barWidth, 0, h); + // TODO: I think there may be a bug in this newest code and/or the GeometryCache code, because it seems like + // sometimes the bandwidth meter doesn't render the vertical lines + renderVerticalLine(0, 0, h, COLOR_FRAME); + renderVerticalLine(barWidth, 0, h, COLOR_FRAME); // Adjust scale int steps; @@ -192,9 +194,8 @@ void BandwidthMeter::render(int screenWidth, int screenHeight) { } // Render scale indicators - setColorRGBA(COLOR_INDICATOR); for (int j = NUMBER_OF_MARKERS; --j > 0;) { - renderVerticalLine((barWidth * j) / NUMBER_OF_MARKERS, 0, h); + renderVerticalLine((barWidth * j) / NUMBER_OF_MARKERS, 0, h, COLOR_INDICATOR); } // Render bars @@ -205,30 +206,27 @@ void BandwidthMeter::render(int screenWidth, int screenHeight) { int wIn = (int)(barWidth * inputStream(chIdx).getValue() * UNIT_SCALE / scaleMax); int wOut = (int)(barWidth * outputStream(chIdx).getValue() * UNIT_SCALE / scaleMax); - setColorRGBA(channelInfo(chIdx).colorRGBA); - if (wIn > 0) { - renderBox(xIn, 0, wIn, barHeight); + renderBox(xIn, 0, wIn, barHeight, channelInfo(chIdx).colorRGBA); } xIn += wIn; if (wOut > 0) { - renderBox(xOut, h - barHeight, wOut, barHeight); + renderBox(xOut, h - barHeight, wOut, barHeight, channelInfo(chIdx).colorRGBA); } xOut += wOut; } // Render numbers char fmtBuf[8]; - setColorRGBA(COLOR_TEXT); sprintf(fmtBuf, "%0.1f", totalIn); _textRenderer->draw(glm::max(xIn - fontMetrics.width(fmtBuf) - PADDING_HORIZ_VALUE, PADDING_HORIZ_VALUE), - textYupperLine, fmtBuf); + textYupperLine, fmtBuf, textColor); sprintf(fmtBuf, "%0.1f", totalOut); _textRenderer->draw(glm::max(xOut - fontMetrics.width(fmtBuf) - PADDING_HORIZ_VALUE, PADDING_HORIZ_VALUE), - textYlowerLine, fmtBuf); + textYlowerLine, fmtBuf, textColor); glPopMatrix(); diff --git a/interface/src/ui/BandwidthMeter.h b/interface/src/ui/BandwidthMeter.h index 0829cc86e9..bca4745cee 100644 --- a/interface/src/ui/BandwidthMeter.h +++ b/interface/src/ui/BandwidthMeter.h @@ -69,9 +69,9 @@ public: ChannelInfo const& channelInfo(ChannelIndex i) const { return _channels[i]; } private: - static void setColorRGBA(unsigned c); - static void renderBox(int x, int y, int w, int h); - static void renderVerticalLine(int x, int y, int h); + static glm::vec4 getColorRGBA(unsigned c); + static void renderBox(int x, int y, int w, int h, unsigned c); + static void renderVerticalLine(int x, int y, int h, unsigned c); static inline int centered(int subject, int object); diff --git a/interface/src/ui/MetavoxelEditor.cpp b/interface/src/ui/MetavoxelEditor.cpp index 41a7c90fc9..e5b69d3ca5 100644 --- a/interface/src/ui/MetavoxelEditor.cpp +++ b/interface/src/ui/MetavoxelEditor.cpp @@ -356,11 +356,9 @@ void MetavoxelEditor::render() { float scale = GRID_DIVISIONS * spacing; glScalef(scale, scale, scale); - glColor3f(GRID_BRIGHTNESS, GRID_BRIGHTNESS, GRID_BRIGHTNESS); - _gridProgram.bind(); - DependencyManager::get()->renderGrid(GRID_DIVISIONS, GRID_DIVISIONS); + DependencyManager::get()->renderGrid(GRID_DIVISIONS, GRID_DIVISIONS, glm::vec4(GRID_BRIGHTNESS, GRID_BRIGHTNESS, GRID_BRIGHTNESS, 1.0f)); _gridProgram.release(); @@ -492,17 +490,17 @@ void BoxTool::render() { if (_state != HOVERING_STATE) { const float BOX_ALPHA = 0.25f; QColor color = getColor(); + glm::vec4 cubeColor; if (color.isValid()) { - glColor4f(color.redF(), color.greenF(), color.blueF(), BOX_ALPHA); + cubeColor = glm::vec4(color.redF(), color.greenF(), color.blueF(), BOX_ALPHA); } else { - glColor4f(GRID_BRIGHTNESS, GRID_BRIGHTNESS, GRID_BRIGHTNESS, BOX_ALPHA); + cubeColor = glm::vec4(GRID_BRIGHTNESS, GRID_BRIGHTNESS, GRID_BRIGHTNESS, BOX_ALPHA); } glEnable(GL_CULL_FACE); - DependencyManager::get()->renderSolidCube(1.0f); + DependencyManager::get()->renderSolidCube(1.0f, cubeColor); glDisable(GL_CULL_FACE); } - glColor3f(GRID_BRIGHTNESS, GRID_BRIGHTNESS, GRID_BRIGHTNESS); - DependencyManager::get()->renderWireCube(1.0f); + DependencyManager::get()->renderWireCube(1.0f, glm::vec4(GRID_BRIGHTNESS, GRID_BRIGHTNESS, GRID_BRIGHTNESS, 1.0f)); glPopMatrix(); } diff --git a/interface/src/ui/NodeBounds.cpp b/interface/src/ui/NodeBounds.cpp index 8497d73503..85cbf72953 100644 --- a/interface/src/ui/NodeBounds.cpp +++ b/interface/src/ui/NodeBounds.cpp @@ -127,8 +127,7 @@ void NodeBounds::draw() { float red, green, blue; getColorForNodeType(selectedNode->getType(), red, green, blue); - glColor4f(red, green, blue, 0.2f); - DependencyManager::get()->renderSolidCube(1.0f); + DependencyManager::get()->renderSolidCube(1.0f, glm::vec4(red, green, blue, 0.2f)); glPopMatrix(); @@ -152,8 +151,7 @@ void NodeBounds::drawNodeBorder(const glm::vec3& center, float scale, float red, glTranslatef(center.x, center.y, center.z); glScalef(scale, scale, scale); glLineWidth(2.5); - glColor3f(red, green, blue); - DependencyManager::get()->renderWireCube(1.0f); + DependencyManager::get()->renderWireCube(1.0f, glm::vec4(red, green, blue, 1.0f)); glPopMatrix(); } @@ -179,9 +177,10 @@ void NodeBounds::drawOverlay() { int mouseX = application->getTrueMouseX(), mouseY = application->getTrueMouseY(), textWidth = widthText(TEXT_SCALE, 0, _overlayText); - glColor4f(0.4f, 0.4f, 0.4f, 0.6f); - renderBevelCornersRect(mouseX + MOUSE_OFFSET, mouseY - TEXT_HEIGHT - PADDING, - textWidth + (2 * PADDING), TEXT_HEIGHT + (2 * PADDING), BACKGROUND_BEVEL); + DependencyManager::get()->renderBevelCornersRect( + mouseX + MOUSE_OFFSET, mouseY - TEXT_HEIGHT - PADDING, + textWidth + (2 * PADDING), TEXT_HEIGHT + (2 * PADDING), BACKGROUND_BEVEL, + glm::vec4(0.4f, 0.4f, 0.4f, 0.6f)); drawText(mouseX + MOUSE_OFFSET + PADDING, mouseY, TEXT_SCALE, ROTATION, FONT, _overlayText, TEXT_COLOR); } } diff --git a/interface/src/ui/RearMirrorTools.cpp b/interface/src/ui/RearMirrorTools.cpp index da1c82494f..ebd70883d3 100644 --- a/interface/src/ui/RearMirrorTools.cpp +++ b/interface/src/ui/RearMirrorTools.cpp @@ -105,10 +105,11 @@ void RearMirrorTools::displayIcon(QRect bounds, QRect iconBounds, GLuint texture glDisable(GL_LIGHTING); glEnable(GL_TEXTURE_2D); + glm::vec4 quadColor; if (selected) { - glColor3f(.5f, .5f, .5f); + quadColor = glm::vec4(.5f, .5f, .5f, 1.0f); } else { - glColor3f(1, 1, 1); + quadColor = glm::vec4(1, 1, 1, 1); } glBindTexture(GL_TEXTURE_2D, textureId); @@ -118,7 +119,7 @@ void RearMirrorTools::displayIcon(QRect bounds, QRect iconBounds, GLuint texture glm::vec2 texCoordTopLeft(0.0f, 1.0f); glm::vec2 texCoordBottomRight(1.0f, 0.0f); - DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, quadColor); glPopMatrix(); diff --git a/interface/src/ui/Stats.cpp b/interface/src/ui/Stats.cpp index 424bd4d50c..e8afddfbfd 100644 --- a/interface/src/ui/Stats.cpp +++ b/interface/src/ui/Stats.cpp @@ -158,14 +158,12 @@ void Stats::resetWidth(int width, int horizontalOffset) { // translucent background box that makes stats more readable void Stats::drawBackground(unsigned int rgba, int x, int y, int width, int height) { - glColor4f(((rgba >> 24) & 0xff) / 255.0f, - ((rgba >> 16) & 0xff) / 255.0f, - ((rgba >> 8) & 0xff) / 255.0f, - (rgba & 0xff) / 255.0f); + glm::vec4 color(((rgba >> 24) & 0xff) / 255.0f, + ((rgba >> 16) & 0xff) / 255.0f, + ((rgba >> 8) & 0xff) / 255.0f, + (rgba & 0xff) / 255.0f); - DependencyManager::get()->renderQuad(x, y, width, height); - - glColor4f(1, 1, 1, 1); + DependencyManager::get()->renderQuad(x, y, width, height, color); } bool Stats::includeTimingRecord(const QString& name) { diff --git a/interface/src/ui/overlays/BillboardOverlay.cpp b/interface/src/ui/overlays/BillboardOverlay.cpp index b34416f566..03decf046c 100644 --- a/interface/src/ui/overlays/BillboardOverlay.cpp +++ b/interface/src/ui/overlays/BillboardOverlay.cpp @@ -95,7 +95,6 @@ void BillboardOverlay::render(RenderArgs* args) { const float MAX_COLOR = 255.0f; xColor color = getColor(); float alpha = getAlpha(); - glColor4f(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); glm::vec2 topLeft(-x, -y); glm::vec2 bottomRight(x, y); @@ -104,7 +103,8 @@ void BillboardOverlay::render(RenderArgs* args) { glm::vec2 texCoordBottomRight(((float)_fromImage.x() + (float)_fromImage.width()) / (float)_size.width(), ((float)_fromImage.y() + (float)_fromImage.height()) / _size.height()); - DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, + glm::vec4(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha)); } } glPopMatrix(); diff --git a/interface/src/ui/overlays/Circle3DOverlay.cpp b/interface/src/ui/overlays/Circle3DOverlay.cpp index 5de36d694b..f890999174 100644 --- a/interface/src/ui/overlays/Circle3DOverlay.cpp +++ b/interface/src/ui/overlays/Circle3DOverlay.cpp @@ -95,10 +95,9 @@ void Circle3DOverlay::render(RenderArgs* args) { const float SLICE_ANGLE = FULL_CIRCLE / SLICES; //const int slices = 15; - xColor color = getColor(); + xColor colorX = getColor(); const float MAX_COLOR = 255.0f; - glColor4f(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); - + glm::vec4 color(colorX.red / MAX_COLOR, colorX.green / MAX_COLOR, colorX.blue / MAX_COLOR, alpha); glDisable(GL_LIGHTING); @@ -162,10 +161,10 @@ void Circle3DOverlay::render(RenderArgs* args) { points << lastOuterPoint << lastInnerPoint; - geometryCache->updateVertices(_quadVerticesID, points); + geometryCache->updateVertices(_quadVerticesID, points, color); } - geometryCache->renderVertices(GL_QUAD_STRIP, _quadVerticesID); + geometryCache->renderVertices(gpu::QUAD_STRIP, _quadVerticesID); } else { if (_lineVerticesID == GeometryCache::UNKNOWN_ID) { @@ -200,13 +199,13 @@ void Circle3DOverlay::render(RenderArgs* args) { glm::vec2 lastPoint(cos(angleInRadians) * outerRadius, sin(angleInRadians) * outerRadius); points << lastPoint; - geometryCache->updateVertices(_lineVerticesID, points); + geometryCache->updateVertices(_lineVerticesID, points, color); } if (getIsDashedLine()) { - geometryCache->renderVertices(GL_LINES, _lineVerticesID); + geometryCache->renderVertices(gpu::LINES, _lineVerticesID); } else { - geometryCache->renderVertices(GL_LINE_STRIP, _lineVerticesID); + geometryCache->renderVertices(gpu::LINE_STRIP, _lineVerticesID); } } @@ -270,17 +269,20 @@ void Circle3DOverlay::render(RenderArgs* args) { } } - geometryCache->updateVertices(_majorTicksVerticesID, majorPoints); - geometryCache->updateVertices(_minorTicksVerticesID, minorPoints); + xColor majorColorX = getMajorTickMarksColor(); + glm::vec4 majorColor(majorColorX.red / MAX_COLOR, majorColorX.green / MAX_COLOR, majorColorX.blue / MAX_COLOR, alpha); + + geometryCache->updateVertices(_majorTicksVerticesID, majorPoints, majorColor); + + xColor minorColorX = getMinorTickMarksColor(); + glm::vec4 minorColor(minorColorX.red / MAX_COLOR, minorColorX.green / MAX_COLOR, minorColorX.blue / MAX_COLOR, alpha); + + geometryCache->updateVertices(_minorTicksVerticesID, minorPoints, minorColor); } - xColor majorColor = getMajorTickMarksColor(); - glColor4f(majorColor.red / MAX_COLOR, majorColor.green / MAX_COLOR, majorColor.blue / MAX_COLOR, alpha); - geometryCache->renderVertices(GL_LINES, _majorTicksVerticesID); + geometryCache->renderVertices(gpu::LINES, _majorTicksVerticesID); - xColor minorColor = getMinorTickMarksColor(); - glColor4f(minorColor.red / MAX_COLOR, minorColor.green / MAX_COLOR, minorColor.blue / MAX_COLOR, alpha); - geometryCache->renderVertices(GL_LINES, _minorTicksVerticesID); + geometryCache->renderVertices(gpu::LINES, _minorTicksVerticesID); } diff --git a/interface/src/ui/overlays/Cube3DOverlay.cpp b/interface/src/ui/overlays/Cube3DOverlay.cpp index aa8be896dd..53d7d4e70b 100644 --- a/interface/src/ui/overlays/Cube3DOverlay.cpp +++ b/interface/src/ui/overlays/Cube3DOverlay.cpp @@ -47,7 +47,7 @@ void Cube3DOverlay::render(RenderArgs* args) { float alpha = getAlpha(); xColor color = getColor(); const float MAX_COLOR = 255.0f; - glColor4f(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); + glm::vec4 cubeColor(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); //glDisable(GL_LIGHTING); @@ -74,13 +74,12 @@ void Cube3DOverlay::render(RenderArgs* args) { // enough for the use-case. glDepthMask(GL_FALSE); glPushMatrix(); - glColor4f(1.0f, 1.0f, 1.0f, alpha); glScalef(dimensions.x * _borderSize, dimensions.y * _borderSize, dimensions.z * _borderSize); if (_drawOnHUD) { - DependencyManager::get()->renderSolidCube(1.0f); + DependencyManager::get()->renderSolidCube(1.0f, glm::vec4(1.0f, 1.0f, 1.0f, alpha)); } else { - DependencyManager::get()->renderSolidCube(1.0f); + DependencyManager::get()->renderSolidCube(1.0f, glm::vec4(1.0f, 1.0f, 1.0f, alpha)); } glPopMatrix(); @@ -88,12 +87,11 @@ void Cube3DOverlay::render(RenderArgs* args) { } glPushMatrix(); - glColor4f(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); glScalef(dimensions.x, dimensions.y, dimensions.z); if (_drawOnHUD) { - DependencyManager::get()->renderSolidCube(1.0f); + DependencyManager::get()->renderSolidCube(1.0f, cubeColor); } else { - DependencyManager::get()->renderSolidCube(1.0f); + DependencyManager::get()->renderSolidCube(1.0f, cubeColor); } glPopMatrix(); } else { @@ -113,24 +111,24 @@ void Cube3DOverlay::render(RenderArgs* args) { auto geometryCache = DependencyManager::get(); - geometryCache->renderDashedLine(bottomLeftNear, bottomRightNear); - geometryCache->renderDashedLine(bottomRightNear, bottomRightFar); - geometryCache->renderDashedLine(bottomRightFar, bottomLeftFar); - geometryCache->renderDashedLine(bottomLeftFar, bottomLeftNear); + geometryCache->renderDashedLine(bottomLeftNear, bottomRightNear, cubeColor); + geometryCache->renderDashedLine(bottomRightNear, bottomRightFar, cubeColor); + geometryCache->renderDashedLine(bottomRightFar, bottomLeftFar, cubeColor); + geometryCache->renderDashedLine(bottomLeftFar, bottomLeftNear, cubeColor); - geometryCache->renderDashedLine(topLeftNear, topRightNear); - geometryCache->renderDashedLine(topRightNear, topRightFar); - geometryCache->renderDashedLine(topRightFar, topLeftFar); - geometryCache->renderDashedLine(topLeftFar, topLeftNear); + geometryCache->renderDashedLine(topLeftNear, topRightNear, cubeColor); + geometryCache->renderDashedLine(topRightNear, topRightFar, cubeColor); + geometryCache->renderDashedLine(topRightFar, topLeftFar, cubeColor); + geometryCache->renderDashedLine(topLeftFar, topLeftNear, cubeColor); - geometryCache->renderDashedLine(bottomLeftNear, topLeftNear); - geometryCache->renderDashedLine(bottomRightNear, topRightNear); - geometryCache->renderDashedLine(bottomLeftFar, topLeftFar); - geometryCache->renderDashedLine(bottomRightFar, topRightFar); + geometryCache->renderDashedLine(bottomLeftNear, topLeftNear, cubeColor); + geometryCache->renderDashedLine(bottomRightNear, topRightNear, cubeColor); + geometryCache->renderDashedLine(bottomLeftFar, topLeftFar, cubeColor); + geometryCache->renderDashedLine(bottomRightFar, topRightFar, cubeColor); } else { glScalef(dimensions.x, dimensions.y, dimensions.z); - DependencyManager::get()->renderWireCube(1.0f); + DependencyManager::get()->renderWireCube(1.0f, cubeColor); } } glPopMatrix(); diff --git a/interface/src/ui/overlays/Grid3DOverlay.cpp b/interface/src/ui/overlays/Grid3DOverlay.cpp index fbcbf97077..b39b2c3274 100644 --- a/interface/src/ui/overlays/Grid3DOverlay.cpp +++ b/interface/src/ui/overlays/Grid3DOverlay.cpp @@ -79,7 +79,7 @@ void Grid3DOverlay::render(RenderArgs* args) { const float MAX_COLOR = 255.0f; - glColor4f(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); + glm::vec4 gridColor(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); _gridProgram.bind(); @@ -92,7 +92,7 @@ void Grid3DOverlay::render(RenderArgs* args) { float scale = MINOR_GRID_DIVISIONS * spacing; glScalef(scale, scale, scale); - DependencyManager::get()->renderGrid(MINOR_GRID_DIVISIONS, MINOR_GRID_DIVISIONS); + DependencyManager::get()->renderGrid(MINOR_GRID_DIVISIONS, MINOR_GRID_DIVISIONS, gridColor); } glPopMatrix(); @@ -107,7 +107,7 @@ void Grid3DOverlay::render(RenderArgs* args) { float scale = MAJOR_GRID_DIVISIONS * spacing; glScalef(scale, scale, scale); - DependencyManager::get()->renderGrid(MAJOR_GRID_DIVISIONS, MAJOR_GRID_DIVISIONS); + DependencyManager::get()->renderGrid(MAJOR_GRID_DIVISIONS, MAJOR_GRID_DIVISIONS, gridColor); } glPopMatrix(); diff --git a/interface/src/ui/overlays/ImageOverlay.cpp b/interface/src/ui/overlays/ImageOverlay.cpp index e354ea539e..3cc1b54b62 100644 --- a/interface/src/ui/overlays/ImageOverlay.cpp +++ b/interface/src/ui/overlays/ImageOverlay.cpp @@ -30,9 +30,9 @@ ImageOverlay::ImageOverlay() : ImageOverlay::ImageOverlay(const ImageOverlay* imageOverlay) : Overlay2D(imageOverlay), - _texture(imageOverlay->_texture), _imageURL(imageOverlay->_imageURL), _textureImage(imageOverlay->_textureImage), + _texture(imageOverlay->_texture), _fromImage(imageOverlay->_fromImage), _renderImage(imageOverlay->_renderImage), _wantClipFromImage(imageOverlay->_wantClipFromImage) @@ -75,7 +75,7 @@ void ImageOverlay::render(RenderArgs* args) { const float MAX_COLOR = 255.0f; xColor color = getColor(); float alpha = getAlpha(); - glColor4f(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); + glm::vec4 quadColor(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); int left = _bounds.left(); int right = _bounds.right() + 1; @@ -85,9 +85,11 @@ void ImageOverlay::render(RenderArgs* args) { glm::vec2 topLeft(left, top); glm::vec2 bottomRight(right, bottom); - if (_renderImage) { - float imageWidth = _texture->getWidth(); - float imageHeight = _texture->getHeight(); + float imageWidth = _texture->getWidth(); + float imageHeight = _texture->getHeight(); + + // if for some reason our image is not over 0 width or height, don't attempt to render the image + if (_renderImage && imageWidth > 0 && imageHeight > 0) { QRect fromImage; if (_wantClipFromImage) { @@ -113,9 +115,9 @@ void ImageOverlay::render(RenderArgs* args) { glm::vec2 texCoordTopLeft(x, y); glm::vec2 texCoordBottomRight(x + w, y + h); - DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, quadColor); } else { - DependencyManager::get()->renderQuad(topLeft, bottomRight); + DependencyManager::get()->renderQuad(topLeft, bottomRight, quadColor); } if (_renderImage) { diff --git a/interface/src/ui/overlays/Line3DOverlay.cpp b/interface/src/ui/overlays/Line3DOverlay.cpp index 48a995b80b..d2bfa1955b 100644 --- a/interface/src/ui/overlays/Line3DOverlay.cpp +++ b/interface/src/ui/overlays/Line3DOverlay.cpp @@ -49,7 +49,7 @@ void Line3DOverlay::render(RenderArgs* args) { float alpha = getAlpha(); xColor color = getColor(); const float MAX_COLOR = 255.0f; - 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::quat rotation = getRotation(); @@ -59,9 +59,10 @@ void Line3DOverlay::render(RenderArgs* args) { glRotatef(glm::degrees(glm::angle(rotation)), axis.x, axis.y, axis.z); if (getIsDashedLine()) { - DependencyManager::get()->renderDashedLine(_position, _end, _geometryCacheID); + // TODO: add support for color to renderDashedLine() + DependencyManager::get()->renderDashedLine(_position, _end, colorv4, _geometryCacheID); } else { - DependencyManager::get()->renderLine(_start, _end, _geometryCacheID); + DependencyManager::get()->renderLine(_start, _end, colorv4, _geometryCacheID); } glEnable(GL_LIGHTING); diff --git a/interface/src/ui/overlays/Rectangle3DOverlay.cpp b/interface/src/ui/overlays/Rectangle3DOverlay.cpp index a59566ebb1..82a5383036 100644 --- a/interface/src/ui/overlays/Rectangle3DOverlay.cpp +++ b/interface/src/ui/overlays/Rectangle3DOverlay.cpp @@ -39,7 +39,7 @@ void Rectangle3DOverlay::render(RenderArgs* args) { float alpha = getAlpha(); xColor color = getColor(); const float MAX_COLOR = 255.0f; - glColor4f(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); + glm::vec4 rectangleColor(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); glDisable(GL_LIGHTING); @@ -72,7 +72,7 @@ void Rectangle3DOverlay::render(RenderArgs* args) { if (getIsSolid()) { glm::vec3 topLeft(-halfDimensions.x, 0.0f, -halfDimensions.y); glm::vec3 bottomRight(halfDimensions.x, 0.0f, halfDimensions.y); - DependencyManager::get()->renderQuad(topLeft, bottomRight); + DependencyManager::get()->renderQuad(topLeft, bottomRight, rectangleColor); } else { if (getIsDashedLine()) { @@ -81,10 +81,10 @@ void Rectangle3DOverlay::render(RenderArgs* args) { glm::vec3 point3(halfDimensions.x, 0.0f, halfDimensions.y); glm::vec3 point4(-halfDimensions.x, 0.0f, halfDimensions.y); - geometryCache->renderDashedLine(point1, point2); - geometryCache->renderDashedLine(point2, point3); - geometryCache->renderDashedLine(point3, point4); - geometryCache->renderDashedLine(point4, point1); + geometryCache->renderDashedLine(point1, point2, rectangleColor); + geometryCache->renderDashedLine(point2, point3, rectangleColor); + geometryCache->renderDashedLine(point3, point4, rectangleColor); + geometryCache->renderDashedLine(point4, point1, rectangleColor); } else { @@ -95,12 +95,12 @@ void Rectangle3DOverlay::render(RenderArgs* args) { border << glm::vec3(halfDimensions.x, 0.0f, halfDimensions.y); border << glm::vec3(-halfDimensions.x, 0.0f, halfDimensions.y); border << glm::vec3(-halfDimensions.x, 0.0f, -halfDimensions.y); - geometryCache->updateVertices(_geometryCacheID, border); + geometryCache->updateVertices(_geometryCacheID, border, rectangleColor); _previousHalfDimensions = halfDimensions; } - geometryCache->renderVertices(GL_LINE_STRIP, _geometryCacheID); + geometryCache->renderVertices(gpu::LINE_STRIP, _geometryCacheID); } } diff --git a/interface/src/ui/overlays/Sphere3DOverlay.cpp b/interface/src/ui/overlays/Sphere3DOverlay.cpp index 3e30ec033d..11bd7b97e8 100644 --- a/interface/src/ui/overlays/Sphere3DOverlay.cpp +++ b/interface/src/ui/overlays/Sphere3DOverlay.cpp @@ -39,7 +39,7 @@ void Sphere3DOverlay::render(RenderArgs* args) { float alpha = getAlpha(); xColor color = getColor(); const float MAX_COLOR = 255.0f; - glColor4f(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); + glm::vec4 sphereColor(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); glDisable(GL_LIGHTING); @@ -62,7 +62,7 @@ void Sphere3DOverlay::render(RenderArgs* args) { glm::vec3 positionToCenter = center - position; glTranslatef(positionToCenter.x, positionToCenter.y, positionToCenter.z); glScalef(dimensions.x, dimensions.y, dimensions.z); - DependencyManager::get()->renderSphere(1.0f, SLICES, SLICES, _isSolid); + DependencyManager::get()->renderSphere(1.0f, SLICES, SLICES, sphereColor, _isSolid); glPopMatrix(); glPopMatrix(); diff --git a/interface/src/ui/overlays/Text3DOverlay.cpp b/interface/src/ui/overlays/Text3DOverlay.cpp index 2f4cde50f7..00fd655f69 100644 --- a/interface/src/ui/overlays/Text3DOverlay.cpp +++ b/interface/src/ui/overlays/Text3DOverlay.cpp @@ -92,7 +92,7 @@ void Text3DOverlay::render(RenderArgs* args) { const float MAX_COLOR = 255.0f; xColor backgroundColor = getBackgroundColor(); - glColor4f(backgroundColor.red / MAX_COLOR, backgroundColor.green / MAX_COLOR, backgroundColor.blue / MAX_COLOR, + glm::vec4 quadColor(backgroundColor.red / MAX_COLOR, backgroundColor.green / MAX_COLOR, backgroundColor.blue / MAX_COLOR, getBackgroundAlpha()); glm::vec2 dimensions = getDimensions(); @@ -102,7 +102,7 @@ void Text3DOverlay::render(RenderArgs* args) { glm::vec3 topLeft(-halfDimensions.x, -halfDimensions.y, SLIGHTLY_BEHIND); glm::vec3 bottomRight(halfDimensions.x, halfDimensions.y, SLIGHTLY_BEHIND); - DependencyManager::get()->renderQuad(topLeft, bottomRight); + DependencyManager::get()->renderQuad(topLeft, bottomRight, quadColor); const int FIXED_FONT_SCALING_RATIO = FIXED_FONT_POINT_SIZE * 40.0f; // this is a ratio determined through experimentation @@ -124,12 +124,11 @@ void Text3DOverlay::render(RenderArgs* args) { enableClipPlane(GL_CLIP_PLANE2, 0.0f, -1.0f, 0.0f, clipMinimum.y + clipDimensions.y); enableClipPlane(GL_CLIP_PLANE3, 0.0f, 1.0f, 0.0f, -clipMinimum.y); - glColor3f(_color.red / MAX_COLOR, _color.green / MAX_COLOR, _color.blue / MAX_COLOR); - float alpha = getAlpha(); + glm::vec4 textColor = {_color.red / MAX_COLOR, _color.green / MAX_COLOR, _color.blue / MAX_COLOR, getAlpha() }; QStringList lines = _text.split("\n"); int lineOffset = maxHeight; foreach(QString thisLine, lines) { - textRenderer->draw(0, lineOffset, qPrintable(thisLine), alpha); + textRenderer->draw(0, lineOffset, qPrintable(thisLine), textColor); lineOffset += maxHeight; } diff --git a/interface/src/ui/overlays/TextOverlay.cpp b/interface/src/ui/overlays/TextOverlay.cpp index ca83e1c595..0e8ca29320 100644 --- a/interface/src/ui/overlays/TextOverlay.cpp +++ b/interface/src/ui/overlays/TextOverlay.cpp @@ -70,7 +70,7 @@ void TextOverlay::render(RenderArgs* args) { const float MAX_COLOR = 255.0f; xColor backgroundColor = getBackgroundColor(); - glColor4f(backgroundColor.red / MAX_COLOR, backgroundColor.green / MAX_COLOR, backgroundColor.blue / MAX_COLOR, + glm::vec4 quadColor(backgroundColor.red / MAX_COLOR, backgroundColor.green / MAX_COLOR, backgroundColor.blue / MAX_COLOR, getBackgroundAlpha()); int left = _bounds.left(); @@ -80,7 +80,7 @@ void TextOverlay::render(RenderArgs* args) { glm::vec2 topLeft(left, top); glm::vec2 bottomRight(right, bottom); - DependencyManager::get()->renderQuad(topLeft, bottomRight); + DependencyManager::get()->renderQuad(topLeft, bottomRight, quadColor); // Same font properties as textSize() TextRenderer* textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, _fontSize, DEFAULT_FONT_WEIGHT); @@ -90,15 +90,15 @@ void TextOverlay::render(RenderArgs* args) { int x = _bounds.left() + _leftMargin + leftAdjust; int y = _bounds.top() + _topMargin + topAdjust; - glColor3f(_color.red / MAX_COLOR, _color.green / MAX_COLOR, _color.blue / MAX_COLOR); float alpha = getAlpha(); + glm::vec4 textColor = {_color.red / MAX_COLOR, _color.green / MAX_COLOR, _color.blue / MAX_COLOR, alpha }; QStringList lines = _text.split("\n"); int lineOffset = 0; foreach(QString thisLine, lines) { if (lineOffset == 0) { lineOffset = textRenderer->calculateHeight(qPrintable(thisLine)); } - lineOffset += textRenderer->draw(x, y + lineOffset, qPrintable(thisLine), alpha); + lineOffset += textRenderer->draw(x, y + lineOffset, qPrintable(thisLine), textColor); const int lineGap = 2; lineOffset += lineGap; diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.cpp b/libraries/entities-renderer/src/EntityTreeRenderer.cpp index 47e9237ddc..adb119cb9b 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.cpp +++ b/libraries/entities-renderer/src/EntityTreeRenderer.cpp @@ -394,62 +394,53 @@ const Model* EntityTreeRenderer::getModelForEntityItem(const EntityItem* entityI void EntityTreeRenderer::renderElementProxy(EntityTreeElement* entityTreeElement) { glm::vec3 elementCenter = entityTreeElement->getAACube().calcCenter() * (float) TREE_SCALE; float elementSize = entityTreeElement->getScale() * (float) TREE_SCALE; - glColor3f(1.0f, 0.0f, 0.0f); glPushMatrix(); glTranslatef(elementCenter.x, elementCenter.y, elementCenter.z); - DependencyManager::get()->renderWireCube(elementSize); + DependencyManager::get()->renderWireCube(elementSize, glm::vec4(1.0f, 0.0f, 0.0f, 1.0f)); glPopMatrix(); if (_displayElementChildProxies) { // draw the children float halfSize = elementSize / 2.0f; float quarterSize = elementSize / 4.0f; - glColor3f(1.0f, 1.0f, 0.0f); glPushMatrix(); glTranslatef(elementCenter.x - quarterSize, elementCenter.y - quarterSize, elementCenter.z - quarterSize); - DependencyManager::get()->renderWireCube(halfSize); + DependencyManager::get()->renderWireCube(halfSize, glm::vec4(1.0f, 1.0f, 0.0f, 1.0f)); glPopMatrix(); - glColor3f(1.0f, 0.0f, 1.0f); glPushMatrix(); glTranslatef(elementCenter.x + quarterSize, elementCenter.y - quarterSize, elementCenter.z - quarterSize); - DependencyManager::get()->renderWireCube(halfSize); + DependencyManager::get()->renderWireCube(halfSize, glm::vec4(1.0f, 0.0f, 1.0f, 1.0f)); glPopMatrix(); - glColor3f(0.0f, 1.0f, 0.0f); glPushMatrix(); glTranslatef(elementCenter.x - quarterSize, elementCenter.y + quarterSize, elementCenter.z - quarterSize); - DependencyManager::get()->renderWireCube(halfSize); + DependencyManager::get()->renderWireCube(halfSize, glm::vec4(0.0f, 1.0f, 0.0f, 1.0f)); glPopMatrix(); - glColor3f(0.0f, 0.0f, 1.0f); glPushMatrix(); glTranslatef(elementCenter.x - quarterSize, elementCenter.y - quarterSize, elementCenter.z + quarterSize); - DependencyManager::get()->renderWireCube(halfSize); + DependencyManager::get()->renderWireCube(halfSize, glm::vec4(0.0f, 0.0f, 1.0f, 1.0f)); glPopMatrix(); - glColor3f(1.0f, 1.0f, 1.0f); glPushMatrix(); glTranslatef(elementCenter.x + quarterSize, elementCenter.y + quarterSize, elementCenter.z + quarterSize); - DependencyManager::get()->renderWireCube(halfSize); + DependencyManager::get()->renderWireCube(halfSize, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); glPopMatrix(); - glColor3f(0.0f, 0.5f, 0.5f); glPushMatrix(); glTranslatef(elementCenter.x - quarterSize, elementCenter.y + quarterSize, elementCenter.z + quarterSize); - DependencyManager::get()->renderWireCube(halfSize); + DependencyManager::get()->renderWireCube(halfSize, glm::vec4(0.0f, 0.5f, 0.5f, 1.0f)); glPopMatrix(); - glColor3f(0.5f, 0.0f, 0.0f); glPushMatrix(); glTranslatef(elementCenter.x + quarterSize, elementCenter.y - quarterSize, elementCenter.z + quarterSize); - DependencyManager::get()->renderWireCube(halfSize); + DependencyManager::get()->renderWireCube(halfSize, glm::vec4(0.5f, 0.0f, 0.0f, 1.0f)); glPopMatrix(); - glColor3f(0.0f, 0.5f, 0.0f); glPushMatrix(); glTranslatef(elementCenter.x + quarterSize, elementCenter.y + quarterSize, elementCenter.z - quarterSize); - DependencyManager::get()->renderWireCube(halfSize); + DependencyManager::get()->renderWireCube(halfSize, glm::vec4(0.0f, 0.5f, 0.0f, 1.0f)); glPopMatrix(); } } @@ -473,25 +464,22 @@ void EntityTreeRenderer::renderProxies(const EntityItem* entity, RenderArgs* arg glm::vec3 entityBoxScale = entityBox.getScale(); // draw the max bounding cube - glColor4f(1.0f, 1.0f, 0.0f, 1.0f); glPushMatrix(); glTranslatef(maxCenter.x, maxCenter.y, maxCenter.z); - DependencyManager::get()->renderWireCube(maxCube.getScale()); + DependencyManager::get()->renderWireCube(maxCube.getScale(), glm::vec4(1.0f, 1.0f, 0.0f, 1.0f)); glPopMatrix(); // draw the min bounding cube - glColor4f(0.0f, 1.0f, 0.0f, 1.0f); glPushMatrix(); glTranslatef(minCenter.x, minCenter.y, minCenter.z); - DependencyManager::get()->renderWireCube(minCube.getScale()); + DependencyManager::get()->renderWireCube(minCube.getScale(), glm::vec4(0.0f, 1.0f, 0.0f, 1.0f)); glPopMatrix(); // draw the entityBox bounding box - glColor4f(0.0f, 0.0f, 1.0f, 1.0f); glPushMatrix(); glTranslatef(entityBoxCenter.x, entityBoxCenter.y, entityBoxCenter.z); glScalef(entityBoxScale.x, entityBoxScale.y, entityBoxScale.z); - DependencyManager::get()->renderWireCube(1.0f); + DependencyManager::get()->renderWireCube(1.0f, glm::vec4(0.0f, 0.0f, 1.0f, 1.0f)); glPopMatrix(); @@ -500,7 +488,6 @@ void EntityTreeRenderer::renderProxies(const EntityItem* entity, RenderArgs* arg glm::vec3 dimensions = entity->getDimensions() * (float) TREE_SCALE; glm::quat rotation = entity->getRotation(); - glColor4f(1.0f, 0.0f, 1.0f, 1.0f); glPushMatrix(); glTranslatef(position.x, position.y, position.z); glm::vec3 axis = glm::axis(rotation); @@ -509,7 +496,7 @@ void EntityTreeRenderer::renderProxies(const EntityItem* entity, RenderArgs* arg glm::vec3 positionToCenter = center - position; glTranslatef(positionToCenter.x, positionToCenter.y, positionToCenter.z); glScalef(dimensions.x, dimensions.y, dimensions.z); - DependencyManager::get()->renderWireCube(1.0f); + DependencyManager::get()->renderWireCube(1.0f, glm::vec4(1.0f, 0.0f, 1.0f, 1.0f)); glPopMatrix(); glPopMatrix(); } diff --git a/libraries/entities-renderer/src/RenderableBoxEntityItem.cpp b/libraries/entities-renderer/src/RenderableBoxEntityItem.cpp index 276db09477..7f2ba410d8 100644 --- a/libraries/entities-renderer/src/RenderableBoxEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableBoxEntityItem.cpp @@ -32,7 +32,7 @@ void RenderableBoxEntityItem::render(RenderArgs* args) { const float MAX_COLOR = 255.0f; - glColor4f(getColor()[RED_INDEX] / MAX_COLOR, getColor()[GREEN_INDEX] / MAX_COLOR, + glm::vec4 cubeColor(getColor()[RED_INDEX] / MAX_COLOR, getColor()[GREEN_INDEX] / MAX_COLOR, getColor()[BLUE_INDEX] / MAX_COLOR, getLocalRenderAlpha()); glPushMatrix(); @@ -43,7 +43,7 @@ void RenderableBoxEntityItem::render(RenderArgs* args) { glm::vec3 positionToCenter = center - position; glTranslatef(positionToCenter.x, positionToCenter.y, positionToCenter.z); glScalef(dimensions.x, dimensions.y, dimensions.z); - DependencyManager::get()->renderSolidCube(1.0f); + DependencyManager::get()->renderSolidCube(1.0f, cubeColor); glPopMatrix(); glPopMatrix(); diff --git a/libraries/entities-renderer/src/RenderableLightEntityItem.cpp b/libraries/entities-renderer/src/RenderableLightEntityItem.cpp index 9097956c88..7eccaff06b 100644 --- a/libraries/entities-renderer/src/RenderableLightEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableLightEntityItem.cpp @@ -63,7 +63,7 @@ void RenderableLightEntityItem::render(RenderArgs* args) { } #ifdef WANT_DEBUG - glColor4f(diffuseR, diffuseG, diffuseB, 1.0f); + glm::vec4 color(diffuseR, diffuseG, diffuseB, 1.0f); glPushMatrix(); glTranslatef(position.x, position.y, position.z); glm::vec3 axis = glm::axis(rotation); @@ -73,7 +73,7 @@ void RenderableLightEntityItem::render(RenderArgs* args) { glTranslatef(positionToCenter.x, positionToCenter.y, positionToCenter.z); glScalef(dimensions.x, dimensions.y, dimensions.z); - DependencyManager::get()->renderWireSphere(0.5f, 15, 15); + DependencyManager::get()->renderWireSphere(0.5f, 15, 15, color); glPopMatrix(); glPopMatrix(); #endif diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp index 699603cb21..48b30868d0 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp @@ -176,27 +176,27 @@ void RenderableModelEntityItem::render(RenderArgs* args) { } } else { // if we couldn't get a model, then just draw a cube - glColor3ub(getColor()[RED_INDEX],getColor()[GREEN_INDEX],getColor()[BLUE_INDEX]); + glm::vec4 color(getColor()[RED_INDEX]/255, getColor()[GREEN_INDEX]/255, getColor()[BLUE_INDEX]/255, 1.0f); glPushMatrix(); glTranslatef(position.x, position.y, position.z); - DependencyManager::get()->renderWireCube(size); + DependencyManager::get()->renderWireCube(size, color); glPopMatrix(); } } else { // if we couldn't get a model, then just draw a cube - glColor3ub(getColor()[RED_INDEX],getColor()[GREEN_INDEX],getColor()[BLUE_INDEX]); + glm::vec4 color(getColor()[RED_INDEX]/255, getColor()[GREEN_INDEX]/255, getColor()[BLUE_INDEX]/255, 1.0f); glPushMatrix(); glTranslatef(position.x, position.y, position.z); - DependencyManager::get()->renderWireCube(size); + DependencyManager::get()->renderWireCube(size, color); glPopMatrix(); } } glPopMatrix(); } else { - glColor3ub(getColor()[RED_INDEX],getColor()[GREEN_INDEX],getColor()[BLUE_INDEX]); + glm::vec4 color(getColor()[RED_INDEX]/255, getColor()[GREEN_INDEX]/255, getColor()[BLUE_INDEX]/255, 1.0f); glPushMatrix(); glTranslatef(position.x, position.y, position.z); - DependencyManager::get()->renderWireCube(size); + DependencyManager::get()->renderWireCube(size, color); glPopMatrix(); } } diff --git a/libraries/entities-renderer/src/RenderableSphereEntityItem.cpp b/libraries/entities-renderer/src/RenderableSphereEntityItem.cpp index 97e3ee7869..427ed20a8b 100644 --- a/libraries/entities-renderer/src/RenderableSphereEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableSphereEntityItem.cpp @@ -32,7 +32,7 @@ void RenderableSphereEntityItem::render(RenderArgs* args) { glm::quat rotation = getRotation(); const float MAX_COLOR = 255.0f; - glColor4f(getColor()[RED_INDEX] / MAX_COLOR, getColor()[GREEN_INDEX] / MAX_COLOR, + glm::vec4 sphereColor(getColor()[RED_INDEX] / MAX_COLOR, getColor()[GREEN_INDEX] / MAX_COLOR, getColor()[BLUE_INDEX] / MAX_COLOR, getLocalRenderAlpha()); glPushMatrix(); @@ -46,7 +46,14 @@ void RenderableSphereEntityItem::render(RenderArgs* args) { glTranslatef(positionToCenter.x, positionToCenter.y, positionToCenter.z); glScalef(dimensions.x, dimensions.y, dimensions.z); - DependencyManager::get()->renderSolidSphere(0.5f, 15, 15); + + // TODO: it would be cool to select different slices/stacks geometry based on the size of the sphere + // and the distance to the viewer. This would allow us to reduce the triangle count for smaller spheres + // that aren't close enough to see the tessellation and use larger triangle count for spheres that would + // expose that effect + const int SLICES = 15; + const int STACKS = 15; + DependencyManager::get()->renderSolidSphere(0.5f, SLICES, STACKS, sphereColor); glPopMatrix(); glPopMatrix(); }; diff --git a/libraries/entities-renderer/src/RenderableTextEntityItem.cpp b/libraries/entities-renderer/src/RenderableTextEntityItem.cpp index 76431b55bc..54b6691eed 100644 --- a/libraries/entities-renderer/src/RenderableTextEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableTextEntityItem.cpp @@ -49,13 +49,13 @@ void RenderableTextEntityItem::render(RenderArgs* args) { const float MAX_COLOR = 255.0f; xColor backgroundColor = getBackgroundColorX(); float alpha = 1.0f; //getBackgroundAlpha(); - glColor4f(backgroundColor.red / MAX_COLOR, backgroundColor.green / MAX_COLOR, backgroundColor.blue / MAX_COLOR, alpha); + glm::vec4 color(backgroundColor.red / MAX_COLOR, backgroundColor.green / MAX_COLOR, backgroundColor.blue / MAX_COLOR, alpha); const float SLIGHTLY_BEHIND = -0.005f; glm::vec3 topLeft(-halfDimensions.x, -halfDimensions.y, SLIGHTLY_BEHIND); glm::vec3 bottomRight(halfDimensions.x, halfDimensions.y, SLIGHTLY_BEHIND); - DependencyManager::get()->renderQuad(topLeft, bottomRight); + DependencyManager::get()->renderQuad(topLeft, bottomRight, color); const int FIXED_FONT_SCALING_RATIO = FIXED_FONT_POINT_SIZE * 40.0f; // this is a ratio determined through experimentation @@ -78,12 +78,11 @@ void RenderableTextEntityItem::render(RenderArgs* args) { enableClipPlane(GL_CLIP_PLANE3, 0.0f, 1.0f, 0.0f, -clipMinimum.y); xColor textColor = getTextColorX(); - glColor3f(textColor.red / MAX_COLOR, textColor.green / MAX_COLOR, textColor.blue / MAX_COLOR); + glm::vec4 textColorV4(textColor.red / MAX_COLOR, textColor.green / MAX_COLOR, textColor.blue / MAX_COLOR, 1.0f); QStringList lines = _text.split("\n"); int lineOffset = maxHeight; - float textAlpha = 1.0f; // getTextAlpha() foreach(QString thisLine, lines) { - textRenderer->draw(0, lineOffset, qPrintable(thisLine), textAlpha); + textRenderer->draw(0, lineOffset, qPrintable(thisLine), textColorV4); lineOffset += maxHeight; } diff --git a/libraries/gpu/src/gpu/Batch.h b/libraries/gpu/src/gpu/Batch.h index f2982e1f52..64b94344c2 100644 --- a/libraries/gpu/src/gpu/Batch.h +++ b/libraries/gpu/src/gpu/Batch.h @@ -46,7 +46,9 @@ enum Primitive { LINE_STRIP, TRIANGLES, TRIANGLE_STRIP, + TRIANGLE_FAN, QUADS, + QUAD_STRIP, NUM_PRIMITIVES, }; diff --git a/libraries/gpu/src/gpu/GLBackendShared.h b/libraries/gpu/src/gpu/GLBackendShared.h index 521c0742f6..1853573522 100644 --- a/libraries/gpu/src/gpu/GLBackendShared.h +++ b/libraries/gpu/src/gpu/GLBackendShared.h @@ -25,7 +25,9 @@ static const GLenum _primitiveToGLmode[NUM_PRIMITIVES] = { GL_LINE_STRIP, GL_TRIANGLES, GL_TRIANGLE_STRIP, + GL_TRIANGLE_FAN, GL_QUADS, + GL_QUAD_STRIP, }; static const GLenum _elementTypeToGLType[NUM_TYPES]= { diff --git a/libraries/gpu/src/gpu/Resource.h b/libraries/gpu/src/gpu/Resource.h index 081eca0d6d..a75c3e8d7c 100644 --- a/libraries/gpu/src/gpu/Resource.h +++ b/libraries/gpu/src/gpu/Resource.h @@ -202,6 +202,14 @@ public: _element(element), _stride(uint16(element.getSize())) {}; + BufferView(const BufferPointer& buffer, Size offset, Size size, uint16 stride, const Element& element = Element(gpu::SCALAR, gpu::UINT8, gpu::RAW)) : + _buffer(buffer), + _offset(offset), + _size(size), + _element(element), + _stride(stride) + {}; + ~BufferView() {} BufferView(const BufferView& view) = default; BufferView& operator=(const BufferView& view) = default; diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 5cbdd04f64..b84b01b16a 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -221,27 +221,27 @@ void DeferredLightingEffect::releaseSimpleProgram() { DependencyManager::get()->setPrimaryDrawBuffers(true, false, false); } -void DeferredLightingEffect::renderSolidSphere(float radius, int slices, int stacks) { +void DeferredLightingEffect::renderSolidSphere(float radius, int slices, int stacks, const glm::vec4& color) { bindSimpleProgram(); - DependencyManager::get()->renderSphere(radius, slices, stacks); + DependencyManager::get()->renderSphere(radius, slices, stacks, color); releaseSimpleProgram(); } -void DeferredLightingEffect::renderWireSphere(float radius, int slices, int stacks) { +void DeferredLightingEffect::renderWireSphere(float radius, int slices, int stacks, const glm::vec4& color) { bindSimpleProgram(); - DependencyManager::get()->renderSphere(radius, slices, stacks, false); + DependencyManager::get()->renderSphere(radius, slices, stacks, color, false); releaseSimpleProgram(); } -void DeferredLightingEffect::renderSolidCube(float size) { +void DeferredLightingEffect::renderSolidCube(float size, const glm::vec4& color) { bindSimpleProgram(); - DependencyManager::get()->renderSolidCube(size); + DependencyManager::get()->renderSolidCube(size, color); releaseSimpleProgram(); } -void DeferredLightingEffect::renderWireCube(float size) { +void DeferredLightingEffect::renderWireCube(float size, const glm::vec4& color) { bindSimpleProgram(); - DependencyManager::get()->renderWireCube(size); + DependencyManager::get()->renderWireCube(size, color); releaseSimpleProgram(); } @@ -303,8 +303,6 @@ void DeferredLightingEffect::prepare() { void DeferredLightingEffect::render() { // perform deferred lighting, rendering to free fbo - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); - glDisable(GL_BLEND); glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); @@ -471,7 +469,7 @@ void DeferredLightingEffect::render() { } else { glTranslatef(light.position.x, light.position.y, light.position.z); - geometryCache->renderSphere(expandedRadius, 32, 32); + geometryCache->renderSphere(expandedRadius, 32, 32, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); } glPopMatrix(); diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index cd8585eade..6c563605ea 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -40,16 +40,16 @@ public: void releaseSimpleProgram(); //// Renders a solid sphere with the simple program. - void renderSolidSphere(float radius, int slices, int stacks); + void renderSolidSphere(float radius, int slices, int stacks, const glm::vec4& color); //// Renders a wireframe sphere with the simple program. - void renderWireSphere(float radius, int slices, int stacks); + void renderWireSphere(float radius, int slices, int stacks, const glm::vec4& color); //// Renders a solid cube with the simple program. - void renderSolidCube(float size); + void renderSolidCube(float size, const glm::vec4& color); //// Renders a wireframe cube with the simple program. - void renderWireCube(float size); + void renderWireCube(float size, const glm::vec4& color); //// Renders a solid cone with the simple program. void renderSolidCone(float base, float height, int slices, int stacks); diff --git a/libraries/render-utils/src/GeometryCache.cpp b/libraries/render-utils/src/GeometryCache.cpp index 0d9fb6f60e..c32b8ff757 100644 --- a/libraries/render-utils/src/GeometryCache.cpp +++ b/libraries/render-utils/src/GeometryCache.cpp @@ -18,6 +18,9 @@ #include #include +#include +#include + #include #include "TextureCache.h" @@ -35,89 +38,12 @@ GeometryCache::GeometryCache() : } GeometryCache::~GeometryCache() { - foreach (const VerticesIndices& vbo, _hemisphereVBOs) { - glDeleteBuffers(1, &vbo.first); - glDeleteBuffers(1, &vbo.second); - } -} - -void GeometryCache::renderHemisphere(int slices, int stacks) { - VerticesIndices& vbo = _hemisphereVBOs[IntPair(slices, stacks)]; - int vertices = slices * (stacks - 1) + 1; - int indices = slices * 2 * 3 * (stacks - 2) + slices * 3; - if (vbo.first == 0) { - GLfloat* vertexData = new GLfloat[vertices * 3]; - GLfloat* vertex = vertexData; - for (int i = 0; i < stacks - 1; i++) { - float phi = PI_OVER_TWO * (float)i / (float)(stacks - 1); - float z = sinf(phi), radius = cosf(phi); - - for (int j = 0; j < slices; j++) { - float theta = TWO_PI * (float)j / (float)slices; - - *(vertex++) = sinf(theta) * radius; - *(vertex++) = cosf(theta) * radius; - *(vertex++) = z; - } - } - *(vertex++) = 0.0f; - *(vertex++) = 0.0f; - *(vertex++) = 1.0f; - - glGenBuffers(1, &vbo.first); - glBindBuffer(GL_ARRAY_BUFFER, vbo.first); - const int BYTES_PER_VERTEX = 3 * sizeof(GLfloat); - glBufferData(GL_ARRAY_BUFFER, vertices * BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW); - delete[] vertexData; - - GLushort* indexData = new GLushort[indices]; - GLushort* index = indexData; - for (int i = 0; i < stacks - 2; i++) { - GLushort bottom = i * slices; - GLushort top = bottom + slices; - for (int j = 0; j < slices; j++) { - int next = (j + 1) % slices; - - *(index++) = bottom + j; - *(index++) = top + next; - *(index++) = top + j; - - *(index++) = bottom + j; - *(index++) = bottom + next; - *(index++) = top + next; - } - } - GLushort bottom = (stacks - 2) * slices; - GLushort top = bottom + slices; - for (int i = 0; i < slices; i++) { - *(index++) = bottom + i; - *(index++) = bottom + (i + 1) % slices; - *(index++) = top; - } - - glGenBuffers(1, &vbo.second); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); - const int BYTES_PER_INDEX = sizeof(GLushort); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * BYTES_PER_INDEX, indexData, GL_STATIC_DRAW); - delete[] indexData; - - } else { - glBindBuffer(GL_ARRAY_BUFFER, vbo.first); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); - } - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - - glVertexPointer(3, GL_FLOAT, 0, 0); - glNormalPointer(GL_FLOAT, 0, 0); - - glDrawRangeElementsEXT(GL_TRIANGLES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); - - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_NORMAL_ARRAY); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + #ifdef WANT_DEBUG + qDebug() << "GeometryCache::~GeometryCache()... "; + qDebug() << " _registeredLine3DVBOs.size():" << _registeredLine3DVBOs.size(); + qDebug() << " _line3DVBOs.size():" << _line3DVBOs.size(); + qDebug() << " BatchItemDetails... population:" << GeometryCache::BatchItemDetails::population; + #endif //def WANT_DEBUG } const int NUM_VERTICES_PER_TRIANGLE = 3; @@ -127,29 +53,38 @@ const int NUM_COORDS_PER_VERTEX = 3; const int NUM_BYTES_PER_VERTEX = NUM_COORDS_PER_VERTEX * sizeof(GLfloat); const int NUM_BYTES_PER_INDEX = sizeof(GLushort); -void GeometryCache::renderSphere(float radius, int slices, int stacks, bool solid) { - VerticesIndices& vbo = _sphereVBOs[IntPair(slices, stacks)]; +void GeometryCache::renderSphere(float radius, int slices, int stacks, const glm::vec4& color, bool solid) { + + Vec2Pair radiusKey(glm::vec2(radius, slices), glm::vec2(stacks, 0)); + IntPair slicesStacksKey(slices, stacks); + Vec3Pair colorKey(glm::vec3(color.x, color.y, slices), glm::vec3(color.z, color.y, stacks)); + int vertices = slices * (stacks - 1) + 2; - int indices = slices * stacks * NUM_VERTICES_PER_TRIANGULATED_QUAD; - if (vbo.first == 0) { + int indices = slices * (stacks - 1) * NUM_VERTICES_PER_TRIANGULATED_QUAD; + + if (!_sphereVertices.contains(radiusKey)) { + gpu::BufferPointer verticesBuffer(new gpu::Buffer()); + _sphereVertices[radiusKey] = verticesBuffer; + GLfloat* vertexData = new GLfloat[vertices * NUM_COORDS_PER_VERTEX]; GLfloat* vertex = vertexData; // south pole *(vertex++) = 0.0f; *(vertex++) = 0.0f; - *(vertex++) = -1.0f; + *(vertex++) = -1.0f * radius; //add stacks vertices climbing up Y axis for (int i = 1; i < stacks; i++) { float phi = PI * (float)i / (float)(stacks) - PI_OVER_TWO; - float z = sinf(phi), radius = cosf(phi); + float z = sinf(phi) * radius; + float stackRadius = cosf(phi) * radius; for (int j = 0; j < slices; j++) { float theta = TWO_PI * (float)j / (float)slices; - *(vertex++) = sinf(theta) * radius; - *(vertex++) = cosf(theta) * radius; + *(vertex++) = sinf(theta) * stackRadius; + *(vertex++) = cosf(theta) * stackRadius; *(vertex++) = z; } } @@ -157,15 +92,29 @@ void GeometryCache::renderSphere(float radius, int slices, int stacks, bool soli // north pole *(vertex++) = 0.0f; *(vertex++) = 0.0f; - *(vertex++) = 1.0f; - - glGenBuffers(1, &vbo.first); - glBindBuffer(GL_ARRAY_BUFFER, vbo.first); - glBufferData(GL_ARRAY_BUFFER, vertices * NUM_BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW); + *(vertex++) = 1.0f * radius; + + verticesBuffer->append(sizeof(GLfloat) * vertices * NUM_COORDS_PER_VERTEX, (gpu::Buffer::Byte*) vertexData); delete[] vertexData; - + + #ifdef WANT_DEBUG + qDebug() << "GeometryCache::renderSphere()... --- CREATING VERTICES BUFFER"; + qDebug() << " radius:" << radius; + qDebug() << " slices:" << slices; + qDebug() << " stacks:" << stacks; + + qDebug() << " _sphereVertices.size():" << _sphereVertices.size(); + #endif + } + + if (!_sphereIndices.contains(slicesStacksKey)) { + gpu::BufferPointer indicesBuffer(new gpu::Buffer()); + _sphereIndices[slicesStacksKey] = indicesBuffer; + GLushort* indexData = new GLushort[indices]; GLushort* index = indexData; + + int indexCount = 0; // South cap GLushort bottom = 0; @@ -174,6 +123,8 @@ void GeometryCache::renderSphere(float radius, int slices, int stacks, bool soli *(index++) = bottom; *(index++) = top + i; *(index++) = top + (i + 1) % slices; + + indexCount += 3; } // (stacks - 2) ribbons @@ -186,10 +137,15 @@ void GeometryCache::renderSphere(float radius, int slices, int stacks, bool soli *(index++) = top + next; *(index++) = bottom + j; *(index++) = top + j; + + indexCount += 3; *(index++) = bottom + next; *(index++) = bottom + j; *(index++) = top + next; + + indexCount += 3; + } } @@ -200,179 +156,94 @@ void GeometryCache::renderSphere(float radius, int slices, int stacks, bool soli *(index++) = bottom + (i + 1) % slices; *(index++) = bottom + i; *(index++) = top; - } - - 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; - } else { - glBindBuffer(GL_ARRAY_BUFFER, vbo.first); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); + indexCount += 3; + + } + indicesBuffer->append(sizeof(GLushort) * indices, (gpu::Buffer::Byte*) indexData); + delete[] indexData; + + #ifdef WANT_DEBUG + qDebug() << "GeometryCache::renderSphere()... --- CREATING INDEX BUFFER"; + qDebug() << " radius:" << radius; + qDebug() << " slices:" << slices; + qDebug() << " stacks:" << stacks; + qDebug() << "indexCount:" << indexCount; + qDebug() << " indices:" << indices; + qDebug() << " _sphereIndices.size():" << _sphereIndices.size(); + #endif } - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - glVertexPointer(3, GL_FLOAT, 0, 0); - glNormalPointer(GL_FLOAT, 0, 0); + if (!_sphereColors.contains(colorKey)) { + gpu::BufferPointer colorBuffer(new gpu::Buffer()); + _sphereColors[colorKey] = colorBuffer; + + int compactColor = ((int(color.x * 255.0f) & 0xFF)) | + ((int(color.y * 255.0f) & 0xFF) << 8) | + ((int(color.z * 255.0f) & 0xFF) << 16) | + ((int(color.w * 255.0f) & 0xFF) << 24); + + int* colorData = new int[vertices]; + int* colorDataAt = colorData; + + for(int v = 0; v < vertices; v++) { + *(colorDataAt++) = compactColor; + } + + colorBuffer->append(sizeof(int) * vertices, (gpu::Buffer::Byte*) colorData); + delete[] colorData; + + #ifdef WANT_DEBUG + qDebug() << "GeometryCache::renderSphere()... --- CREATING COLORS BUFFER"; + qDebug() << " vertices:" << vertices; + qDebug() << " color:" << color; + qDebug() << " slices:" << slices; + qDebug() << " stacks:" << stacks; + qDebug() << " _sphereColors.size():" << _sphereColors.size(); + #endif + + } + gpu::BufferPointer verticesBuffer = _sphereVertices[radiusKey]; + gpu::BufferPointer indicesBuffer = _sphereIndices[slicesStacksKey]; + gpu::BufferPointer colorBuffer = _sphereColors[colorKey]; + + const int VERTICES_SLOT = 0; + const int NORMALS_SLOT = 1; + const int COLOR_SLOT = 2; + gpu::Stream::FormatPointer streamFormat(new gpu::Stream::Format()); // 1 for everyone + streamFormat->setAttribute(gpu::Stream::POSITION, VERTICES_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0); + streamFormat->setAttribute(gpu::Stream::NORMAL, NORMALS_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ)); + streamFormat->setAttribute(gpu::Stream::COLOR, COLOR_SLOT, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + + gpu::BufferView verticesView(verticesBuffer, streamFormat->getAttributes().at(gpu::Stream::POSITION)._element); + gpu::BufferView normalsView(verticesBuffer, streamFormat->getAttributes().at(gpu::Stream::NORMAL)._element); + gpu::BufferView colorView(colorBuffer, streamFormat->getAttributes().at(gpu::Stream::COLOR)._element); + + gpu::Batch batch; + + batch.setInputFormat(streamFormat); + batch.setInputBuffer(VERTICES_SLOT, verticesView); + batch.setInputBuffer(NORMALS_SLOT, normalsView); + batch.setInputBuffer(COLOR_SLOT, colorView); + batch.setIndexBuffer(gpu::UINT16, indicesBuffer, 0); - glPushMatrix(); - glScalef(radius, radius, radius); if (solid) { - glDrawRangeElementsEXT(GL_TRIANGLES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); + batch.drawIndexed(gpu::TRIANGLES, indices); } else { - glDrawRangeElementsEXT(GL_LINES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); + batch.drawIndexed(gpu::LINES, indices); } - glPopMatrix(); + + gpu::GLBackend::renderBatch(batch); glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_NORMAL_ARRAY); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); -} + glDisableClientState(GL_COLOR_ARRAY); -void GeometryCache::renderSquare(int xDivisions, int yDivisions) { - VerticesIndices& vbo = _squareVBOs[IntPair(xDivisions, yDivisions)]; - int xVertices = xDivisions + 1; - int yVertices = yDivisions + 1; - int vertices = xVertices * yVertices; - int indices = 2 * 3 * xDivisions * yDivisions; - if (vbo.first == 0) { - GLfloat* vertexData = new GLfloat[vertices * 3]; - GLfloat* vertex = vertexData; - for (int i = 0; i <= yDivisions; i++) { - float y = (float)i / yDivisions; - - for (int j = 0; j <= xDivisions; j++) { - *(vertex++) = (float)j / xDivisions; - *(vertex++) = y; - *(vertex++) = 0.0f; - } - } - - 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 < yDivisions; i++) { - GLushort bottom = i * xVertices; - GLushort top = bottom + xVertices; - for (int j = 0; j < xDivisions; j++) { - int next = j + 1; - - *(index++) = bottom + j; - *(index++) = top + next; - *(index++) = top + j; - - *(index++) = bottom + j; - *(index++) = bottom + next; - *(index++) = top + next; - } - } - - 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; - - } else { - glBindBuffer(GL_ARRAY_BUFFER, vbo.first); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); - } - glEnableClientState(GL_VERTEX_ARRAY); - - // all vertices have the same normal - glNormal3f(0.0f, 0.0f, 1.0f); - - glVertexPointer(3, GL_FLOAT, 0, 0); - - glDrawRangeElementsEXT(GL_TRIANGLES, 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::renderHalfCylinder(int slices, int stacks) { - VerticesIndices& vbo = _halfCylinderVBOs[IntPair(slices, stacks)]; - int vertices = (slices + 1) * stacks; - int indices = 2 * 3 * slices * (stacks - 1); - if (vbo.first == 0) { - GLfloat* vertexData = new GLfloat[vertices * 2 * 3]; - GLfloat* vertex = vertexData; - for (int i = 0; i <= (stacks - 1); i++) { - float y = (float)i / (stacks - 1); - - for (int j = 0; j <= slices; j++) { - float theta = 3.0f * PI_OVER_TWO + PI * (float)j / (float)slices; - - //normals - *(vertex++) = sinf(theta); - *(vertex++) = 0; - *(vertex++) = cosf(theta); - - // vertices - *(vertex++) = sinf(theta); - *(vertex++) = y; - *(vertex++) = cosf(theta); - } - } - - glGenBuffers(1, &vbo.first); - glBindBuffer(GL_ARRAY_BUFFER, vbo.first); - glBufferData(GL_ARRAY_BUFFER, 2 * vertices * NUM_BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW); - delete[] vertexData; - - GLushort* indexData = new GLushort[indices]; - GLushort* index = indexData; - for (int i = 0; i < stacks - 1; i++) { - GLushort bottom = i * (slices + 1); - GLushort top = bottom + slices + 1; - for (int j = 0; j < slices; j++) { - int next = j + 1; - - *(index++) = bottom + j; - *(index++) = top + next; - *(index++) = top + j; - - *(index++) = bottom + j; - *(index++) = bottom + next; - *(index++) = top + next; - } - } - - 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; - - } else { - glBindBuffer(GL_ARRAY_BUFFER, vbo.first); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); - } - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - - glNormalPointer(GL_FLOAT, 6 * sizeof(float), 0); - glVertexPointer(3, GL_FLOAT, (6 * sizeof(float)), (const void *)(3 * sizeof(float))); - - glDrawRangeElementsEXT(GL_TRIANGLES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); - - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_NORMAL_ARRAY); - glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } void GeometryCache::renderCone(float base, float height, int slices, int stacks) { - VerticesIndices& vbo = _halfCylinderVBOs[IntPair(slices, stacks)]; + VerticesIndices& vbo = _coneVBOs[IntPair(slices, stacks)]; int vertices = (stacks + 2) * slices; int baseTriangles = slices - 2; int indices = NUM_VERTICES_PER_TRIANGULATED_QUAD * slices * stacks + NUM_VERTICES_PER_TRIANGLE * baseTriangles; @@ -471,12 +342,17 @@ void GeometryCache::renderCone(float base, float height, int slices, int stacks) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } -void GeometryCache::renderGrid(int xDivisions, int yDivisions) { - QOpenGLBuffer& buffer = _gridBuffers[IntPair(xDivisions, yDivisions)]; +void GeometryCache::renderGrid(int xDivisions, int yDivisions, const glm::vec4& color) { + IntPair key(xDivisions, yDivisions); + Vec3Pair colorKey(glm::vec3(color.x, color.y, yDivisions), glm::vec3(color.z, color.y, xDivisions)); + int vertices = (xDivisions + 1 + yDivisions + 1) * 2; - if (!buffer.isCreated()) { + if (!_gridBuffers.contains(key)) { + gpu::BufferPointer verticesBuffer(new gpu::Buffer()); + GLfloat* vertexData = new GLfloat[vertices * 2]; GLfloat* vertex = vertexData; + for (int i = 0; i <= xDivisions; i++) { float x = (float)i / xDivisions; @@ -495,52 +371,82 @@ void GeometryCache::renderGrid(int xDivisions, int yDivisions) { *(vertex++) = 1.0f; *(vertex++) = y; } - buffer.create(); - buffer.setUsagePattern(QOpenGLBuffer::StaticDraw); - buffer.bind(); - buffer.allocate(vertexData, vertices * 2 * sizeof(GLfloat)); + + verticesBuffer->append(sizeof(GLfloat) * vertices * 2, (gpu::Buffer::Byte*) vertexData); delete[] vertexData; - } else { - buffer.bind(); + _gridBuffers[key] = verticesBuffer; } - glEnableClientState(GL_VERTEX_ARRAY); + + if (!_gridColors.contains(colorKey)) { + gpu::BufferPointer colorBuffer(new gpu::Buffer()); + _gridColors[colorKey] = colorBuffer; + + int compactColor = ((int(color.x * 255.0f) & 0xFF)) | + ((int(color.y * 255.0f) & 0xFF) << 8) | + ((int(color.z * 255.0f) & 0xFF) << 16) | + ((int(color.w * 255.0f) & 0xFF) << 24); + + int* colorData = new int[vertices]; + int* colorDataAt = colorData; + + for(int v = 0; v < vertices; v++) { + *(colorDataAt++) = compactColor; + } + + colorBuffer->append(sizeof(int) * vertices, (gpu::Buffer::Byte*) colorData); + delete[] colorData; + } + gpu::BufferPointer verticesBuffer = _gridBuffers[key]; + gpu::BufferPointer colorBuffer = _gridColors[colorKey]; + + const int VERTICES_SLOT = 0; + const int COLOR_SLOT = 1; + gpu::Stream::FormatPointer streamFormat(new gpu::Stream::Format()); // 1 for everyone - glVertexPointer(2, GL_FLOAT, 0, 0); - - glDrawArrays(GL_LINES, 0, vertices); + streamFormat->setAttribute(gpu::Stream::POSITION, VERTICES_SLOT, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::XYZ), 0); + streamFormat->setAttribute(gpu::Stream::COLOR, COLOR_SLOT, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + + gpu::BufferView verticesView(verticesBuffer, 0, verticesBuffer->getSize(), streamFormat->getAttributes().at(gpu::Stream::POSITION)._element); + gpu::BufferView colorView(colorBuffer, streamFormat->getAttributes().at(gpu::Stream::COLOR)._element); + gpu::Batch batch; + + batch.setInputFormat(streamFormat); + batch.setInputBuffer(VERTICES_SLOT, verticesView); + batch.setInputBuffer(COLOR_SLOT, colorView); + batch.draw(gpu::LINES, vertices, 0); + + gpu::GLBackend::renderBatch(batch); + glDisableClientState(GL_VERTEX_ARRAY); - - buffer.release(); + glDisableClientState(GL_COLOR_ARRAY); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + } -void GeometryCache::renderGrid(int x, int y, int width, int height, int rows, int cols, int id) { +// TODO: properly handle the x,y,w,h changing for an ID +// TODO: why do we seem to create extra BatchItemDetails when we resize the window?? what's that?? +void GeometryCache::renderGrid(int x, int y, int width, int height, int rows, int cols, const glm::vec4& color, int id) { + #ifdef WANT_DEBUG + qDebug() << "GeometryCache::renderGrid(x["<append(sizeof(GLfloat) * vertices * 2, (gpu::Buffer::Byte*) vertexData); delete[] vertexData; - - #ifdef WANT_DEBUG - if (id == UNKNOWN_ID) { - qDebug() << "new grid buffer made -- _alternateGridBuffers.size():" << _alternateGridBuffers.size(); - } else { - qDebug() << "new registered grid buffer made -- _registeredAlternateGridBuffers.size():" << _registeredAlternateGridBuffers.size(); - } - #endif - } else { - buffer.bind(); + if (registered) { + _registeredAlternateGridBuffers[id] = verticesBuffer; + } else { + _alternateGridBuffers[key] = verticesBuffer; + } } - glEnableClientState(GL_VERTEX_ARRAY); + + if (!_gridColors.contains(colorKey)) { + gpu::BufferPointer colorBuffer(new gpu::Buffer()); + _gridColors[colorKey] = colorBuffer; + + int compactColor = ((int(color.x * 255.0f) & 0xFF)) | + ((int(color.y * 255.0f) & 0xFF) << 8) | + ((int(color.z * 255.0f) & 0xFF) << 16) | + ((int(color.w * 255.0f) & 0xFF) << 24); + + int* colorData = new int[vertices]; + int* colorDataAt = colorData; + + + for(int v = 0; v < vertices; v++) { + *(colorDataAt++) = compactColor; + } + + colorBuffer->append(sizeof(int) * vertices, (gpu::Buffer::Byte*) colorData); + delete[] colorData; + } + gpu::BufferPointer verticesBuffer = registered ? _registeredAlternateGridBuffers[id] : _alternateGridBuffers[key]; - glVertexPointer(2, GL_FLOAT, 0, 0); + gpu::BufferPointer colorBuffer = _gridColors[colorKey]; + + const int VERTICES_SLOT = 0; + const int COLOR_SLOT = 1; + gpu::Stream::FormatPointer streamFormat(new gpu::Stream::Format()); // 1 for everyone - glDrawArrays(GL_LINES, 0, vertices); + streamFormat->setAttribute(gpu::Stream::POSITION, VERTICES_SLOT, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::XYZ), 0); + streamFormat->setAttribute(gpu::Stream::COLOR, COLOR_SLOT, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + + gpu::BufferView verticesView(verticesBuffer, 0, verticesBuffer->getSize(), streamFormat->getAttributes().at(gpu::Stream::POSITION)._element); + gpu::BufferView colorView(colorBuffer, streamFormat->getAttributes().at(gpu::Stream::COLOR)._element); + gpu::Batch batch; + + batch.setInputFormat(streamFormat); + batch.setInputBuffer(VERTICES_SLOT, verticesView); + batch.setInputBuffer(COLOR_SLOT, colorView); + batch.draw(gpu::LINES, vertices, 0); + + gpu::GLBackend::renderBatch(batch); + glDisableClientState(GL_VERTEX_ARRAY); - - buffer.release(); + glDisableClientState(GL_COLOR_ARRAY); + + glBindBuffer(GL_ARRAY_BUFFER, 0); } -void GeometryCache::updateVertices(int id, const QVector& points) { - BufferDetails& details = _registeredVertices[id]; +void GeometryCache::updateVertices(int id, const QVector& points, const glm::vec4& color) { + BatchItemDetails& details = _registeredVertices[id]; - if (details.buffer.isCreated()) { - details.buffer.destroy(); + if (details.isCreated) { + details.clear(); #ifdef WANT_DEBUG qDebug() << "updateVertices()... RELEASING REGISTERED"; #endif // def WANT_DEBUG } const int FLOATS_PER_VERTEX = 2; + details.isCreated = true; details.vertices = points.size(); details.vertexSize = FLOATS_PER_VERTEX; + 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()); + + details.verticesBuffer = verticesBuffer; + details.colorBuffer = colorBuffer; + details.streamFormat = streamFormat; + details.stream = stream; + + details.streamFormat->setAttribute(gpu::Stream::POSITION, 0, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::XYZ), 0); + details.streamFormat->setAttribute(gpu::Stream::COLOR, 1, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + + details.stream->addBuffer(details.verticesBuffer, 0, details.streamFormat->getChannels().at(0)._stride); + details.stream->addBuffer(details.colorBuffer, 0, details.streamFormat->getChannels().at(1)._stride); + + details.vertices = points.size(); + details.vertexSize = FLOATS_PER_VERTEX; + + int compactColor = ((int(color.x * 255.0f) & 0xFF)) | + ((int(color.y * 255.0f) & 0xFF) << 8) | + ((int(color.z * 255.0f) & 0xFF) << 16) | + ((int(color.w * 255.0f) & 0xFF) << 24); + GLfloat* vertexData = new GLfloat[details.vertices * FLOATS_PER_VERTEX]; GLfloat* vertex = vertexData; + int* colorData = new int[details.vertices]; + int* colorDataAt = colorData; + foreach (const glm::vec2& point, points) { *(vertex++) = point.x; *(vertex++) = point.y; + + *(colorDataAt++) = compactColor; } - details.buffer.create(); - details.buffer.setUsagePattern(QOpenGLBuffer::StaticDraw); - details.buffer.bind(); - details.buffer.allocate(vertexData, details.vertices * FLOATS_PER_VERTEX * sizeof(GLfloat)); - delete[] vertexData; - - #ifdef WANT_DEBUG - qDebug() << "new registered vertices buffer made -- _registeredVertices.size():" << _registeredVertices.size(); - #endif -} - -void GeometryCache::updateVertices(int id, const QVector& points) { - BufferDetails& details = _registeredVertices[id]; - - if (details.buffer.isCreated()) { - details.buffer.destroy(); - #ifdef WANT_DEBUG - qDebug() << "updateVertices()... RELEASING REGISTERED"; - #endif // def WANT_DEBUG - } - - const int FLOATS_PER_VERTEX = 3; - details.vertices = points.size(); - details.vertexSize = FLOATS_PER_VERTEX; - - GLfloat* vertexData = new GLfloat[details.vertices * FLOATS_PER_VERTEX]; - GLfloat* vertex = vertexData; - - foreach (const glm::vec3& point, points) { - *(vertex++) = point.x; - *(vertex++) = point.y; - *(vertex++) = point.z; - } - - details.buffer.create(); - details.buffer.setUsagePattern(QOpenGLBuffer::StaticDraw); - details.buffer.bind(); - details.buffer.allocate(vertexData, details.vertices * FLOATS_PER_VERTEX * sizeof(GLfloat)); + details.verticesBuffer->append(sizeof(GLfloat) * FLOATS_PER_VERTEX * details.vertices, (gpu::Buffer::Byte*) vertexData); + details.colorBuffer->append(sizeof(int) * details.vertices, (gpu::Buffer::Byte*) colorData); delete[] vertexData; + delete[] colorData; #ifdef WANT_DEBUG qDebug() << "new registered linestrip buffer made -- _registeredVertices.size():" << _registeredVertices.size(); #endif } -void GeometryCache::renderVertices(GLenum mode, int id) { - BufferDetails& details = _registeredVertices[id]; - if (details.buffer.isCreated()) { - details.buffer.bind(); - glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(details.vertexSize, GL_FLOAT, 0, 0); - glDrawArrays(mode, 0, details.vertices); +void GeometryCache::updateVertices(int id, const QVector& points, const glm::vec4& color) { + BatchItemDetails& details = _registeredVertices[id]; + + if (details.isCreated) { + details.clear(); + #ifdef WANT_DEBUG + qDebug() << "updateVertices()... RELEASING REGISTERED"; + #endif // def WANT_DEBUG + } + + const int FLOATS_PER_VERTEX = 3; + details.isCreated = true; + details.vertices = points.size(); + details.vertexSize = FLOATS_PER_VERTEX; + + 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()); + + details.verticesBuffer = verticesBuffer; + details.colorBuffer = colorBuffer; + details.streamFormat = streamFormat; + details.stream = stream; + + details.streamFormat->setAttribute(gpu::Stream::POSITION, 0, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0); + details.streamFormat->setAttribute(gpu::Stream::COLOR, 1, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + + details.stream->addBuffer(details.verticesBuffer, 0, details.streamFormat->getChannels().at(0)._stride); + details.stream->addBuffer(details.colorBuffer, 0, details.streamFormat->getChannels().at(1)._stride); + + details.vertices = points.size(); + details.vertexSize = FLOATS_PER_VERTEX; + + int compactColor = ((int(color.x * 255.0f) & 0xFF)) | + ((int(color.y * 255.0f) & 0xFF) << 8) | + ((int(color.z * 255.0f) & 0xFF) << 16) | + ((int(color.w * 255.0f) & 0xFF) << 24); + + GLfloat* vertexData = new GLfloat[details.vertices * FLOATS_PER_VERTEX]; + GLfloat* vertex = vertexData; + + int* colorData = new int[details.vertices]; + int* colorDataAt = colorData; + + foreach (const glm::vec3& point, points) { + *(vertex++) = point.x; + *(vertex++) = point.y; + *(vertex++) = point.z; + + *(colorDataAt++) = compactColor; + } + + details.verticesBuffer->append(sizeof(GLfloat) * FLOATS_PER_VERTEX * details.vertices, (gpu::Buffer::Byte*) vertexData); + details.colorBuffer->append(sizeof(int) * details.vertices, (gpu::Buffer::Byte*) colorData); + delete[] vertexData; + delete[] colorData; + + #ifdef WANT_DEBUG + qDebug() << "new registered linestrip buffer made -- _registeredVertices.size():" << _registeredVertices.size(); + #endif +} + +void GeometryCache::renderVertices(gpu::Primitive primitiveType, int id) { + BatchItemDetails& details = _registeredVertices[id]; + if (details.isCreated) { + gpu::Batch batch; + + batch.setInputFormat(details.streamFormat); + batch.setInputStream(0, *details.stream); + batch.draw(primitiveType, details.vertices, 0); + + gpu::GLBackend::renderBatch(batch); + glDisableClientState(GL_VERTEX_ARRAY); - details.buffer.release(); + glDisableClientState(GL_COLOR_ARRAY); + + glBindBuffer(GL_ARRAY_BUFFER, 0); } } -void GeometryCache::renderSolidCube(float size) { - VerticesIndices& vbo = _solidCubeVBOs[size]; +void GeometryCache::renderSolidCube(float size, const glm::vec4& color) { + Vec2Pair colorKey(glm::vec2(color.x, color.y), glm::vec2(color.z, color.y)); const int FLOATS_PER_VERTEX = 3; const int VERTICES_PER_FACE = 4; const int NUMBER_OF_FACES = 6; const int TRIANGLES_PER_FACE = 2; const int VERTICES_PER_TRIANGLE = 3; - const int vertices = NUMBER_OF_FACES * VERTICES_PER_FACE * FLOATS_PER_VERTEX; + const int vertices = NUMBER_OF_FACES * VERTICES_PER_FACE; const int indices = NUMBER_OF_FACES * TRIANGLES_PER_FACE * VERTICES_PER_TRIANGLE; const int vertexPoints = vertices * FLOATS_PER_VERTEX; - if (vbo.first == 0) { + const int VERTEX_STRIDE = sizeof(GLfloat) * FLOATS_PER_VERTEX * 2; // vertices and normals + const int NORMALS_OFFSET = sizeof(GLfloat) * FLOATS_PER_VERTEX; + + if (!_solidCubeVerticies.contains(size)) { + gpu::BufferPointer verticesBuffer(new gpu::Buffer()); + _solidCubeVerticies[size] = verticesBuffer; + GLfloat* vertexData = new GLfloat[vertexPoints * 2]; // vertices and normals GLfloat* vertex = vertexData; float halfSize = size / 2.0f; - + static GLfloat cannonicalVertices[vertexPoints] = { 1, 1, 1, -1, 1, 1, -1,-1, 1, 1,-1, 1, // v0,v1,v2,v3 (front) 1, 1, 1, 1,-1, 1, 1,-1,-1, 1, 1,-1, // v0,v3,v4,v5 (right) @@ -710,7 +717,26 @@ void GeometryCache::renderSolidCube(float size) { 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, // v7,v4,v3,v2 (bottom) 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1 }; // v4,v7,v6,v5 (back) - // index array of vertex array for glDrawElements() & glDrawRangeElement() + + GLfloat* cannonicalVertex = &cannonicalVertices[0]; + GLfloat* cannonicalNormal = &cannonicalNormals[0]; + + for (int i = 0; i < vertices; i++) { + // vertices + *(vertex++) = halfSize * *cannonicalVertex++; + *(vertex++) = halfSize * *cannonicalVertex++; + *(vertex++) = halfSize * *cannonicalVertex++; + + //normals + *(vertex++) = *cannonicalNormal++; + *(vertex++) = *cannonicalNormal++; + *(vertex++) = *cannonicalNormal++; + } + + verticesBuffer->append(sizeof(GLfloat) * vertexPoints * 2, (gpu::Buffer::Byte*) vertexData); + } + + if (!_solidCubeIndexBuffer) { static GLubyte cannonicalIndices[indices] = { 0, 1, 2, 2, 3, 0, // front 4, 5, 6, 6, 7, 4, // right @@ -718,62 +744,67 @@ void GeometryCache::renderSolidCube(float size) { 12,13,14, 14,15,12, // left 16,17,18, 18,19,16, // bottom 20,21,22, 22,23,20 }; // back - - - GLfloat* cannonicalVertex = &cannonicalVertices[0]; - GLfloat* cannonicalNormal = &cannonicalNormals[0]; - - for (int i = 0; i < vertices; i++) { - //normals - *(vertex++) = *cannonicalNormal++; - *(vertex++) = *cannonicalNormal++; - *(vertex++) = *cannonicalNormal++; - - // vertices - *(vertex++) = halfSize * *cannonicalVertex++; - *(vertex++) = halfSize * *cannonicalVertex++; - *(vertex++) = halfSize * *cannonicalVertex++; - - } - - 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; + gpu::BufferPointer indexBuffer(new gpu::Buffer()); + _solidCubeIndexBuffer = indexBuffer; - } else { - glBindBuffer(GL_ARRAY_BUFFER, vbo.first); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); + _solidCubeIndexBuffer->append(sizeof(cannonicalIndices), (gpu::Buffer::Byte*) cannonicalIndices); } - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - glNormalPointer(GL_FLOAT, 6 * sizeof(float), 0); - glVertexPointer(3, GL_FLOAT, (6 * sizeof(float)), (const void *)(3 * sizeof(float))); - - glDrawRangeElementsEXT(GL_TRIANGLES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); - - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_NORMAL_ARRAY); + if (!_solidCubeColors.contains(colorKey)) { + gpu::BufferPointer colorBuffer(new gpu::Buffer()); + _solidCubeColors[colorKey] = colorBuffer; + + const int NUM_COLOR_SCALARS_PER_CUBE = 24; + int compactColor = ((int(color.x * 255.0f) & 0xFF)) | + ((int(color.y * 255.0f) & 0xFF) << 8) | + ((int(color.z * 255.0f) & 0xFF) << 16) | + ((int(color.w * 255.0f) & 0xFF) << 24); + int colors[NUM_COLOR_SCALARS_PER_CUBE] = { compactColor, compactColor, compactColor, compactColor, + compactColor, compactColor, compactColor, compactColor, + compactColor, compactColor, compactColor, compactColor, + compactColor, compactColor, compactColor, compactColor, + compactColor, compactColor, compactColor, compactColor, + compactColor, compactColor, compactColor, compactColor }; + + colorBuffer->append(sizeof(colors), (gpu::Buffer::Byte*) colors); + } + gpu::BufferPointer verticesBuffer = _solidCubeVerticies[size]; + gpu::BufferPointer colorBuffer = _solidCubeColors[colorKey]; + + const int VERTICES_SLOT = 0; + const int NORMALS_SLOT = 1; + const int COLOR_SLOT = 2; + gpu::Stream::FormatPointer streamFormat(new gpu::Stream::Format()); // 1 for everyone + streamFormat->setAttribute(gpu::Stream::POSITION, VERTICES_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0); + streamFormat->setAttribute(gpu::Stream::NORMAL, NORMALS_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ)); + streamFormat->setAttribute(gpu::Stream::COLOR, COLOR_SLOT, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + + gpu::BufferView verticesView(verticesBuffer, 0, verticesBuffer->getSize(), VERTEX_STRIDE, streamFormat->getAttributes().at(gpu::Stream::POSITION)._element); + gpu::BufferView normalsView(verticesBuffer, NORMALS_OFFSET, verticesBuffer->getSize(), VERTEX_STRIDE, streamFormat->getAttributes().at(gpu::Stream::NORMAL)._element); + gpu::BufferView colorView(colorBuffer, streamFormat->getAttributes().at(gpu::Stream::COLOR)._element); + + gpu::Batch batch; + + batch.setInputFormat(streamFormat); + batch.setInputBuffer(VERTICES_SLOT, verticesView); + batch.setInputBuffer(NORMALS_SLOT, normalsView); + batch.setInputBuffer(COLOR_SLOT, colorView); + batch.setIndexBuffer(gpu::UINT8, _solidCubeIndexBuffer, 0); + batch.drawIndexed(gpu::TRIANGLES, indices); + + gpu::GLBackend::renderBatch(batch); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); + glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } -void GeometryCache::renderWireCube(float size) { - VerticesIndices& vbo = _wireCubeVBOs[size]; +void GeometryCache::renderWireCube(float size, const glm::vec4& color) { + Vec2Pair colorKey(glm::vec2(color.x, color.y),glm::vec2(color.z, color.y)); const int FLOATS_PER_VERTEX = 3; const int VERTICES_PER_EDGE = 2; const int TOP_EDGES = 4; @@ -781,7 +812,11 @@ void GeometryCache::renderWireCube(float size) { const int SIDE_EDGES = 4; const int vertices = 8; const int indices = (TOP_EDGES + BOTTOM_EDGES + SIDE_EDGES) * VERTICES_PER_EDGE; - if (vbo.first == 0) { + + if (!_cubeVerticies.contains(size)) { + gpu::BufferPointer verticesBuffer(new gpu::Buffer()); + _cubeVerticies[size] = verticesBuffer; + int vertexPoints = vertices * FLOATS_PER_VERTEX; GLfloat* vertexData = new GLfloat[vertexPoints]; // only vertices, no normals because we're a wire cube GLfloat* vertex = vertexData; @@ -792,81 +827,119 @@ void GeometryCache::renderWireCube(float size) { 1,-1, 1, 1,-1,-1, -1,-1,-1, -1,-1, 1 // v4, v5, v6, v7 (bottom) }; - // index array of vertex array for glDrawRangeElement() as a GL_LINES for each edge + for (int i = 0; i < vertexPoints; i++) { + vertex[i] = cannonicalVertices[i] * halfSize; + } + + verticesBuffer->append(sizeof(GLfloat) * vertexPoints, (gpu::Buffer::Byte*) vertexData); // I'm skeptical that this is right + } + + if (!_wireCubeIndexBuffer) { static GLubyte cannonicalIndices[indices] = { 0, 1, 1, 2, 2, 3, 3, 0, // (top) 4, 5, 5, 6, 6, 7, 7, 4, // (bottom) 0, 4, 1, 5, 2, 6, 3, 7, // (side edges) }; - - for (int i = 0; i < vertexPoints; i++) { - vertex[i] = cannonicalVertices[i] * halfSize; - } - - 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; + + gpu::BufferPointer indexBuffer(new gpu::Buffer()); + _wireCubeIndexBuffer = indexBuffer; - } else { - glBindBuffer(GL_ARRAY_BUFFER, vbo.first); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); + _wireCubeIndexBuffer->append(sizeof(cannonicalIndices), (gpu::Buffer::Byte*) cannonicalIndices); } - 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); + + if (!_cubeColors.contains(colorKey)) { + gpu::BufferPointer colorBuffer(new gpu::Buffer()); + _cubeColors[colorKey] = colorBuffer; + + const int NUM_COLOR_SCALARS_PER_CUBE = 8; + int compactColor = ((int(color.x * 255.0f) & 0xFF)) | + ((int(color.y * 255.0f) & 0xFF) << 8) | + ((int(color.z * 255.0f) & 0xFF) << 16) | + ((int(color.w * 255.0f) & 0xFF) << 24); + int colors[NUM_COLOR_SCALARS_PER_CUBE] = { compactColor, compactColor, compactColor, compactColor, + compactColor, compactColor, compactColor, compactColor }; + + colorBuffer->append(sizeof(colors), (gpu::Buffer::Byte*) colors); + } + gpu::BufferPointer verticesBuffer = _cubeVerticies[size]; + gpu::BufferPointer colorBuffer = _cubeColors[colorKey]; + + const int VERTICES_SLOT = 0; + const int COLOR_SLOT = 1; + gpu::Stream::FormatPointer streamFormat(new gpu::Stream::Format()); // 1 for everyone + streamFormat->setAttribute(gpu::Stream::POSITION, VERTICES_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0); + streamFormat->setAttribute(gpu::Stream::COLOR, COLOR_SLOT, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + gpu::BufferView verticesView(verticesBuffer, streamFormat->getAttributes().at(gpu::Stream::POSITION)._element); + gpu::BufferView colorView(colorBuffer, streamFormat->getAttributes().at(gpu::Stream::COLOR)._element); + + gpu::Batch batch; + + batch.setInputFormat(streamFormat); + batch.setInputBuffer(VERTICES_SLOT, verticesView); + batch.setInputBuffer(COLOR_SLOT, colorView); + batch.setIndexBuffer(gpu::UINT8, _wireCubeIndexBuffer, 0); + batch.drawIndexed(gpu::LINES, indices); + + gpu::GLBackend::renderBatch(batch); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); + glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } -void GeometryCache::renderBevelCornersRect(int x, int y, int width, int height, int bevelDistance, int id) { - bool registeredRect = (id != UNKNOWN_ID); +void GeometryCache::renderBevelCornersRect(int x, int y, int width, int height, int bevelDistance, const glm::vec4& color, int id) { + bool registered = (id != UNKNOWN_ID); Vec3Pair key(glm::vec3(x, y, 0.0f), glm::vec3(width, height, bevelDistance)); - VerticesIndices& vbo = registeredRect ? _registeredRectVBOs[id] : _rectVBOs[key]; - + BatchItemDetails& details = registered ? _registeredBevelRects[id] : _bevelRects[key]; // if this is a registered quad, and we have buffers, then check to see if the geometry changed and rebuild if needed - if (registeredRect && vbo.first != 0) { - Vec3Pair& lastKey = _lastRegisteredRect[id]; + if (registered && details.isCreated) { + Vec3Pair& lastKey = _lastRegisteredBevelRects[id]; if (lastKey != key) { - glDeleteBuffers(1, &vbo.first); - glDeleteBuffers(1, &vbo.second); - vbo.first = vbo.second = 0; + details.clear(); + _lastRegisteredBevelRects[id] = key; #ifdef WANT_DEBUG - qDebug() << "renderBevelCornersRect()... RELEASING REGISTERED RECT"; + qDebug() << "renderBevelCornersRect()... RELEASING REGISTERED"; #endif // def WANT_DEBUG } #ifdef WANT_DEBUG else { - qDebug() << "renderBevelCornersRect()... REUSING PREVIOUSLY REGISTERED RECT"; + qDebug() << "renderBevelCornersRect()... REUSING PREVIOUSLY REGISTERED"; } #endif // def WANT_DEBUG } - const int FLOATS_PER_VERTEX = 2; - const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); + const int FLOATS_PER_VERTEX = 2; // vertices const int vertices = 8; - const int indices = 8; - if (vbo.first == 0) { - _lastRegisteredRect[id] = key; + + if (!details.isCreated) { + + details.isCreated = true; + details.vertices = vertices; + details.vertexSize = FLOATS_PER_VERTEX; + + 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()); + + details.verticesBuffer = verticesBuffer; + details.colorBuffer = colorBuffer; + details.streamFormat = streamFormat; + details.stream = stream; + + details.streamFormat->setAttribute(gpu::Stream::POSITION, 0, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::XYZ), 0); + details.streamFormat->setAttribute(gpu::Stream::COLOR, 1, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + + details.stream->addBuffer(details.verticesBuffer, 0, details.streamFormat->getChannels().at(0)._stride); + details.stream->addBuffer(details.colorBuffer, 0, details.streamFormat->getChannels().at(1)._stride); + 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, 2, 3, 4, 5, 6, 7}; - + GLfloat* vertexBuffer = new GLfloat[vertexPoints]; // only vertices, no normals because we're a 2D quad + GLfloat* vertex = vertexBuffer; int vertexPoint = 0; // left side @@ -895,318 +968,293 @@ void GeometryCache::renderBevelCornersRect(int x, int y, int width, int height, vertex[vertexPoint++] = x +bevelDistance; vertex[vertexPoint++] = 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; - - #ifdef WANT_DEBUG - if (id == UNKNOWN_ID) { - qDebug() << "new rect VBO made -- _rectVBOs.size():" << _rectVBOs.size(); - } else { - qDebug() << "new registered rect VBO made -- _registeredRectVBOs.size():" << _registeredRectVBOs.size(); - } - #endif - - } else { - glBindBuffer(GL_ARRAY_BUFFER, vbo.first); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); + const int NUM_COLOR_SCALARS_PER_QUAD = 8; + int compactColor = ((int(color.x * 255.0f) & 0xFF)) | + ((int(color.y * 255.0f) & 0xFF) << 8) | + ((int(color.z * 255.0f) & 0xFF) << 16) | + ((int(color.w * 255.0f) & 0xFF) << 24); + int colors[NUM_COLOR_SCALARS_PER_QUAD] = { compactColor, compactColor, compactColor, compactColor, + compactColor, compactColor, compactColor, compactColor }; + + + details.verticesBuffer->append(sizeof(vertexBuffer), (gpu::Buffer::Byte*) vertexBuffer); + details.colorBuffer->append(sizeof(colors), (gpu::Buffer::Byte*) colors); + + delete[] vertexBuffer; } - glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(FLOATS_PER_VERTEX, GL_FLOAT, FLOATS_PER_VERTEX * sizeof(float), 0); - glDrawRangeElementsEXT(GL_POLYGON, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); + + gpu::Batch batch; + + batch.setInputFormat(details.streamFormat); + batch.setInputStream(0, *details.stream); + batch.draw(gpu::QUADS, 4, 0); + + gpu::GLBackend::renderBatch(batch); + glDisableClientState(GL_VERTEX_ARRAY); - + glDisableClientState(GL_COLOR_ARRAY); + glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } -void GeometryCache::renderQuad(const glm::vec2& minCorner, const glm::vec2& maxCorner, int id) { - - bool registeredQuad = (id != UNKNOWN_ID); +void GeometryCache::renderQuad(const glm::vec2& minCorner, const glm::vec2& maxCorner, const glm::vec4& color, int id) { + bool registered = (id != UNKNOWN_ID); Vec2Pair key(minCorner, maxCorner); - VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[id] : _quad2DVBOs[key]; - + BatchItemDetails& details = registered ? _registeredQuad2D[id] : _quad2D[key]; + // if this is a registered quad, and we have buffers, then check to see if the geometry changed and rebuild if needed - if (registeredQuad && vbo.first != 0) { + if (registered && details.isCreated) { Vec2Pair& lastKey = _lastRegisteredQuad2D[id]; if (lastKey != key) { - glDeleteBuffers(1, &vbo.first); - glDeleteBuffers(1, &vbo.second); - vbo.first = vbo.second = 0; + details.clear(); + _lastRegisteredQuad2D[id] = key; #ifdef WANT_DEBUG - qDebug() << "renderQuad() vec2... RELEASING REGISTERED QUAD"; + qDebug() << "renderQuad() 2D ... RELEASING REGISTERED"; #endif // def WANT_DEBUG } #ifdef WANT_DEBUG else { - qDebug() << "renderQuad() vec2... REUSING PREVIOUSLY REGISTERED QUAD"; + qDebug() << "renderQuad() 2D ... REUSING PREVIOUSLY REGISTERED"; } #endif // def WANT_DEBUG } - const int FLOATS_PER_VERTEX = 2; - const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); + const int FLOATS_PER_VERTEX = 2; // vertices const int vertices = 4; - const int indices = 4; - if (vbo.first == 0) { - _lastRegisteredQuad2D[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, 2, 3}; - vertex[0] = minCorner.x; - vertex[1] = minCorner.y; - vertex[2] = maxCorner.x; - vertex[3] = minCorner.y; - vertex[4] = maxCorner.x; - vertex[5] = maxCorner.y; - vertex[6] = minCorner.x; - vertex[7] = maxCorner.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; - - #ifdef WANT_DEBUG - if (id == UNKNOWN_ID) { - qDebug() << "new quad VBO made -- _quad2DVBOs.size():" << _quad2DVBOs.size(); - } else { - qDebug() << "new registered quad VBO made -- _registeredQuadVBOs.size():" << _registeredQuadVBOs.size(); - } - #endif + if (!details.isCreated) { + + details.isCreated = true; + details.vertices = vertices; + details.vertexSize = FLOATS_PER_VERTEX; + + 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()); + + details.verticesBuffer = verticesBuffer; + details.colorBuffer = colorBuffer; + details.streamFormat = streamFormat; + details.stream = stream; - } else { - glBindBuffer(GL_ARRAY_BUFFER, vbo.first); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); + details.streamFormat->setAttribute(gpu::Stream::POSITION, 0, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::XYZ), 0); + details.streamFormat->setAttribute(gpu::Stream::COLOR, 1, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + + details.stream->addBuffer(details.verticesBuffer, 0, details.streamFormat->getChannels().at(0)._stride); + details.stream->addBuffer(details.colorBuffer, 0, details.streamFormat->getChannels().at(1)._stride); + + + float vertexBuffer[vertices * FLOATS_PER_VERTEX] = { + minCorner.x, minCorner.y, + maxCorner.x, minCorner.y, + maxCorner.x, maxCorner.y, + minCorner.x, maxCorner.y }; + + const int NUM_COLOR_SCALARS_PER_QUAD = 4; + int compactColor = ((int(color.x * 255.0f) & 0xFF)) | + ((int(color.y * 255.0f) & 0xFF) << 8) | + ((int(color.z * 255.0f) & 0xFF) << 16) | + ((int(color.w * 255.0f) & 0xFF) << 24); + int colors[NUM_COLOR_SCALARS_PER_QUAD] = { compactColor, compactColor, compactColor, compactColor }; + + + details.verticesBuffer->append(sizeof(vertexBuffer), (gpu::Buffer::Byte*) vertexBuffer); + details.colorBuffer->append(sizeof(colors), (gpu::Buffer::Byte*) colors); } - glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(FLOATS_PER_VERTEX, GL_FLOAT, FLOATS_PER_VERTEX * sizeof(float), 0); - glDrawRangeElementsEXT(GL_QUADS, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); + + gpu::Batch batch; + + batch.setInputFormat(details.streamFormat); + batch.setInputStream(0, *details.stream); + batch.draw(gpu::QUADS, 4, 0); + + gpu::GLBackend::renderBatch(batch); + glDisableClientState(GL_VERTEX_ARRAY); - + glDisableClientState(GL_COLOR_ARRAY); + glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } - void GeometryCache::renderQuad(const glm::vec2& minCorner, const glm::vec2& maxCorner, - const glm::vec2& texCoordMinCorner, const glm::vec2& texCoordMaxCorner, int id) { + const glm::vec2& texCoordMinCorner, const glm::vec2& texCoordMaxCorner, + const glm::vec4& color, int id) { - bool registeredQuad = (id != UNKNOWN_ID); + bool registered = (id != UNKNOWN_ID); Vec2PairPair key(Vec2Pair(minCorner, maxCorner), Vec2Pair(texCoordMinCorner, texCoordMaxCorner)); - VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[id] : _quad2DTextureVBOs[key]; - + BatchItemDetails& details = registered ? _registeredQuad2DTextures[id] : _quad2DTextures[key]; + // if this is a registered quad, and we have buffers, then check to see if the geometry changed and rebuild if needed - if (registeredQuad && vbo.first != 0) { + if (registered && details.isCreated) { Vec2PairPair& lastKey = _lastRegisteredQuad2DTexture[id]; if (lastKey != key) { - glDeleteBuffers(1, &vbo.first); - glDeleteBuffers(1, &vbo.second); - vbo.first = vbo.second = 0; + details.clear(); + _lastRegisteredQuad2DTexture[id] = key; #ifdef WANT_DEBUG - qDebug() << "renderQuad() vec2 + texture... RELEASING REGISTERED QUAD"; + qDebug() << "renderQuad() 2D+texture ... RELEASING REGISTERED"; #endif // def WANT_DEBUG } #ifdef WANT_DEBUG else { - qDebug() << "renderQuad() vec2 + texture... REUSING PREVIOUSLY REGISTERED QUAD"; + qDebug() << "renderQuad() 2D+texture ... REUSING PREVIOUSLY REGISTERED"; } #endif // def WANT_DEBUG } const int FLOATS_PER_VERTEX = 2 * 2; // text coords & vertices - const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); const int vertices = 4; - const int indices = 4; - if (vbo.first == 0) { - _lastRegisteredQuad2DTexture[id] = key; - int vertexPoints = vertices * FLOATS_PER_VERTEX; - GLfloat* vertexData = new GLfloat[vertexPoints]; // text coords & vertices - GLfloat* vertex = vertexData; - static GLubyte cannonicalIndices[indices] = {0, 1, 2, 3}; - int v = 0; + const int NUM_POS_COORDS = 2; + const int VERTEX_TEXCOORD_OFFSET = NUM_POS_COORDS * sizeof(float); - vertex[v++] = minCorner.x; - vertex[v++] = minCorner.y; - vertex[v++] = texCoordMinCorner.x; - vertex[v++] = texCoordMinCorner.y; - - vertex[v++] = maxCorner.x; - vertex[v++] = minCorner.y; - vertex[v++] = texCoordMaxCorner.x; - vertex[v++] = texCoordMinCorner.y; - - vertex[v++] = maxCorner.x; - vertex[v++] = maxCorner.y; - vertex[v++] = texCoordMaxCorner.x; - vertex[v++] = texCoordMaxCorner.y; - - vertex[v++] = minCorner.x; - vertex[v++] = maxCorner.y; - vertex[v++] = texCoordMinCorner.x; - vertex[v++] = texCoordMaxCorner.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; - - #ifdef WANT_DEBUG - if (id == UNKNOWN_ID) { - qDebug() << "new quad + texture VBO made -- _quad2DTextureVBOs.size():" << _quad2DTextureVBOs.size(); - } else { - qDebug() << "new registered quad VBO made -- _registeredQuadVBOs.size():" << _registeredQuadVBOs.size(); - } - #endif - } else { - glBindBuffer(GL_ARRAY_BUFFER, vbo.first); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); + if (!details.isCreated) { + + details.isCreated = true; + details.vertices = vertices; + details.vertexSize = FLOATS_PER_VERTEX; + + 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()); + + details.verticesBuffer = verticesBuffer; + details.colorBuffer = colorBuffer; + details.streamFormat = streamFormat; + details.stream = stream; + + details.streamFormat->setAttribute(gpu::Stream::POSITION, 0, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::XYZ), 0); + details.streamFormat->setAttribute(gpu::Stream::TEXCOORD, 0, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::UV), VERTEX_TEXCOORD_OFFSET); + details.streamFormat->setAttribute(gpu::Stream::COLOR, 1, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + + details.stream->addBuffer(details.verticesBuffer, 0, details.streamFormat->getChannels().at(0)._stride); + details.stream->addBuffer(details.colorBuffer, 0, details.streamFormat->getChannels().at(1)._stride); + + + float vertexBuffer[vertices * FLOATS_PER_VERTEX] = { + minCorner.x, minCorner.y, texCoordMinCorner.x, texCoordMinCorner.y, + maxCorner.x, minCorner.y, texCoordMaxCorner.x, texCoordMinCorner.y, + maxCorner.x, maxCorner.y, texCoordMaxCorner.x, texCoordMaxCorner.y, + minCorner.x, maxCorner.y, texCoordMinCorner.x, texCoordMaxCorner.y }; + + + const int NUM_COLOR_SCALARS_PER_QUAD = 4; + int compactColor = ((int(color.x * 255.0f) & 0xFF)) | + ((int(color.y * 255.0f) & 0xFF) << 8) | + ((int(color.z * 255.0f) & 0xFF) << 16) | + ((int(color.w * 255.0f) & 0xFF) << 24); + int colors[NUM_COLOR_SCALARS_PER_QUAD] = { compactColor, compactColor, compactColor, compactColor }; + + + details.verticesBuffer->append(sizeof(vertexBuffer), (gpu::Buffer::Byte*) vertexBuffer); + details.colorBuffer->append(sizeof(colors), (gpu::Buffer::Byte*) colors); } - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glVertexPointer(2, GL_FLOAT, NUM_BYTES_PER_VERTEX, 0); - glTexCoordPointer(2, GL_FLOAT, NUM_BYTES_PER_VERTEX, (const void *)(2 * sizeof(float))); + gpu::Batch batch; - glDrawRangeElementsEXT(GL_QUADS, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); + glEnable(GL_TEXTURE_2D); + //glBindTexture(GL_TEXTURE_2D, _currentTextureID); // this is quad specific... + + batch.setInputFormat(details.streamFormat); + batch.setInputStream(0, *details.stream); + batch.draw(gpu::QUADS, 4, 0); + + gpu::GLBackend::renderBatch(batch); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); - + glDisableClientState(GL_COLOR_ARRAY); + glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glBindTexture(GL_TEXTURE_2D, 0); + glDisable(GL_TEXTURE_2D); } -void GeometryCache::renderQuad(const glm::vec3& minCorner, const glm::vec3& maxCorner, int id) { - - bool registeredQuad = (id != UNKNOWN_ID); +void GeometryCache::renderQuad(const glm::vec3& minCorner, const glm::vec3& maxCorner, const glm::vec4& color, int id) { + bool registered = (id != UNKNOWN_ID); Vec3Pair key(minCorner, maxCorner); - VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[id] : _quad3DVBOs[key]; - + BatchItemDetails& details = registered ? _registeredQuad3D[id] : _quad3D[key]; + // if this is a registered quad, and we have buffers, then check to see if the geometry changed and rebuild if needed - if (registeredQuad && vbo.first != 0) { + if (registered && details.isCreated) { Vec3Pair& lastKey = _lastRegisteredQuad3D[id]; if (lastKey != key) { - glDeleteBuffers(1, &vbo.first); - glDeleteBuffers(1, &vbo.second); - vbo.first = vbo.second = 0; + details.clear(); + _lastRegisteredQuad3D[id] = key; #ifdef WANT_DEBUG - qDebug() << "renderQuad() vec3... RELEASING REGISTERED QUAD"; + qDebug() << "renderQuad() 3D ... RELEASING REGISTERED"; #endif // def WANT_DEBUG } #ifdef WANT_DEBUG else { - qDebug() << "renderQuad() vec3... REUSING PREVIOUSLY REGISTERED QUAD"; + qDebug() << "renderQuad() 3D ... REUSING PREVIOUSLY REGISTERED"; } #endif // def WANT_DEBUG } - const int FLOATS_PER_VERTEX = 3; - const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); + const int FLOATS_PER_VERTEX = 3; // vertices const int vertices = 4; - const int indices = 4; - if (vbo.first == 0) { - _lastRegisteredQuad3D[id] = key; - int vertexPoints = vertices * FLOATS_PER_VERTEX; - GLfloat* vertexData = new GLfloat[vertexPoints]; // only vertices - GLfloat* vertex = vertexData; - static GLubyte cannonicalIndices[indices] = {0, 1, 2, 3}; - int v = 0; - vertex[v++] = minCorner.x; - vertex[v++] = minCorner.y; - vertex[v++] = minCorner.z; + if (!details.isCreated) { - vertex[v++] = maxCorner.x; - vertex[v++] = minCorner.y; - vertex[v++] = minCorner.z; + details.isCreated = true; + details.vertices = vertices; + details.vertexSize = FLOATS_PER_VERTEX; - vertex[v++] = maxCorner.x; - vertex[v++] = maxCorner.y; - vertex[v++] = maxCorner.z; + 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()); - vertex[v++] = minCorner.x; - vertex[v++] = maxCorner.y; - vertex[v++] = maxCorner.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; - - #ifdef WANT_DEBUG - if (id == UNKNOWN_ID) { - qDebug() << "new quad VBO made -- _quad3DVBOs.size():" << _quad3DVBOs.size(); - } else { - qDebug() << "new registered quad VBO made -- _registeredQuadVBOs.size():" << _registeredQuadVBOs.size(); - } - #endif + details.verticesBuffer = verticesBuffer; + details.colorBuffer = colorBuffer; + details.streamFormat = streamFormat; + details.stream = stream; - } else { - glBindBuffer(GL_ARRAY_BUFFER, vbo.first); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); + details.streamFormat->setAttribute(gpu::Stream::POSITION, 0, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0); + details.streamFormat->setAttribute(gpu::Stream::COLOR, 1, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + + details.stream->addBuffer(details.verticesBuffer, 0, details.streamFormat->getChannels().at(0)._stride); + details.stream->addBuffer(details.colorBuffer, 0, details.streamFormat->getChannels().at(1)._stride); + + + float vertexBuffer[vertices * FLOATS_PER_VERTEX] = { + minCorner.x, minCorner.y, minCorner.z, + maxCorner.x, minCorner.y, minCorner.z, + maxCorner.x, maxCorner.y, maxCorner.z, + minCorner.x, maxCorner.y, maxCorner.z }; + + const int NUM_COLOR_SCALARS_PER_QUAD = 4; + int compactColor = ((int(color.x * 255.0f) & 0xFF)) | + ((int(color.y * 255.0f) & 0xFF) << 8) | + ((int(color.z * 255.0f) & 0xFF) << 16) | + ((int(color.w * 255.0f) & 0xFF) << 24); + int colors[NUM_COLOR_SCALARS_PER_QUAD] = { compactColor, compactColor, compactColor, compactColor }; + + + details.verticesBuffer->append(sizeof(vertexBuffer), (gpu::Buffer::Byte*) vertexBuffer); + details.colorBuffer->append(sizeof(colors), (gpu::Buffer::Byte*) colors); } - glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(FLOATS_PER_VERTEX, GL_FLOAT, FLOATS_PER_VERTEX * sizeof(float), 0); - glDrawRangeElementsEXT(GL_QUADS, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); - glDisableClientState(GL_VERTEX_ARRAY); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); -} + gpu::Batch batch; + + batch.setInputFormat(details.streamFormat); + batch.setInputStream(0, *details.stream); + batch.draw(gpu::QUADS, 4, 0); + + gpu::GLBackend::renderBatch(batch); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); + + glBindBuffer(GL_ARRAY_BUFFER, 0); +} void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottomLeft, const glm::vec3& bottomRight, const glm::vec3& topRight, const glm::vec2& texCoordTopLeft, const glm::vec2& texCoordBottomLeft, - const glm::vec2& texCoordBottomRight, const glm::vec2& texCoordTopRight, int id) { + const glm::vec2& texCoordBottomRight, const glm::vec2& texCoordTopRight, + const glm::vec4& color, int id) { #ifdef WANT_DEBUG qDebug() << "renderQuad() vec3 + texture VBO..."; @@ -1216,117 +1264,113 @@ void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottom qDebug() << " topRight:" << topRight; qDebug() << " texCoordTopLeft:" << texCoordTopLeft; qDebug() << " texCoordBottomRight:" << texCoordBottomRight; + qDebug() << " color:" << color; #endif //def WANT_DEBUG - bool registeredQuad = (id != UNKNOWN_ID); - Vec3PairVec2Pair key(Vec3Pair(topLeft, bottomRight), Vec2Pair(texCoordTopLeft, texCoordBottomRight)); - VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[id] : _quad3DTextureVBOs[key]; - + bool registered = (id != UNKNOWN_ID); + Vec3PairVec4Pair key(Vec3Pair(topLeft, bottomRight), + Vec4Pair(glm::vec4(texCoordTopLeft.x,texCoordTopLeft.y,texCoordBottomRight.x,texCoordBottomRight.y), + color)); + + BatchItemDetails& details = registered ? _registeredQuad3DTextures[id] : _quad3DTextures[key]; + // if this is a registered quad, and we have buffers, then check to see if the geometry changed and rebuild if needed - if (registeredQuad && vbo.first != 0) { - Vec3PairVec2Pair& lastKey = _lastRegisteredQuad3DTexture[id]; + if (registered && details.isCreated) { + Vec3PairVec4Pair& lastKey = _lastRegisteredQuad3DTexture[id]; if (lastKey != key) { - glDeleteBuffers(1, &vbo.first); - glDeleteBuffers(1, &vbo.second); - vbo.first = vbo.second = 0; + details.clear(); + _lastRegisteredQuad3DTexture[id] = key; #ifdef WANT_DEBUG - qDebug() << "renderQuad() vec3 + texture VBO... RELEASING REGISTERED QUAD"; + qDebug() << "renderQuad() 3D+texture ... RELEASING REGISTERED"; #endif // def WANT_DEBUG } #ifdef WANT_DEBUG else { - qDebug() << "renderQuad() vec3 + texture... REUSING PREVIOUSLY REGISTERED QUAD"; + qDebug() << "renderQuad() 3D+texture ... REUSING PREVIOUSLY REGISTERED"; } #endif // def WANT_DEBUG } - - const int FLOATS_PER_VERTEX = 5; // text coords & vertices - const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); + + const int FLOATS_PER_VERTEX = 3 + 2; // 3d vertices + text coords const int vertices = 4; - const int indices = 4; - if (vbo.first == 0) { - _lastRegisteredQuad3DTexture[id] = key; - int vertexPoints = vertices * FLOATS_PER_VERTEX; - GLfloat* vertexData = new GLfloat[vertexPoints]; // text coords & vertices - GLfloat* vertex = vertexData; - static GLubyte cannonicalIndices[indices] = {0, 1, 2, 3}; - int v = 0; + const int NUM_POS_COORDS = 3; + const int VERTEX_TEXCOORD_OFFSET = NUM_POS_COORDS * sizeof(float); - vertex[v++] = topLeft.x; - vertex[v++] = topLeft.y; - vertex[v++] = topLeft.z; - vertex[v++] = texCoordTopLeft.x; - vertex[v++] = texCoordTopLeft.y; - - vertex[v++] = bottomLeft.x; - vertex[v++] = bottomLeft.y; - vertex[v++] = bottomLeft.z; - vertex[v++] = texCoordBottomLeft.x; - vertex[v++] = texCoordBottomLeft.y; - - vertex[v++] = bottomRight.x; - vertex[v++] = bottomRight.y; - vertex[v++] = bottomRight.z; - vertex[v++] = texCoordBottomRight.x; - vertex[v++] = texCoordBottomRight.y; - - vertex[v++] = topRight.x; - vertex[v++] = topRight.y; - vertex[v++] = topRight.z; - vertex[v++] = texCoordTopRight.x; - vertex[v++] = texCoordTopRight.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 (!details.isCreated) { - #ifdef WANT_DEBUG - if (id == UNKNOWN_ID) { - qDebug() << " _quad3DTextureVBOs.size():" << _quad3DTextureVBOs.size(); - } else { - qDebug() << "new registered quad VBO made -- _registeredQuadVBOs.size():" << _registeredQuadVBOs.size(); - } - #endif - } else { - glBindBuffer(GL_ARRAY_BUFFER, vbo.first); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); + details.isCreated = true; + details.vertices = vertices; + details.vertexSize = FLOATS_PER_VERTEX; // NOTE: this isn't used for BatchItemDetails maybe we can get rid of it + + 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()); + + details.verticesBuffer = verticesBuffer; + details.colorBuffer = colorBuffer; + details.streamFormat = streamFormat; + details.stream = stream; + + details.streamFormat->setAttribute(gpu::Stream::POSITION, 0, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0); + details.streamFormat->setAttribute(gpu::Stream::TEXCOORD, 0, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::UV), VERTEX_TEXCOORD_OFFSET); + details.streamFormat->setAttribute(gpu::Stream::COLOR, 1, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + + details.stream->addBuffer(details.verticesBuffer, 0, details.streamFormat->getChannels().at(0)._stride); + details.stream->addBuffer(details.colorBuffer, 0, details.streamFormat->getChannels().at(1)._stride); + + + float vertexBuffer[vertices * FLOATS_PER_VERTEX] = { + topLeft.x, topLeft.y, topLeft.z, texCoordTopLeft.x, texCoordTopLeft.y, + bottomLeft.x, bottomLeft.y, bottomLeft.z, texCoordBottomLeft.x, texCoordBottomLeft.y, + bottomRight.x, bottomRight.y, bottomRight.z, texCoordBottomRight.x, texCoordBottomRight.y, + topRight.x, topRight.y, topRight.z, texCoordTopRight.x, texCoordTopRight.y, + }; + + const int NUM_COLOR_SCALARS_PER_QUAD = 4; + int compactColor = ((int(color.x * 255.0f) & 0xFF)) | + ((int(color.y * 255.0f) & 0xFF) << 8) | + ((int(color.z * 255.0f) & 0xFF) << 16) | + ((int(color.w * 255.0f) & 0xFF) << 24); + int colors[NUM_COLOR_SCALARS_PER_QUAD] = { compactColor, compactColor, compactColor, compactColor }; + + details.verticesBuffer->append(sizeof(vertexBuffer), (gpu::Buffer::Byte*) vertexBuffer); + details.colorBuffer->append(sizeof(colors), (gpu::Buffer::Byte*) colors); } - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glVertexPointer(3, GL_FLOAT, NUM_BYTES_PER_VERTEX, 0); - glTexCoordPointer(2, GL_FLOAT, NUM_BYTES_PER_VERTEX, (const void *)(3 * sizeof(float))); + gpu::Batch batch; - glDrawRangeElementsEXT(GL_QUADS, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); + glEnable(GL_TEXTURE_2D); + //glBindTexture(GL_TEXTURE_2D, _currentTextureID); // this is quad specific... + + batch.setInputFormat(details.streamFormat); + batch.setInputStream(0, *details.stream); + batch.draw(gpu::QUADS, 4, 0); + + gpu::GLBackend::renderBatch(batch); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); - + glDisableClientState(GL_COLOR_ARRAY); + glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + // TODO: The callers of this method (renderMagnifier and renderReticle) assume that we won't disable an unbind + // the texture after rendering. I'm not sure if this is correct in general but it's currently required for the + // oculus overlay to work. + //glBindTexture(GL_TEXTURE_2D, 0); + //glDisable(GL_TEXTURE_2D); } -void GeometryCache::renderDashedLine(const glm::vec3& start, const glm::vec3& end, int id) { +void GeometryCache::renderDashedLine(const glm::vec3& start, const glm::vec3& end, const glm::vec4& color, int id) { bool registered = (id != UNKNOWN_ID); - Vec3Pair key(start, end); - BufferDetails& details = registered ? _registeredDashedLines[id] : _dashedLines[key]; + Vec3PairVec2Pair key(Vec3Pair(start, end), Vec2Pair(glm::vec2(color.x, color.y), glm::vec2(color.z, color.w))); + BatchItemDetails& details = registered ? _registeredDashedLines[id] : _dashedLines[key]; // if this is a registered , and we have buffers, then check to see if the geometry changed and rebuild if needed - if (registered && details.buffer.isCreated()) { + if (registered && details.isCreated) { if (_lastRegisteredDashedLines[id] != key) { - details.buffer.destroy(); + details.clear(); _lastRegisteredDashedLines[id] = key; #ifdef WANT_DEBUG qDebug() << "renderDashedLine()... RELEASING REGISTERED"; @@ -1334,8 +1378,13 @@ void GeometryCache::renderDashedLine(const glm::vec3& start, const glm::vec3& en } } - if (!details.buffer.isCreated()) { - + if (!details.isCreated) { + + int compactColor = ((int(color.x * 255.0f) & 0xFF)) | + ((int(color.y * 255.0f) & 0xFF) << 8) | + ((int(color.z * 255.0f) & 0xFF) << 16) | + ((int(color.w * 255.0f) & 0xFF) << 24); + // draw each line segment with appropriate gaps const float DASH_LENGTH = 0.05f; const float GAP_LENGTH = 0.025f; @@ -1351,6 +1400,26 @@ void GeometryCache::renderDashedLine(const glm::vec3& start, const glm::vec3& en const int FLOATS_PER_VERTEX = 3; details.vertices = (segmentCountFloor + 1) * 2; details.vertexSize = FLOATS_PER_VERTEX; + details.isCreated = true; + + 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()); + + details.verticesBuffer = verticesBuffer; + details.colorBuffer = colorBuffer; + details.streamFormat = streamFormat; + details.stream = stream; + + details.streamFormat->setAttribute(gpu::Stream::POSITION, 0, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0); + details.streamFormat->setAttribute(gpu::Stream::COLOR, 1, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + + details.stream->addBuffer(details.verticesBuffer, 0, details.streamFormat->getChannels().at(0)._stride); + details.stream->addBuffer(details.colorBuffer, 0, details.streamFormat->getChannels().at(1)._stride); + + int* colorData = new int[details.vertices]; + int* colorDataAt = colorData; GLfloat* vertexData = new GLfloat[details.vertices * FLOATS_PER_VERTEX]; GLfloat* vertex = vertexData; @@ -1359,27 +1428,30 @@ void GeometryCache::renderDashedLine(const glm::vec3& start, const glm::vec3& en *(vertex++) = point.x; *(vertex++) = point.y; *(vertex++) = point.z; + *(colorDataAt++) = compactColor; for (int i = 0; i < segmentCountFloor; i++) { point += dashVector; *(vertex++) = point.x; *(vertex++) = point.y; *(vertex++) = point.z; + *(colorDataAt++) = compactColor; point += gapVector; *(vertex++) = point.x; *(vertex++) = point.y; *(vertex++) = point.z; + *(colorDataAt++) = compactColor; } *(vertex++) = end.x; *(vertex++) = end.y; *(vertex++) = end.z; + *(colorDataAt++) = compactColor; - details.buffer.create(); - details.buffer.setUsagePattern(QOpenGLBuffer::StaticDraw); - details.buffer.bind(); - details.buffer.allocate(vertexData, details.vertices * FLOATS_PER_VERTEX * sizeof(GLfloat)); + details.verticesBuffer->append(sizeof(GLfloat) * FLOATS_PER_VERTEX * details.vertices, (gpu::Buffer::Byte*) vertexData); + details.colorBuffer->append(sizeof(int) * details.vertices, (gpu::Buffer::Byte*) colorData); delete[] vertexData; + delete[] colorData; #ifdef WANT_DEBUG if (registered) { @@ -1388,29 +1460,96 @@ void GeometryCache::renderDashedLine(const glm::vec3& start, const glm::vec3& en qDebug() << "new dashed lines buffer made -- _dashedLines:" << _dashedLines.size(); } #endif - } else { - details.buffer.bind(); } - glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(details.vertexSize, GL_FLOAT, 0, 0); - glDrawArrays(GL_LINES, 0, details.vertices); + gpu::Batch batch; + + batch.setInputFormat(details.streamFormat); + batch.setInputStream(0, *details.stream); + batch.draw(gpu::LINES, details.vertices, 0); + + gpu::GLBackend::renderBatch(batch); + glDisableClientState(GL_VERTEX_ARRAY); - details.buffer.release(); + glDisableClientState(GL_COLOR_ARRAY); + + glBindBuffer(GL_ARRAY_BUFFER, 0); } -void GeometryCache::renderLine(const glm::vec3& p1, const glm::vec3& p2, int id) { - bool registeredLine = (id != UNKNOWN_ID); + +int GeometryCache::BatchItemDetails::population = 0; + +GeometryCache::BatchItemDetails::BatchItemDetails() : + verticesBuffer(NULL), + colorBuffer(NULL), + streamFormat(NULL), + stream(NULL), + vertices(0), + vertexSize(0), + isCreated(false) +{ + population++; + #ifdef WANT_DEBUG + qDebug() << "BatchItemDetails()... population:" << population << "**********************************"; + #endif +} + +GeometryCache::BatchItemDetails::BatchItemDetails(const GeometryCache::BatchItemDetails& other) : + verticesBuffer(other.verticesBuffer), + colorBuffer(other.colorBuffer), + streamFormat(other.streamFormat), + stream(other.stream), + vertices(other.vertices), + vertexSize(other.vertexSize), + isCreated(other.isCreated) +{ + population++; + #ifdef WANT_DEBUG + qDebug() << "BatchItemDetails()... population:" << population << "**********************************"; + #endif +} + +GeometryCache::BatchItemDetails::~BatchItemDetails() { + population--; + clear(); + #ifdef WANT_DEBUG + qDebug() << "~BatchItemDetails()... population:" << population << "**********************************"; + #endif +} + +void GeometryCache::BatchItemDetails::clear() { + isCreated = false; + verticesBuffer.clear(); + colorBuffer.clear(); + streamFormat.clear(); + stream.clear(); +} + +void GeometryCache::renderLine(const glm::vec3& p1, const glm::vec3& p2, + const glm::vec4& color1, const glm::vec4& color2, int id) { + + bool registered = (id != UNKNOWN_ID); 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 (registeredLine && vbo.first != 0) { + if (registered && details.isCreated) { Vec3Pair& lastKey = _lastRegisteredLine3D[id]; if (lastKey != key) { - glDeleteBuffers(1, &vbo.first); - glDeleteBuffers(1, &vbo.second); - vbo.first = vbo.second = 0; + details.clear(); + _lastRegisteredLine3D[id] = key; #ifdef WANT_DEBUG qDebug() << "renderLine() 3D ... RELEASING REGISTERED line"; #endif // def WANT_DEBUG @@ -1423,46 +1562,38 @@ void GeometryCache::renderLine(const glm::vec3& p1, const glm::vec3& p2, int id) } 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; + if (!details.isCreated) { - 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; + details.isCreated = true; + details.vertices = vertices; + details.vertexSize = FLOATS_PER_VERTEX; - // p1 - vertex[vertexPoint++] = p1.x; - vertex[vertexPoint++] = p1.y; - vertex[vertexPoint++] = p1.z; + 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()); - // p2 - vertex[vertexPoint++] = p2.x; - vertex[vertexPoint++] = p2.y; - vertex[vertexPoint++] = p2.z; - + details.verticesBuffer = verticesBuffer; + details.colorBuffer = colorBuffer; + details.streamFormat = streamFormat; + details.stream = stream; + + details.streamFormat->setAttribute(gpu::Stream::POSITION, 0, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0); + details.streamFormat->setAttribute(gpu::Stream::COLOR, 1, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + + details.stream->addBuffer(details.verticesBuffer, 0, details.streamFormat->getChannels().at(0)._stride); + details.stream->addBuffer(details.colorBuffer, 0, details.streamFormat->getChannels().at(1)._stride); + + + float vertexBuffer[vertices * FLOATS_PER_VERTEX] = { p1.x, p1.y, p1.z, p2.x, p2.y, p2.z }; + + const int NUM_COLOR_SCALARS = 2; + int colors[NUM_COLOR_SCALARS] = { compactColor1, compactColor2 }; + + details.verticesBuffer->append(sizeof(vertexBuffer), (gpu::Buffer::Byte*) vertexBuffer); + details.colorBuffer->append(sizeof(colors), (gpu::Buffer::Byte*) colors); - 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; - #ifdef WANT_DEBUG if (id == UNKNOWN_ID) { qDebug() << "new renderLine() 3D VBO made -- _line3DVBOs.size():" << _line3DVBOs.size(); @@ -1470,101 +1601,112 @@ void GeometryCache::renderLine(const glm::vec3& p1, const glm::vec3& p2, int id) 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); + + 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_ELEMENT_ARRAY_BUFFER, 0); } -void GeometryCache::renderLine(const glm::vec2& p1, const glm::vec2& p2, int id) { - bool registeredLine = (id != UNKNOWN_ID); +void GeometryCache::renderLine(const glm::vec2& p1, const glm::vec2& p2, + const glm::vec4& color1, const glm::vec4& color2, int id) { + + bool registered = (id != UNKNOWN_ID); Vec2Pair key(p1, p2); - VerticesIndices& vbo = registeredLine ? _registeredLine2DVBOs[id] : _line2DVBOs[key]; + + BatchItemDetails& details = registered ? _registeredLine2DVBOs[id] : _line2DVBOs[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 (registeredLine && vbo.first != 0) { + if (registered && details.isCreated) { Vec2Pair& lastKey = _lastRegisteredLine2D[id]; if (lastKey != key) { - glDeleteBuffers(1, &vbo.first); - glDeleteBuffers(1, &vbo.second); - vbo.first = vbo.second = 0; + details.clear(); + _lastRegisteredLine2D[id] = key; #ifdef WANT_DEBUG - qDebug() << "renderLine() 2D... RELEASING REGISTERED line"; + qDebug() << "renderLine() 2D ... RELEASING REGISTERED line"; #endif // def WANT_DEBUG } #ifdef WANT_DEBUG else { - qDebug() << "renderLine() 2D... REUSING PREVIOUSLY REGISTERED line"; + 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; + if (!details.isCreated) { - 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; + details.isCreated = true; + details.vertices = vertices; + details.vertexSize = FLOATS_PER_VERTEX; - // p1 - vertex[vertexPoint++] = p1.x; - vertex[vertexPoint++] = p1.y; + 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()); - // p2 - vertex[vertexPoint++] = p2.x; - vertex[vertexPoint++] = p2.y; - + details.verticesBuffer = verticesBuffer; + details.colorBuffer = colorBuffer; + details.streamFormat = streamFormat; + details.stream = stream; + + details.streamFormat->setAttribute(gpu::Stream::POSITION, 0, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0); + details.streamFormat->setAttribute(gpu::Stream::COLOR, 1, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + + details.stream->addBuffer(details.verticesBuffer, 0, details.streamFormat->getChannels().at(0)._stride); + details.stream->addBuffer(details.colorBuffer, 0, details.streamFormat->getChannels().at(1)._stride); + + + float vertexBuffer[vertices * FLOATS_PER_VERTEX] = { p1.x, p1.y, p2.x, p2.y }; + + const int NUM_COLOR_SCALARS = 2; + int colors[NUM_COLOR_SCALARS] = { compactColor1, compactColor2 }; + + details.verticesBuffer->append(sizeof(vertexBuffer), (gpu::Buffer::Byte*) vertexBuffer); + details.colorBuffer->append(sizeof(colors), (gpu::Buffer::Byte*) colors); - 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; - #ifdef WANT_DEBUG if (id == UNKNOWN_ID) { - qDebug() << "new renderLine() 2D VBO made -- _line2DVBOs.size():" << _line2DVBOs.size(); + qDebug() << "new renderLine() 2D VBO made -- _line3DVBOs.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); + + 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_ELEMENT_ARRAY_BUFFER, 0); } diff --git a/libraries/render-utils/src/GeometryCache.h b/libraries/render-utils/src/GeometryCache.h index dc367b1f8c..300e68aadd 100644 --- a/libraries/render-utils/src/GeometryCache.h +++ b/libraries/render-utils/src/GeometryCache.h @@ -25,17 +25,24 @@ #include -#include "gpu/Stream.h" +#include +#include + class NetworkGeometry; class NetworkMesh; class NetworkTexture; +typedef glm::vec3 Vec3Key; + typedef QPair Vec2Pair; typedef QPair Vec2PairPair; typedef QPair Vec3Pair; +typedef QPair Vec4Pair; typedef QPair Vec3PairVec2Pair; +typedef QPair Vec3PairVec4Pair; +typedef QPair Vec4PairVec4Pair; inline uint qHash(const glm::vec2& v, uint seed) { // multiply by prime numbers greater than the possible size @@ -47,6 +54,11 @@ inline uint qHash(const Vec2Pair& v, uint seed) { return qHash(v.first.x + 5009 * v.first.y + 5011 * v.second.x + 5021 * v.second.y, seed); } +inline uint qHash(const glm::vec4& v, uint seed) { + // multiply by prime numbers greater than the possible size + return qHash(v.x + 5009 * v.y + 5011 * v.z + 5021 * v.w, seed); +} + inline uint qHash(const Vec2PairPair& v, uint seed) { // multiply by prime numbers greater than the possible size return qHash(v.first.first.x + 5009 * v.first.first.y @@ -55,17 +67,18 @@ inline uint qHash(const Vec2PairPair& v, uint seed) { + 5051 * v.second.second.x + 5059 * v.second.second.y, seed); } -inline uint qHash(const glm::vec3& v, uint seed) { - // multiply by prime numbers greater than the possible size - return qHash(v.x + 5009 * v.y + 5011 * v.z, seed); -} - inline uint qHash(const Vec3Pair& v, uint seed) { // multiply by prime numbers greater than the possible size return qHash(v.first.x + 5009 * v.first.y + 5011 * v.first.z + 5021 * v.second.x + 5023 * v.second.y + 5039 * v.second.z, seed); } +inline uint qHash(const Vec4Pair& v, uint seed) { + // multiply by prime numbers greater than the possible size + return qHash(v.first.x + 5009 * v.first.y + 5011 * v.first.z + 5021 * v.first.w + + 5023 * v.second.x + 5039 * v.second.y + 5051 * v.second.z + 5059 * v.second.w , seed); +} + inline uint qHash(const Vec3PairVec2Pair& v, uint seed) { // multiply by prime numbers greater than the possible size return qHash(v.first.first.x + 5009 * v.first.first.y + 5011 * v.first.first.z + @@ -74,6 +87,23 @@ inline uint qHash(const Vec3PairVec2Pair& v, uint seed) { 5077 * v.second.second.x + 5081 * v.second.second.y, seed); } +inline uint qHash(const Vec3PairVec4Pair& v, uint seed) { + // multiply by prime numbers greater than the possible size + return qHash(v.first.first.x + 5009 * v.first.first.y + 5011 * v.first.first.z + + 5023 * v.first.second.x + 5039 * v.first.second.y + 5051 * v.first.second.z + + 5077 * v.second.first.x + 5081 * v.second.first.y + 5087 * v.second.first.z + 5099 * v.second.first.w + + 5101 * v.second.second.x + 5107 * v.second.second.y + 5113 * v.second.second.z + 5119 * v.second.second.w, + seed); +} + +inline uint qHash(const Vec4PairVec4Pair& v, uint seed) { + // multiply by prime numbers greater than the possible size + return qHash(v.first.first.x + 5009 * v.first.first.y + 5011 * v.first.first.z + 5021 * v.first.first.w + + 5023 * v.first.second.x + 5039 * v.first.second.y + 5051 * v.first.second.z + 5059 * v.first.second.w + + 5077 * v.second.first.x + 5081 * v.second.first.y + 5087 * v.second.first.z + 5099 * v.second.first.w + + 5101 * v.second.second.x + 5107 * v.second.second.y + 5113 * v.second.second.z + 5119 * v.second.second.w, + seed); +} /// Stores cached geometry. class GeometryCache : public ResourceCache, public Dependency { @@ -84,40 +114,72 @@ public: int allocateID() { return _nextID++; } static const int UNKNOWN_ID; - void renderHemisphere(int slices, int stacks); - void renderSphere(float radius, int slices, int stacks, bool solid = true); - void renderSquare(int xDivisions, int yDivisions); - void renderHalfCylinder(int slices, int stacks); void renderCone(float base, float height, int slices, int stacks); - void renderGrid(int xDivisions, int yDivisions); - void renderGrid(int x, int y, int width, int height, int rows, int cols, int id = UNKNOWN_ID); - void renderSolidCube(float size); - void renderWireCube(float size); - void renderBevelCornersRect(int x, int y, int width, int height, int bevelDistance, int id = UNKNOWN_ID); - void renderQuad(int x, int y, int width, int height, int id = UNKNOWN_ID) - { renderQuad(glm::vec2(x,y), glm::vec2(x + width, y + height), id); } + void renderSphere(float radius, int slices, int stacks, const glm::vec3& color, bool solid = true) + { renderSphere(radius, slices, stacks, glm::vec4(color, 1.0f), solid); } + + void renderSphere(float radius, int slices, int stacks, const glm::vec4& color, bool solid = true); + void renderGrid(int xDivisions, int yDivisions, const glm::vec4& color); + void renderGrid(int x, int y, int width, int height, int rows, int cols, const glm::vec4& color, int id = UNKNOWN_ID); + void renderSolidCube(float size, const glm::vec4& color); + void renderWireCube(float size, const glm::vec4& color); + void renderBevelCornersRect(int x, int y, int width, int height, int bevelDistance, const glm::vec4& color, int id = UNKNOWN_ID); + + void renderQuad(int x, int y, int width, int height, const glm::vec4& color, int id = UNKNOWN_ID) + { renderQuad(glm::vec2(x,y), glm::vec2(x + width, y + height), color, id); } - void renderQuad(const glm::vec2& minCorner, const glm::vec2& maxCorner, int id = UNKNOWN_ID); + // TODO: I think there's a bug in this version of the renderQuad() that's not correctly rebuilding the vbos + // if the color changes by the corners are the same, as evidenced by the audio meter which should turn white + // when it's clipping + void renderQuad(const glm::vec2& minCorner, const glm::vec2& maxCorner, const glm::vec4& color, int id = UNKNOWN_ID); void renderQuad(const glm::vec2& minCorner, const glm::vec2& maxCorner, - const glm::vec2& texCoordMinCorner, const glm::vec2& texCoordMaxCorner, int id = UNKNOWN_ID); + const glm::vec2& texCoordMinCorner, const glm::vec2& texCoordMaxCorner, + const glm::vec4& color, int id = UNKNOWN_ID); - void renderQuad(const glm::vec3& minCorner, const glm::vec3& maxCorner, int id = UNKNOWN_ID); + void renderQuad(const glm::vec3& minCorner, const glm::vec3& maxCorner, const glm::vec4& color, int id = UNKNOWN_ID); void renderQuad(const glm::vec3& topLeft, const glm::vec3& bottomLeft, const glm::vec3& bottomRight, const glm::vec3& topRight, const glm::vec2& texCoordTopLeft, const glm::vec2& texCoordBottomLeft, - const glm::vec2& texCoordBottomRight, const glm::vec2& texCoordTopRight, int id = UNKNOWN_ID); + const glm::vec2& texCoordBottomRight, const glm::vec2& texCoordTopRight, + const glm::vec4& color, int id = UNKNOWN_ID); - void renderLine(const glm::vec3& p1, const glm::vec3& p2, 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::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 updateVertices(int id, const QVector& points); - void updateVertices(int id, const QVector& points); - void renderVertices(GLenum mode, int 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, const glm::vec4& color, int id = UNKNOWN_ID); + + void renderLine(const glm::vec2& p1, const glm::vec2& p2, const glm::vec3& color, int id = UNKNOWN_ID) + { renderLine(p1, p2, glm::vec4(color, 1.0f), id); } + + void renderLine(const glm::vec2& p1, const glm::vec2& p2, const glm::vec4& color, int id = UNKNOWN_ID) + { renderLine(p1, p2, color, color, id); } + + + void renderLine(const glm::vec2& p1, const glm::vec2& 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::vec2& p1, const glm::vec2& p2, + const glm::vec4& color1, const glm::vec4& color2, int id = UNKNOWN_ID); + + void updateVertices(int id, const QVector& points, const glm::vec4& color); + void updateVertices(int id, const QVector& points, const glm::vec4& color); + void renderVertices(gpu::Primitive primitiveType, int id); /// Loads geometry from the specified URL. /// \param fallback a fallback URL to load if the desired one is unavailable @@ -128,64 +190,91 @@ protected: virtual QSharedPointer createResource(const QUrl& url, const QSharedPointer& fallback, bool delayLoad, const void* extra); - + private: GeometryCache(); virtual ~GeometryCache(); typedef QPair IntPair; typedef QPair VerticesIndices; - - QHash _hemisphereVBOs; - QHash _sphereVBOs; - QHash _squareVBOs; - QHash _halfCylinderVBOs; - QHash _coneVBOs; - QHash _wireCubeVBOs; - QHash _solidCubeVBOs; - QHash _quad2DVBOs; - QHash _quad2DTextureVBOs; - QHash _quad3DVBOs; - QHash _quad3DTextureVBOs; - QHash _registeredQuadVBOs; - int _nextID; - - QHash _lastRegisteredQuad2D; - QHash _lastRegisteredQuad2DTexture; - QHash _lastRegisteredQuad3D; - QHash _lastRegisteredQuad3DTexture; - - QHash _lastRegisteredRect; - QHash _rectVBOs; - QHash _registeredRectVBOs; - - QHash _lastRegisteredLine3D; - QHash _line3DVBOs; - QHash _registeredLine3DVBOs; - - QHash _lastRegisteredLine2D; - QHash _line2DVBOs; - QHash _registeredLine2DVBOs; - struct BufferDetails { QOpenGLBuffer buffer; int vertices; int vertexSize; }; - QHash _registeredVertices; + QHash _cubeVerticies; + QHash _cubeColors; + gpu::BufferPointer _wireCubeIndexBuffer; - QHash _lastRegisteredDashedLines; - QHash _dashedLines; - QHash _registeredDashedLines; + QHash _solidCubeVerticies; + QHash _solidCubeColors; + gpu::BufferPointer _solidCubeIndexBuffer; + + class BatchItemDetails { + public: + static int population; + gpu::BufferPointer verticesBuffer; + gpu::BufferPointer colorBuffer; + gpu::Stream::FormatPointer streamFormat; + gpu::BufferStreamPointer stream; + int vertices; + int vertexSize; + bool isCreated; + + BatchItemDetails(); + BatchItemDetails(const GeometryCache::BatchItemDetails& other); + ~BatchItemDetails(); + void clear(); + }; + + QHash _coneVBOs; + int _nextID; + QHash _lastRegisteredQuad3DTexture; + QHash _quad3DTextures; + QHash _registeredQuad3DTextures; + QHash _lastRegisteredQuad2DTexture; + QHash _quad2DTextures; + QHash _registeredQuad2DTextures; - QHash _gridBuffers; - QHash _registeredAlternateGridBuffers; - QHash _alternateGridBuffers; - QHash _lastRegisteredGrid; + QHash _lastRegisteredQuad3D; + QHash _quad3D; + QHash _registeredQuad3D; + + QHash _lastRegisteredQuad2D; + QHash _quad2D; + QHash _registeredQuad2D; + + QHash _lastRegisteredBevelRects; + QHash _bevelRects; + QHash _registeredBevelRects; + + QHash _lastRegisteredLine3D; + QHash _line3DVBOs; + QHash _registeredLine3DVBOs; + + QHash _lastRegisteredLine2D; + QHash _line2DVBOs; + QHash _registeredLine2DVBOs; + + QHash _registeredVertices; + + QHash _lastRegisteredDashedLines; + QHash _dashedLines; + QHash _registeredDashedLines; + + QHash _gridBuffers; + QHash _registeredAlternateGridBuffers; + QHash _alternateGridBuffers; + QHash _gridColors; + + QHash _sphereVertices; + QHash _sphereIndices; + QHash _sphereColors; + QHash > _networkGeometry; }; @@ -211,7 +300,7 @@ public: const FBXGeometry& getFBXGeometry() const { return _geometry; } const QVector& getMeshes() const { return _meshes; } - +// QVector getJointMappings(const AnimationPointer& animation); virtual void setLoadPriority(const QPointer& owner, float priority); diff --git a/libraries/render-utils/src/GlowEffect.cpp b/libraries/render-utils/src/GlowEffect.cpp index 22eb9e1764..628dbf455f 100644 --- a/libraries/render-utils/src/GlowEffect.cpp +++ b/libraries/render-utils/src/GlowEffect.cpp @@ -167,7 +167,6 @@ QOpenGLFramebufferObject* GlowEffect::render(bool toTexture) { } glEnable(GL_TEXTURE_2D); glDisable(GL_LIGHTING); - glColor3f(1.0f, 1.0f, 1.0f); renderFullscreenQuad(); glDisable(GL_TEXTURE_2D); glEnable(GL_LIGHTING); diff --git a/libraries/render-utils/src/RenderUtil.cpp b/libraries/render-utils/src/RenderUtil.cpp index 140ced4c52..7744817625 100644 --- a/libraries/render-utils/src/RenderUtil.cpp +++ b/libraries/render-utils/src/RenderUtil.cpp @@ -17,9 +17,11 @@ #include "RenderUtil.h" void renderFullscreenQuad(float sMin, float sMax, float tMin, float tMax) { + glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f); glm::vec2 topLeft(-1.0f, -1.0f); glm::vec2 bottomRight(1.0f, 1.0f); glm::vec2 texCoordTopLeft(sMin, tMin); glm::vec2 texCoordBottomRight(sMax, tMax); - DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); + + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color); } diff --git a/libraries/render-utils/src/TextRenderer.cpp b/libraries/render-utils/src/TextRenderer.cpp index ae7523a817..bf718b468e 100644 --- a/libraries/render-utils/src/TextRenderer.cpp +++ b/libraries/render-utils/src/TextRenderer.cpp @@ -70,16 +70,11 @@ int TextRenderer::calculateHeight(const char* str) { return maxHeight; } -int TextRenderer::draw(int x, int y, const char* str, float alpha) { - // Grab the current color - float currentColor[4]; - glGetFloatv(GL_CURRENT_COLOR, currentColor); - alpha = std::max(0.0f, std::min(alpha, 1.0f)); - currentColor[3] *= alpha; - int compactColor = ((int(currentColor[0] * 255.0f) & 0xFF)) | - ((int(currentColor[1] * 255.0f) & 0xFF) << 8) | - ((int(currentColor[2] * 255.0f) & 0xFF) << 16) | - ((int(currentColor[3] * 255.0f) & 0xFF) << 24); +int TextRenderer::draw(int x, int y, const char* str, const glm::vec4& color) { + int compactColor = ((int(color.x * 255.0f) & 0xFF)) | + ((int(color.y * 255.0f) & 0xFF) << 8) | + ((int(color.z * 255.0f) & 0xFF) << 16) | + ((int(color.w * 255.0f) & 0xFF) << 24); int maxHeight = 0; for (const char* ch = str; *ch != 0; ch++) { diff --git a/libraries/render-utils/src/TextRenderer.h b/libraries/render-utils/src/TextRenderer.h index 078efb0463..671258e335 100644 --- a/libraries/render-utils/src/TextRenderer.h +++ b/libraries/render-utils/src/TextRenderer.h @@ -13,6 +13,7 @@ #define hifi_TextRenderer_h #include +#include #include #include @@ -70,7 +71,7 @@ public: int calculateHeight(const char* str); // also returns the height of the tallest character - int draw(int x, int y, const char* str, float alpha = 1.0f); + int draw(int x, int y, const char* str, const glm::vec4& color); int computeWidth(char ch); int computeWidth(const char* str); diff --git a/libraries/render-utils/src/TextureCache.cpp b/libraries/render-utils/src/TextureCache.cpp index 624dd3d99e..022e722c14 100644 --- a/libraries/render-utils/src/TextureCache.cpp +++ b/libraries/render-utils/src/TextureCache.cpp @@ -154,14 +154,16 @@ const gpu::TexturePointer& TextureCache::getPermutationNormalTexture() { } const unsigned char OPAQUE_WHITE[] = { 0xFF, 0xFF, 0xFF, 0xFF }; -const unsigned char TRANSPARENT_WHITE[] = { 0xFF, 0xFF, 0xFF, 0x0 }; -const unsigned char OPAQUE_BLACK[] = { 0x0, 0x0, 0x0, 0xFF }; +//const unsigned char TRANSPARENT_WHITE[] = { 0xFF, 0xFF, 0xFF, 0x0 }; +//const unsigned char OPAQUE_BLACK[] = { 0x0, 0x0, 0x0, 0xFF }; const unsigned char OPAQUE_BLUE[] = { 0x80, 0x80, 0xFF, 0xFF }; +/* static void loadSingleColorTexture(const unsigned char* color) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, color); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } +*/ const gpu::TexturePointer& TextureCache::getWhiteTexture() { if (_whiteTexture.isNull()) { diff --git a/libraries/shared/src/StreamUtils.cpp b/libraries/shared/src/StreamUtils.cpp index 47bb8dd4e4..96967648ed 100644 --- a/libraries/shared/src/StreamUtils.cpp +++ b/libraries/shared/src/StreamUtils.cpp @@ -115,6 +115,16 @@ QDebug& operator<<(QDebug& dbg, const glm::vec3& v) { return dbg; } +QDebug& operator<<(QDebug& dbg, const glm::vec4& v) { + dbg.nospace() << "{type='glm::vec4'" + ", x=" << v.x << + ", y=" << v.y << + ", z=" << v.z << + ", w=" << v.w << + "}"; + return dbg; +} + QDebug& operator<<(QDebug& dbg, const glm::quat& q) { dbg.nospace() << "{type='glm::quat'" ", x=" << q.x << diff --git a/libraries/shared/src/StreamUtils.h b/libraries/shared/src/StreamUtils.h index d176d68e45..b9823a6743 100644 --- a/libraries/shared/src/StreamUtils.h +++ b/libraries/shared/src/StreamUtils.h @@ -51,6 +51,7 @@ class QDebug; // Add support for writing these to qDebug(). QDebug& operator<<(QDebug& s, const glm::vec2& v); QDebug& operator<<(QDebug& s, const glm::vec3& v); +QDebug& operator<<(QDebug& s, const glm::vec4& v); QDebug& operator<<(QDebug& s, const glm::quat& q); QDebug& operator<<(QDebug& s, const glm::mat4& m); #endif // QT_NO_DEBUG_STREAM