gpt4 book ai didi

c++ - Freetype 和 OpenGL 的访问冲突

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:28 24 4
gpt4 key购买 nike

当我运行我的程序时,窗口显示大约 3 秒(如预期的那样具有白色背景,但没有文本)然后中断,说 FreeType 代码中存在访问冲突。我很确定这是加载字体,因为我的调试文本打印它找不到它,而且它也不能使用面部的宽度和高度,所以它返回一个错误。我试过将 arial.ttf 放在 .exe 目录、项目目录中的任何地方。没有骰子。这是我的 main.cpp:

int main()
{
int running = GL_TRUE;

debug.Print("Program initialized...");
window.Create( 800, 600, "OpenGL");

TEXT text;

while( running )
{
glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT );

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

text.Create("The quick brown fox jumps over the lazy dog", "arial.ttf", 300, 400, 1, 1, 1, 2/600, 2/800);

glfwSwapBuffers();
}

glDeleteTextures(1, &tex);

glDeleteProgram( shaderProgram );
glDeleteShader( fragmentShader );
glDeleteShader( vertexShader );

glDeleteBuffers( 1, &vbo );

debug.Print("Program terminated.");
}

这是我的 Text.h:

#ifndef _TEXT
#define _TEXT

#include "debug.h"
#include <GL/glew.h>
#include <GL/glfw.h>
#include <ft2build.h>
#include FT_FREETYPE_H

class TEXT
{
public:
void Create(char* Text, char* fontName, int posx, int posy, int r, int g, int b, int sizex, int sizey);
};

#endif

这是我的 Text.cpp:

#define GLEW_STATIC
#include "text.h"

const char* textvertexSource =
"version 150\n"
"in vec4 coord;"
"out vec2 texcoord;"
"void main {"
" gl_Position = vec4(coord.xy, 0, 1);"
" texcoord = coord.zw;"
"}";

const char* textfragmentSource =
"version 150\n"
"in vec2 texcoord;"
"uniform sampler2D tex;"
"uniform vec4 color;"
"void main() {"
" gl_FragColor = vec4(1, 1, 1, texture2D(tex, texcoord).a * color;"
"}";

void TEXT::Create(char* text, char* fontName, int posx, int posy, int r, int g, int b, int sizex, int sizey)
{

// OpenGL functions
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);

GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER );
glShaderSource( vertexShader, 1, &textvertexSource, NULL );
glCompileShader( vertexShader );

GLuint fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( fragmentShader, 1, &textfragmentSource, NULL );
glCompileShader( fragmentShader );

GLuint shaderProgram = glCreateProgram();
glAttachShader( shaderProgram, vertexShader );
glAttachShader( shaderProgram, fragmentShader );
glBindFragDataLocation( shaderProgram, 0, "outColor" );
glLinkProgram( shaderProgram );
glUseProgram( shaderProgram );

GLint posAttrib = glGetAttribLocation( shaderProgram, "coord" );
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 4, GL_FLOAT, GL_FALSE, 0, 0);

GLint colAttrib = glGetAttribLocation( shaderProgram, "color" );
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 4, GL_INT, GL_FALSE, 0, 0);

GLint uniform_tex = glGetUniformLocation(shaderProgram, "tex");

GLuint tex;
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(uniform_tex, 0);

GLuint uniform_color = glGetUniformLocation(shaderProgram, "color");

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

// Actual font rendering
FT_Library ft;

if(FT_Init_FreeType(&ft))
debug.Print("Could not initiate freetype library.");

FT_Face face;
if(FT_New_Face(ft, fontName, 0, &face))
{
debug.Print("Could not load font: ");
debug.Print(fontName);
}

FT_Set_Pixel_Sizes(face, sizex, sizey);

FT_GlyphSlot glyph = face->glyph;

const char *p;

for(p = text; *p; p++) {
if(FT_Load_Char(face, *p, FT_LOAD_RENDER))
continue;

glTexImage2D(
GL_TEXTURE_2D,
0,
GL_ALPHA,
glyph->bitmap.width,
glyph->bitmap.rows,
0,
GL_ALPHA,
GL_UNSIGNED_BYTE,
glyph->bitmap.buffer
);

float x2 = posx + glyph->bitmap_left * sizex;
float y2 = -posy - glyph->bitmap_top * sizey;
float w = glyph->bitmap.width * sizex;
float h = glyph->bitmap.rows * sizey;

GLfloat box[4][4] = {
{x2, -y2 , 0, 0},
{x2 + w, -y2 , 1, 0},
{x2, -y2 - h, 0, 1},
{x2 + w, -y2 - h, 1, 1},
};

glBufferData(GL_ARRAY_BUFFER, sizeof box, box, GL_DYNAMIC_DRAW);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

posx += (glyph->advance.x >> 6) * sizex;
posy += (glyph->advance.y >> 6) * sizey;

GLfloat color[4] = {r, g, b, 1};
glUniform4fv(uniform_color, 1, color);
}
}

最佳答案

我将尝试扩展我的评论。

在您的 while( running ) 循环中,您调用 text.Create("The quick brown fox jumps over the lazy dog", "arial.ttf", 300, 400, 1 , 1, 1, 2/600, 2/800);。当查看此函数的细节时,它所做的不仅仅是简单地创建一个新的文本条目,它还初始化 FreeType、加载 fonts 等...

您希望您的游戏/模拟只初始化一次 FreeType 并让 text.Create 返回一个将持续整个游戏/模拟的对象。

while(running) 部分,您应该调用对象的 Render 方法以将其呈现在屏幕上。

关于c++ - Freetype 和 OpenGL 的访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13521466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com