<commit_before>#include "DynamicVertexBufferDemo.hpp"
#include <ciri/util/Log.hpp>
#include <cc/MatrixFunc.hpp>

DynamicVertexBufferDemo::DynamicVertexBufferDemo()
	: IDemo(), _graphicsDevice(nullptr), _window(nullptr), _depthStencilState(nullptr), _rasterizerState(nullptr),
		_clothRunning(true) {
}

DynamicVertexBufferDemo::~DynamicVertexBufferDemo() {
}

DemoConfig DynamicVertexBufferDemo::getConfig() {
	DemoConfig cfg;
	cfg.windowWidth = 1280;
	cfg.windowHeight = 720;
	cfg.deviceType = ciri::GraphicsDeviceFactory::DirectX;
	return cfg;
}

void DynamicVertexBufferDemo::onInitialize( ciri::Window* window, ciri::IGraphicsDevice* graphicsDevice ) {
	_window = window;
	_graphicsDevice = graphicsDevice;

	// window size
	const cc::Vec2ui windowSize = window->getSize();

	// configure the camera
	_camera.setAspect((float)windowSize.x / (float)windowSize.y);
	_camera.setPlanes(0.1f, 1000.0f);
	_camera.setYaw(247.9f);
	_camera.setPitch(22.4f);
	_camera.setOffset(49.5f);
	_camera.setSensitivity(1.0f, 1.0f, 0.25f);
	_camera.setLerpStrength(100.0f);
	_camera.resetPosition();
}

void DynamicVertexBufferDemo::onLoadContent() {
	ciri::DepthStencilDesc depthDesc;
	_depthStencilState = _graphicsDevice->createDepthStencilState(depthDesc);
	if( nullptr == _depthStencilState ) {
		ciri::Logs::get(ciri::Logs::Debug).printError("Failed to create depth stencil state.");
	}

	ciri::RasterizerDesc rasterDesc;
	rasterDesc.cullMode = ciri::CullMode::None;//Clockwise;
	//rasterDesc.fillMode = ciri::FillMode::Wireframe;
	_rasterizerState = _graphicsDevice->createRasterizerState(rasterDesc);
	if( nullptr == _rasterizerState ) {
		ciri::Logs::get(ciri::Logs::Debug).printError("Failed to create rasterizer state.");
	}

	if( !_grid.create(_graphicsDevice) ) {
		ciri::Logs::get(ciri::Logs::Debug).printError("Failed to create grid.");
	}

	if( !_axis.create(5.0f, _graphicsDevice) ) {
		ciri::Logs::get(ciri::Logs::Debug).printError("Failed to create axis.");
	}

	if( !_simpleShader.create(_graphicsDevice) ) {
		ciri::Logs::get(ciri::Logs::Debug).printError("Failed to create simple shader.");
	}

	if( _flagpole.addFromObj("dynvb/flag-pole.obj") ) {
		if( !_flagpole.build(_graphicsDevice) ) {
			ciri::Logs::get(ciri::Logs::Debug).printError("Failed to build flagpole model.obj.");
		}
	} else {
		ciri::Logs::get(ciri::Logs::Debug).printError("Failed to load flag-pole.obj.");
	}
	_flagpole.setShader(_simpleShader.getShader());

	_cloth.setDivisions(20, 20);
	_cloth.setSize(7);
	_cloth.setMass(1.0f);
	_cloth.setDamping(-0.0125f);
	_cloth.setGravity(cc::Vec3f(0.0f, -9.81f, 0.0f));
	_cloth.build(_graphicsDevice);
}

void DynamicVertexBufferDemo::onEvent( ciri::WindowEvent evt ) {
	switch( evt.type) {
		case ciri::WindowEvent::Resized: {
			if( _graphicsDevice->resizeDefaultRenderTargets() != ciri::err::CIRI_OK ) {
				printf("Failed to resize default render targets.\n");
			}
			break;
		}
	}
}

