Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  More efficient use of Texture.bind() when rendering terrain patches?  (Read 405 times)
0 Members and 1 Guest are viewing this topic.
Offline pixelprime

Junior Member


Medals: 3



« Posted 2013-03-26 12:35:20 »

Hey peeps!

I'm currently using LWJGL to write a terrain renderer for an upcoming project, and wanted to ask a bit of a newbie question about texture binding and performance.

The Set-Up:
Here's a quick schematic of how my terrain works at present:



I load in a greyscale height map of 64 x 64 pixels, where each pixel represents a terrain quad of a set height. The entire data set is split into 16 patches each containing 16 x 16 (256) individual quads.

The Problem:

At runtime, I scan the entire set of terrain patches (16 in total), and work out which ones are in the view frustum (shown in green in the left part of the diagram, from the perspective of the yellow player marker).

For each of these patches in view, I then cycle through each of its individual quads (i.e. the right portion of the diagram) and render them at the required position. No frustum culling is done on these to minimise the amount of view-calculations I'm doing at run time. All of these objects are rendered as VBOs.

My problem is with texture binding. You'll see that in any single patch of terrain, I might have two (or potentially more) different textures to apply. If it was one texture, I'd just do a Texture.bind() once for each patch (or maybe just once per render cycle, until it needs changing for something else), but when there's more than one I'm starting to wonder about the best way to optimise it.

Would a solution be to map out a list of indices for each unique texture to be rendered in any given patch, then cycle through each one - rendering as I go?

So, each patch could have the following code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
// Index arrays for each unique type of texture
boolean[] texIndex1 = new boolean[16*16];
boolean[] texIndex2 = new boolean[16*16];

protected void resetIndices()
{
    for (int i = 0; i < (16*16); i++) { texIndex1[i] = false; texIndex2[i] = false; }
}

protected void setupIndices()
{
    // N.B. These values will be set during the initial terrain load
   // Obviously there would be 'true' values for all 256 quads, but for now
   // I'm just using 8 as an example!
   texIndex1[14] = true;
    texIndex1[18] = true;
    texIndex1[25] = true;
    texIndex1[29] = true;

    texIndex2[6] = true;
    texIndex2[8] = true;
    texIndex2[11] = true;
    texIndex2[21] = true;
}

protected void renderPatch()
{
    // bind the first texture
   texture1.bind();
    for (int i = 0; i < (16*16); i++)
    {
        if (texIndex1[i])
        {
            // render the quad VBO object
       }
    }

    texture2.bind();
    for (int i = 0; i < (16*16); i++)
    {
        if (texIndex2[i])
        {
            // render the quad VBO object
       }
    }
}


Am I thinking along the right lines here? Or should I simply switch texture bindings when needed?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
for (int i = 0; i < (16*16); i++)
{
    // do we need to use texture 1, but are currently using texture 2?
   if (texIndex1[i] && texActive2)
    {
        texActive2 = false;
        texActive1 = true;
        texture1.bind();
    }

    // do we need to use texture 2, but are currently using texture 1?
   if (texIndex2[i] && texActive1)
    {
        texActive1 = false;
        texActive2 = true;
        texture2.bind();
    }

    // render the quad VBO object
}


If someone could provide some insight into whether my ideas above are along the right lines, I'd be really grateful! I might be doing something dreadfully wrong here!

Thanks!
Offline theagentd
« Reply #1 - Posted 2013-03-26 13:05:36 »

You could create a list of geometry for each texture in each patch. Instead of looping sequentially through the patch and drawing everything in order, you'd draw everything that uses texture 0 first, then everything that uses texture 1, etc. You'll need some code to assign each object/tile/whatever to its texture when you start the game, but that's about it. Doing that should give you a maximum number of texture binds per patch equal to the number of unique textures the patch uses. You could even go one step further and loop over all visible patches and draw everything that passed the frustum test that uses texture 0, then all geometry that uses texture 1, etc which would keep the number of texture binds constant regardless of how much you draw or how many patches are visible.

Myomyomyo.
Offline pixelprime

Junior Member


Medals: 3



« Reply #2 - Posted 2013-03-26 17:24:35 »

This was really useful, thanks!

I ended up creating three render loops - one for each renderable texture type (one for the sides of the terrain, two for the tops).

Thanks for the tip!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (22 views)
2014-04-15 18:08:23

BurntPizza (17 views)
2014-04-15 03:46:01

UprightPath (31 views)
2014-04-14 17:39:50

UprightPath (15 views)
2014-04-14 17:35:47

Porlus (31 views)
2014-04-14 15:48:38

tom_mai78101 (57 views)
2014-04-10 04:04:31

BurntPizza (115 views)
2014-04-08 23:06:04

tom_mai78101 (214 views)
2014-04-05 13:34:39

trollwarrior1 (182 views)
2014-04-04 12:06:45

CJLetsGame (189 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!