/// //Initializes a sphere collider // //Parameters: // collider: The collider being initialized // rad: The radius of the collider to initialize //TODO: // centroid: A pointer to a vector to copy as the centroid of the sphere collider void SphereCollider_Initialize(Collider* collider, float rad) { //Initialize collider Collider_Initialize(collider, COLLIDER_SPHERE, AssetManager_LookupMesh("Sphere")); //Allocate data collider->data->sphereData = SphereCollider_AllocateData(); //Initialize data collider->data->sphereData->radius = rad; }
/// //Renders a gameobject as it's mesh. // //Parameters: // GO: Game object to render void RenderingManager_Render(LinkedList* gameObjects) { //Clear buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Set directional light glProgramUniform3fv(renderingBuffer->shaderPrograms[0]->shaderProgramID, renderingBuffer->shaderPrograms[0]->directionalLightVectorLocation, 1, renderingBuffer->directionalLightVector->components); Matrix modelMatrix; Matrix_INIT_ON_STACK(modelMatrix, 4, 4); Matrix viewMatrix; Matrix_INIT_ON_STACK(viewMatrix, 4, 4); Matrix modelViewProjectionMatrix; Matrix_INIT_ON_STACK(modelViewProjectionMatrix, 4, 4); //Turn camera's frame of reference into view matrix Camera_ToMatrix4(renderingBuffer->camera, &viewMatrix); //Set viewMatrix uniform glProgramUniformMatrix4fv(renderingBuffer->shaderPrograms[0]->shaderProgramID, renderingBuffer->shaderPrograms[0]->viewMatrixLocation, 1, GL_TRUE, viewMatrix.components); //Set projectionMatrix Uniform glProgramUniformMatrix4fv(renderingBuffer->shaderPrograms[0]->shaderProgramID, renderingBuffer->shaderPrograms[0]->projectionMatrixLocation, 1, GL_TRUE, renderingBuffer->camera->projectionMatrix->components); struct LinkedList_Node* current = gameObjects->head; while (current != NULL) { GObject* gameObj = (GObject*)(current->data); //Render gameobject's mesh if it exists if (gameObj->mesh != NULL) { //Set color matrix glProgramUniformMatrix4fv(renderingBuffer->shaderPrograms[0]->shaderProgramID, renderingBuffer->shaderPrograms[0]->colorMatrixLocation, 1, GL_TRUE, gameObj->colorMatrix->components); //Set modelMatrix uniform FrameOfReference_ToMatrix4(gameObj->frameOfReference, &modelMatrix); glProgramUniformMatrix4fv(renderingBuffer->shaderPrograms[0]->shaderProgramID, renderingBuffer->shaderPrograms[0]->modelMatrixLocation, 1, GL_TRUE, modelMatrix.components); //Construct modelViewProjectionMatrix Matrix_Copy(&modelViewProjectionMatrix, &modelMatrix); Matrix_TransformMatrix(&viewMatrix, &modelViewProjectionMatrix); Matrix_TransformMatrix(renderingBuffer->camera->projectionMatrix, &modelViewProjectionMatrix); //Set modelViewProjectionMatrix uniform glProgramUniformMatrix4fv(renderingBuffer->shaderPrograms[0]->shaderProgramID, renderingBuffer->shaderPrograms[0]->modelViewProjectionMatrixLocation, 1, GL_TRUE, modelViewProjectionMatrix.components); if (gameObj->texture != NULL) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, gameObj->texture->textureID); //Send texture to uniform glUniform1i(renderingBuffer->shaderPrograms[0]->textureLocation, 0); } else { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, AssetManager_LookupTexture("Test")->textureID); //Send texture to uniform glUniform1i(renderingBuffer->shaderPrograms[0]->textureLocation, 0); } //Setup GPU program to draw this mesh Mesh_Render(gameObj->mesh, gameObj->mesh->primitive); } //Render gameObject's collider if it exists & in debug mode if(gameObj->collider != NULL && gameObj->collider->debug) { //Set color matrix glProgramUniformMatrix4fv(renderingBuffer->shaderPrograms[0]->shaderProgramID, renderingBuffer->shaderPrograms[0]->colorMatrixLocation, 1, GL_TRUE, gameObj->collider->colorMatrix->components); //Create modelMatrix from correct Frame Of Reference if(gameObj->body != NULL) { FrameOfReference_ToMatrix4(gameObj->body->frame, &modelMatrix); } else { FrameOfReference_ToMatrix4(gameObj->frameOfReference, &modelMatrix); } //If the object has an AABB collider, take into account the offset if(gameObj->collider->type == COLLIDER_AABB) { ColliderData_AABB* AABB = gameObj->collider->data->AABBData; *Matrix_Index(&modelMatrix, 0, 3) += AABB->centroid->components[0]; *Matrix_Index(&modelMatrix, 1, 3) += AABB->centroid->components[1]; *Matrix_Index(&modelMatrix, 2, 3) += AABB->centroid->components[2]; } //Set the modelMatrix glProgramUniformMatrix4fv(renderingBuffer->shaderPrograms[0]->shaderProgramID, renderingBuffer->shaderPrograms[0]->modelMatrixLocation, 1, GL_TRUE, modelMatrix.components); //Construct modelViewProjectionMatrix Matrix_Copy(&modelViewProjectionMatrix, &modelMatrix); Matrix_TransformMatrix(&viewMatrix, &modelViewProjectionMatrix); Matrix_TransformMatrix(renderingBuffer->camera->projectionMatrix, &modelViewProjectionMatrix); //Set modelViewProjectionMatrix uniform glProgramUniformMatrix4fv(renderingBuffer->shaderPrograms[0]->shaderProgramID, renderingBuffer->shaderPrograms[0]->modelViewProjectionMatrixLocation, 1, GL_TRUE, modelViewProjectionMatrix.components); //Bind white texture glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, AssetManager_LookupTexture("White")->textureID); //Send texture to uniform glUniform1i(renderingBuffer->shaderPrograms[0]->textureLocation, 0); //Setup GPU program to draw this mesh Mesh_Render(gameObj->collider->representation, GL_LINES); //TODO: Remove //Change the color of colliders to green until they collide *Matrix_Index(gameObj->collider->colorMatrix, 0, 0) = 0.0f; *Matrix_Index(gameObj->collider->colorMatrix, 1, 1) = 1.0f; *Matrix_Index(gameObj->collider->colorMatrix, 2, 2) = 0.0f; } current = current->next; } //Render the oct tree if(renderingBuffer->debugOctTree) { //Set the color matrix Matrix octTreeColor; Matrix_INIT_ON_STACK(octTreeColor, 4, 4); *Matrix_Index(&octTreeColor, 0, 0) = 0.0f; *Matrix_Index(&octTreeColor, 1, 1) = 1.0f; *Matrix_Index(&octTreeColor, 2, 2) = 0.0f; glProgramUniformMatrix4fv(renderingBuffer->shaderPrograms[0]->shaderProgramID, renderingBuffer->shaderPrograms[0]->colorMatrixLocation, 1, GL_TRUE, octTreeColor.components); Mesh* cube = AssetManager_LookupMesh("CubeWire"); Texture* white = AssetManager_LookupTexture("White"); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, white->textureID); //Send texture to uniform glUniform1i(renderingBuffer->shaderPrograms[0]->textureLocation, 0); RenderingManager_RenderOctTree(ObjectManager_GetObjectBuffer().octTree->root, &modelViewProjectionMatrix, &viewMatrix, renderingBuffer->camera->projectionMatrix, cube); } //Start drawing threads on gpu glFlush(); }
void State_ParkourController_Shoot(GObject* obj, State* state) { //Get the members of the state struct State_ParkourController_Members* members = (struct State_ParkourController_Members*)state->members; //Get a reference to the camera Camera* cam = RenderingManager_GetRenderingBuffer()->camera; if(InputManager_GetInputBuffer().mouseLock) { //IF we can shoot again if(members->shootTimer >= members->shootCooldown) { //Get the forward vector of the camera Vector direction; Vector_INIT_ON_STACK(direction, 3); Matrix_SliceRow(&direction, cam->rotationMatrix, 2, 0, 3); Vector_Scale(&direction, -1.0f); //Create the bullet object GObject* bullet = GObject_Allocate(); GObject_Initialize(bullet); //Set the appearance bullet->mesh = AssetManager_LookupMesh("Cube"); //bullet->texture = AssetManager_LookupTexture("White"); bullet->material = Material_Allocate(); Material_Initialize(bullet->material, AssetManager_LookupTexture("Jacob")); //*Matrix_Index(bullet->material->colorMatrix, 1, 1) = 0.0f; //*Matrix_Index(bullet->material->colorMatrix, 2, 2) = 0.0f; //Create ridgid body bullet->body = RigidBody_Allocate(); RigidBody_Initialize(bullet->body, bullet->frameOfReference, 1.0f); bullet->body->coefficientOfRestitution = 0.2f; bullet->body->rollingResistance = 0.2f; bullet->body->staticFriction = 0.4f; bullet->body->dynamicFriction = 0.2f; //Create collider bullet->collider = Collider_Allocate(); ConvexHullCollider_Initialize(bullet->collider); ConvexHullCollider_MakeRectangularCollider(bullet->collider->data->convexHullData, 2.0f, 2.0f, 2.0f); //Position bullet Vector transform; Vector_INIT_ON_STACK(transform, 3); Vector_GetScalarProduct(&transform, &direction, 2.8243f); Vector_Increment(&transform, obj->frameOfReference->position); GObject_Translate(bullet, &transform); Vector_Copy(&transform, &Vector_ZERO); transform.components[2] = 1.0f; GObject_Rotate(bullet, &transform, 3.14159f); //Scale bullet Vector_Copy(&transform, &Vector_ZERO); transform.components[0] = transform.components[1] = transform.components[2] = 0.5f; GObject_Scale(bullet, &transform); //Apply impulse Vector_Scale(&direction, 25.0f); RigidBody_ApplyImpulse(bullet->body, &direction, &Vector_ZERO); //Add the remove state State* state = State_Allocate(); State_Remove_Initialize(state, 7.0f); GObject_AddState(bullet, state); //Add the bullet to the world ObjectManager_AddObject(bullet); //Set shoot timer to 0 members->shootTimer = 0.0f; } } }
// Create an object in front of the character and fire // Parameters: // GO: The object getting passed in, in this case the character // State: Needed to grab members void State_CharacterController_ShootBullet(GObject* GO, State* state) { //Get members struct State_CharacterController_Members* members = (struct State_CharacterController_Members*)state->members; // Camera local Camera* cam = RenderingManager_GetRenderingBuffer()->camera; // Gets the time per second float dt = TimeManager_GetDeltaSec(); members->timer += dt; if(InputManager_GetInputBuffer().mouseLock) { // Create a net movement vector Vector direction; Vector_INIT_ON_STACK(direction, 3); if (InputManager_IsMouseButtonPressed(0) && members->timer >= members->coolDown) { //Get "forward" Vector Matrix_SliceRow(&direction, cam->rotationMatrix, 2, 0, 3); Vector_Scale(&direction,-1.0f); // Create the bullet object GObject* bullet = GObject_Allocate(); GObject_Initialize(bullet); //bullet->mesh = AssetManager_LookupMesh("Sphere"); bullet->mesh = AssetManager_LookupMesh("Arrow"); bullet->texture = AssetManager_LookupTexture("Arrow"); bullet->body = RigidBody_Allocate(); RigidBody_Initialize(bullet->body, bullet->frameOfReference, 0.45f); bullet->body->coefficientOfRestitution = 0.2f; bullet->collider = Collider_Allocate(); ConvexHullCollider_Initialize(bullet->collider); ConvexHullCollider_MakeRectangularCollider(bullet->collider->data->convexHullData, 0.1f, 2.0f, 0.1f); //AABBCollider_Initialize(bullet->collider, 2.0f, 2.0f, 2.0f, &Vector_ZERO); //Lay arrow flat GObject_Rotate(bullet, &Vector_E1, -3.14159f / 2.0f); //Construct a rotation matrix to orient bullet Matrix rot; Matrix_INIT_ON_STACK(rot, 3, 3); //Grab 4,4 minor to get 3x3 rotation matrix of camera Matrix_GetMinor(&rot, cam->rotationMatrix, 3, 3); //Transpose it to get correct direction Matrix_Transpose(&rot); //Rotate the bullet Matrix_TransformMatrix(&rot, bullet->frameOfReference->rotation); Matrix_TransformMatrix(&rot, bullet->body->frame->rotation); Vector vector; Vector_INIT_ON_STACK(vector,3); vector.components[0] = 0.9f; vector.components[1] = 1.0f; vector.components[2] = 0.9f; GObject_Scale(bullet, &vector); Vector translation; Vector_INIT_ON_STACK(translation, 3); Vector_GetScalarProduct(&translation, &direction, 2.82843); GObject_Translate(bullet, GO->frameOfReference->position); GObject_Translate(bullet, &translation); Vector_Scale(&direction, 25.0f); //Vector_Increment(bullet->body->velocity,&direction); RigidBody_ApplyImpulse(bullet->body,&direction,&Vector_ZERO); //Add remove state State* state = State_Allocate(); State_Remove_Initialize(state, 5.0f); GObject_AddState(bullet, state); ObjectManager_AddObject(bullet); members->timer = 0; } } }
/// //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); }