gpt4 book ai didi

c++ - 从屏幕底部绘制纹理

转载 作者:搜寻专家 更新时间:2023-10-31 02:07:06 25 4
gpt4 key购买 nike

我有点不明白为什么。

出于某种原因,我的绘制功能使我的纹理从下往上绘制。

所以 0 是底部是我的代码做错了什么。宽度像往常一样从左到右绘制。

void Texture::draw(float x, float y)
{
// OpenGL calls
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

GLfloat vertices_position[] = {
x, y,
x + m_width, y,
x + m_width, y + m_height,
x, y + m_height,
};
GLfloat texture_coord[] = {
1.0f, 1.0f, // top right
1.0f, 0.0f,// bottom right
0.0f, 0.0f,// bottom left
0.0f, 1.0f,// top left
};

// snap to pixel
int shiftX = (int)(x + 0.5f);
int shiftY = (int)(y + 0.5f);


glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vertices_position);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, texture_coord);

glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
//glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
float color[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
glColor4fv(color);

glPushMatrix();
//glTranslatef(m_width, 1.0f, 0);
//glRotatef(90, 0.0f, 0, 1);

glBindTexture(GL_TEXTURE_2D, m_object);
glDrawArrays(GL_QUADS, 0, 4);

glPopMatrix();

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

glDisable(GL_BLEND);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glBindTexture(GL_TEXTURE_2D, 0);
}

编辑:这修复了我的图像的旋转

GLfloat vertices_position[] = {
x + m_width, y,
x + m_width, y + m_height,
x, y + m_height,
x, y
};

最佳答案

参见 OpenGL 4.6 API core profile specification; 8.5. TEXTURE IMAGE SPECIFICATION; page 214

An element (i, j, k) of the texture image is called a texel (for a two-dimensional texture or one-dimensional array texture, k is irrelevant; for a one-dimensional texture, j and k are both irrelevant). The texture value used in texturing a fragment is determined by sampling the texture in a shader, but may not correspond to any actual texel. See figure 8.3.

textureimage

Figure 8.3. A texture image and the coordinates used to access it. This is a twodimensional texture with width 8 and height 4. A one-dimensional texture would consist of a single horizontal strip. α and β, values used in blending adjacent texels to obtain a texture value are also shown.


查看基元类型 GL_QUAD 的基元顺序:

quad

这意味着您的代码会产生以下结果:

GLfloat vertices_position[] = {
x, y,
x + m_width, y,
x + m_width, y + m_height,
x, y + m_height,
};
GLfloat texture_coord[] = {
1.0f, 1.0f,
1.0f, 0.0f
0.0f, 0.0f,
0.0f, 1.0f,
};

            x, y           x + m_width, y              1.0f, 1.0f           1.0f, 0.0f
o------->o o------->o
| |
| |
v v
o<-------o o<-------o
x, y + m_height x + m_width, y + m_height 0.0f, 1.0f 0.0f, 0.0f

要实现您想要的结果,您要么必须更改纹理坐标:

GLfloat texture_coord[] = {
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 0.0f
};

            x, y           x + m_width, y              0.0f, 1.0f           1.0f, 1.0f
o------->o o------->o
| |
| |
v v
o<-------o o<-------o
x, y + m_height x + m_width, y + m_height 0.0f, 0.0f 1.0f, 0.0f

或者顶点坐标:

GLfloat vertices_position[] = {
x + m_width, y,
x + m_width, y + m_height,
x, y + m_height,
x, y
};

            x, y          x + m_width, y               0.0f, 1.0f           1.0, 1.0
o o o o
^ | ^ |
| | | |
| v | v
o<-------o o<-------o
x, y + m_height x + m_width, y + m_height 0.0f, 0.0f 1.0f, 0.0f



关于以下评论的答案扩展:

如果你想把纹理坐标编码成一个数组(x, y, z, u, v)

GLfloat vertex_attributes[] = {
x, y, z, 0.0f, 1.0f,
x + m_width, y, z, 1.0f, 1.0f,
x + m_width, y + m_height, z, 1.0f, 0.0f,
x, y + m_height, z, 0.0f, 0.0f
};

然后你必须在 glVertexPointer 中设置 stride 参数和 glTexCoordPointer

GLsizei stride = 5 * sizeof(GLfloat); // 5 because of x, y, z, u, v

GLfloat *vertex_coordinates = vertex_attributes;
GLfloat *texture_coordiantes = vertex_attributes; + 3 // 3 because of x, y, z

glVertexPointer(3, GL_FLOAT, stride, vertex_coordinates);
glTexCoordPointer(2, GL_FLOAT, stride, texture_coordiantes);



答案的第二次扩展,关于以下评论:

Fixed Function Pipeline纹理坐标由纹理矩阵转换。如果要翻转 v 坐标,则必须反转纹理矩阵的 y 轴(参见 glMatrixMode):

glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glScalef(1.0f, -1.0f, 1.0f);

请注意,不要忘记在“glOrtho”之前正确设置矩阵模式 (glMatrixMode(GL_PROJECTION);)。

关于c++ - 从屏幕底部绘制纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49204750/

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