我的OpenGL平面有问题
Having problems with my OpenGL plane
我在使用顶点数组创建平面(使用GL_TRIANGLE)时遇到了一点麻烦。
下面是我的代码:
float halfW = (getPlaneWidth() / 2);
float halfD = (getPlaneDepth() / 2);
//Generate vertices
for(int z = halfD; z >= -halfD; z--)
{
for(int x = -halfW; x <= halfW; x++)
{
float xV = x * cellWidth;
float yV = 0;
float zV = z * cellDepth;
Vertices.push_back(xV);
Vertices.push_back(yV);
Vertices.push_back(zV);
float xN = xV - getX();
float yN = yV - getY();
float zN = zV - getZ();
setNormals(&xN, &yN, &zN); //Calculate normals
Normals.push_back(xN);
Normals.push_back(yN);
Normals.push_back(zN);
}
}
//Generate indices
for(int y = 0; y < getPlaneDepth(); y++)
{
for(int x = 0; x < getPlaneWidth(); x++)
{
int curVertex = (x + (y * (getPlaneDepth() + 1))); //Bottom left vertex ID
if(curVertex%2 == 0)
{
Indices.push_back((x) + (y) * (getPlaneDepth()+1)); //Bottom Left
Indices.push_back((x+1) + (y) * (getPlaneDepth()+1)); //Bottom Right
Indices.push_back((x+1) + (y+1) * (getPlaneDepth()+1)); //Top Right
Indices.push_back((x+1) + (y+1) * (getPlaneDepth()+1)); //Top Right
Indices.push_back((x) + (y+1) * (getPlaneDepth()+1)); //Top Left
Indices.push_back((x) + (y) * (getPlaneDepth()+1)); //Bottom Left
}
else //reverse triangle
{
Indices.push_back((x+1) + (y) * (getPlaneDepth()+1)); //Bottom Right
Indices.push_back((x) + (y) * (getPlaneDepth()+1)); //Bottom Left
Indices.push_back((x) + (y+1) * (getPlaneDepth()+1)); //Top Left
Indices.push_back((x) + (y+1) * (getPlaneDepth()+1)); //Top Left
Indices.push_back((x+1) + (y+1) * (getPlaneDepth()+1)); //Top Right
Indices.push_back((x+1) + (y) * (getPlaneDepth()+1)); //Bottom Right
}
}
}
如果宽度和深度相同,代码可以正常工作,但如果它们不同,它就会出错。
有人看到问题了吗?
我已经这样编码了,这样枢轴点就在平面的中间。
int curVertex = (x + (y * (getPlaneWidth() + 1))); //Bottom left vertex ID
if(curVertex%2 == 0)
{
Indices.push_back((x) + (y) * (getPlaneWidth()+1)); //Bottom Left
Indices.push_back((x+1) + (y) * (getPlaneWidth()+1)); //Bottom Right
Indices.push_back((x+1) + (y+1) * (getPlaneWidth()+1)); //Top Right
Indices.push_back((x+1) + (y+1) * (getPlaneWidth()+1)); //Top Right
Indices.push_back((x) + (y+1) * (getPlaneWidth()+1)); //Top Left
Indices.push_back((x) + (y) * (getPlaneWidth()+1)); //Bottom Left
}
else //reverse triangle
{
Indices.push_back((x+1) + (y) * (getPlaneWidth()+1)); //Bottom Right
Indices.push_back((x) + (y) * (getPlaneWidth()+1)); //Bottom Left
Indices.push_back((x) + (y+1) * (getPlaneWidth()+1)); //Top Left
Indices.push_back((x) + (y+1) * (getPlaneWidth()+1)); //Top Left
Indices.push_back((x+1) + (y+1) * (getPlaneWidth()+1)); //Top Right
Indices.push_back((x+1) + (y) * (getPlaneWidth()+1)); //Bottom Right
}
?
相关文章:
- 我似乎对if/else的基本语句有问题:/
- 我在范围内未声明的错误类有问题
- 我的C++语言蛮力算法有问题
- 我关于函数"Assert"的C++代码有问题
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- C++类中的友元函数有问题?
- 我对数组即 0x6dfe78 有问题
- 我对 std::unique(算法)C++有问题
- 使用 fstream 库并在屏幕上打印的文件有问题?
- 这个模板为什么有问题?如何正确编译
- 我对 MyGraph 属性顶点名称和边权重有问题
- delete[]有问题,如何部分删除内存
- 在C++中使用exit()退出程序有问题吗
- 跳转到if(false)块有问题吗
- 查找有问题的宏
- 使用 rand() 和 srand() 有问题
- C++ 回文程序总是给出 0(假)作为输出问题;我的代码哪里有问题?
- 我在 C++ "out_of_range at memory location"有问题
- 我在 c++ 中的 AES 代码有问题。明文正在更改,但加密邮件是相同的。我找不到我的错误
- 我的OpenGL平面有问题