Question

I have created a class to load Terrain points from a heightmap file; but drawing them causes a crash. I have narrowed down the error to the glVertexPointer() function which returns 1281 (invalid value I am led to believe). I have been through the full 65k-ish points and can't seem to find a problem with it. The only other things that happens before this in the render loop are setting up GL_PROJECTION and GL_MODELVIEW as well as a glClear() (and obviously a swap buffer after this draw call).

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;
}
};
Was it helpful?

Solution

The following code snippet suggests that you use the first parameter of glVertexPointer incorrectly:

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

It should be the number of components of your position vector per vertex, and only 2, 3 or 4 are valid here. You seem to use 3D postions, so 3 would be the correct value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top