OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
physics.cs
Go to the documentation of this file.
1 #version 310 es
2 
3 /* Copyright (c) 2014-2017, ARM Limited and Contributors
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge,
8  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 // Not a very interesting shader.
24 // It's purpose is to move the spheres around a bit to see how Hi-Z works on more dynamic objects.
25 
26 precision highp float;
27 precision highp int;
28 
29 layout(local_size_x = 128) in;
30 layout(location = 0) uniform uint uNumBoundingBoxes;
31 layout(location = 1) uniform float uDeltaTime;
32 
33 struct SphereInstance
34 {
35  vec4 position;
36  vec4 velocity;
37 };
38 
39 layout(std430, binding = 0) buffer SphereInstances
40 {
41  SphereInstance instance[];
43 
44 #define RANGE 20.0
45 #define RANGE_Y 10.0
46 // Super basic collision against some arbitrary walls.
47 void compute_collision(inout vec3 pos, float radius, inout vec3 velocity)
48 {
49  vec3 dist = pos - vec3(0.0, 2.0, 0.0);
50  float dist_sqr = dot(dist, dist);
51  float minimum_distance = 2.0 + radius;
52  if (dist_sqr < minimum_distance * minimum_distance)
53  {
54  if (dot(dist, velocity) < 0.0) // Sphere is heading towards us, "reflect" it away.
55  velocity = reflect(velocity, normalize(dist));
56  }
57  else
58  {
59  // If we collide against our invisible walls, reflect the velocity.
60  if (pos.x - radius < -RANGE)
61  velocity.x = abs(velocity.x);
62  else if (pos.x + radius > RANGE)
63  velocity.x = -abs(velocity.x);
64 
65  if (pos.y - radius < 0.0)
66  velocity.y = abs(velocity.y);
67  else if (pos.y + radius > RANGE_Y)
68  velocity.y = -abs(velocity.y);
69 
70  if (pos.z - radius < -RANGE)
71  velocity.z = abs(velocity.z);
72  else if (pos.z + radius > RANGE)
73  velocity.z = -abs(velocity.z);
74  }
75 }
76 
77 void main()
78 {
79  uint ident = gl_GlobalInvocationID.x;
80  if (ident >= uNumBoundingBoxes)
81  return;
82 
83  // Load instance data.
84  // position.w is sphere radius.
85  SphereInstance sphere = spheres.instance[ident];
86 
87  // Move the sphere.
88  sphere.position.xyz += sphere.velocity.xyz * uDeltaTime;
89 
90  // Test collision.
91  compute_collision(sphere.position.xyz, sphere.position.w, sphere.velocity.xyz);
92 
93  // Write back result.
94  spheres.instance[ident] = sphere;
95 }
96 
void compute_collision(inout vec3 pos, float radius, inout vec3 velocity)
Definition: physics.cs:47
#define RANGE_Y
Definition: physics.cs:45
precision highp int
Definition: physics.cs:27
vec3 xyz() const
Definition: matrix.h:101
vec4 velocity
Definition: hiz_cull.cs:66
Definition: matrix.h:51
precision highp float
Definition: physics.cs:26
Definition: matrix.h:75
vec4 position
Definition: hiz_cull.cs:65
spheres
Definition: physics.cs:42
static vec3 normalize(const vec3 &v)
Definition: matrix.h:154
GLint location
Definition: gl2ext.h:180
float w
Definition: matrix.h:80
layout(local_size_x=128) in
Mesh sphere
Definition: app.cpp:40
void uniform(string name, const mat4 &v)
Definition: glutil.cpp:97
float velocity
Definition: Native.cpp:157
GLint GLint GLint GLint GLint x
Definition: gl2ext.h:574
#define RANGE
Definition: physics.cs:44
GLenum GLuint buffer
Definition: gl2ext.h:628
void main()
Definition: physics.cs:77