From c814d274d4fcd135224ea4a5c1942a45d445bf73 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Fri, 16 Mar 2018 14:25:14 +0100 Subject: [PATCH] Unify drawing logic into glUtils. --- src/ImageInteractionAnimation.cpp | 12 +----------- src/MultiImageVisualisation.cpp | 12 +----------- src/PoolingLayerAnimation.cpp | 12 +----------- src/glutils.cpp | 15 +++++++++++++++ src/glutils.hpp | 12 ++++++++++++ 5 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/ImageInteractionAnimation.cpp b/src/ImageInteractionAnimation.cpp index 550157a..ee576af 100644 --- a/src/ImageInteractionAnimation.cpp +++ b/src/ImageInteractionAnimation.cpp @@ -12,17 +12,7 @@ void ImageInteractionAnimation::draw(float step) caffe::caffe_scal(deltas.size(), step, vertexBuffer.data()); caffe::caffe_add(vertexBuffer.size(), vertexBuffer.data(), startingPositions.data(), vertexBuffer.data()); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glEnableClientState(GL_VERTEX_ARRAY); - glEnable(GL_TEXTURE_2D); - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); - texture.bind(GL_TEXTURE_2D); - glTexCoordPointer(2, GL_FLOAT, 0, textureCoordinates.data()); - glVertexPointer(3, GL_FLOAT, 0, vertexBuffer.data()); - glDrawArrays(GL_QUADS, 0, vertexBuffer.size() / 3); - glDisable(GL_TEXTURE_2D); - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); + drawImageTiles(vertexBuffer.size() / 3, vertexBuffer.data(), textureCoordinates.data(), texture); } ImageInteractionAnimation::ImageInteractionAnimation(const DType *data, const std::vector &shape, diff --git a/src/MultiImageVisualisation.cpp b/src/MultiImageVisualisation.cpp index e2d30cc..0c67722 100644 --- a/src/MultiImageVisualisation.cpp +++ b/src/MultiImageVisualisation.cpp @@ -26,17 +26,7 @@ MultiImageVisualisation::MultiImageVisualisation(const fmri::LayerData &layer) void MultiImageVisualisation::render() { - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glEnableClientState(GL_VERTEX_ARRAY); - glEnable(GL_TEXTURE_2D); - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); - texture.bind(GL_TEXTURE_2D); - glTexCoordPointer(2, GL_FLOAT, 0, texCoordBuffer.data()); - glVertexPointer(3, GL_FLOAT, 0, vertexBuffer.data()); - glDrawArrays(GL_QUADS, 0, vertexBuffer.size() / 3); - glDisable(GL_TEXTURE_2D); - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); + drawImageTiles(vertexBuffer.size() / 3, vertexBuffer.data(), texCoordBuffer.data(), texture); } vector MultiImageVisualisation::getVertices(const std::vector &nodePositions, float scaling) diff --git a/src/PoolingLayerAnimation.cpp b/src/PoolingLayerAnimation.cpp index eb978b8..a79d09d 100644 --- a/src/PoolingLayerAnimation.cpp +++ b/src/PoolingLayerAnimation.cpp @@ -35,17 +35,7 @@ void PoolingLayerAnimation::draw(float timeStep) caffe::caffe_scal(vertexBuffer.size(), timeStep, vertexBuffer.data()); caffe::caffe_add(startingPositions.size(), startingPositions.data(), vertexBuffer.data(), vertexBuffer.data()); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glEnable(GL_TEXTURE_2D); - original.bind(GL_TEXTURE_2D); - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); - glVertexPointer(3, GL_FLOAT, 0, vertexBuffer.data()); - glTexCoordPointer(2, GL_FLOAT, 0, textureCoordinates.data()); - glDrawArrays(GL_QUADS, 0, vertexBuffer.size() / 3); - glDisable(GL_TEXTURE_2D); - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); + drawImageTiles(vertexBuffer.size() / 3, vertexBuffer.data(), textureCoordinates.data(), original); } Texture PoolingLayerAnimation::loadTextureForData(const LayerData &data) diff --git a/src/glutils.cpp b/src/glutils.cpp index 0f9a28a..d9affb2 100644 --- a/src/glutils.cpp +++ b/src/glutils.cpp @@ -138,3 +138,18 @@ void fmri::setOrthographicProjection() { // switch back to modelview mode glMatrixMode(GL_MODELVIEW); } + +void fmri::drawImageTiles(int n, const float *vertexBuffer, const float *textureCoords, const Texture &texture) +{ + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glEnableClientState(GL_VERTEX_ARRAY); + glEnable(GL_TEXTURE_2D); + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); + texture.bind(GL_TEXTURE_2D); + glTexCoordPointer(2, GL_FLOAT, 0, textureCoords); + glVertexPointer(3, GL_FLOAT, 0, vertexBuffer); + glDrawArrays(GL_QUADS, 0, n); + glDisable(GL_TEXTURE_2D); + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); +} diff --git a/src/glutils.hpp b/src/glutils.hpp index bdfbb53..11224e1 100644 --- a/src/glutils.hpp +++ b/src/glutils.hpp @@ -49,4 +49,16 @@ namespace fmri { void setOrthographicProjection(); void restorePerspectiveProjection(); + + /** + * Draw a series of textured tiles to the screen. + * + * This function ends up drawing GL_QUADS. + * + * @param n Number of vertices + * @param vertexBuffer + * @param textureCoords + * @param texture + */ + void drawImageTiles(int n, const float* vertexBuffer, const float* textureCoords, const Texture& texture); }