Prepping to allow textures to have different render modes/types. Prepping for freetype support
This commit is contained in:
		| @@ -7,10 +7,18 @@ | ||||
|  | ||||
| using namespace Dawn; | ||||
|  | ||||
| Texture::Texture() : ITexture() { | ||||
| } | ||||
|  | ||||
| void Texture::bind(textureslot_t slot) { | ||||
|   assertTrue(this->id != -1); | ||||
|   glActiveTexture(GL_TEXTURE0 + slot); | ||||
|   glBindTexture(GL_TEXTURE_2D, this->id); | ||||
|  | ||||
|   if(this->texturePropertiesNeedUpdating) { | ||||
|     this->updateTextureProperties(); | ||||
|     this->texturePropertiesNeedUpdating = false; | ||||
|   } | ||||
| } | ||||
|  | ||||
| int32_t Texture::getWidth() { | ||||
| @@ -21,56 +29,140 @@ int32_t Texture::getHeight() { | ||||
|   return this->height; | ||||
| } | ||||
|  | ||||
| void Texture::setSize(int32_t width, int32_t height) { | ||||
| void Texture::setSize(int32_t width, int32_t height, enum TextureFormat format) { | ||||
|   if(this->id != -1) glDeleteTextures(1, &this->id); | ||||
|  | ||||
|   this->width = width; | ||||
|   this->height = height; | ||||
|   this->format = format; | ||||
|  | ||||
|   glGenTextures(1, &this->id); | ||||
|   if(this->id <= 0) throw "Texture generation failed!"; | ||||
|    | ||||
|   glActiveTexture(GL_TEXTURE0); | ||||
|   glBindTexture(GL_TEXTURE_2D, this->id); | ||||
|  | ||||
|   // Setup our preferred texture params, later this will be configurable. | ||||
|   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | ||||
|   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); | ||||
|   // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | ||||
|   // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | ||||
|   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | ||||
|   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | ||||
|  | ||||
|   // Initialize the texture to blank | ||||
|   glTexImage2D( | ||||
|     GL_TEXTURE_2D, 0, GL_RGBA, | ||||
|     width, height, | ||||
|     0, GL_RGBA, GL_UNSIGNED_BYTE, NULL | ||||
|   ); | ||||
|   glActiveTexture(GL_TEXTURE0); | ||||
|   this->bufferRaw(NULL); | ||||
| } | ||||
|  | ||||
| void Texture::fill(struct Color color) { | ||||
|   struct Color *pixels = (struct Color *)memoryAllocate( | ||||
|     sizeof(struct Color) * this->width * this->height | ||||
|   ); | ||||
|  | ||||
|   this->buffer(pixels); | ||||
|  | ||||
|   memoryFree(pixels); | ||||
| } | ||||
|  | ||||
| bool_t Texture::isReady() { | ||||
|   return this->id != -1; | ||||
| } | ||||
|        | ||||
| void Texture::buffer(struct Color pixels[]) { | ||||
|  | ||||
| void Texture::updateTextureProperties() { | ||||
|   switch(((enum TextureWrapMode)this->wrapModeX)) { | ||||
|     case TEXTURE_WRAP_MODE_REPEAT: | ||||
|       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | ||||
|       break; | ||||
|      | ||||
|     case TEXTURE_WRAP_MODE_MIRRORED_REPEAT: | ||||
|       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); | ||||
|       break; | ||||
|  | ||||
|     case TEXTURE_WRAP_MODE_CLAMP_TO_EDGE: | ||||
|       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||||
|       break; | ||||
|  | ||||
|     case TEXTURE_WRAP_MODE_CLAMP_TO_BORDER: | ||||
|       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); | ||||
|       break; | ||||
|  | ||||
|     default: | ||||
|       assertUnreachable(); | ||||
|   } | ||||
|  | ||||
|   switch(((enum TextureWrapMode)this->wrapModeY)) { | ||||
|     case TEXTURE_WRAP_MODE_REPEAT: | ||||
|       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T ,GL_REPEAT); | ||||
|       break; | ||||
|      | ||||
|     case TEXTURE_WRAP_MODE_MIRRORED_REPEAT: | ||||
|       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); | ||||
|       break; | ||||
|  | ||||
|     case TEXTURE_WRAP_MODE_CLAMP_TO_EDGE: | ||||
|       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||||
|       break; | ||||
|  | ||||
|     case TEXTURE_WRAP_MODE_CLAMP_TO_BORDER: | ||||
|       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); | ||||
|       break; | ||||
|  | ||||
|     default: | ||||
|       assertUnreachable(); | ||||
|   } | ||||
|  | ||||
|   switch(((enum TextureFilterMode)this->filterModeMin)) { | ||||
|     case TEXTURE_FILTER_MODE_NEAREST: | ||||
|       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); | ||||
|       break; | ||||
|      | ||||
|     case TEXTURE_FILTER_MODE_LINEAR: | ||||
|       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); | ||||
|       break; | ||||
|  | ||||
|     default: | ||||
|       assertUnreachable(); | ||||
|   } | ||||
|  | ||||
|   switch(((enum TextureFilterMode)this->filterModeMag)) { | ||||
|     case TEXTURE_FILTER_MODE_NEAREST: | ||||
|       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST); | ||||
|       break; | ||||
|      | ||||
|     case TEXTURE_FILTER_MODE_LINEAR: | ||||
|       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); | ||||
|       break; | ||||
|  | ||||
|     default: | ||||
|       assertUnreachable(); | ||||
|   } | ||||
| } | ||||
|  | ||||
| void Texture::bufferRaw(void *data) { | ||||
|   assertTrue(this->isReady()); | ||||
|   glBindTexture(GL_TEXTURE_2D, this->id); | ||||
|  | ||||
|   GLenum format; | ||||
|   switch(this->format) { | ||||
|     case TEXTURE_FORMAT_R: | ||||
|       format = GL_RED; | ||||
|       break; | ||||
|  | ||||
|     case TEXTURE_FORMAT_RG: | ||||
|       format = GL_RG; | ||||
|       break; | ||||
|  | ||||
|     case TEXTURE_FORMAT_RGB: | ||||
|       format = GL_RGB; | ||||
|       break; | ||||
|  | ||||
|     case TEXTURE_FORMAT_RGBA: | ||||
|       format = GL_RGBA; | ||||
|       break; | ||||
|      | ||||
|     default: | ||||
|       assertUnreachable(); | ||||
|   } | ||||
|  | ||||
|   glTexImage2D( | ||||
|     GL_TEXTURE_2D, 0, GL_RGBA, | ||||
|     GL_TEXTURE_2D, 0, format, | ||||
|     this->width, this->height, | ||||
|     0, GL_RGBA, GL_UNSIGNED_BYTE, (void *)pixels | ||||
|     0, format, GL_UNSIGNED_BYTE, data | ||||
|   ); | ||||
|   glGenerateMipmap(GL_TEXTURE_2D); | ||||
|   this->texturePropertiesNeedUpdating = true; | ||||
| } | ||||
|  | ||||
| void Texture::buffer(struct Color pixels[]) { | ||||
|   this->bufferRaw((void*)pixels); | ||||
| } | ||||
|  | ||||
| Texture::~Texture() { | ||||
|   | ||||
| @@ -19,11 +19,16 @@ namespace Dawn { | ||||
|       int32_t width = -1; | ||||
|       int32_t height = -1; | ||||
|       GLuint id = -1; | ||||
|       enum TextureFormat format; | ||||
|  | ||||
|       void updateTextureProperties(); | ||||
|       void bufferRaw(void *data); | ||||
|  | ||||
|     public: | ||||
|       Texture(); | ||||
|       int32_t getWidth() override; | ||||
|       int32_t getHeight() override; | ||||
|       void setSize(int32_t width, int32_t height) override; | ||||
|       void setSize(int32_t width, int32_t height, enum TextureFormat format) override; | ||||
|       void fill(struct Color) override; | ||||
|       bool_t isReady() override; | ||||
|       void buffer(struct Color pixels[]) override; | ||||
|   | ||||
| @@ -26,7 +26,7 @@ void TextureRenderTarget::setSize(float_t width, float_t height) { | ||||
|   if(this->fboId != -1) glDeleteFramebuffers(1, &this->fboId); | ||||
|  | ||||
|   // Resize texture | ||||
|   this->texture.setSize((int32_t)width, (int32_t) height); | ||||
|   this->texture.setSize((int32_t)width, (int32_t)height, TEXTURE_FORMAT_RGBA); | ||||
|   this->eventRenderTargetResized.invoke(this, width, height); | ||||
|  | ||||
|   // Create Frame Buffer | ||||
|   | ||||
		Reference in New Issue
	
	Block a user