gpt4 book ai didi

c++ - OpenGL glVertexPointer 返回 1281 - 不知道为什么

转载 作者:太空狗 更新时间:2023-10-29 20:41:27 25 4
gpt4 key购买 nike

我创建了一个类来从高度图文件加载地形点;但绘制它们会导致崩溃。我已将错误缩小到返回 1281 的 glVertexPointer() 函数(我被引导相信是无效值)。我已经完成了 65k 左右的点数,但似乎找不到问题。在渲染循环中在此之前发生的唯一其他事情是设置 GL_PROJECTION 和 GL_MODELVIEW 以及 glClear()(显然是此绘制调用之后的交换缓冲区)。

class Terrain : public Model
{
public:
Terrain(char* pFile){
if(!LoadTerrain(pFile))
{
OutputDebugString("Loading terain failed.\n");
}
};

~Terrain(){};

void Draw()
{
glPushMatrix();
//glEnableClientState(GL_VERTEX_ARRAY); // Depreciated? Causes crash

glBindBuffer(GL_ARRAY_BUFFER, mVBO);
glVertexPointer(mNumberPoints, GL_FLOAT, 0, (GLvoid*)((char*)NULL));

GLenum a = glGetError(); // 1281

glPointSize(5.0f);
glDrawArrays(GL_POINTS, 0, mNumberPoints);

glBindBuffer(GL_ARRAY_BUFFER, 0);

//glDisableClientState(GL_VERTEX_ARRAY);

glPopMatrix();
}

private:
GLuint mVBO;
int mWidth;
int mHeight;
int mNumberPoints;

bool LoadTerrain(char* pFile)
{
FILE* filePtr;
int error;
unsigned int count;
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
int imageSize, i, j, k, index;
unsigned char* bitmapImage;
unsigned char height;

error = fopen_s(&filePtr, pFile, "rb");
if(error)
return false;
count = fread(&bfh, sizeof(BITMAPFILEHEADER), 1, filePtr);
if(count != 1)
return false;
count = fread(&bih, sizeof(BITMAPINFOHEADER), 1, filePtr);
if(count != 1)
return false;

mWidth = bih.biWidth;
mHeight = bih.biHeight;
mNumberPoints = mWidth * mHeight;
imageSize = mWidth * mHeight * 3; // 3 values * width * height = total data size

bitmapImage = new unsigned char[imageSize];
fseek(filePtr, bfh.bfOffBits, SEEK_SET);
count = fread(bitmapImage, 1, imageSize, filePtr);
if(count != imageSize)
return false;
error = fclose(filePtr);
if(error)
return false;

float* mapArray = new float[imageSize];
k = 0;
for(j = 0; j < mHeight; j++)
{
for(i = 0; i < mWidth; i++)
{
height = bitmapImage[k];

index = (mHeight * j) + i;

mapArray[k++] = (float)i;
mapArray[k++] = (float)height;
mapArray[k++] = (float)j;

/*OutputDebugString(convertInt(i).c_str());
OutputDebugString(",");
OutputDebugString(convertInt(height).c_str());
OutputDebugString(",");
OutputDebugString(convertInt(j).c_str());
OutputDebugString("\n");*/
}
}


// COPY MAP ARRAY INTO BUFFERS
glGenBuffers(1, &mVBO);
glBindBuffer(GL_ARRAY_BUFFER, mVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * imageSize, mapArray, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

delete [] bitmapImage;
bitmapImage = 0;

return true;
};

string convertInt(int number)
{
if (number == 0)
return "0";
string temp="";
string returnvalue="";
while (number>0)
{
temp+=number%10+48;
number/=10;
}
for (int i=0;i<temp.length();i++)
returnvalue+=temp[temp.length()-i-1];
return returnvalue;
}
};

最佳答案

以下代码片段表明您错误地使用了 glVertexPointer 的第一个参数:

glVertexPointer(mNumberPoints, GL_FLOAT, 0, (GLvoid*)
[...]
glDrawArrays(GL_POINTS, 0, mNumberPoints);

它应该是你的位置 vector 每个顶点的分量数,这里只有 2、3 或 4 是有效的。您似乎使用 3D 位置,因此 3 是正确的值。

关于c++ - OpenGL glVertexPointer 返回 1281 - 不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21270365/

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