2

For example...

If I'm developing an application that requires more than one texture in it (unique sprites, basically), do I need to call context3D.createProgram(); and assemble a new Program3D (with a VertexShaderAssembler and a FragmentShaderAssembler) for each individual textures that I wish to use in the application?

How does a Program3D work within an engine typically? Does one program run the whole thing, or does it use one program per textures, models, maps?

And am I correct to assume that you only need to create the Program3D once during initialization time (Event.ADDED_TO_STAGE), and not during each frames (Event.ENTER_FRAME), right?

chamberlainpi
  • 4,854
  • 8
  • 32
  • 63

2 Answers2

3

For one program (shader) you can have multiple textures. Usually you write a program for each shader that you have. For example in my game I have one shader (program) for the terrain lighting, texturing and coloring. I have another shader (program) for the water.

So the programs are made once, but I tell context3d which program to use before drawing the scene. That way it will draw whatever I'm about to draw with the current program.

Example usage:

context3d.setProgram(WaterShader);
water.drawTriangles();

context3d.setProgram(TerrainShader)
terrain.drawTriangles();

I draw the water first and then the terrain, each using a different shader. My TerrainShader has multiple textures in, e.g. sand, rock and dirt textures. The shader decides which texture to use at given time. E.g. if the height of the vertex is < 10, then use the sand texture.

So, create the programs once and use them when needed.

I hope this helps you in the right way.

Jón Trausti Arason
  • 4,548
  • 1
  • 39
  • 46
1

You don't need a new Program3D for each texture. The renderer uses the last program set whenever you call drawTriangles(). If you need to treat particular textures differently such that you need a different fragment shader, then you do need more than one Program3D object, but it would be a rare case in which you would need a different shader for every object and texture.

Not having written an engine, I can't tell you how this would be done typically. I would think you would have different Program3D objects for different classes of models, environment maps, etc. Probably not one per entity. You are limited to about 4000 Program3D objects.

Yes, you should create and initialize your Program3D objects ahead of time. During rendering, you call Context3D.setProgram() to specify the active program. That program is used for rendering until you change it.

Joe Ward
  • 749
  • 4
  • 7
  • Ah k, so the order in which I would call Context3D.setProgram(...) is probably important to get the desired Z-Depth results, right? – chamberlainpi Oct 20 '11 at 18:55
  • So that also means, I don't need to clear the Context3D when I alternate between Program3Ds I would imagine, especially if one render-pass is just to overlay another previous pass? – chamberlainpi Oct 20 '11 at 18:56
  • You need to draw things in the correct order to get your depth tests to work. That might also determine the order in which you call setProgram(). You only need to clear once per present() call. – Joe Ward Oct 21 '11 at 16:33