OPENGL C++ 瓦片在它们之间呈现小间隙
OPENGL C++ tiles rendered with small gaps between them
由于 pygame 中的性能问题,我一直在尝试用 c++ 重新创建我的 pygame RPG。我已经开始渲染瓷砖。但是,当您进入全屏渲染这些图块时,您会看到图块之间的线条,我认为这是由计算图块位置或纹理顶点时的舍入误差引起的。我制作了一个将整数 -1.0f 转换为 1.0f 坐标 opengl 使用的系统,我认为它是四舍五入或不正确,我无法弄清楚要更改什么以消除它们之间的差距。
这是我看到的图片
这是我的图块的代码class
class Tile {
public:
//coordinates for placement and texture coordinates
/*
b = bottom
t = top
l = left
r = right5
x,y = x,y lol dumbass
t = texture coordinate
*/
float bl_x, tl_x, tr_x, br_x, bl_y, tl_y, tr_y, br_y;
float t_bl_x, t_bl_y, t_tl_x, t_tl_y, t_tr_x, t_tr_y, t_br_x, t_br_y;
Tile(int x, int y, int width, int height, std::vector<float>* vertices, std::vector<unsigned int>* indices) {
unsigned int tex_x = 0.0f;
unsigned int tex_y = 2.0f * 32.0f;
int SCR_WIDTH = 640;
int SCR_HEIGHT = 360;
//convert x,y coords to open gl coords (between -1 and 1) (prop means proportion of screen)
float x_prop = float(float(x) / float(SCR_WIDTH)) * 2.0f - 1.0f;
float y_prop = float(float(y) / float(SCR_HEIGHT)) * 2 - 1;
float width_prop = float(float(x + width) / float(SCR_WIDTH)) * 2.0f - 1.0f;
float height_prop = float(float(y + height) / float(SCR_HEIGHT)) * 2 - 1;
//convert texture x,y coords open gl coords (BETWEEN 0 and 1) (prop means proportion of screen)
float tex_x_prop = float(float(tex_x) / 641.0f);
float tex_y_prop = float(float(tex_y) / 288.0f);
float tex_width_prop = float(float(tex_x + width) / 641.0f);
float tex_height_prop = float(float(tex_y + height) / 288.0f);
tl_x = x_prop;
tl_y = y_prop;
bl_x = x_prop;
bl_y = height_prop;
br_x = width_prop;
br_y = height_prop;
tr_x = width_prop;
tr_y = y_prop;
//texture coordinates
t_bl_x = tex_x_prop;
t_bl_y = tex_y_prop;
t_tl_x = tex_x_prop;
t_tl_y = tex_height_prop;
t_tr_x = tex_width_prop;
t_tr_y = tex_height_prop;
t_br_x = tex_width_prop;
t_br_y = tex_y_prop;
// --- push back coordinates to the vertex buffer----
//bottom left coords
vertices->push_back(bl_x);
vertices->push_back(bl_y);
vertices->push_back(0.0f);
//bottom left color
vertices->push_back(0.0f);
vertices->push_back(0.0f);
vertices->push_back(0.0f);
//bottom left texture coords
vertices->push_back(t_bl_x);
vertices->push_back(t_bl_y);
//top left coords
vertices->push_back(tl_x);
vertices->push_back(tl_y);
vertices->push_back(0.0f);
//top left color
vertices->push_back(0.0f);
vertices->push_back(0.0f);
vertices->push_back(0.0f);
//top left texture coords
vertices->push_back(t_tl_x);
vertices->push_back(t_tl_y);
//top right coords
vertices->push_back(tr_x);
vertices->push_back(tr_y);
vertices->push_back(0.0f);
//top right color
vertices->push_back(0.0f);
vertices->push_back(0.0f);
vertices->push_back(0.0f);
//top right texture coords
vertices->push_back(t_tr_x);
vertices->push_back(t_tr_y);
//bottom right coords
vertices->push_back(br_x);
vertices->push_back(br_y);
vertices->push_back(0.0f);
//bottom right color
vertices->push_back(0.0f);
vertices->push_back(0.0f);
vertices->push_back(0.0f);
//bottom right texture coords
vertices->push_back(t_br_x);
vertices->push_back(t_br_y);
// --- push back indices ---
unsigned int p = indices->size() / 6 * 4;
indices->push_back(0 + p);
indices->push_back(1 + p);
indices->push_back(3 + p);
indices->push_back(1 + p);
indices->push_back(2 + p);
indices->push_back(3 + p);
}
};
通过在渲染时从 GL_LINEAR 更改为 GL_NEAREST 来修复 :) 谢谢 Raildex
一旦我发现我处于相同的情况(一些图块之间的细线),我决定从每个图块纹理方法跳到“单一纹理,逻辑图块”方法。所以,所有的瓦片都应该根据它们在屏幕上的逻辑位置渲染成一个大纹理。然后,将计算出的大纹理传递给 OpenGL 渲染器。
由于 pygame 中的性能问题,我一直在尝试用 c++ 重新创建我的 pygame RPG。我已经开始渲染瓷砖。但是,当您进入全屏渲染这些图块时,您会看到图块之间的线条,我认为这是由计算图块位置或纹理顶点时的舍入误差引起的。我制作了一个将整数 -1.0f 转换为 1.0f 坐标 opengl 使用的系统,我认为它是四舍五入或不正确,我无法弄清楚要更改什么以消除它们之间的差距。
这是我看到的图片
这是我的图块的代码class
class Tile {
public:
//coordinates for placement and texture coordinates
/*
b = bottom
t = top
l = left
r = right5
x,y = x,y lol dumbass
t = texture coordinate
*/
float bl_x, tl_x, tr_x, br_x, bl_y, tl_y, tr_y, br_y;
float t_bl_x, t_bl_y, t_tl_x, t_tl_y, t_tr_x, t_tr_y, t_br_x, t_br_y;
Tile(int x, int y, int width, int height, std::vector<float>* vertices, std::vector<unsigned int>* indices) {
unsigned int tex_x = 0.0f;
unsigned int tex_y = 2.0f * 32.0f;
int SCR_WIDTH = 640;
int SCR_HEIGHT = 360;
//convert x,y coords to open gl coords (between -1 and 1) (prop means proportion of screen)
float x_prop = float(float(x) / float(SCR_WIDTH)) * 2.0f - 1.0f;
float y_prop = float(float(y) / float(SCR_HEIGHT)) * 2 - 1;
float width_prop = float(float(x + width) / float(SCR_WIDTH)) * 2.0f - 1.0f;
float height_prop = float(float(y + height) / float(SCR_HEIGHT)) * 2 - 1;
//convert texture x,y coords open gl coords (BETWEEN 0 and 1) (prop means proportion of screen)
float tex_x_prop = float(float(tex_x) / 641.0f);
float tex_y_prop = float(float(tex_y) / 288.0f);
float tex_width_prop = float(float(tex_x + width) / 641.0f);
float tex_height_prop = float(float(tex_y + height) / 288.0f);
tl_x = x_prop;
tl_y = y_prop;
bl_x = x_prop;
bl_y = height_prop;
br_x = width_prop;
br_y = height_prop;
tr_x = width_prop;
tr_y = y_prop;
//texture coordinates
t_bl_x = tex_x_prop;
t_bl_y = tex_y_prop;
t_tl_x = tex_x_prop;
t_tl_y = tex_height_prop;
t_tr_x = tex_width_prop;
t_tr_y = tex_height_prop;
t_br_x = tex_width_prop;
t_br_y = tex_y_prop;
// --- push back coordinates to the vertex buffer----
//bottom left coords
vertices->push_back(bl_x);
vertices->push_back(bl_y);
vertices->push_back(0.0f);
//bottom left color
vertices->push_back(0.0f);
vertices->push_back(0.0f);
vertices->push_back(0.0f);
//bottom left texture coords
vertices->push_back(t_bl_x);
vertices->push_back(t_bl_y);
//top left coords
vertices->push_back(tl_x);
vertices->push_back(tl_y);
vertices->push_back(0.0f);
//top left color
vertices->push_back(0.0f);
vertices->push_back(0.0f);
vertices->push_back(0.0f);
//top left texture coords
vertices->push_back(t_tl_x);
vertices->push_back(t_tl_y);
//top right coords
vertices->push_back(tr_x);
vertices->push_back(tr_y);
vertices->push_back(0.0f);
//top right color
vertices->push_back(0.0f);
vertices->push_back(0.0f);
vertices->push_back(0.0f);
//top right texture coords
vertices->push_back(t_tr_x);
vertices->push_back(t_tr_y);
//bottom right coords
vertices->push_back(br_x);
vertices->push_back(br_y);
vertices->push_back(0.0f);
//bottom right color
vertices->push_back(0.0f);
vertices->push_back(0.0f);
vertices->push_back(0.0f);
//bottom right texture coords
vertices->push_back(t_br_x);
vertices->push_back(t_br_y);
// --- push back indices ---
unsigned int p = indices->size() / 6 * 4;
indices->push_back(0 + p);
indices->push_back(1 + p);
indices->push_back(3 + p);
indices->push_back(1 + p);
indices->push_back(2 + p);
indices->push_back(3 + p);
}
};
通过在渲染时从 GL_LINEAR 更改为 GL_NEAREST 来修复 :) 谢谢 Raildex
一旦我发现我处于相同的情况(一些图块之间的细线),我决定从每个图块纹理方法跳到“单一纹理,逻辑图块”方法。所以,所有的瓦片都应该根据它们在屏幕上的逻辑位置渲染成一个大纹理。然后,将计算出的大纹理传递给 OpenGL 渲染器。