/// //Initializes a new force state // //Parameters: // state: A pointer to the state to initialize as a force state // force: A pointer to a vector to copy as the force this state will apply // radius: A pointer to a vector to copy as the radius this state will apply the force at void State_Force_Initialize(State* state, Vector* force, Vector* radius) { struct State_Force_Members* members = (struct State_Force_Members*)malloc(sizeof(struct State_Force_Members)); state->members = (State_Members)members; members->force = Vector_Allocate(); Vector_Initialize(members->force, 3); Vector_Copy(members->force, force); members->radius = Vector_Allocate(); Vector_Initialize(members->radius, 3); Vector_Copy(members->radius, radius); state->State_Members_Free = State_Force_Free; state->State_Update = State_Force_Update; }
/// //Initializes a rendering buffer // //Parameters: // buffer: Rendering buffer to initialize static void RenderingManager_InitializeBuffer(RenderingBuffer* buffer) { //Shaders buffer->shaderPrograms = (ShaderProgram**)malloc(sizeof(ShaderProgram*)); buffer->shaderPrograms[0] = ShaderProgram_Allocate(); ShaderProgram_Initialize(buffer->shaderPrograms[0], "./Shader/VertexShader.glsl", "./Shader/FragmentShader.glsl"); //Checking shaders if (buffer->shaderPrograms[0]->shaderProgramID == 0) { printf("\nError Creating Shader Program!\nShader Results:\nV: %d\tF: %d\tP: %d\n", buffer->shaderPrograms[0]->vertexShaderID, buffer->shaderPrograms[0]->fragmentShaderID, buffer->shaderPrograms[0]->shaderProgramID); } //Camera buffer->camera = Camera_Allocate(); Camera_Initialize(buffer->camera); //Lighting buffer->directionalLightVector = Vector_Allocate(); Vector_Initialize(buffer->directionalLightVector, 3); //buffer->directionalLightVector->components[0] = -0.77f; //buffer->directionalLightVector->components[2] = -0.77f; buffer->directionalLightVector->components[2] = -1.0f; //Debug buffer->debugOctTree = 0; }
/// //Initializes a Template state // //Parameters: // s: A pointer to the state to initialize // acceleration: The acceleration of the parkourController // maxVelocity: The maximum linear velocity of the parkourController // angularVelocity: The angular velocity of the parkour controller (How fast can you turn?) // jumpImpulse: The force exerted when jumping // shootSpeed: The amount of seconds you must wait before shooting againS void State_ParkourController_Initialize(State* s, const float acceleration, const float maxVelocity, const float angularVelocity, const float jumpImpulse, const float shootSpeed) { struct State_ParkourController_Members* members = (struct State_ParkourController_Members*)malloc(sizeof(struct State_ParkourController_Members)); s->members = (State_Members*)members; //Set state members members->acceleration = acceleration; members->angularVelocity = angularVelocity; members->maxVelocity = maxVelocity; members->angularVelocity = angularVelocity; members->jumpMag = jumpImpulse; members->verticalRunning = 0; members->horizontalRunning = 0; members->spinningTime = 3.14159f / spinningRate; members->spinningTimer = members->spinningTime; members->shootTimer = 0.0f; members->shootCooldown = shootSpeed; members->wallNormal = Vector_Allocate(); Vector_Initialize(members->wallNormal, 3); //Set functions s->State_Update = State_ParkourController_Update; s->State_Members_Free = State_ParkourController_Free; }
/// //Rotates the runner controller // obj: A pointer to the game object to rotate // state: A pointer to the runner controller rotating the object void State_RunnerController_Rotate(GObject* obj, State* state) { // create a camera object Camera* cam = RenderingManager_GetRenderingBuffer()->camera; //Grab the state members struct State_RunnerController_Members* members = (struct State_RunnerController_Members*)state->members; // if player's mouse is locked if(InputManager_GetInputBuffer().mouseLock) { int deltaMouseX = (InputManager_GetInputBuffer().mousePosition[0] - InputManager_GetInputBuffer().previousMousePosition[0]); int deltaMouseY = (InputManager_GetInputBuffer().mousePosition[1] - InputManager_GetInputBuffer().previousMousePosition[1]); Vector* axis = Vector_Allocate(); Vector_Initialize(axis,3); if(deltaMouseX != 0) { axis->components[1] = 1.0f; // rotate the camera Camera_ChangeYaw(cam, members->angularVelocity * deltaMouseX); axis->components[1] = 0.0f; } if (deltaMouseY != 0) { Vector forwardVector; Vector_INIT_ON_STACK(forwardVector, 3); Matrix_SliceRow(&forwardVector, cam->rotationMatrix, 2, 0, 3); // Keep camera from overextending it's boundaries. if (deltaMouseY > 0) { if (Vector_DotProduct(&forwardVector, &Vector_E2) < 0.7f) { axis->components[0] = 1.0f; Camera_ChangePitch(cam, members->angularVelocity * deltaMouseY); axis->components[0] = 0.0f; } } else if (deltaMouseY < 0) { if (Vector_DotProduct(&forwardVector, &Vector_E2) > -0.7f) { axis->components[0] = 1.0f; Camera_ChangePitch(cam, members->angularVelocity * deltaMouseY); axis->components[0] = 0.0f; } } } } }
/// //Initializes a material // //Parameters: // mat: A pointer to the material to initialize // t: A pointer to the texture which this material represents an instance of void Material_Initialize(Material* mat, Texture* t) { //Allocate & Initialize color matrix mat->colorMatrix = Matrix_Allocate(); Matrix_Initialize(mat->colorMatrix, 4, 4); //Allocate & initialize tile vector mat->tile = Vector_Allocate(); Vector_Initialize(mat->tile, 2); mat->tile->components[0] = mat->tile->components[1] = 1.0f; mat->texture = t; }
/// //Initializes a runner controller state // //Parameters: // state: A pointer to the state to initialize as a runner controller // acceleration: The acceleration of the runner // maxvelocity: The max speed of the runner // angularVelocity: the angular velocity of the runner // jumpMag: The magnitude of the impulse which will be applied when the player jumps void State_RunnerController_Initialize(State* state, const float acceleration, const float maxVelocity, const float angularVelocity, const float jumpMag) { struct State_RunnerController_Members* members = (struct State_RunnerController_Members*)malloc(sizeof(struct State_RunnerController_Members)); state->members = (State_Members)members; members->acceleration = acceleration; members->angularVelocity = angularVelocity; members->maxVelocity = maxVelocity; members->jumpMag = jumpMag; members->verticalRunning = 0; members->horizontalRunning = 0; members->spinningTime = 3.14159f / spinningRate; members->spinningTimer = members->spinningTime; members->wallNormal = Vector_Allocate(); Vector_Initialize(members->wallNormal, 3); state->State_Members_Free = State_RunnerController_Free; state->State_Update = State_RunnerController_Update; }
/// //Rotates the rendering manager's camera according to change in mouse position // //Parameters: // GO: The game object this state is attached to // state: The First person camera state updating the gameObject void State_FirstPersonCamera_Rotate(GObject* GO, State* state) { Camera* cam = RenderingManager_GetRenderingBuffer().camera; if(InputManager_GetInputBuffer().mouseLock) { float dt = TimeManager_GetDeltaSec(); int deltaMouseX = (InputManager_GetInputBuffer().mousePosition[0] - InputManager_GetInputBuffer().previousMousePosition[0]); int deltaMouseY = (InputManager_GetInputBuffer().mousePosition[1] - InputManager_GetInputBuffer().previousMousePosition[1]); Vector* axis = Vector_Allocate(); Vector_Initialize(axis, 3); if (deltaMouseX != 0) { axis->components[1] = 1.0f; Camera_Rotate(cam, axis, state->members->rotationSpeed * deltaMouseX); axis->components[1] = 0.0f; } if (deltaMouseY != 0) { axis->components[0] = 1.0f; Camera_Rotate(cam, axis, state->members->rotationSpeed * deltaMouseY); axis->components[0] = 0.0f; } Vector_Free(axis); } }
/// //Initializes the scene within the engine. //This must be done after all engine components are initialized. void InitializeScene(void) { /// //Camera controller simulation GObject* cam = GObject_Allocate(); GObject_Initialize(cam); State* state = State_Allocate(); cam->body = RigidBody_Allocate(); RigidBody_Initialize(cam->body, cam->frameOfReference, 1.0f); cam->body->coefficientOfRestitution = 0.1f; cam->collider = Collider_Allocate(); AABBCollider_Initialize(cam->collider, 2.5f, 3.0f, 2.5f, &Vector_ZERO); State_ParkourController_Initialize(state, 7.0f, 10.0f, 0.05f, 50.0f, 0.1f); GObject_AddState(cam,state); ObjectManager_AddObject(cam); //Create floor GObject* block = GObject_Allocate(); GObject_Initialize(block); block->mesh = AssetManager_LookupMesh("Cube"); block->collider = Collider_Allocate(); AABBCollider_Initialize(block->collider, 2.0f, 2.0f, 2.0f, &Vector_ZERO); block->body = RigidBody_Allocate(); RigidBody_Initialize(block->body, block->frameOfReference, 0.0f); block->body->freezeTranslation = block->body->freezeRotation = 1; block->body->dynamicFriction = block->body->staticFriction = 0.1f; block->body->rollingResistance = 0.25f; Vector v; Vector_INIT_ON_STACK(v, 3); v.components[0] = v.components[2] = 40.0f; v.components[1] = 1.0f; GObject_Scale(block, &v); Vector_Copy(&v, &Vector_ZERO); v.components[1] = -10.0f; GObject_Translate(block, &v); ObjectManager_AddObject(block); //Create sphere block = GObject_Allocate(); GObject_Initialize(block); block->mesh = AssetManager_LookupMesh("Sphere"); block->material = Material_Allocate(); Material_Initialize(block->material, AssetManager_LookupTexture("Earth")); //*Matrix_Index(block->material->colorMatrix, 2, 2) = 0.0f; //*Matrix_Index(block->material->colorMatrix, 1, 1) = 0.0f; block->collider = Collider_Allocate(); SphereCollider_Initialize(block->collider, 1.0f); block->body = RigidBody_Allocate(); RigidBody_Initialize(block->body, block->frameOfReference, 0.0f); block->body->dynamicFriction = block->body->staticFriction = 1.0f; block->body->coefficientOfRestitution = 0.9f; Vector_Copy(&v, &Vector_ZERO); v.components[0] = -3.0f; v.components[1] = 5.0f; v.components[2] = -10.0f; GObject_Translate(block, &v); Vector_Copy(&v, &Vector_ZERO); v.components[0] = v.components[1] = v.components[2] = 10.0f; GObject_Scale(block, &v); ObjectManager_AddObject(block); //Set gravity Vector* gravity = Vector_Allocate(); Vector_Initialize(gravity, 3); gravity->components[1] = -9.81f; PhysicsManager_AddGlobalAcceleration(gravity); }