From 7bc3a2fb302a079d8d1b308c52b965372fdc54f6 Mon Sep 17 00:00:00 2001 From: brunoherbelin Date: Sat, 4 Apr 2020 23:45:18 +0200 Subject: [PATCH] Define black opengl texture in resource --- ImGuiVisitor.cpp | 6 ++++++ ImGuiVisitor.h | 11 +++++++++++ MediaPlayer.cpp | 19 ++----------------- Primitives.cpp | 39 +++++++++++++++++++++++++++------------ Primitives.h | 3 ++- Resource.cpp | 47 ++++++++++++++++++++++++++++++++--------------- Resource.h | 7 +++++-- 7 files changed, 85 insertions(+), 47 deletions(-) create mode 100644 ImGuiVisitor.cpp create mode 100644 ImGuiVisitor.h diff --git a/ImGuiVisitor.cpp b/ImGuiVisitor.cpp new file mode 100644 index 0000000..25de8d9 --- /dev/null +++ b/ImGuiVisitor.cpp @@ -0,0 +1,6 @@ +#include "ImGuiVisitor.h" + +ImGuiVisitor::ImGuiVisitor() +{ + +} diff --git a/ImGuiVisitor.h b/ImGuiVisitor.h new file mode 100644 index 0000000..8e72ca0 --- /dev/null +++ b/ImGuiVisitor.h @@ -0,0 +1,11 @@ +#ifndef IMGUIVISITOR_H +#define IMGUIVISITOR_H + + +class ImGuiVisitor +{ +public: + ImGuiVisitor(); +}; + +#endif // IMGUIVISITOR_H \ No newline at end of file diff --git a/MediaPlayer.cpp b/MediaPlayer.cpp index 26fbfc7..3385253 100644 --- a/MediaPlayer.cpp +++ b/MediaPlayer.cpp @@ -23,22 +23,7 @@ #define MEDIA_PLAYER_DEBUG #endif -// opengl texture -static GLuint tex_index_black = 0; -GLuint blackTexture() -{ - // generate texture (once) & clear - if (tex_index_black == 0) { - glGenTextures(1, &tex_index_black); - glBindTexture( GL_TEXTURE_2D, tex_index_black); - unsigned char clearColor[3] = {0}; - // texture with one black pixel - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, clearColor); - } - - return tex_index_black; -} MediaPlayer::MediaPlayer(string name) : id_(name) { @@ -90,7 +75,7 @@ void MediaPlayer::bind() guint MediaPlayer::texture() const { if (textureindex_ == 0) - return blackTexture(); + return Resource::getTextureBlack(); return textureindex_; } @@ -222,7 +207,7 @@ void MediaPlayer::close() } // nothing to display - textureindex_ = blackTexture(); + textureindex_ = Resource::getTextureBlack(); // un-ready the media player ready_ = false; diff --git a/Primitives.cpp b/Primitives.cpp index 15cea1b..636b3fa 100644 --- a/Primitives.cpp +++ b/Primitives.cpp @@ -23,17 +23,13 @@ static const std::vector square_points { glm::vec3( -1.f, -1.f, 0.f ) glm::vec3( 1.f, 1.f, 0.f ), glm::vec3( 1.f, -1.f, 0.f ) }; static uint square_vao = 0; static uint circle_vao = 0; -static const std::string default_texture = "images/busy.png"; -ImageSurface::ImageSurface(const std::string& path) : Primitive() +ImageSurface::ImageSurface(const std::string& path) : Primitive(), textureindex_(0) { + // for image texture filename_ = path; - float ar = 1.0; - textureindex_ = Resource::getTextureImage(filename_, &ar); - transform_ = glm::scale(glm::identity(), glm::vec3(ar, 1.f, 1.f)); - // geometry points_ = std::vector { glm::vec3( -1.f, -1.f, 0.f ), glm::vec3( -1.f, 1.f, 0.f ), glm::vec3( 1.f, -1.f, 0.f ), glm::vec3( 1.f, 1.f, 0.f ) }; @@ -42,15 +38,23 @@ ImageSurface::ImageSurface(const std::string& path) : Primitive() texCoords_ = std::vector { glm::vec2( 0.f, 1.f ), glm::vec2( 0.f, 0.f ), glm::vec2( 1.f, 1.f ), glm::vec2( 1.f, 0.f ) }; indices_ = std::vector { 0, 1, 2, 3 }; - - // setup shader for textured image - shader_ = new ImageShader(); drawingPrimitive_ = GL_TRIANGLE_STRIP; } void ImageSurface::init() { + // load image if specified + if ( filename_.empty()) + textureindex_ = Resource::getTextureBlack(); + else { + float ar = 1.0; + textureindex_ = Resource::getTextureImage(filename_, &ar); + transform_ = glm::scale(glm::identity(), glm::vec3(ar, 1.f, 1.f)); + } + // create shader for textured image + shader_ = new ImageShader(); + // use static global vertex array object if (square_vao) // if set, use the global vertex array object @@ -63,7 +67,9 @@ void ImageSurface::init() // 3. vao_ will not be deleted because deleteGLBuffers_() is empty } + // all good visible_ = true; + initialized_ = true; } void ImageSurface::draw(glm::mat4 modelview, glm::mat4 projection) @@ -81,12 +87,10 @@ void ImageSurface::accept(Visitor& v) v.visit(*this); } -MediaSurface::MediaSurface(const std::string& path) : ImageSurface(default_texture) +MediaSurface::MediaSurface(const std::string& path) : ImageSurface() { filename_ = path; mediaplayer_ = new MediaPlayer; - mediaplayer_->open(filename_); - mediaplayer_->play(true); } MediaSurface::~MediaSurface() @@ -94,6 +98,17 @@ MediaSurface::~MediaSurface() delete mediaplayer_; } +void MediaSurface::init() +{ + std::string tmp = filename_; + filename_ = ""; + ImageSurface::init(); + filename_ = tmp; + + mediaplayer_->open(filename_); + mediaplayer_->play(true); +} + void MediaSurface::draw(glm::mat4 modelview, glm::mat4 projection) { if ( mediaplayer_->isOpen() ) diff --git a/Primitives.h b/Primitives.h index 838ac24..370d0fe 100644 --- a/Primitives.h +++ b/Primitives.h @@ -11,7 +11,7 @@ class ImageSurface : public Primitive { void deleteGLBuffers_() {} public: - ImageSurface(const std::string& path); + ImageSurface(const std::string& path = "" ); void init() override; void draw(glm::mat4 modelview, glm::mat4 projection) override; @@ -36,6 +36,7 @@ public: MediaSurface(const std::string& path); ~MediaSurface(); + void init() override; void draw(glm::mat4 modelview, glm::mat4 projection) override; void accept(Visitor& v) override; void update ( float dt ) override; diff --git a/Resource.cpp b/Resource.cpp index 0a2cc57..f9af839 100644 --- a/Resource.cpp +++ b/Resource.cpp @@ -21,9 +21,26 @@ CMRC_DECLARE(vmix); -std::map textureIndex; +std::map textureIndex; std::map textureAspectRatio; +// opengl texture +uint Resource::getTextureBlack() +{ + static uint tex_index_black = 0; + + // generate texture (once) & clear + if (tex_index_black == 0) { + glGenTextures(1, &tex_index_black); + glBindTexture( GL_TEXTURE_2D, tex_index_black); + unsigned char clearColor[3] = {0}; + // texture with one black pixel + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, clearColor); + } + + return tex_index_black; +} + const char *Resource::getData(const std::string& path, size_t* out_file_size){ auto fs = cmrc::vmix::get_filesystem(); @@ -66,7 +83,7 @@ std::string Resource::getText(const std::string& path){ #define FOURCC_DXT3 0x33545844 // Equivalent to "DXT3" in ASCII #define FOURCC_DXT5 0x35545844 // Equivalent to "DXT5" in ASCII -unsigned int Resource::getTextureDDS(const std::string& path, float *aspect_ratio) +uint Resource::getTextureDDS(const std::string& path, float *aspect_ratio) { GLuint textureID = 0; @@ -92,21 +109,21 @@ unsigned int Resource::getTextureDDS(const std::string& path, float *aspect_rati // get the surface desc = bytes [4 - 127] const char *header = fp + 4; - unsigned int height = *(unsigned int*)&(header[8 ]); - unsigned int width = *(unsigned int*)&(header[12]); - unsigned int linearSize = *(unsigned int*)&(header[16]); - unsigned int mipMapCount = *(unsigned int*)&(header[24]); - unsigned int fourCC = *(unsigned int*)&(header[80]); + uint height = *(uint*)&(header[8 ]); + uint width = *(uint*)&(header[12]); + uint linearSize = *(uint*)&(header[16]); + uint mipMapCount = *(uint*)&(header[24]); + uint fourCC = *(uint*)&(header[80]); // how big is it going to be including all mipmaps? - unsigned int bufsize; + uint bufsize; bufsize = mipMapCount > 1 ? linearSize * 2 : linearSize; // get the buffer = bytes [128 - ] const char *buffer = fp + 128; - unsigned int components = (fourCC == FOURCC_DXT1) ? 3 : 4; - unsigned int format = 0; + uint components = (fourCC == FOURCC_DXT1) ? 3 : 4; + uint format = 0; switch(fourCC) { case FOURCC_DXT1: @@ -139,13 +156,13 @@ unsigned int Resource::getTextureDDS(const std::string& path, float *aspect_rati glPixelStorei(GL_UNPACK_ALIGNMENT,1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - unsigned int blockSize = (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) ? 8 : 16; - unsigned int offset = 0; + uint blockSize = (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) ? 8 : 16; + uint offset = 0; // load the mipmaps - for (unsigned int level = 0; level < mipMapCount && (width || height); ++level) + for (uint level = 0; level < mipMapCount && (width || height); ++level) { - unsigned int size = ((width+3)/4)*((height+3)/4)*blockSize; + uint size = ((width+3)/4)*((height+3)/4)*blockSize; glCompressedTexImage2D(GL_TEXTURE_2D, level, format, width, height, 0, size, buffer + offset); glGenerateMipmap(GL_TEXTURE_2D); @@ -170,7 +187,7 @@ unsigned int Resource::getTextureDDS(const std::string& path, float *aspect_rati } -unsigned int Resource::getTextureImage(const std::string& path, float *aspect_ratio) +uint Resource::getTextureImage(const std::string& path, float *aspect_ratio) { GLuint textureID = 0; diff --git a/Resource.h b/Resource.h index 983b76f..6b54925 100644 --- a/Resource.h +++ b/Resource.h @@ -13,11 +13,14 @@ namespace Resource // Support DDS files, DXT1, DXT5 and DXT5 // Returns the OpenGL generated Texture index - unsigned int getTextureDDS(const std::string& path, float *aspect_ratio = nullptr); + uint getTextureDDS(const std::string& path, float *aspect_ratio = nullptr); // Support PNG, JPEG, TGA, BMP, PSD, GIF, HDR, PIC, PNM // Returns the OpenGL generated Texture index - unsigned int getTextureImage(const std::string& path, float *aspect_ratio = nullptr); + uint getTextureImage(const std::string& path, float *aspect_ratio = nullptr); + + // Returns the OpenGL generated Texture index for an empty 1x1 black pixel texture + uint getTextureBlack(); // Generic access to pointer to data const char *getData(const std::string& path, size_t* out_file_size);