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

Started by
14 comments, last by Compaqx101x 19 years, 5 months ago
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
Advertisement
Load a texture with an alpha channel (like a tga) and then use alpha blending.
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]
u need to disable depth writes
glDepthMask(GL_FALSE)
of course its still not gonna look 100% if u do that as well
Is that how most engines do it? Is there a better way if that way makes it not look 100%?
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]
>>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
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
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
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)?

This topic is closed to new replies.

Advertisement