Commits

Christian Fischer committed ddb0894

allowing to create empty textures

  • Participants
  • Parent commits cf9468b

Comments (0)

Files changed (4)

File src/core/wiesel/video/texture.cpp

 
 
 Texture::Texture() {
+	data = NULL;
 	return;
 }
 
 }
 
 
+Texture *Texture::createEmptyTexture(const dimension& size) {
+	Texture *texture = new Texture();
+	texture->requested_size = size;
+	return texture;
+}
+
+
 
 bool Texture::doLoadContent() {
 	assert(getContent() == NULL);

File src/core/wiesel/video/texture.h

 		static Texture *fromDataSource(DataSource *data);
 
 		/**
+		 * @brief Creates an empty texture.
+		 */
+		static Texture *createEmptyTexture(const dimension& size);
+
+		/**
 		 * @brief Get the datasource, where we get the texture's data from.
 		 */
 		inline DataSource *getSource() {
 		}
 
 		/**
+		 * @brief Get the requested size for this texture.
+		 */
+		inline const dimension& getRequestedSize() const {
+			return requested_size;
+		}
+
+		/**
 		 * @brief get the texture's size.
 		 * The actual size of the texture may differ from the texture-file's size,
 		 * when the hardware doesn't support the file's dimension.
 
 	private:
 		DataSource*		data;
+		dimension		requested_size;
+
 		dimension		size;
 		dimension		original_size;
 	};

File src/opengl/wiesel/video/gl/gl_texture_content.cpp

 #include <wiesel/resources/graphics/image_loader.h>
 #include <wiesel/module_registry.h>
 
+#include <string.h>
+
 
 using namespace wiesel;
 using namespace wiesel::video;
 GlTextureContent *GlTextureContent::createContentFor(Texture *texture) {
 	GlTextureContent *gl_texture = new GlTextureContent(texture);
 
-	if (gl_texture->createHardwareTexture() == false) {
+	if (gl_texture->initTexture() == false) {
 		delete gl_texture;
 
 		return NULL;
 }
 
 
-bool GlTextureContent::createHardwareTexture() {
+bool GlTextureContent::initTexture() {
 	assert(handle == 0);
 
 	// release the previous buffer
 	releaseTexture();
 
-	DataSource *data = getTexture()->getSource();
+	if (getTexture()->getSource()) {
+		return loadTextureFromSource(getTexture()->getSource());
+	}
+
+	if (getTexture()->getRequestedSize().getMin() > 0.0f) {
+		return loadEmptyTexture(PixelFormat_RGBA_8888, getTexture()->getRequestedSize());
+	}
+
+	return false;
+}
+
+
+bool GlTextureContent::loadEmptyTexture(PixelFormat format, const dimension& size) {
+	dimension new_size = size;
+
+	// convert into power-of-two
+	new_size.width  = getNextPowerOfTwo(static_cast<unsigned int>(size.width));
+	new_size.height = getNextPowerOfTwo(static_cast<unsigned int>(size.height));
+
+	size_t data_size = new_size.width * new_size.height * getBytesPerPixel(format);
+
+	// create an empty buffer containing the initial texture data
+	ref<DataBuffer> data = ExclusiveDataBuffer::create(data_size);
+	if (data->getData() == NULL) {
+		return false;
+	}
+
+	memset(data->getMutableData(), 0, data_size);
+
+	if (!createHardwareTexture(PixelFormat_RGBA_8888, new_size, data)) {
+		return false;
+	}
+
+	this->original_size = size;
+
+	return true;
+}
+
+
+bool GlTextureContent::loadTextureFromSource(DataSource *data) {
 	ref<Image> image = NULL;
 	dimension new_original_size;
 
 	if (image->ensurePowerOfTwo() == false) {
 		return false;
 	}
+	
+	if (!createHardwareTexture(image->getPixelFormat(), image->getSize(), image->getPixelData())) {
+		return false;
+	}
 
+	original_size = new_original_size;
+	
+	return true;
+}
+
+
+bool GlTextureContent::createHardwareTexture(PixelFormat format, const dimension& size, DataBuffer *data) {
 	GLint internalFormat;
 	GLenum image_format;
 	GLenum image_type;
 
-	switch(image->getPixelFormat()) {
+	switch(format) {
 		case PixelFormat_RGBA_8888: {
 			internalFormat = GL_RGBA;
 			image_format   = GL_RGBA;
 	glBindTexture(GL_TEXTURE_2D, handle);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-
+	
 	glTexImage2D(
 					GL_TEXTURE_2D, 0,
 					internalFormat,
-					image->getSize().width, image->getSize().height,
+					size.width, size.height,
 					0,
 					image_format, image_type,
-					image->getPixelData()->getData()
+					data->getData()
 	);
 
-	size          = image->getSize();
-	original_size = new_original_size;
+	this->size = size;
 
 	return true;
 }

File src/opengl/wiesel/video/gl/gl_texture_content.h

 #include <wiesel/geometry.h>
 #include <wiesel/io/datasource.h>
 #include <wiesel/io/file.h>
+#include <wiesel/resources/graphics/image.h>
 #include <wiesel/video/texture.h>
 #include <string>
 
 		}
 
 	private:
+		bool initTexture();
+
+		bool loadEmptyTexture(PixelFormat format, const dimension& size);
+		bool loadTextureFromSource(DataSource *data);
+
 		/// creates the texture on hardware
-		bool createHardwareTexture();
+		bool createHardwareTexture(PixelFormat format, const dimension& size, DataBuffer* data);
 
 		/// release the texture.
 		void releaseTexture();