Edit: I’m trying to use opengl 2.1 to avoid shaders for now.
I can render a simple triangle without issue as a vbo but when I made this function and try to render a height map it doesn’t display anything. I’m guess it’s something to do with trying to fill the data with a vector<float> but not to sure.
It’s probably something simple but I’m pretty new to opengl and 3d programming in general -.-, thanks for any help.
I load a bmp image (gray scale) and just fill a vector<float> like so…
std::vector<float> loadHeightMap(const char* fileName, float tileSize, float maxHeight)
{
std::vector<float> vec;
SDL_Surface* img = SDL_LoadBMP(fileName);
if(!img)
{
std::cout<<"Failed to load " << fileName << "." << std::endl;
return vec;
}
for(int i = 0; i < img->h; i++)
{
for(int j = 0; j < img->w; j++)
{
Uint32 pixel = ((Uint32*)img->pixels)[i * img->pitch/4 + j];
Uint8 r, g, b;
SDL_GetRGB(pixel, img->format, &r, &g, &b);
vec.push_back((float)i * tileSize);
vec.push_back((float)(r / 255) * maxHeight);
vec.push_back((float)j * tileSize);
}
}
SDL_FreeSurface(img);
return vec;
};
Init the vbo…
glGenBuffers(1, &vboModel);
glBindBuffer(GL_ARRAY_BUFFER, vboModel);
glBufferData(GL_ARRAY_BUFFER, heightMap.size(), heightMap.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
and finally I try to draw the vbo
glBindBuffer(GL_ARRAY_BUFFER, vboModel);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, NULL);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Edit: Well I see why it doesn’t display anything cause i’m not actually making tryings filling the vector like I currently am… Guess i’ll have to think more lol.
I edited the height map function to actually make vertice but I see only 1 triangle.
std::vector<float> loadHeightMap(const char* fileName, float tileSize, float maxHeight)
{
std::vector<float> vec;
SDL_Surface* img = SDL_LoadBMP(fileName);
if(!img)
{
std::cout<<"Failed to load " << fileName << "." << std::endl;
return vec;
}
for(int i = 0; i < img->h; i++)
{
for(int j = 0; j < img->w; j++)
{
Uint32 pixel = ((Uint32*)img->pixels)[i * img->pitch/4 + j];
Uint8 r, g, b;
SDL_GetRGB(pixel, img->format, &r, &g, &b);
vec.push_back((float)i * tileSize);
vec.push_back((float)(r / 255) * maxHeight);
vec.push_back((float)j * tileSize);
vec.push_back((float)i+1 * tileSize);
vec.push_back((float)(r / 255) * maxHeight);
vec.push_back((float)j * tileSize);
vec.push_back((float)i * tileSize);
vec.push_back((float)(r / 255) * maxHeight);
vec.push_back((float)j+1 * tileSize);
}
}
SDL_FreeSurface(img);
return vec;
};
edit: I changed
glDrawArrays(GL_TRIANGLES, 0, (heightMap.size()));
and now I have a huge square but the height doesn’t seem to be right… hmm
Edit: I changes the load height map function to load a 4th vertex and use triangle strips to render it… seems to work better but I still have no Z or height value…
Seems that r is also equal to 0… odd. r should be a range from 0-255… the image I use has colors from black to near white all over it…
Edit: The issue is Uint32 pixel = ((Uint32*)img->pixels)[i * img->pitch / 4 + j]; … seems that it doesn’t return the correct pixel so I guess i’ll have to find a new way to find the currect pixel.
Edit…again: Well I have it loading mountains and stuff now but… all the vertex are flat so it’s got gaps lol… I see why it’s happening but not sure of a solution just yet -.-