source: nanovis/branches/1.2/shaders/update_pos_vel.cg @ 5046

Last change on this file since 5046 was 4904, checked in by ldelgass, 9 years ago

Merge serveral changes from trunk. Does not include threading, world space
changes, etc.

File size: 1.7 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright (c) 2004-2013  HUBzero Foundation, LLC
4 *
5 * Authors:
6 *   Wei Qiao <qiaow@purdue.edu>
7 */
8
9// update velocity and position in a single pass using MRT
10
11#include "common.cg"
12
13void main(in float2 uv : TEXCOORD0,
14          uniform samplerRECT pos_tex,
15          uniform samplerRECT vel_tex,
16          uniform sampler2D terrain_tex,
17          uniform float timestep = 0.01,
18          uniform float damping = 0.99,
19          uniform float3 gravity = float3(0, -1, 0),
20          uniform float3 spherePos,
21          uniform float3 sphereVel,
22          out float4 new_pos : COLOR0,
23          out float4 new_vel : COLOR1)
24{
25    const float3 terrain_scale = float3(8.0, 2.0, 8.0);
26    const float3 terrain_offset = float3(-4.0, 0.01, -4.0);
27
28    float3 pos = texRECT(pos_tex, uv).xyz;
29    float3 vel = texRECT(vel_tex, uv).xyz;
30
31    float3 pos_next = pos + vel * timestep;  // predicted position next timestep
32
33    // update velocity
34    float3 force = gravity;
35    // float3 force = 0.0;
36    // Gravitation(pos, spherePos, force, 0.1);
37
38    SphereCollide(pos_next, vel, spherePos, 1.0, sphereVel, 0.5, force);
39    // FloorCollide(pos_next, vel, 0.0, 0.5, force);   
40    TerrainCollide(pos_next, vel, terrain_tex, terrain_scale, terrain_offset, 0.5);
41
42    const float inv_mass = 1.0;   
43    vel += force * inv_mass * timestep; // F = ma     
44    vel *= damping;
45
46    // update position
47    SphereConstraint(pos, spherePos, 1.0f);
48    // FloorConstraint(pos, 0.0f);
49    TerrainConstraint(pos, terrain_tex, terrain_scale, terrain_offset);
50
51    pos += vel * timestep;
52
53    new_vel = float4(vel, 1);
54    new_pos = float4(pos, 1);
55}
Note: See TracBrowser for help on using the repository browser.