/*
glfont.hpp
sdragonx 2019-08-15 00:03:33
opengl字体类,提供初学者参考学习
opengl初始化之后,创建字体
font.init(L"微软雅黑", 32, 512);
然后在绘制函数里面添加以下测试代码:
//开启2D模式,后面的800x600要根据窗口的实际客户区大小设置,不然缩放之后效果不好
push_view2D(0, 0, 800, 600);
wchar_t* str = L"abcdef字体绘制abcdef?123456ijk微软雅黑";
font.color = vec4ub(255, 255, 255, 128);
font.print(0, 0, str, wcslen(str));
font.tab_print(0, 32, L"abc\t123456\t7890", TEXT_MAX);
wchar_t* tabled = L"abcdef字体绘制\tabc制表符\t123456";
font.color = vec4ub(255, 0, 0, 255);
font.tab_print(0, 64, tabled, wcslen(tabled));
font.color = vec4ub(255, 0, 0, 255);
font.draw_text(0, 200, 200, 400, str, wcslen(str), PT_LEFT);
font.color = vec4ub(0, 255, 0, 255);
font.draw_text(300, 200, 200, 400, str, wcslen(str), PT_CENTER);
font.color = vec4ub(255, 0, 255, 255);
font.draw_text(600, 200, 200, 400, str, wcslen(str), PT_RIGHT);
pop_view();
//代码结束
*/
#ifndef GLFONT_HPP_20190815000333
#define GLFONT_HPP_20190815000333
#include <windows.h>
#include <stdint.h>
#include <map>
#include <vector>
//opengl头文件,根据环境更改
#ifdef __glew_h__
#include <glew.h>
#elif defined(__glad_h_)
#include <glad.h>
#else
#include <gl/gl.h>
#endif
#if defined(__GNUC__) || defined(__clang__)
#define TYPENAME typename
#else
#define TYPENAME
#endif
#define CGL_DEFAULT_FONT_SIZE 16 //默认字体大小
#define TEXT_MAX UINT32_MAX //0xFFFFFFFF
namespace cgl{
#pragma pack(push, 1)
//大纹理里面小图块结构
class teximage
{
public:
typedef teximage this_type;
public:
intptr_t image; //纹理
uint16_t x, y, width, height; //小图在大图里面的位置信息
float u1, v1, u2, v2; //小图的uv坐标信息
public:
teximage():image(), x(), y(), width(), height(), u1(0.0f), v1(0.0f), u2(1.0f), v2(1.0f)
{
}
this_type& operator=(const this_type& div)
{
image = div.image;
x = div.x;
y = div.y;
width = div.width;
height = div.height;
u1 = div.u1;
v1 = div.v1;
u2 = div.u2;
v2 = div.v2;
return *this;
}
};
//字符信息
//如果一个字符需要输出到left、top的位置,字符实际位置是left+x、top+y
//输出完毕之后,下一个字符的位置是left+next_x、top+next_y
struct char_info
{
int16_t x; //字符偏移位置
int16_t y;
int16_t next_x;//字符大小,下一字符偏移位置
int16_t next_y;
};
//vec3<T>
template<typename T>
struct vec3
{
T x, y, z;
};
typedef vec3<int> vec3i;
typedef vec3<float> vec3f;
//vec4<T>
template<typename T>
struct vec4
{
union{
T data[4];
struct{
T x, y, width, height;
};
struct{
T red, green, blue, alpha;
};
};
vec4() : x(), y(), width(), height(){/*void*/}
vec4(T vx, T vy, T vw, T vh) : x(vx), y(vy), width(vw), height(vh){/*void*/}
};
typedef vec4<int> vec4i;
typedef vec4<float> vec4f;
typedef vec4<BYTE> vec4ub;
template<typename VT, typename TT, typename CT>
struct vtx3t2c4
{
typedef vtx3t2c4 this_type;
typedef vec4<CT> color_type;
VT x, y, z;
TT u, v;
color_type color;
vtx3t2c4() : x(), y(), z(), u(), v(), color(){/*void*/}
vtx3t2c4(const vec3<VT>& vtx, TT tu, TT tv, const color_type& cc) :
x(v.x), y(v.y), z(v.z), u(tu), v(tv), color(cc) { /*void*/ }
vtx3t2c4(VT vx, VT vy, VT vz, TT vu, TT vv, const color_type& cc) :
x(vx), y(vy), z(vz), u(vu), v(vv), color(cc) { /*void*/ }
this_type& operator=(const vec3<VT>& p)
{
x = p.x;
y = p.y;
z = p.z;
return *this;
}
};
typedef vtx3t2c4<float, float, uint8_t> vtx3f;
#pragma pack(pop)
//---------------------------------------------------------------------------
//opengl的一些扩展函数
//2D视觉模式,如果你使用的是矩阵操作,把这里面的函数替换成矩阵操作
void push_view2D(int left, int top, int width, int height)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
/*
#if CGL_COORDINATE == CGL_LOWER_LEFT
//直角坐标系
this->ortho(left, width, top, height, 0, INT16_MAX);
//重新设置正面,默认GL_CCW
glFrontFace(GL_CCW);
#else*/
//windows坐标系
glOrtho(left, left+width, top+height, top, 0, INT16_MAX);
glFrontFace(GL_CW);
//#endif
//十字坐标系
//glOrtho(-width/2, width/2, -height/2, height/2, 0, INT_MAX);//
//反转屏
OpenGL字体绘制
最新推荐文章于 2024-01-19 01:45:00 发布