OpenGL + GLSL + SDL_TTF Текст не отображается правильно [закрыто]C++

Программы на C++. Форум разработчиков
Anonymous
OpenGL + GLSL + SDL_TTF Текст не отображается правильно [закрыто]

Сообщение Anonymous »

Я пытаюсь отобразить текст, используя SDL_TTF, OpenGL и Shaders. Это мой код. < /P>
vertex shader < /p>

Код: Выделить всё

#version 330

layout (location = 0) in vec2 AttribVertex;
layout (location = 1) in vec2 AttribCoordUV;

out vec2 CoordUV;

uniform mat4 UniformProjection;
uniform mat4 UniformModelView;

void main(){
mat4 MVP = UniformProjection * UniformModelView;

gl_Position = MVP * vec4(AttribVertex,1.0,1.0);
CoordUV = AttribCoordUV;
}
< /code>
фрагментный шейдер < /p>
#version 330

uniform sampler2D UniformTexture;

in  vec2 CoordUV;
out vec4 FragColor;

void main(){
FragColor = texture(UniformTexture,CoordUV);
}
< /code>
texrender function < /p>
void TextRender(string Text){
TTF_Font *Font = TTF_OpenFont("assets/Fonts/Roboto-Bold.ttf",72);
if(Font == NULL){
cout h/2.0f,
-(GLfloat)Surface->w/2.0f,-(GLfloat)Surface->h/2.0f,
(GLfloat)Surface->w/2.0f,-(GLfloat)Surface->h/2.0f};

GLfloat coordtex[8] = {0.0f,0.0f,
1.0f,0.0f,
0.0f,1.0f,
1.0f,1.0f};

GLfloat buffer[16] = {vertice[0],vertice[1],coordtex[0],coordtex[1],
vertice[2],vertice[3],coordtex[2],coordtex[3],
vertice[4],vertice[5],coordtex[4],coordtex[5],
vertice[6],vertice[7],coordtex[6],coordtex[7]};

GLuint vbo,vao;
glGenVertexArrays(1,&vao);
glGenBuffers(1,&vbo);

glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER,vbo);

glBufferData(GL_ARRAY_BUFFER,sizeof(buffer),buffer,GL_STATIC_DRAW);

glVertexAttribPointer(attribtVRT,2,GL_FLOAT,GL_FALSE,4 * sizeof(GLfloat),(void*)0);
glEnableVertexAttribArray(attribtVRT);

glVertexAttribPointer(attribtCUV,2,GL_FLOAT,GL_FALSE,4 * sizeof(GLfloat),(void*)(2*sizeof(GLfloat)));
glEnableVertexAttribArray(attribtCUV);

glBindBuffer(GL_ARRAY_BUFFER,0);
glBindVertexArray(0);

GLuint Texture;
glGenTextures(1,&Texture);
glBindTexture(GL_TEXTURE_2D,Texture);

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

glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,Surface->w,Surface->h,0,GL_RGBA,GL_UNSIGNED_BYTE,Surface->pixels);
glBindTexture(GL_TEXTURE_2D,0);

glm::mat4 Model = glm::translate(glm::mat4(1.0f),glm::vec3(100.0f,100.0f,-0.1f));
glm::mat4 ModelView = mxGlobal.Matriz(VIEW) * Model;
glm::mat4 Projection = mxGlobal.Matriz(PROYECTION);

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

Shader->Use(true);
glUniformMatrix4fv(uniformPRY,1,GL_FALSE,&Projection[0][0]);
glUniformMatrix4fv(uniformMVW,1,GL_FALSE,&ModelView[0][0]);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,Texture);
glUniform1i(uniformTEX,0);

glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLE_STRIP,0,4);
glBindVertexArray(0);

glBindTexture(GL_TEXTURE_2D,0);
Shader->Use(false);

glDeleteTextures(1,&Texture);
glDeleteVertexArrays(1,&vao);
glDeleteBuffers(1,&vbo);

glDisable(GL_BLEND);

TTF_CloseFont(Font);
}
Но когда я запускаю свою программу, я получаю это ...

Я попробовал изменить параметры в функции GltexixAge2d (GL_RGBA, GL_BGRA и т. Д.) Изменение значений, таких как ... < /p>

Код: Выделить всё

glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,Surface->w,Surface->h,0,GL_RGBA,GL_UNSIGNED_BYTE,Surface->pixels);
< /code>
или < /p>
glTexImage2D(GL_TEXTURE_2D,0,GL_BGRA,Surface->w,Surface->h,0,GL_RGBA,GL_UNSIGNED_BYTE,Surface->pixels);
< /code>
или < /p>
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,Surface->w,Surface->h,0,GL_BGRA,GL_UNSIGNED_BYTE,Surface->pixels);
< /code>
или < /p>
glTexImage2D(GL_TEXTURE_2D,0,GL_BGRA,Surface->w,Surface->h,0,GL_BGRA,GL_UNSIGNED_BYTE,Surface->pixels);
< /code>
, и я даже использовал такие функции, как ... < /p>
GLuint colours = image->format->BytesPerPixel;
GLuint externalFormat, internalFormat;
SDL_PixelFormat *format = image->format;
if (colours == 4) {

if (image->format->Rmask == 0x000000ff)
externalFormat = GL_RGBA;
else
externalFormat = GL_BGRA;
}
else {

// no alpha
if (image->format->Rmask == 0x000000ff)
externalFormat = GL_RGB;
else
externalFormat = GL_BGR;
}
internalFormat = (colours == 4) ? GL_RGBA : GL_RGB;
< /code>
Я также использовал < /p>
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Но это не работает .... мой вопрос: что я делаю не так?

Подробнее здесь: https://stackoverflow.com/questions/796 ... -correctly

Вернуться в «C++»