2
\$\begingroup\$

Im trying to find the "best way" to apply a texture to a heightmap with opengl 3.x.

Its really hard to find something on google because tutorials are olds and they're all using different methods, im really lost and i dont know what to use at all.

Here is my code that generates the heightmap (its basic)

    float[] vertexes = null;
    float[] textureCoords = null;

    for(int x = 0; x < this.m_size.width; x++)
    {
        for(int y = 0; y < this.m_size.height; y++)
        {
            vertexes ~= [x, 1.0f, y];
            textureCoords ~= [cast(float)x / 50, cast(float)y / 50];
        }
    }

As you can see, i dont know how to apply the texture at all (i was using / 50 for my tests).

Result of that code :

enter image description here

I would like to have something very basic like :

enter image description here

(you can find more pics in his blog)

Edit : my texture size is 1024x1024.

\$\endgroup\$
5
  • \$\begingroup\$ You already have a one texture there, you mean you want to have multiple textures on terrain? \$\endgroup\$
    – Kikaimaru
    Jul 11, 2013 at 9:23
  • \$\begingroup\$ No, i don't want to have multiple textures for the moment. Yes i have a texture but i don't know how apply it in a "good way", you can see on my code i just do x / 50 and y / 50 (i don't even know why 50). \$\endgroup\$
    – Aldo
    Jul 11, 2013 at 9:31
  • \$\begingroup\$ what language is this? which frameworks / engines are you using? \$\endgroup\$ Jul 11, 2013 at 10:52
  • 1
    \$\begingroup\$ Normally you have texture coordinates from 0 to 1, but with heightmaps you usually have larger numbers and if texture is for example 2.5 it behaves like 0.5 (its wrapped). Without / 50, first row would have these texcoords: 0, 1, 2, 3... That means that first two vertices (creating first triangle) would be mapped by whole texture (since its 0 and 1). But with / 2, texcoords would be 0, 0.5, 1, 1.5. That means that first vertices would be mapped with just half of texture. So the larger number you put after / larger area will be covered by texture without repeating the texture. \$\endgroup\$
    – Kikaimaru
    Jul 11, 2013 at 11:00
  • \$\begingroup\$ Ok thanks i understand. I saw this when i was doing my tests. Timothy Groote im using D language with opengl from scratch. \$\endgroup\$
    – Aldo
    Jul 11, 2013 at 11:47

1 Answer 1

2
\$\begingroup\$

Basicly it's all the same method.

You have a texture at the same size of your grid. lets go with your 1024x1024. that would be ideal to have a grid that is 1024 x 1024 then.

The first thing you do is to get a pointer or some access to the texture, so you can iterate over it.

The second thing you do is to have access to the vertex buffer. from there you iterate all of the vertices and give them values corresponding to the texture.

So, lets say this:

For x < 1024; ++x
for y < 1204; ++y
vertex[ y * 1024 + x ] = float4( x, textureArray[ y * 1024 + x].value, y );

This is the most simplest way i can explain this to you. but that is the basic idea. and the code will go thrue all the vertices and find the exact same pixel from the texture.

And from there you can add spacing, and what ever you want!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .