Removed GL calls in texfont.

This commit is contained in:
Leonardo Zide 2021-11-21 12:16:19 -08:00
parent 36dbcec238
commit a9ab7c0c64
11 changed files with 53 additions and 49 deletions

View file

@ -18,7 +18,7 @@ class Image
public:
Image();
Image(Image&& Other);
virtual ~Image();
~Image();
Image(const Image&) = delete;
Image& operator=(const Image&) = delete;

View file

@ -485,7 +485,7 @@ void lcContext::ClearResources()
{
ClearVertexBuffer();
ClearIndexBuffer();
BindTexture2D(0);
ClearTexture2D();
}
void lcContext::SetMaterial(lcMaterialType MaterialType)
@ -628,36 +628,40 @@ void lcContext::SetLineWidth(float LineWidth)
mLineWidth = LineWidth;
}
void lcContext::BindTexture2D(GLuint Texture)
void lcContext::BindTexture2D(const lcTexture* Texture)
{
if (mTexture2D == Texture)
GLuint TextureObject = Texture->mTexture;
if (mTexture2D == TextureObject)
return;
glBindTexture(GL_TEXTURE_2D, Texture);
mTexture2D = Texture;
glBindTexture(GL_TEXTURE_2D, TextureObject);
mTexture2D = TextureObject;
}
void lcContext::BindTextureCubeMap(GLuint Texture)
void lcContext::BindTextureCubeMap(const lcTexture* Texture)
{
if (mTextureCubeMap == Texture)
GLuint TextureObject = Texture->mTexture;
if (mTextureCubeMap == TextureObject)
return;
glBindTexture(GL_TEXTURE_CUBE_MAP, Texture);
mTextureCubeMap = Texture;
glBindTexture(GL_TEXTURE_CUBE_MAP, TextureObject);
mTextureCubeMap = TextureObject;
}
void lcContext::UnbindTexture2D(GLuint Texture)
void lcContext::ClearTexture2D()
{
if (mTexture2D != Texture)
if (mTexture2D == 0)
return;
glBindTexture(GL_TEXTURE_2D, 0);
mTexture2D = 0;
}
void lcContext::UnbindTextureCubeMap(GLuint Texture)
void lcContext::ClearTextureCubeMap()
{
if (mTextureCubeMap != Texture)
if (mTexture2D == 0)
return;
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

View file

@ -167,10 +167,11 @@ public:
void SetDepthFunction(lcDepthFunction DepthFunction);
void EnableCullFace(bool Enable);
void SetLineWidth(float LineWidth);
void BindTexture2D(GLuint Texture);
void BindTextureCubeMap(GLuint Texture);
void UnbindTexture2D(GLuint Texture);
void UnbindTextureCubeMap(GLuint Texture);
void BindTexture2D(const lcTexture* Texture);
void BindTextureCubeMap(const lcTexture* Texture);
void ClearTexture2D();
void ClearTextureCubeMap();
void SetColor(const lcVector4& Color)
{

View file

@ -280,7 +280,7 @@ void lcScene::DrawOpaqueMeshes(lcContext* Context, bool DrawLit, int PrimitiveTy
Texture->Upload(Context);
Context->SetMaterial(TexturedMaterial);
Context->BindTexture2D(Texture->mTexture);
Context->BindTexture2D(Texture);
}
else
{
@ -300,7 +300,7 @@ void lcScene::DrawOpaqueMeshes(lcContext* Context, bool DrawLit, int PrimitiveTy
#endif
}
Context->BindTexture2D(0);
Context->ClearTexture2D();
Context->SetPolygonOffset(lcPolygonOffset::None);
}
@ -392,7 +392,7 @@ void lcScene::DrawTranslucentMeshes(lcContext* Context, bool DrawLit, bool DrawF
Context->SetMaterial(TexturedMaterial);
VertexBufferOffset += Mesh->mNumVertices * sizeof(lcVertex);
Context->SetVertexFormat(VertexBufferOffset, 3, 1, 2, 0, DrawLit);
Context->BindTexture2D(Texture->mTexture);
Context->BindTexture2D(Texture);
}
const GLenum DrawPrimitiveType = Section->PrimitiveType & (LC_MESH_TRIANGLES | LC_MESH_TEXTURED_TRIANGLES) ? GL_TRIANGLES : GL_LINES;
@ -403,7 +403,7 @@ void lcScene::DrawTranslucentMeshes(lcContext* Context, bool DrawLit, bool DrawF
#endif
}
Context->BindTexture2D(0);
Context->ClearTexture2D();
Context->SetPolygonOffset(lcPolygonOffset::None);
if (!DrawFadePrepass)

View file

@ -100,7 +100,7 @@ void lcStringCache::CacheStrings(const QStringList& Strings)
DestHeight = qMax(DestHeight, SourceRect.height());
}
mTexture->SetImage(&TextureImage, LC_TEXTURE_LINEAR);
mTexture->SetImage(std::move(TextureImage), LC_TEXTURE_LINEAR);
}
void lcStringCache::GetStringDimensions(int* cx, int* cy, const QString& String) const
@ -193,7 +193,7 @@ void lcStringCache::DrawStrings(lcContext* Context, const lcMatrix44* Transforms
Context->SetVertexBufferPointer(Verts);
Context->SetVertexFormat(0, 3, 0, 2, 0, false);
Context->BindTexture2D(mTexture->mTexture);
Context->BindTexture2D(mTexture);
Context->SetColor(0.0f, 0.0f, 0.0f, 1.0f);
Context->DrawPrimitives(GL_TRIANGLES, 0, Strings.size() * 6);
}

