如何从VertexArray SFML中找到顶点的坐标
How to find the coordinates of a Vertex from a VertexArray SFML
我使用带有Quads的VertexArray来制作一个小测试的TileMap。我试图找出如何找出什么顶点(四边形)的游戏对象上。
bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
{
// load the tileset texture
if (!m_tileset.loadFromFile(tileset))
return false;
// resize the vertex array to fit the level size
m_vertices.setPrimitiveType(sf::Quads);
m_vertices.resize(width * height * 4);
// populate the vertex array, with one quad per tile
for (unsigned int i = 0; i < width; ++i)
for (unsigned int j = 0; j < height; ++j)
{
// current tile number
int tileNumber = tiles[i + j * width];
// find its position in the tileset texture
int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);
//std::cout << tu << " : " << tv << std::endl; <- Used this to out put it all, but it kept on showing the same coordinates and slowed the program down a lot.
// current tile's quad
sf::Vertex* quad = &m_vertices[(i + j * width) * 4];
// 4 corners
quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
// 4 coordinates
quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
}
return true;
}
私人:virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// apply the transform
states.transform *= getTransform();
// apply the tileset texture
states.texture = &m_tileset;
// draw the vertex array
target.draw(m_vertices, states);
}
sf::VertexArray m_vertices;
sf::Texture m_tileset;
};
我已经尝试了一些其他的方法来显示它们,但是它们要么不能正确地执行,要么减慢了程序的速度。有人能给我指个正确的方向吗?
对
使用if语句检查TU和TV整数,这是您的瓷砖编号,因此检查它们是否等于您正在检查的瓷砖上的对象。
if (tu &TV == 1){std: <<"1"& lt; & lt;std:: endl;}
相关文章:
- 如何循环打印顶点结构
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- D3D11-将混合权重和索引传递到顶点着色器
- 从返回的顶点缓冲区查询顶点结构
- Vulkan 中的动态顶点缓冲区格式设置
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 从 CGAL 3D 网格生成中获取顶点坐标的 -6.27744e+66:mesh_implicit_sphere示例
- 如何使用OpenGL知道所有顶点的当前坐标
- 如何从 DirectXTK (ToolKit) DirectX::Model 类获取所有顶点坐标以用于冲突检测
- 计算世界坐标中z_far平面的顶点
- CGAL 曲面网格 - 修改顶点坐标
- 顶点坐标从General_polygon_2
- 从视口坐标转换为[-1,1]^2顶点[ENGGL]
- 在SFML中获取绝对顶点坐标
- 对象模型加载纹理坐标和顶点位置没有正确加载
- 将顶点坐标转换为屏幕坐标"same"
- 如何从VertexArray SFML中找到顶点的坐标
- 这段代码中的顶点和纹理坐标代表什么?