This discussion about gras got me an idea for camera-aligned gras blades en masse:



You just need DynamicModels for the mesh generation, a dummy gras model (contains just a cube) and a gras texture. Use this code:

Click to reveal..

main.c
Code:
#include <acknex.h>
#include <default.c>

#include "DynamicModels.h"

float camera_pan = 0;

BMAP *bmpGras = "grass.tga";

MATERIAL *mtlGras = 
{
	effect = "spritegras.fx";
	skin1 = bmpGras;
}

function main()
{
	level_load(NULL);
	
	bmap_to_mipmap(bmpGras);
	
	//ENTITY *terrain = ent_create("terrain.hmp", nullvector, NULL);
	
	DMDLSettings.flags = 0; // No flags
	
	DynamicModel *dmdl = dmdl_create();
	
	int i;
	for(i = 0; i < 10000; i++)
	{
		DYNAMIC_QUAD quad;
		memset(quad, 0, sizeof(quad));
		
		float px = random(200) - 100;
		float py = random(200) - 100;
		
		int j;
		for(j = 0; j < 4; j++)
		{
			quad.v[j].x = px;
			quad.v[j].z = py;
		}
		
		quad.v[0].u1 = 0;
		quad.v[0].v1 = 0;
		
		quad.v[1].u1 = 1;
		quad.v[1].v1 = 0;
		
		quad.v[2].u1 = 0;
		quad.v[2].v1 = 1;
		
		quad.v[3].u1 = 1;
		quad.v[3].v1 = 1;
		
		dmdl_add_quad(dmdl, quad);
	}
	
	LPD3DXMESH mesh = dmdl_create_mesh(dmdl);
	
	dmdl_delete(dmdl);
	
	
	you = ent_create("gras.mdl", vector(200 * x, 200 * y, 0), NULL);
	ent_setmesh(you, mesh, 0, 0);	
	ent_remove(you);
	
	int x, y, size;
	size = 4;
	for(x = -size; x <= size; x++)
	{
		for(y = -size; y <= size; y++)
		{
			you = ent_create("gras.mdl", vector(200 * x, 200 * y, 0), NULL);
			you.material = mtlGras;
		}
	}
	
	while(1)
	{
		camera_pan = 3.1415 * camera.pan / 180.0;
		wait(1);
	}
}



spritegras.c
Code:
float camera_pan_flt;

float4x4 matWorld;
float4x4 matView;
float4x4 matProj;

Texture mtlSkin1;

sampler2D smpTex = sampler_state
{
	Texture = <mtlSkin1>;
	AddressU = Clamp;
	AddressV = Clamp;
};

struct vsOut
{
	float4 pos : POSITION;
	float2 uv : TEXCOORD0;
};

vsOut vs(float4 pos : POSITION, float2 uv : TEXCOORD0)
{
	float4 p = mul(pos, matWorld);
	
	p.x -= 6 * sin(camera_pan_flt) * uv.x;
	p.z += 6 * cos(camera_pan_flt) * uv.x;
	p.y += 6 * uv.y;
	
	p = mul(p, matView);
	
	vsOut o;
	o.pos = mul(p, matProj);
	o.uv = uv;
	return o;
}

float4 ps(vsOut i) : COLOR
{
	return tex2D(smpTex, float2(i.uv.x, 1 - i.uv.y));
}

technique
{
	pass
	{
		CullMode = None;
		AlphaTestEnable = true;
		AlphaBlendEnable = false;
		ZWriteEnable = true;
		VertexShader = compile vs_2_0 vs();
		PixelShader = compile ps_2_0 ps();
	}
}





Neither the shader nor the generation is perfect, but i think you can improve this a lot.

EDIT: Forgot to say: Should be easy to combine with the techniques above...

Last edited by MasterQ32; 12/19/13 16:47.

Visit my site: www.masterq32.de