use of org.joml.Vector2f in project chunkstories-core by Hugobros3.
the class VoxelSign method onPlace.
@Override
public void onPlace(FutureCell cell, WorldModificationCause cause) throws IllegalBlockModificationException {
// We don't create the components here, as the cell isn't actually changed yet!
int x = cell.getX();
int y = cell.getY();
int z = cell.getZ();
if (cause != null && cause instanceof Entity) {
Vector3d blockLocation = new Vector3d(x + 0.5, y, z + 0.5);
blockLocation.sub(((Entity) cause).getLocation());
blockLocation.negate();
Vector2f direction = new Vector2f((float) (double) blockLocation.x(), (float) (double) blockLocation.z());
direction.normalize();
// System.out.println("x:"+direction.x+"y:"+direction.y);
double asAngle = Math.acos(direction.y()) / Math.PI * 180;
asAngle *= -1;
if (direction.x() < 0)
asAngle *= -1;
// asAngle += 180.0;
asAngle %= 360.0;
asAngle += 360.0;
asAngle %= 360.0;
// System.out.println(asAngle);
int meta = (int) (16 * asAngle / 360);
cell.setMetaData(meta);
}
}
use of org.joml.Vector2f in project chunkstories by Hugobros3.
the class DefaultWeatherEffectsRenderer method generateRainForOneSecond.
private void generateRainForOneSecond(RenderingInterface renderingContex, float rainPresence) {
float rainIntensity = Math.min(Math.max(0.0f, rainPresence - 0.5f) / 0.3f, 1.0f);
bufferOffset %= 110000;
bufferOffset += 10000;
Vector2f view2drop = new Vector2f();
for (int i = 0; i < 100000; i++) {
// We want to always leave alone the topmost part of the array until it has gone out of view
int location = (bufferOffset + i) % 110000;
// Randomize location
float rdX = viewX + (random.nextFloat() * 2.0f - 1.0f) * (int) (Math2.mix(25, 15, rainIntensity));
float rdY = viewY + (random.nextFloat() * 2.0f - 0.5f) * (int) (Math2.mix(20, 20, rainIntensity));
float rdZ = viewZ + (random.nextFloat() * 2.0f - 1.0f) * (int) (Math2.mix(25, 15, rainIntensity));
// Max height it can fall to before reverting to used
float rdMh = world.getRegionsSummariesHolder().getHeightAtWorldCoordinates((int) rdX, (int) rdZ);
// Raindrop size, change orientation to face viewer
view2drop.x = (rdX - viewX);
view2drop.y = (rdZ - viewZ);
view2drop.normalize();
float mx = 0.005f * -view2drop.y();
float mz = 0.005f * view2drop.x();
float rainDropletSize = 0.2f + random.nextFloat() * 0.18f;
// Build triangle strip
// 00
raindrops[location * 6 * 4 + 0 * 4 + 0] = rdX - mx;
raindrops[location * 6 * 4 + 0 * 4 + 1] = rdY;
raindrops[location * 6 * 4 + 0 * 4 + 2] = rdZ - mz;
raindrops[location * 6 * 4 + 0 * 4 + 3] = rdMh;
// 01
raindrops[location * 6 * 4 + 1 * 4 + 0] = rdX - mx;
raindrops[location * 6 * 4 + 1 * 4 + 1] = rdY + rainDropletSize;
raindrops[location * 6 * 4 + 1 * 4 + 2] = rdZ - mz;
raindrops[location * 6 * 4 + 1 * 4 + 3] = rdMh + rainDropletSize;
// 10
raindrops[location * 6 * 4 + 2 * 4 + 0] = rdX + mx;
raindrops[location * 6 * 4 + 2 * 4 + 1] = rdY;
raindrops[location * 6 * 4 + 2 * 4 + 2] = rdZ + mz;
raindrops[location * 6 * 4 + 2 * 4 + 3] = rdMh;
// 11
raindrops[location * 6 * 4 + 3 * 4 + 0] = rdX - mx;
raindrops[location * 6 * 4 + 3 * 4 + 1] = rdY + rainDropletSize;
raindrops[location * 6 * 4 + 3 * 4 + 2] = rdZ - mz;
raindrops[location * 6 * 4 + 3 * 4 + 3] = rdMh + rainDropletSize;
// 01
raindrops[location * 6 * 4 + 4 * 4 + 0] = rdX + mx;
raindrops[location * 6 * 4 + 4 * 4 + 1] = rdY;
raindrops[location * 6 * 4 + 4 * 4 + 2] = rdZ + mz;
raindrops[location * 6 * 4 + 4 * 4 + 3] = rdMh;
// 00
raindrops[location * 6 * 4 + 5 * 4 + 0] = rdX + mx;
raindrops[location * 6 * 4 + 5 * 4 + 1] = rdY + rainDropletSize;
raindrops[location * 6 * 4 + 5 * 4 + 2] = rdZ + mz;
raindrops[location * 6 * 4 + 5 * 4 + 3] = rdMh + rainDropletSize;
}
raindropsData.clear();
raindropsData.position(0);
raindropsData.put(raindrops, 0, raindrops.length);
raindropsData.flip();
rainVerticesBuffer.uploadData(raindropsData);
lastX = viewX;
lastZ = viewZ;
}
use of org.joml.Vector2f in project lwjgl3-demos by LWJGL.
the class GrassDemo method generateGrassPatchVao.
private void generateGrassPatchVao() {
int n = NUM_FACES_PER_PATCH;
// Generate the model attributes of a single grass patch
FloatBuffer fb = BufferUtils.createFloatBuffer(NUM_FACES_PER_PATCH * 6 * (3 + 2));
for (int i = 0; i < n; i++) {
float x0 = (float) (Math.cos((double) i / n * Math.PI)) * 0.5f;
float z0 = (float) (Math.sin((double) i / n * Math.PI)) * 0.5f;
float x1 = (float) (Math.cos((double) i / n * Math.PI + Math.PI)) * 0.5f;
float z1 = (float) (Math.sin((double) i / n * Math.PI + Math.PI)) * 0.5f;
fb.put(x0).put(0.0f).put(z0).put(0).put(1);
fb.put(x1).put(0.0f).put(z1).put(1).put(1);
fb.put(x1).put(1.0f).put(z1).put(1).put(0);
fb.put(x1).put(1.0f).put(z1).put(1).put(0);
fb.put(x0).put(1.0f).put(z0).put(0).put(0);
fb.put(x0).put(0.0f).put(z0).put(0).put(1);
}
fb.flip();
// Generate the world-space positions of each patch
final FloatBuffer pb = BufferUtils.createFloatBuffer(NUM_GRASS_PATCHES * 4);
new BestCandidateSampling.Quad().numSamples(NUM_GRASS_PATCHES).numCandidates(20).generate(new Callback2d() {
final Random rnd = new Random(0L);
int index = 0;
public void onNewSample(float x, float y) {
float px = x * MEADOW_SIZE;
float py = y * MEADOW_SIZE;
grassPatchPositions[index++] = new Vector2f(px, py);
pb.put(x * MEADOW_SIZE).put(y * MEADOW_SIZE).put(rnd.nextFloat() * 0.2f + 0.9f).put((SimplexNoise.noise(x * 2.342f, y * 2.0352f) + 1.0f) * 0.5f);
}
});
pb.flip();
// Generate the random rotations for each grass patch
FloatBuffer rb = BufferUtils.createFloatBuffer(NUM_GRASS_PATCHES * 4);
Random rnd = new Random();
Matrix3x2f m = new Matrix3x2f();
for (int i = 0; i < NUM_GRASS_PATCHES; i++) {
float angle = 2.0f * (float) Math.PI * rnd.nextFloat();
m.rotation(angle);
rb.put(m.m00).put(m.m01).put(m.m10).put(m.m11);
}
rb.flip();
grassVao = ARBVertexArrayObject.glGenVertexArrays();
ARBVertexArrayObject.glBindVertexArray(grassVao);
int modelBuffer = glGenBuffers();
int grassBladesPositionsVbo = glGenBuffers();
grassDisplacementVbo = glGenBuffers();
int grassRotationVbo = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, modelBuffer);
glBufferData(GL_ARRAY_BUFFER, fb, GL_STATIC_DRAW);
glVertexAttribPointer(grassPositionAttribute, 3, GL_FLOAT, false, 4 * (3 + 2), 0L);
glVertexAttribPointer(grassTexCoordAttribute, 2, GL_FLOAT, false, 4 * (3 + 2), 4 * 3L);
glEnableVertexAttribArray(grassPositionAttribute);
glEnableVertexAttribArray(grassTexCoordAttribute);
glBindBuffer(GL_ARRAY_BUFFER, grassBladesPositionsVbo);
glBufferData(GL_ARRAY_BUFFER, pb, GL_STATIC_DRAW);
glVertexAttribPointer(grassWorldPositionAttribute, 4, GL_FLOAT, false, 0, 0L);
ARBInstancedArrays.glVertexAttribDivisorARB(grassWorldPositionAttribute, 1);
glEnableVertexAttribArray(grassWorldPositionAttribute);
glBindBuffer(GL_ARRAY_BUFFER, grassDisplacementVbo);
glBufferData(GL_ARRAY_BUFFER, 4 * 2 * NUM_GRASS_PATCHES, GL_STATIC_DRAW);
glVertexAttribPointer(grassDisplacementAttribute, 2, GL_FLOAT, false, 0, 0L);
ARBInstancedArrays.glVertexAttribDivisorARB(grassDisplacementAttribute, 1);
glEnableVertexAttribArray(grassDisplacementAttribute);
glBindBuffer(GL_ARRAY_BUFFER, grassRotationVbo);
glBufferData(GL_ARRAY_BUFFER, rb, GL_STATIC_DRAW);
glVertexAttribPointer(grassRotationAttribute, 4, GL_FLOAT, false, 0, 0L);
ARBInstancedArrays.glVertexAttribDivisorARB(grassRotationAttribute, 1);
glEnableVertexAttribArray(grassRotationAttribute);
glBindBuffer(GL_ARRAY_BUFFER, 0);
ARBVertexArrayObject.glBindVertexArray(0);
}
use of org.joml.Vector2f in project lwjgl3-demos by LWJGL.
the class GrassDemo method update.
private void update() {
long thisTime = System.nanoTime();
float diff = (thisTime - lastTime) / 1E9f;
lastTime = thisTime;
time += diff;
vpMatrix.setPerspective((float) Math.toRadians(30), (float) width / height, 0.1f, 300.0f).lookAt(4, 7, 110, 0, 0, 80, 0, 1, 0).rotateY(time * 0.01f);
/* Update grass displacement using simplex noise */
grassDisplacement.clear();
for (int i = 0; i < NUM_GRASS_PATCHES; i++) {
Vector2f g = grassPatchPositions[i];
grassDisplacement.put(DISPLACEMENT_FACTOR * SimplexNoise.noise(g.x * NOISE_SPATIAL_FACTOR_X + time * NOISE_TIME_FACTOR, g.y * NOISE_SPATIAL_FACTOR_Y + time * NOISE_TIME_FACTOR));
grassDisplacement.put(DISPLACEMENT_FACTOR * SimplexNoise.noise(g.y * NOISE_SPATIAL_FACTOR_Y + time * NOISE_TIME_FACTOR, g.x * NOISE_SPATIAL_FACTOR_X + time * NOISE_TIME_FACTOR));
}
grassDisplacement.flip();
glBindBuffer(GL_ARRAY_BUFFER, grassDisplacementVbo);
glBufferSubData(GL_ARRAY_BUFFER, 0L, grassDisplacement);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
use of org.joml.Vector2f in project Terasology by MovingBlocks.
the class BlockRegionTest method testIntersectionLineSegment.
@Test
void testIntersectionLineSegment() {
BlockRegion a = new BlockRegion(0, 0, 0, 1, 1, 1);
// no intersection
assertEquals(a.intersectLineSegment(3f, 3f, 3f, 2f, 3f, 3f, new Vector2f()), -1);
LineSegmentf l1 = new LineSegmentf(3f, 2f, 3f, 2f, 3f, 2f);
assertEquals(a.intersectLineSegment(l1, new Vector2f()), -1);
// one intersection
assertEquals(a.intersectLineSegment(1.2f, 1.2f, 1.2f, 1.6f, 1.6f, 1.6f, new Vector2f()), 1);
LineSegmentf l2 = new LineSegmentf(-0.6f, 0f, 0f, -0.2f, 1.2f, 0f);
assertEquals(a.intersectLineSegment(l2, new Vector2f()), 1);
// two intersections
assertEquals(a.intersectLineSegment(1.2f, 1.2f, 2f, -0.6f, 0f, -0.2f, new Vector2f()), 2);
LineSegmentf l3 = new LineSegmentf(2f, 2f, 2f, -0.6f, -2f, 0f);
assertEquals(a.intersectLineSegment(l3, new Vector2f()), 2);
// segment inside the BlocRegion
assertEquals(a.intersectLineSegment(0f, 1f, 1.2f, 1f, -0.2f, 0.2f, new Vector2f()), 3);
LineSegmentf l4 = new LineSegmentf(1f, 1f, 1.2f, -0.2f, 0f, 1f);
assertEquals(a.intersectLineSegment(l4, new Vector2f()), 3);
}
Aggregations