Generate a Index for indexed drawing

EDIT: This is solved… when I used a smaller height map the problem was more obvious and the indexing was wrong.
The solution if anyone wants to know.

for(int z = 0; z < img->h-1; z++)
	{
		for(int x = 0; x < img->w-1; x++)
		{
			int top = z * img->w + x;
			int bottom = (z+1)*img->w + x;
 
			index.push_back(top);
			index.push_back(bottom);
			index.push_back(top+1);
 
			index.push_back(top+1);
			index.push_back(bottom);
			index.push_back(bottom+1);
		}
	}

The result after adding colors = to the heights. :smiley: 800x800 heightmap created with gimp and render clouds function.

Edit: I put a picture up and I fixed a issue in the drawing steps… I add index.size()*sizeof(unsigned int)… I’m pretty sure it should just be index.size() since the next parameter is the type. When I make a smaller heightmap it takes the end triangles and stretches them across the whole map.

I’m till trying to create this heightmap into a VBO and display it and after some advice I’m trying to display it using glDrawElements. It seems to be drawing triangles across the map from one end to the other similar to my GL_TRIANGLE STRIP issue I was having but I’ve printed out the data and it’s format is exactly what I need…
0, height, 0,
1, height, 0,
2, height, 0, …so on till it hits image->width then
0, height, 1,
1, height, 1,
2, height, 1,… repeats till image->height and image->width…

also when I test the index it’s format it like… (image width and height are both 800)
0, 800, 1,
1, 800, 801,

1, 801, 2,
2, 801, 802, … so on

I init everything like this

GLuint vboModel, vboModelInd;
std::vector<unsigned int> index; //Temp global variable for storing the index array.
std::vector<float> heightMap = loadHeightMap("heightmap.bmp", 1, 175);

glGenBuffers(1, &vboModel);
glBindBuffer(GL_ARRAY_BUFFER, vboModel);
glBufferData(GL_ARRAY_BUFFER, heightMap.size()*sizeof(float), heightMap.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glGenBuffers(1, &vboModelInd);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboModelInd);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, index.size()*sizeof(unsigned int), index.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

This is what I use to draw everything…

glBindBuffer(GL_ARRAY_BUFFER, vboModel);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboModelInd);
glEnableClientState(GL_VERTEX_ARRAY);	
glVertexPointer(3, GL_FLOAT, 0, NULL);
glDrawElements(GL_LINES, index.size()*sizeof(unsigned int), GL_UNSIGNED_INT, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

Finally the loadHeightMap function is… sorry it’s a bit messy atm I’ve been testing a crap ton of different thins…

std::vector<float> loadHeightMap(const char* fileName, float tileSize, float maxHeight)
{
	std::vector<float> vec;
	std::vector<std::vector<float>> heights;

	//Load image with SDL
	SDL_Surface* img = SDL_LoadBMP(fileName);
	if(!img)
	{
		std::cout<<"Failed to load " << fileName << "." << std::endl;
		return vec;
	}

	//Store height values in heights vector.
	std::vector<float> tmp;
	for(int i = 0; i < img->h; i++)
	{
		tmp.clear();
		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);

			tmp.push_back((float)r/255.0);
		}
		heights.push_back(tmp);
	}

	//Fill the vertex data.
	for(int z = 0; z < img->h; z++)
	{
		for(int x = 0; x < img->w; x++)
		{
			vec.push_back((float)x * tileSize);
			vec.push_back((float)heights[x][z] * maxHeight);
			vec.push_back((float)z * tileSize);
		}
	}

	//Create a index array for the vertex data.
	for(int z = 0; z < img->h-1; z++)
	{
		for(int x = 0; x < img->w-1; x++)
		{
			int top = z * img->w + x;
			int bottom = (z+1)*img->w + x;

			index.push_back(top);
			index.push_back(bottom-1);
			index.push_back(top+1);

			index.push_back(top+1);
			index.push_back(bottom-1);
			index.push_back(bottom);
		}
	}
	SDL_FreeSurface(img);
	return vec;
};

I’ll change this later once all the bugs are fixed to just pass the data and index by reference.