View file

@ -189,10 +189,11 @@ bool lcTexture::Load(lcMemFile& File, int Flags)
return Load(Flags);
}
void lcTexture::SetImage(Image* Image, int Flags)
void lcTexture::SetImage(Image&& Image, int Flags)
{
mImages.clear();
mImages.emplace_back(std::move(*Image));
mImages.emplace_back(std::move(Image));
mFlags = Flags;
Load(Flags);
}
@ -200,6 +201,7 @@ void lcTexture::SetImage(Image* Image, int Flags)
void lcTexture::SetImage(std::vector<Image>&& Images, int Flags)
{
mImages = std::move(Images);
mFlags = Flags;
Load(Flags);
}
@ -228,13 +230,13 @@ void lcTexture::Upload(lcContext* Context)
{
Faces = 1;
Target = GL_TEXTURE_2D;
Context->BindTexture2D(mTexture);
Context->BindTexture2D(this);
}
else
{
Faces = 6;
Target = GL_TEXTURE_CUBE_MAP;
Context->BindTextureCubeMap(mTexture);
Context->BindTextureCubeMap(this);
}
glTexParameteri(Target, GL_TEXTURE_WRAP_S, (mFlags & LC_TEXTURE_WRAPU) ? GL_REPEAT : GL_CLAMP_TO_EDGE);
@ -320,9 +322,9 @@ void lcTexture::Upload(lcContext* Context)
}
if ((mFlags & LC_TEXTURE_CUBEMAP) == 0)
Context->UnbindTexture2D(mTexture);
Context->ClearTexture2D();
else
Context->UnbindTextureCubeMap(mTexture);
Context->ClearTextureCubeMap();
}
bool lcTexture::Load(int Flags)

View file

@ -32,7 +32,7 @@ public:
bool Load(const QString& FileName, int Flags = 0);
bool Load(lcMemFile& File, int Flags = 0);
void SetImage(Image* Image, int Flags = 0);
void SetImage(Image&& Image, int Flags = 0);
void SetImage(std::vector<Image>&& Images, int Flags = 0);
void Upload(lcContext* Context);
void Unload();

View file

@ -1451,7 +1451,7 @@ void lcView::DrawGrid()
if (Preferences.mDrawGridStuds)
{
mContext->BindTexture2D(gGridTexture->mTexture);
mContext->BindTexture2D(gGridTexture);
mContext->SetDepthWrite(false);
glEnable(GL_BLEND);

View file

@ -208,7 +208,7 @@ void lcViewSphere::Draw()
Context->DrawIndexedPrimitives(GL_TRIANGLES, mSubdivisions * mSubdivisions * 6 * 6, GL_UNSIGNED_SHORT, 0);
Context->SetMaterial(lcMaterialType::UnlitViewSphere);
Context->BindTextureCubeMap(mTexture->mTexture);
Context->BindTextureCubeMap(mTexture);
Context->SetWorldMatrix(lcMatrix44Identity());
Context->SetViewMatrix(GetViewMatrix());

View file

@ -2,6 +2,8 @@
#include <string.h>
#include "texfont.h"
#include "lc_context.h"
#include "lc_texture.h"
#include "image.h"
static const unsigned char TextureData[2048] =
{
@ -93,7 +95,6 @@ TexFont gTexFont;
TexFont::TexFont()
{
mTexture = 0;
memset(&mGlyphs, 0, sizeof(mGlyphs));
}
@ -103,26 +104,22 @@ bool TexFont::Initialize(lcContext* Context)
return true;
mFontHeight = 16;
mTextureWidth = 128;
mTextureHeight = 128;
glGenTextures(1, &mTexture);
Context->BindTexture2D(mTexture);
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);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Image Image;
Image.Allocate(mTextureWidth, mTextureHeight, lcPixelFormat::L8A8);
unsigned char ExpandedData[sizeof(TextureData) * 8 * 2];
unsigned char* ExpandedData = Image.mData;
for (unsigned int TexelIdx = 0; TexelIdx < sizeof(TextureData) * 8; TexelIdx++)
{
const unsigned char Texel = TextureData[TexelIdx / 8] & (1 << (TexelIdx % 8)) ? 255 : 0;
ExpandedData[TexelIdx * 2] = ExpandedData[TexelIdx * 2 + 1] = Texel;
}
mTextureWidth = 128;
mTextureHeight = 128;
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, mTextureWidth, mTextureHeight, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, ExpandedData);
mTexture = new lcTexture();
mTexture->SetImage(std::move(Image), LC_TEXTURE_WRAPU | LC_TEXTURE_WRAPV | LC_TEXTURE_POINT);
mTexture->Upload(Context);
const unsigned char* Ptr = GlyphData;

View file

@ -7,10 +7,10 @@ public:
bool IsLoaded() const
{
return mTexture != 0;
return mTexture != nullptr;
}
GLuint GetTexture() const
lcTexture* GetTexture() const
{
return mTexture;
}
@ -30,7 +30,7 @@ protected:
float left, right, top, bottom;
} mGlyphs[256];
GLuint mTexture;
lcTexture* mTexture = nullptr;
int mTextureWidth;
int mTextureHeight;
int mFontHeight;