Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
OpenGLTexture.cpp
1/*
2Copyright (C) 2016-2021,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#include "OpenGLFunctions.hpp"
18#include "OpenGLRenderer.hpp"
19#include "OpenGLTexture.hpp"
20#include "aeongames/Texture.hpp"
21
22namespace AeonGames
23{
25 mOpenGLRenderer{aOpenGLTexture.mOpenGLRenderer}
26 {
27 std::swap ( mTexture, aOpenGLTexture.mTexture );
28 std::swap ( mTextureId, aOpenGLTexture.mTextureId );
29 }
30
31 OpenGLTexture::OpenGLTexture ( OpenGLRenderer& aOpenGLRenderer, const Texture& aTexture ) :
32 mOpenGLRenderer{aOpenGLRenderer}, mTexture{&aTexture}
33 {
34 glGenTextures ( 1, &mTextureId );
35 OPENGL_CHECK_ERROR_THROW;
36
37 if ( glIsTexture ( mTextureId ) )
38 {
39 GLint w, h;
40 glGetTextureLevelParameteriv ( mTextureId, 0, GL_TEXTURE_WIDTH, &w );
41 glGetTextureLevelParameteriv ( mTextureId, 0, GL_TEXTURE_HEIGHT, &h );
42 if ( aTexture.GetWidth() == static_cast<uint32_t> ( w ) && aTexture.GetHeight() == static_cast<uint32_t> ( h ) )
43 {
44 glTextureSubImage2D ( mTextureId, 0, 0, 0, aTexture.GetWidth(), aTexture.GetHeight(),
45 ( aTexture.GetFormat() == Texture::Format::RGB ) ? GL_RGB : ( aTexture.GetFormat() == Texture::Format::BGRA ) ? GL_BGRA : GL_RGBA,
46 ( aTexture.GetType() == Texture::Type::UNSIGNED_BYTE ) ? GL_UNSIGNED_BYTE : ( aTexture.GetType() == Texture::Type::UNSIGNED_SHORT ) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT_8_8_8_8_REV,
47 aTexture.GetPixels().data() );
48 return;
49 }
50 }
51
52 glBindTexture ( GL_TEXTURE_2D, mTextureId );
53 OPENGL_CHECK_ERROR_THROW;
54 glTexImage2D ( GL_TEXTURE_2D,
55 0,
56 ( aTexture.GetFormat() == Texture::Format::RGB ) ? GL_RGB : GL_RGBA,
57 aTexture.GetWidth(),
58 aTexture.GetHeight(),
59 0,
60 ( aTexture.GetFormat() == Texture::Format::RGB ) ? GL_RGB : ( aTexture.GetFormat() == Texture::Format::BGRA ) ? GL_BGRA : GL_RGBA,
61 ( aTexture.GetType() == Texture::Type::UNSIGNED_BYTE ) ? GL_UNSIGNED_BYTE : ( aTexture.GetType() == Texture::Type::UNSIGNED_SHORT ) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT_8_8_8_8_REV,
62 aTexture.GetPixels().data() );
63 OPENGL_CHECK_ERROR_THROW;
64 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
65 OPENGL_CHECK_ERROR_THROW;
66 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
67 OPENGL_CHECK_ERROR_THROW;
68 glBindTexture ( GL_TEXTURE_2D, 0 );
69 OPENGL_CHECK_ERROR_THROW;
70 }
71
72 OpenGLTexture::~OpenGLTexture()
73 {
74 if ( glIsTexture ( mTextureId ) == GL_TRUE )
75 {
76 glDeleteTextures ( 1, &mTextureId );
77 OPENGL_CHECK_ERROR_NO_THROW;
78 }
79 }
80
81
83 {
84 return mTextureId;
85 }
86}
OpenGL rendering backend implementing the Renderer interface.
OpenGLTexture(OpenGLRenderer &aOpenGLRenderer, const Texture &aTexture)
GLuint GetTextureId() const
Get the OpenGL texture object identifier.
Represents a 2D texture image resource.
Definition Texture.hpp:33
DLL uint32_t GetWidth() const
Returns the texture width in pixels.
Definition Texture.cpp:75
DLL Format GetFormat() const
Returns the pixel format.
Definition Texture.cpp:83
DLL const std::vector< uint8_t > & GetPixels() const
Returns a reference to the raw pixel data.
Definition Texture.cpp:92
@ UNSIGNED_SHORT
16-bit unsigned integer per channel.
Definition Texture.hpp:54
@ UNSIGNED_BYTE
8-bit unsigned integer per channel.
Definition Texture.hpp:53
@ BGRA
4-channel blue, green, red, alpha.
Definition Texture.hpp:44
@ RGB
3-channel red, green, blue.
Definition Texture.hpp:42
DLL Type GetType() const
Returns the pixel component type.
Definition Texture.cpp:87
DLL uint32_t GetHeight() const
Returns the texture height in pixels.
Definition Texture.cpp:79
<- This is here just for the literals
Definition AABB.hpp:31