Freetype 和 OpenGL 的访问冲突
Access Violation With Freetype and OpenGL
当我运行程序时,窗口显示大约 3 秒(如预期的那样是白色背景,但没有文本),然后中断,说 FreeType 代码中存在访问冲突。我很确定这是加载字体,因为我的调试文本打印它找不到它,而且它也不能使用面部的宽度和高度,所以它返回一个错误。我尝试将 arial.ttf 放在 .exe 目录、项目目录中,无处不在。没有骰子。这是我的主要.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.");
}
这是我的文字.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
这是我的文字.cpp:
#define GLEW_STATIC
#include "text.h"
const char* textvertexSource =
"version 150n"
"in vec4 coord;"
"out vec2 texcoord;"
"void main {"
" gl_Position = vec4(coord.xy, 0, 1);"
" texcoord = coord.zw;"
"}";
const char* textfragmentSource =
"version 150n"
"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
方法以使其在屏幕上呈现。
问题是您正在使用 sizex 和 sizey 设置FT_Set_Pixel_Sizes,然后在为文本创建框时,您使用 sizex 和 sizey 来缩放框。
因此,如果您将 sizex 和 sizey 设置为 48,这也意味着您将文本框缩放 48,这很大,如果您将 sizex 和 sizey 设置为 1,那么字体的像素大小非常小。
修复:添加参数大小或高度和宽度以创建函数并将其用于FT_Set_Pixel_Size
前任。
void Create(char* Text, char* fontName, int posx, int posy, int r, int g, int b, int sizex, int sizey, int height, int width){
...
FT_Set_Pixel_Size(face, width, height); //Not sizex and sizey because thats what you are using to scale the text box
}
相关文章:
- 写入位置0x0000000C时发生访问冲突
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 链表中写入访问冲突的未知原因
- C++中的openCV Mat访问冲突
- C++尝试深度复制唯一指针时出现内存访问冲突
- C++ 中动态二维数组的访问冲突
- 从嵌套循环中的 std::list 中删除将返回访问冲突
- 写入访问冲突异常
- 在类 12.exe 中0x7B37FF80 (ucrtbased.dll) 引发异常: 0xC0000005:访问冲突读
- 0xC0000005:访问冲突写入位置0xCDCDCDCD动态分配错误
- 集成显卡上的 OpenGL 访问冲突
- 在Visual Studio Community中使用glfw3 glew和opengl获取访问冲突异常
- OpenGL:glDrawArrays()引发异常(nvoglv32.dll)读取位置0x00000000时发生访问冲突
- Freetype 和 OpenGL 的访问冲突
- openGL - 调用 glMatrixMode(..) 时的访问冲突
- 使用OpenGL和DevIL调用glTextureParameteri时发生访问冲突
- OpenGL中的访问冲突
- opengl / glsl / sdl代码中的c++访问冲突
- Cuda+OpenGL互操作性,glDrawArrays()访问冲突
- 0xC0000005:访问冲突执行位置0x00000000.(OpenGL)