Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
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 612 times)
0 Members and 1 Guest are viewing this topic.
Offline pixelprime

Junior Devvie


Medals: 3



« Posted 2013-03-26 11: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

« JGO Bitwise Duke »


Medals: 365
Projects: 2
Exp: 8 years



« Reply #1 - Posted 2013-03-26 12: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 Devvie


Medals: 3



« Reply #2 - Posted 2013-03-26 16: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.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!