Attempting to convert code from Fixed Function Pipeline to Programmable

The silly problem has been resolved, thank you. :slight_smile:

Ughh the strugglezz :sleeping:

So I tried my best to modify the code so that it sends the data to the GPU using modern OpenGL rendering techniques but now I can’t see my terrain anymore!! :sorrow:

Can anyone please help me with this?


// This function is only called once 
void LoadHeightmapImage(const char* FileName)
{
	m_Image = SDL_LoadBMP(FileName);
	Uint32 Pixel = 0;

	if (!m_Image)
	{
		std::cerr << "ERROR: Unable to load heightmap.
";
		return;
	}

	m_HeightmapWidth = m_Image->w;
	m_HeightmapHeight = m_Image->h;

	std::vector<float> tmp;
	for (int i = 0; i < m_Image->h; ++i)
	{
		tmp.clear();
		for (int j = 0; j < m_Image->w; ++j)
		{
			Pixel = ((Uint32*)m_Image->pixels)[i * m_Image->pitch / 4 + j];
			Uint8 r, g, b;
			SDL_GetRGB(Pixel, m_Image->format, &r, &g, &b);
			tmp.push_back((float)r / 255.0f);
		}
		g_vHeights.push_back(tmp);
	}

	SDL_FreeSurface(m_Image);

	std::vector<vec3> Vertices;
	for (int i = 0; i < m_HeightmapHeight - 1; ++i)
	{
		for (int j = 0; j < m_HeightmapWidth - 1; ++j)
		{
			Vertices.push_back(vec3(i, g_vHeights[i][j], j));
			Vertices.push_back(vec3(i + 1, g_vHeights[i][j + 1], j));
			Vertices.push_back(vec3(i + 1, g_vHeights[i + 1][j + 1], j + 1));

			Vertices.push_back(vec3(i, g_vHeights[i][j], j));
			Vertices.push_back(vec3(i + 1, g_vHeights[i + 1][j + 1], j + 1));
			Vertices.push_back(vec3(i, g_vHeights[i + 1][j], j + 1));
		}
	}

	// Send data to GPU 
	m_HeightmapVertexArrayID;
	GLuint VertexBufferObjID;

	glGenVertexArrays(1, &m_HeightmapVertexArrayID);
	glBindVertexArray(m_HeightmapVertexArrayID);

	glGenBuffers(1, &VertexBufferObjID);
	glBindBuffer(GL_ARRAY_BUFFER, VertexBufferObjID);
	glBufferData(GL_ARRAY_BUFFER, Vertices.size() * sizeof(vec3), &Vertices[0], GL_STATIC_DRAW);

	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
	
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);
}

// This function is called every tick (60 times per frame to be more precise!) ^^
void RenderTerrain()
{
	m_TerrainShader.UseProgram();
	glBindVertexArray(m_HeightmapVertexArrayID);
	glDrawArrays(GL_TRIANGLES, 0, (m_HeightmapWidth - 1) * (m_HeightmapHeight - 1) * 6);
	glBindVertexArray(0);
}

The reason I wasn’t able to see the terrain is because I wasn’t setting its position. :doh:

Now I can see it, but it’s all flat, like one big plane.

I’m sure I’ll be able to figure out why it’s rendering that way and post the solution to whoever’s interested.