示例#1
0
static void generateChunk(const Vec2i &chunk_index, MeshWrapper &mesh, btCollisionWorld *collision_world, FastNoise &noise, Platform *platform, RenderContext *render_context) {
	const u32 terrain_patch_size = CHUNK_SIZE+1;
	const u32 vertex_count = terrain_patch_size * terrain_patch_size;
	Vertex *vertices = (Vertex *)platform->alloc(sizeof(Vertex) * vertex_count);
	f32 *heightmap = (f32 *)platform->alloc(sizeof(f32) * (terrain_patch_size) * (terrain_patch_size));
	u32 vertex_index = 0;
	f32 min_value = 0.0f;
	f32 max_value = FLT_MIN;
	for(u32 z = 0; z < terrain_patch_size; z++) {
		for(u32 x = 0; x < terrain_patch_size; x++) {
			Vertex vertex = {};
			f32 y = (noise.GetValue(x, z) * 0.5f + 0.5f) * 20.0f;
			if(y > max_value)
				max_value = y;

			heightmap[vertex_index] = y;
			vertex.position = Vec3(x, y, z);
			vertex.normal = Vec3::up;
			vertices[vertex_index++] = vertex;
		}
	}

	const u32 index_count = (terrain_patch_size-1) * (terrain_patch_size-1) * 6;
	u16 *indices = (u16 *)platform->alloc(sizeof(u16) * index_count);
	u32 index = 0;
	for(u32 z = 0; z < terrain_patch_size-1; z++) {
		for(u32 x = 0; x < terrain_patch_size-1; x++) {
			indices[index++] = z * terrain_patch_size + x;
			indices[index++] = (z+1) * terrain_patch_size + x;
			indices[index++] = (z+1) * terrain_patch_size + (x+1);

			indices[index++] = z * terrain_patch_size + x;
			indices[index++] = (z+1) * terrain_patch_size + (x+1);
			indices[index++] = z * terrain_patch_size + (x+1);
		}
	}

	mesh = MeshWrapper();

	mesh.vertex_count = vertex_count;
	mesh.vertices = vertices;
	mesh.index_count = index_count;
	mesh.indices = indices;

	mesh.vertex_buffer = render_context->createVertexBuffer(vertices, sizeof(Vertex), vertex_count);
	mesh.index_buffer = render_context->createVertexBuffer(indices, sizeof(u16), index_count, RenderContext::BufferType::Index);

	btHeightfieldTerrainShape *terrain_shape = new btHeightfieldTerrainShape(terrain_patch_size, terrain_patch_size, heightmap, 1.0f, -max_value, max_value, 1, PHY_FLOAT, false);
	btCollisionObject *terrain_obj = new btCollisionObject();
	terrain_obj->setCollisionShape(terrain_shape);
	terrain_obj->setWorldTransform(btTransform(btQuaternion(0, 0, 0, 1), btVector3((terrain_patch_size-1) * 0.5f, 0.0f, (terrain_patch_size-1)*0.5f)));
	collision_world->addCollisionObject(terrain_obj, btBroadphaseProxy::StaticFilter);
}
示例#2
0
void GameState::init(Platform *platform, PlatformWindow *window, RenderContext *render_context, Assets *assets, AudioEngine *audio) {
	DebugRenderQueue::init(render_context, &assets->shaders.editor);
	timer = 0.0f;
	exposure = 1.0f;
	camera.position = Vec3(0.0f, 0.0f, -5.0f);
	camera.yaw = 90.0f;
	camera.beginFrame(platform, window);

	npc_position = Vec3(10.0f, 0.0f, 0.0f);

	f32 light_value = 300.0f;
	lights[0] = {
		Vec3(-10.0f,  10.0f, -10.0f),
		Vec4(light_value, light_value, light_value, 10.0f),
		10.0f,
	};
	
	
	lights[1] = {
		Vec3( 10.0f,  10.0f, -10.0f),
		Vec4(light_value, light_value, light_value, 1.0f),
		10.0f,
	};
	
	lights[2] = {
		Vec3(-10.0f, -10.0f, -10.0f),
		Vec4(light_value, light_value, light_value, 1.0f),
		10.0f,
	};
	
	lights[3] = {
		Vec3( 10.0f, -10.0f, -10.0f),
		Vec4(light_value, light_value, light_value, 1.0f),
		10.0f,
	};

	test_sphere_mesh = MeshUtils::genSphereMesh(platform, render_context, Vec3(), 1.0f);
	test_cube_mesh = MeshUtils::genCubeMesh(platform, render_context);

	alpha_blend = render_context->createBlendState();
	raster_state = render_context->createRasterState(false, true, true);
	wireframe_raster_state = render_context->createRasterState(false, true, true, true);
	depth_state = render_context->createDepthStencilState(true);
	no_depth_state = render_context->createDepthStencilState(false);
	no_depth_raster_state = render_context->createRasterState(false, false, true);

	RenderContext::LayoutElement layout[] = {
		{"POSITION", RenderContext::Format::Vec3}
	};

	RenderContext::LayoutElement static_layout[] = {
		{"POSITION", RenderContext::Format::Vec3},
		{"NORMAL", RenderContext::Format::Vec3},
		{"COLOR", RenderContext::Format::Vec3},
	};

	RenderContext::LayoutElement textured_layout[] = {
		{"POSITION", RenderContext::Format::Vec3},
		{"TEXCOORD", RenderContext::Format::Vec2}
	};

	RenderContext::LayoutElement vertex_layout_desc[] = {
		{"POSITION", RenderContext::Format::Vec3},
		{"NORMAL", RenderContext::Format::Vec3},
		{"TEXCOORD", RenderContext::Format::Vec2},
	};

	RenderContext::LayoutElement skin_vertex_layout_desc[] = {
		{"POSITION", RenderContext::Format::Vec3}, // 
		{"NORMAL", RenderContext::Format::Vec3},
		{"TEXCOORD", RenderContext::Format::Vec2},
		{"BLENDWEIGHT", RenderContext::Format::Vec4}, // weights 
		{"BLENDINDICES", RenderContext::Format::Int4}, // weights 
	};

	skybox_layout = render_context->createShaderLayout(layout, ArrayCount(layout), &assets->shaders.skybox);
	textired_geom_layout = render_context->createShaderLayout(textured_layout, ArrayCount(textured_layout), &assets->shaders.hdr_blit);
	vertex_layout = render_context->createShaderLayout(vertex_layout_desc, ArrayCount(vertex_layout_desc), &assets->shaders.geometry);
	static_vertex_layout = render_context->createShaderLayout(static_layout, ArrayCount(static_layout), &assets->shaders.statics);
	skin_vertex_layout = render_context->createShaderLayout(skin_vertex_layout_desc, ArrayCount(skin_vertex_layout_desc), &assets->shaders.skinned);

	per_scene_constants = render_context->createShaderConstant(sizeof(PerSceneConstants));
	per_object_constants = render_context->createShaderConstant(sizeof(PerObjectConstants));
	hdr_blit_constants = render_context->createShaderConstant(sizeof(HdrBlitConstants));
	light_constants = render_context->createShaderConstant(sizeof(ShaderLight));
	ao_pass_constants = render_context->createShaderConstant(sizeof(AOPassConstants));
	blur_constants = render_context->createShaderConstant(sizeof(BlurConstants));
	skinned_mesh_constants = render_context->createShaderConstant(sizeof(SkinnedMeshConstants));

	texture_sampler = render_context->createSampler();
	wrap_texture_sampler = render_context->createSampler(true);
	pn_prepass.init(render_context, camera.window_dimensions.x, camera.window_dimensions.y);
	// hdr_render_texture = render_context->createRenderTexture(camera.window_dimensions.x, camera.window_dimensions.y, RenderContext::Format::Vec4, 1);
	// hdr_depth_texture = render_context->createDepthStencilTexture(camera.window_dimensions.x, camera.window_dimensions.y, 1);

	Vec3 kernel_noise[16];
	{
		std::uniform_real_distribution<float> random_floats(0.0f, 1.0f);
		std::default_random_engine generator;
		for(u32 i = 0; i < 64; i++) {
			Vec4 sample = Vec4(
				random_floats(generator) * 2.0f - 1.0f,
				random_floats(generator) * 2.0f - 1.0f,
				random_floats(generator),
				0.0f
			);

			sample = Vec4(Vec3::normalize(sample.xyz), 0.0f);
			sample *= random_floats(generator);
			f32 scale = (f32)i / 64.0f;
			scale = Math::lerp(0.1f, 1.0f, scale * scale);
			sample *= scale;
			ssao_pass_constant_data.kernel[i] = sample;
		}

		for(u32 i = 0; i < 16; i++) {
			kernel_noise[i] = Vec3(
				random_floats(generator) * 2.0f - 1.0f,
				random_floats(generator) * 2.0f - 1.0f,
				0.0f
			);
		}
	}
	ssao_pass_constant_data.kernel_radius = 1.0f;
	ssao_pass_constant_data.kernel_bias = 0.0f;
	ssao_pass_constant_data.kernel_size = 32;
	ssao_noise_texture = render_context->createTexture2D(&kernel_noise, 4, 4, RenderContext::Format::Vec3);
	blur_constants_data.blur_amount = 4;


	createRenderTextures(render_context);
	
	btDefaultCollisionConfiguration *collision_config = new btDefaultCollisionConfiguration();
	btCollisionDispatcher *dispatcher = new btCollisionDispatcher(collision_config);
	btBroadphaseInterface *overlapping_pair_cache = new btDbvtBroadphase();
	overlapping_pair_cache->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
	btSequentialImpulseConstraintSolver *solver = new btSequentialImpulseConstraintSolver();
	collision_world = new btCollisionWorld(dispatcher, overlapping_pair_cache, collision_config);
	physics_drawer = new PhysicsDebugDrawer();
	collision_world->setDebugDrawer(physics_drawer);


	btStaticPlaneShape *plane = new btStaticPlaneShape(btVector3(0, 1, 0), 0.0f);
	btCollisionObject *plane_obj = new btCollisionObject();
	plane_obj->setCollisionShape(plane);
	collision_world->addCollisionObject(plane_obj);
	
	btCapsuleShapeZ *player_collider = new btCapsuleShapeZ(1.0f, 0.5f);

	btPairCachingGhostObject *ghost_object = new btPairCachingGhostObject();
	ghost_object->setWorldTransform(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 30, 0)));
	ghost_object->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);
	ghost_object->setCollisionShape(player_collider);
	
	char_controller = new btKinematicCharacterController(ghost_object, player_collider, 0.5f, btVector3(0, 1, 0));
	char_controller->setGravity(btVector3(0, 0, 0));
	
	collision_world->addCollisionObject(ghost_object, btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);

	btCollisionObject *obs_obj = new btCollisionObject();
	btBoxShape *obs_shape = new btBoxShape(btVector3(6.0f, 3.0f, 6.0f));
	obs_obj->setCollisionShape(obs_shape);
	obs_obj->setWorldTransform(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0)));
	// collision_world->addCollisionObject(obs_obj, btBroadphaseProxy::StaticFilter);

	{
		FastNoise noise;
		noise.SetSeed(time(0));
		const u32 terrain_patch_size = CHUNK_SIZE+1;
		const u32 vertex_count = terrain_patch_size * terrain_patch_size;
		Vertex *vertices = (Vertex *)platform->alloc(sizeof(Vertex) * vertex_count);
		f32 *heightmap = (f32 *)platform->alloc(sizeof(f32) * (terrain_patch_size) * (terrain_patch_size));
		u32 vertex_index = 0;
		f32 min_value = 0.0f;
		f32 max_value = FLT_MIN;
		for(u32 z = 0; z < terrain_patch_size; z++) {
			for(u32 x = 0; x < terrain_patch_size; x++) {
				Vertex vertex = {};
				f32 y = (noise.GetValue(x, z) * 0.5f + 0.5f) * 20.0f;
				if(y > max_value)
					max_value = y;

				heightmap[vertex_index] = y;
				vertex.position = Vec3(x, y, z);
				vertex.normal = Vec3::up;
				vertices[vertex_index++] = vertex;
			}
		}

		const u32 index_count = (terrain_patch_size-1) * (terrain_patch_size-1) * 6;
		u16 *indices = (u16 *)platform->alloc(sizeof(u16) * index_count);
		u32 index = 0;
		for(u32 z = 0; z < terrain_patch_size-1; z++) {
			for(u32 x = 0; x < terrain_patch_size-1; x++) {
				indices[index++] = z * terrain_patch_size + x;
				indices[index++] = (z+1) * terrain_patch_size + x;
				indices[index++] = (z+1) * terrain_patch_size + (x+1);

				indices[index++] = z * terrain_patch_size + x;
				indices[index++] = (z+1) * terrain_patch_size + (x+1);
				indices[index++] = z * terrain_patch_size + (x+1);
			}
		}

		terrain_mesh = MeshWrapper();

		terrain_mesh.vertex_count = vertex_count;
		terrain_mesh.vertices = vertices;
		terrain_mesh.index_count = index_count;
		terrain_mesh.indices = indices;

		terrain_mesh.vertex_buffer = render_context->createVertexBuffer(vertices, sizeof(Vertex), vertex_count);
		terrain_mesh.index_buffer = render_context->createVertexBuffer(indices, sizeof(u16), index_count, RenderContext::BufferType::Index);

		btHeightfieldTerrainShape *terrain_shape = new btHeightfieldTerrainShape(terrain_patch_size, terrain_patch_size, heightmap, 1.0f, -max_value, max_value, 1, PHY_FLOAT, false);
		btCollisionObject *terrain_obj = new btCollisionObject();
		terrain_obj->setCollisionShape(terrain_shape);
		terrain_obj->setWorldTransform(btTransform(btQuaternion(0, 0, 0, 1), btVector3((terrain_patch_size-1) * 0.5f, 0.0f, (terrain_patch_size-1)*0.5f)));
		collision_world->addCollisionObject(terrain_obj, btBroadphaseProxy::StaticFilter);
	}

	// for(int i = 0; i < assets->static_meshes.count; i++) {
	// 	StaticMeshData *mesh_data = &assets->static_meshes.meshes[i];
	// 	btTriangleMesh *mesh_shape = new btTriangleMesh();
	// 	btIndexedMesh indexed_mesh;
	// 	indexed_mesh.m_indexType = PHY_ScalarType::PHY_SHORT;
	// 	indexed_mesh.m_numTriangles = mesh_data->index_count;
	// 	indexed_mesh.m_numVertices = mesh_data->vertex_count;
	// 	indexed_mesh.m_triangleIndexStride = sizeof(u16);
	// 	indexed_mesh.m_vertexStride = sizeof(StaticMeshVertex);
	// 	indexed_mesh.m_vertexType = PHY_ScalarType::PHY_FLOAT;
	// 	indexed_mesh.m_triangleIndexBase = (u8 *)mesh_data->indices;
	// 	indexed_mesh.m_vertexBase = (u8 *)mesh_data->vertices;
	// 	mesh_shape->addIndexedMesh(indexed_mesh, PHY_ScalarType::PHY_SHORT);
		
	// 	btBvhTriangleMeshShape *shape = new btBvhTriangleMeshShape(mesh_shape, false);
	// 	mesh_data->physics_obj = (void *)shape;
	// }

	// StaticMeshReference *reference = new StaticMeshReference();
	// reference->index = 29;
	// reference->transform = Mat4::translate(Vec3(-10.0f, 0.0f, 0.0f));
	// static_mesh_references = reference;
	// static_mesh_reference_count = 1;

	// genStaticMeshColliders(assets);
	generateMap();

	char_sequence.clips.add(&assets->walking_anim);
	char_sequence.clips.add(&assets->turning_anim);
}
示例#3
0
World::World(int seed) : _seed(seed) {
    FastNoise *noise = new FastNoise;
    noise->SetSeed(seed);
    noise->SetNoiseType(FastNoise::Simplex);
    _chunks.push_back(new Chunk(noise));
}