void DynamicVertexBufferDemo::onUpdate( double deltaTime, double elapsedTime ) {
	// get current input states
	ciri::KeyboardState currKeyState;
	ciri::MouseState currMouseState;
	ciri::Input::getKeyboardState(&currKeyState);
	ciri::Input::getMouseState(&currMouseState, _window);

	// check for close w/ escape
	if( currKeyState.isKeyDown(ciri::Keyboard::Escape) ) {
		_window->destroy();
		return;
	}

	// debug camera info
	if( currKeyState.isKeyDown(ciri::Keyboard::F9) && _prevKeyState.isKeyUp(ciri::Keyboard::F9) ) {
		const cc::Vec3f& pos = _camera.getPosition();
		const float yaw = _camera.getYaw();
		const float pitch = _camera.getPitch();
		const float dolly = _camera.getOffset();
		printf("pos(%f/%f/%f); yaw(%f); pitch(%f); dolly(%f)\n", pos.x, pos.y, pos.z, yaw, pitch, dolly);
	}

	// camera movement
	if( currKeyState.isKeyDown(ciri::Keyboard::LAlt) ) {
		// rotation
		if( currMouseState.isButtonDown(ciri::MouseButton::Left) ) {
			const float dx = (float)currMouseState.x - (float)_prevMouseState.x;
			const float dy = (float)currMouseState.y - (float)_prevMouseState.y;
			_camera.rotateYaw(-dx);
			_camera.rotatePitch(-dy);
		}
		// dolly
		if( currMouseState.isButtonDown(ciri::MouseButton::Right) ) {
			const float dx = (float)currMouseState.x - (float)_prevMouseState.x;
			const float dy = (float)currMouseState.y - (float)_prevMouseState.y;
			const float val = (fabsf(dx) > fabsf(dy)) ? dx : dy;
			_camera.dolly(val);
		}
		// pan
		if( currMouseState.isButtonDown(ciri::MouseButton::Middle) ) {
			const float dx = (float)currMouseState.x - (float)_prevMouseState.x;
			const float dy = (float)currMouseState.y - (float)_prevMouseState.y;
			_camera.pan(dx, -dy);
		}
	}
	_camera.update(deltaTime);

	// update cloth<commit_msg>Moved the camera in the dynamic vertex buffer test to be at a more reasonable starting location.<commit_after>#include "DynamicVertexBufferDemo.hpp"
#include <ciri/util/Log.hpp>
#include <cc/MatrixFunc.hpp>

DynamicVertexBufferDemo::DynamicVertexBufferDemo()
	: IDemo(), _graphicsDevice(nullptr), _window(nullptr), _depthStencilState(nullptr), _rasterizerState(nullptr),
		_clothRunning(true) {
}

DynamicVertexBufferDemo::~DynamicVertexBufferDemo() {
}

DemoConfig DynamicVertexBufferDemo::getConfig() {
	DemoConfig cfg;
	cfg.windowWidth = 1280;
	cfg.windowHeight = 720;
	cfg.deviceType = ciri::GraphicsDeviceFactory::OpenGL;
	return cfg;
}

void DynamicVertexBufferDemo::onInitialize( ciri::Window* window, ciri::IGraphicsDevice* graphicsDevice ) {
	_window = window;
	_graphicsDevice = graphicsDevice;

	// window size
	const cc::Vec2ui windowSize = window->getSize();

	// configure the camera
	_camera.setAspect((float)windowSize.x / (float)windowSize.y);
	_camera.setPlanes(0.1f, 1000.0f);
	_camera.setYaw(238.9f);
	_camera.setPitch(16.4f);
	_camera.setOffset(33.5f);
	_camera.setSensitivity(1.0f, 1.0f, 0.25f);
	_camera.setLerpStrength(100.0f);
	_camera.setTarget(cc::Vec3f(0.0f, 8.0f, 5.0f));
	_camera.resetPosition();
}

void DynamicVertexBufferDemo::onLoadContent() {
	ciri::DepthStencilDesc depthDesc;
	_depthStencilState = _graphicsDevice->createDepthStencilState(depthDesc);
	if( nullptr == _depthStencilState ) {
		ciri::Logs::get(ciri::Logs::Debug).printError("Failed to create depth stencil state.");
	}

	ciri::RasterizerDesc rasterDesc;
	rasterDesc.cullMode = ciri::CullMode::None;//Clockwise;
	//rasterDesc.fillMode = ciri::FillMode::Wireframe;
	_rasterizerState = _graphicsDevice->createRasterizerState(rasterDesc);
	if( nullptr == _rasterizerState ) {
		ciri::Logs::get(ciri::Logs::Debug).printError("Failed to create rasterizer state.");
	}

	if( !_grid.create(_graphicsDevice) ) {
		ciri::Logs::get(ciri::Logs::Debug).printError("Failed to create grid.");
	}

	if( !_axis.create(5.0f, _graphicsDevice) ) {
		ciri::Logs::get(ciri::Logs::Debug).printError("Failed to create axis.");
	}

	if( !_simpleShader.create(_graphicsDevice) ) {
		ciri::Logs::get(ciri::Logs::Debug).printError("Failed to create simple shader.");
	}

	if( _flagpole.addFromObj("dynvb/flag-pole.obj") ) {
		if( !_flagpole.build(_graphicsDevice) ) {
			ciri::Logs::get(ciri::Logs::Debug).printError("Failed to build flagpole model.obj.");
		}
	} else {
		ciri::Logs::get(ciri::Logs::Debug).printError("Failed to load flag-pole.obj.");
	}
	_flagpole.setShader(_simpleShader.getShader());

	_cloth.setDivisions(20, 20);
	_cloth.setSize(7);
	_cloth.setMass(1.0f);
	_cloth.setDamping(-0.0125f);
	_cloth.setGravity(cc::Vec3f(0.0f, -9.81f, 0.0f));
	_cloth.build(_graphicsDevice);
}

void DynamicVertexBufferDemo::onEvent( ciri::WindowEvent evt ) {
	switch( evt.type) {
		case ciri::WindowEvent::Resized: {
			if( _graphicsDevice->resizeDefaultRenderTargets() != ciri::err::CIRI_OK ) {
				printf("Failed to resize default render targets.\n");
			}
			break;
		}
	}
}

void DynamicVertexBufferDemo::onUpdate( double deltaTime, double elapsedTime ) {
	// get current input states
	ciri::KeyboardState currKeyState;
	ciri::MouseState currMouseState;
	ciri::Input::getKeyboardState(&currKeyState);
	ciri::Input::getMouseState(&currMouseState, _window);

	// check for close w/ escape
	if( currKeyState.isKeyDown(ciri::Keyboard::Escape) ) {
		_window->destroy();
		return;
	}

	// debug camera info
	if( currKeyState.isKeyDown(ciri::Keyboard::F9) && _prevKeyState.isKeyUp(ciri::Keyboard::F9) ) {
		const cc::Vec3f& pos = _camera.getPosition();
		const float yaw = _camera.getYaw();
		const float pitch = _camera.getPitch();
		const float dolly = _camera.getOffset();
		const cc::Vec3f& target = _camera.getTarget();
		printf("pos(%f/%f/%f); yaw(%f); pitch(%f); dolly(%f); target(%f/%f/%f)\n", pos.x, pos.y, pos.z, yaw, pitch, dolly, target.x, target.y, target.z);
	}

	// camera movement
	if( currKeyState.isKeyDown(ciri::Keyboard::LAlt) ) {
		// rotation
		if( currMouseState.isButtonDown(ciri::MouseButton::Left) ) {
			const float dx = (float)currMouseState.x - (float)_prevMouseState.x;
			const float dy = (float)currMouseState.y - (float)_prevMouseState.y;
			_camera.rotateYaw(-dx);
			_camera.rotatePitch(-dy);
		}
		// dolly
		if( currMouseState.isButtonDown(ciri::MouseButton::Right) ) {
			const float dx = (float)currMouseState.x - (float)_prevMouseState.x;
			const float dy = (float)currMouseState.y - (float)_prevMouseState.y;
			const float val = (fabsf(dx) > fabsf(dy)) ? dx : dy;
			_camera.dolly(val);
		}
		// pan
		if( currMouseState.isButtonDown(ciri::MouseButton::Middle) ) {
			const float dx = (float)currMouseState.x - (float)_prevMouseState.x;
			const float dy = (float)currMouseState.y - (float)_prevMouseState.y;
			_camera.pan(dx, -dy);
		}
	}
	_camera.update(deltaTime);

	// update cloth
{'fraction_non_alphanumeric': 0.15733389051399915, 'fraction_numerical': 0.013581278729628082, 'mean_word_length': 5.561343385880741, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4434182', 'n_tokens_mistral': 3717, 'n_tokens_neox': 3479, 'n_words': 558}