# How do I load and implement see-through grass texture?

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys! I just wrote some code to simulate grass (using sine wave equations and idea in the Codecreatures Benchmark)...but I don't know both where to find a grass texture that is see through or how to load it. I can load bmp files but haven't tried anything else (I'm relatively new at this...). Just wondering if there is a way to set a certain color (ie: white) as alpha=0 or maybe I'm doing this completely wrong. I just need to show some grass but have it so you can look beyond the blades even though the square texture might take up more room...thanks for the help, Dave

##### Share on other sites
Load a texture with an alpha channel (like a tga) and then use alpha blending.

##### Share on other sites
I tried that and I still get some black. I haven't delt with blending very much...the way I currently do it is in the InitGL() function i put:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

and then when I go to draw the grass I just do:
glEnable(GL_BLEND);glEnable(GL_CULL_FACE);//drawing grass hereglDisable(GL_BLEND);glDisable(GL_CULL_FACE);

then I disable them after I draw the grass (just gets reenabled every time). Here's a screenshot of what I mean... (don't mind the position of the water and grass I just put them there randomly earlier to see how both effects worked). If you guys need any more information just let me know.

The grass consists of a bunch of randomly oriented three planes of grass (if that helps).

Thanks guys! Sorry I'm so new at this...

[Edited by - Compaqx101x on October 22, 2004 3:50:29 PM]

##### Share on other sites
u need to disable depth writes
of course its still not gonna look 100% if u do that as well

##### Share on other sites
Is that how most engines do it? Is there a better way if that way makes it not look 100%?

##### Share on other sites
Be sure to draw the pieces of grass ordered by depth (far to near) or you will have far pieces overdrawing the nearer pieces of grass.
I am struggling with this too, i solved it now by making sure the outlines of the grass cylinders (a cross of 3 or more quads) don't intersect with eachother so i can easily sort them without too much calculations. Looking at codecreatures screenshots and at the untextured screenie i guess they use fragment shaders to cancel or pass individual fragments using the alpha channel, i don't think it can be done so nicely using just blending.

EDIT: I ran the codecreatures benchmark at my laptop containing a mobility radeon 7500, the polygons are more roughly (per polygon or even per 'cross' of grass) ordered when pixel shaders aren't available (the screenshots at my laptop have a lot of artifacts while the screenshots in a book called GPU Gems are perfect) so i guess it isn't possible (in a reasonable way).

EDIT #2: A quote from someone who contributed to codecreatures:

"If we set these grass objects quite close together in a large area, sort them back-to-front at runtime, use alpha blending, and enable z-testing/writing in the draw call, then the impression of a naturally and thickly grown meadow appears."

[Edited by - Tree Penguin on October 22, 2004 6:19:07 PM]

##### Share on other sites
>>Be sure to draw the pieces of grass ordered by depth (far to near) or you will have far pieces overdrawing the nearer pieces of grass.
I am struggling with this too, i solved it now by making sure the outlines of the grass cylinders (a cross of 3 or more quads) don't intersect with eachother so i can easily sort them without too much calculations. Looking at codecreatures screenshots and at the untextured screenie i guess they use fragment shaders to cancel or pass individual fragments using the alpha channel, i don't think it can be done so nicely using just blending.<<

a method i use which gives quite reasonable results (and doesnt require sorting which is a PITA)
but it requires (one pass fragment shaders)

the algorithm goes something like this
depthwrites ON
depthtest ON
blending ON
if the fragments alpha value is greater than ~0.5 write depth else dont write depth, of course by writing the fragments depth in the fragment shader, u do lose early z test out, its not the quickest shader, but if visual quality is what youre after it does give quite good results.

this can be done in more than 1 pass without using fragment shaders

##### Share on other sites
Ok I implemented the sorting by distance from the camera...although that seems to REALLY slow it down when I have a lot of grass objects...is there a way to do this on the graphics card or something? I've never messed with anything except C++ and OpenGL (basics) so I don't know too much about the topic. Thanks!
Dave

##### Share on other sites
Thanks Tree Penguin and zedzeek! I've never messed with fragment shaders but I'll check them out...any articles or links you know of? I'll be searching myself but if anyone knows of some article that was really useful please let me know. Thanks!
Dave

##### Share on other sites
NVidia (developer.nvidia.com) and ATI have a bunch of pixel and vertex shader tutorials and a lot of free shader code, you could take a look at CG and GLSL and maybe at some other shader interfaces that allow you to write shaders using C-like code.
What gfx card are you using (only relatively new cards will support pixel shaders in a reasonable way)?

##### Share on other sites
Well in about a week I'll have my new computer made with the Raedon X800 Pro...but currently I'm using the GeForce3 Ti 500. So I guess in about a week I'll be able to implement the shaders. Thanks a lot for the help guys! Oh one more question. I wrote some code that simulates waves using Gerstner wave equations. Would it be beneficial to implement those in CG also? I wonder if I'd still have to do the calculations anyway...or maybe just move them all over. Anyway thanks! I'll definately check those sites out! :)
Dave

##### Share on other sites
I don't know if CG is compatible with ATI as it's an NVidia thing and i couldn't get simple CG vertex shaders to work at my 9600XT, you should look it up.

Anyway i think it would be faster using your GPU (as an X800 is very fast, my athlon xp 1.8 ghz beats my slow GF4ti4200 however) but when you are using the CPU more intensively it will be wise to move computations to your gfx card as much as possible to unstress your CPU a little or just do other things with it.

Your CPU is used for everything so all hardware that can do some of it's work instead just means more computing power, which gives you better results and more room for other components such as sound, AI and networking.

##### Share on other sites
Thanks for the advice Tree Penguin! I'll definately read up on ATI and CG. And I'll keep in mind what you said about moving my computations to the graphics card! Thanks again!
Dave

##### Share on other sites
>>Anyway i think it would be faster using your GPU (as an X800 is very fast, my athlon xp 1.8 ghz beats my slow GF4ti4200 however) but when you are using the CPU more intensively it will be wise to move computations to your gfx card as much as possible to unstress your CPU a little or just do other things with it.<<

whilst this is true, its generally better to do as much on the card as possible, be aware though, if u animate the grass in the vertexprogram then for each pass eg one for this light one for the next light etc the vertexprograms will get recalculated again but on the cpu u only have to do the calculations once and then pass that data to the gpu.

##### Share on other sites
Quote:
 Original post by zedzeek>>Anyway i think it would be faster using your GPU (as an X800 is very fast, my athlon xp 1.8 ghz beats my slow GF4ti4200 however) but when you are using the CPU more intensively it will be wise to move computations to your gfx card as much as possible to unstress your CPU a little or just do other things with it.<
yea that especially applies for model anmiation etc. but some how i doubt he'd need to do more than 1 pass of lighting on billboarded alpha grass...