diff options
| author | victorfisac <victorfisac@gmail.com> | 2016-01-20 13:48:00 +0100 |
|---|---|---|
| committer | victorfisac <victorfisac@gmail.com> | 2016-01-20 13:48:00 +0100 |
| commit | c04752c0e4b646638ce6adf991750763c2dbf393 (patch) | |
| tree | 9fdafdbdd04dda343a25ac40768900d2d5016da6 /src/models.c | |
| parent | 4cc394c376c83926da67afe14855d2a3e2b06cfd (diff) | |
| parent | 29c618a35e19c1c00be94bf423ad6af7ecf1d3f8 (diff) | |
| download | raylib-c04752c0e4b646638ce6adf991750763c2dbf393.tar.gz raylib-c04752c0e4b646638ce6adf991750763c2dbf393.zip | |
Merge remote-tracking branch 'refs/remotes/raysan5/develop' into develop
Diffstat (limited to 'src/models.c')
| -rw-r--r-- | src/models.c | 360 |
1 files changed, 198 insertions, 162 deletions
diff --git a/src/models.c b/src/models.c index dd170e0b..06044820 100644 --- a/src/models.c +++ b/src/models.c @@ -56,7 +56,7 @@ extern unsigned int whiteTexture; // Module specific Functions Declaration //---------------------------------------------------------------------------------- static float GetHeightValue(Color pixel); -static VertexData LoadOBJ(const char *fileName); +static Mesh LoadOBJ(const char *fileName); //---------------------------------------------------------------------------------- // Module Functions Definition @@ -558,23 +558,23 @@ void DrawGizmo(Vector3 position) Model LoadModel(const char *fileName) { Model model; - VertexData vData = { 0 }; + Mesh mesh = { 0 }; // NOTE: Initialize default data for model in case loading fails, maybe a cube? - if (strcmp(GetExtension(fileName),"obj") == 0) vData = LoadOBJ(fileName); + if (strcmp(GetExtension(fileName),"obj") == 0) mesh = LoadOBJ(fileName); else TraceLog(WARNING, "[%s] Model extension not recognized, it can't be loaded", fileName); - // NOTE: At this point we have all vertex, texcoord, normal data for the model in vData struct + // NOTE: At this point we have all vertex, texcoord, normal data for the model in mesh struct - if (vData.vertexCount == 0) + if (mesh.vertexCount == 0) { TraceLog(WARNING, "Model could not be loaded"); } else { // NOTE: model properties (transform, texture, shader) are initialized inside rlglLoadModel() - model = rlglLoadModel(vData); // Upload vertex data to GPU + model = rlglLoadModel(mesh); // Upload vertex data to GPU // Now that vertex data is uploaded to GPU, we can free arrays // NOTE 1: We don't need CPU vertex data on OpenGL 3.3 or ES2... for static meshes... @@ -583,10 +583,10 @@ Model LoadModel(const char *fileName) /* if (rlGetVersion() != OPENGL_11) { - free(vData.vertices); - free(vData.texcoords); - free(vData.normals); - free(vData.colors); + free(mesh.vertices); + free(mesh.texcoords); + free(mesh.normals); + free(mesh.colors); } */ } @@ -595,7 +595,7 @@ Model LoadModel(const char *fileName) } // Load a 3d model (from vertex data) -Model LoadModelEx(VertexData data) +Model LoadModelEx(Mesh data) { Model model; @@ -610,7 +610,7 @@ Model LoadModelEx(VertexData data) // Load a heightmap image as a 3d model Model LoadHeightmap(Image heightmap, float maxHeight) { - VertexData vData; + Mesh mesh; int mapX = heightmap.width; int mapZ = heightmap.height; @@ -621,12 +621,12 @@ Model LoadHeightmap(Image heightmap, float maxHeight) // TODO: Consider resolution when generating model data? int numTriangles = (mapX-1)*(mapZ-1)*2; // One quad every four pixels - vData.vertexCount = numTriangles*3; + mesh.vertexCount = numTriangles*3; - vData.vertices = (float *)malloc(vData.vertexCount*3*sizeof(float)); - vData.normals = (float *)malloc(vData.vertexCount*3*sizeof(float)); - vData.texcoords = (float *)malloc(vData.vertexCount*2*sizeof(float)); - vData.colors = (unsigned char *)malloc(vData.vertexCount*4*sizeof(unsigned char)); // Not used... + mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float)); + mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float)); + mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float)); + mesh.colors = (unsigned char *)malloc(mesh.vertexCount*4*sizeof(unsigned char)); // Not used... int vCounter = 0; // Used to count vertices float by float int tcCounter = 0; // Used to count texcoords float by float @@ -644,51 +644,51 @@ Model LoadHeightmap(Image heightmap, float maxHeight) //---------------------------------------------------------- // one triangle - 3 vertex - vData.vertices[vCounter] = x; - vData.vertices[vCounter + 1] = GetHeightValue(heightmapPixels[x + z*mapX])*scaleFactor; - vData.vertices[vCounter + 2] = z; + mesh.vertices[vCounter] = x; + mesh.vertices[vCounter + 1] = GetHeightValue(heightmapPixels[x + z*mapX])*scaleFactor; + mesh.vertices[vCounter + 2] = z; - vData.vertices[vCounter + 3] = x; - vData.vertices[vCounter + 4] = GetHeightValue(heightmapPixels[x + (z+1)*mapX])*scaleFactor; - vData.vertices[vCounter + 5] = z+1; + mesh.vertices[vCounter + 3] = x; + mesh.vertices[vCounter + 4] = GetHeightValue(heightmapPixels[x + (z+1)*mapX])*scaleFactor; + mesh.vertices[vCounter + 5] = z+1; - vData.vertices[vCounter + 6] = x+1; - vData.vertices[vCounter + 7] = GetHeightValue(heightmapPixels[(x+1) + z*mapX])*scaleFactor; - vData.vertices[vCounter + 8] = z; + mesh.vertices[vCounter + 6] = x+1; + mesh.vertices[vCounter + 7] = GetHeightValue(heightmapPixels[(x+1) + z*mapX])*scaleFactor; + mesh.vertices[vCounter + 8] = z; // another triangle - 3 vertex - vData.vertices[vCounter + 9] = vData.vertices[vCounter + 6]; - vData.vertices[vCounter + 10] = vData.vertices[vCounter + 7]; - vData.vertices[vCounter + 11] = vData.vertices[vCounter + 8]; + mesh.vertices[vCounter + 9] = mesh.vertices[vCounter + 6]; + mesh.vertices[vCounter + 10] = mesh.vertices[vCounter + 7]; + mesh.vertices[vCounter + 11] = mesh.vertices[vCounter + 8]; - vData.vertices[vCounter + 12] = vData.vertices[vCounter + 3]; - vData.vertices[vCounter + 13] = vData.vertices[vCounter + 4]; - vData.vertices[vCounter + 14] = vData.vertices[vCounter + 5]; + mesh.vertices[vCounter + 12] = mesh.vertices[vCounter + 3]; + mesh.vertices[vCounter + 13] = mesh.vertices[vCounter + 4]; + mesh.vertices[vCounter + 14] = mesh.vertices[vCounter + 5]; - vData.vertices[vCounter + 15] = x+1; - vData.vertices[vCounter + 16] = GetHeightValue(heightmapPixels[(x+1) + (z+1)*mapX])*scaleFactor; - vData.vertices[vCounter + 17] = z+1; + mesh.vertices[vCounter + 15] = x+1; + mesh.vertices[vCounter + 16] = GetHeightValue(heightmapPixels[(x+1) + (z+1)*mapX])*scaleFactor; + mesh.vertices[vCounter + 17] = z+1; vCounter += 18; // 6 vertex, 18 floats // Fill texcoords array with data //-------------------------------------------------------------- - vData.texcoords[tcCounter] = (float)x / (mapX-1); - vData.texcoords[tcCounter + 1] = (float)z / (mapZ-1); + mesh.texcoords[tcCounter] = (float)x / (mapX-1); + mesh.texcoords[tcCounter + 1] = (float)z / (mapZ-1); - vData.texcoords[tcCounter + 2] = (float)x / (mapX-1); - vData.texcoords[tcCounter + 3] = (float)(z+1) / (mapZ-1); + mesh.texcoords[tcCounter + 2] = (float)x / (mapX-1); + mesh.texcoords[tcCounter + 3] = (float)(z+1) / (mapZ-1); - vData.texcoords[tcCounter + 4] = (float)(x+1) / (mapX-1); - vData.texcoords[tcCounter + 5] = (float)z / (mapZ-1); + mesh.texcoords[tcCounter + 4] = (float)(x+1) / (mapX-1); + mesh.texcoords[tcCounter + 5] = (float)z / (mapZ-1); - vData.texcoords[tcCounter + 6] = vData.texcoords[tcCounter + 4]; - vData.texcoords[tcCounter + 7] = vData.texcoords[tcCounter + 5]; + mesh.texcoords[tcCounter + 6] = mesh.texcoords[tcCounter + 4]; + mesh.texcoords[tcCounter + 7] = mesh.texcoords[tcCounter + 5]; - vData.texcoords[tcCounter + 8] = vData.texcoords[tcCounter + 2]; - vData.texcoords[tcCounter + 9] = vData.texcoords[tcCounter + 3]; + mesh.texcoords[tcCounter + 8] = mesh.texcoords[tcCounter + 2]; + mesh.texcoords[tcCounter + 9] = mesh.texcoords[tcCounter + 3]; - vData.texcoords[tcCounter + 10] = (float)(x+1) / (mapX-1); - vData.texcoords[tcCounter + 11] = (float)(z+1) / (mapZ-1); + mesh.texcoords[tcCounter + 10] = (float)(x+1) / (mapX-1); + mesh.texcoords[tcCounter + 11] = (float)(z+1) / (mapZ-1); tcCounter += 12; // 6 texcoords, 12 floats // Fill normals array with data @@ -696,9 +696,9 @@ Model LoadHeightmap(Image heightmap, float maxHeight) // NOTE: Current Model implementation doe not use normals! for (int i = 0; i < 18; i += 3) { - vData.normals[nCounter + i] = 0.0f; - vData.normals[nCounter + i + 1] = 1.0f; - vData.normals[nCounter + i + 2] = 0.0f; + mesh.normals[nCounter + i] = 0.0f; + mesh.normals[nCounter + i + 1] = 1.0f; + mesh.normals[nCounter + i + 2] = 0.0f; } // TODO: Calculate normals in an efficient way @@ -713,20 +713,20 @@ Model LoadHeightmap(Image heightmap, float maxHeight) // Fill color data // NOTE: Not used any more... just one plain color defined at DrawModel() - for (int i = 0; i < (4*vData.vertexCount); i++) vData.colors[i] = 255; + for (int i = 0; i < (4*mesh.vertexCount); i++) mesh.colors[i] = 255; - // NOTE: At this point we have all vertex, texcoord, normal data for the model in vData struct + // NOTE: At this point we have all vertex, texcoord, normal data for the model in mesh struct - Model model = rlglLoadModel(vData); + Model model = rlglLoadModel(mesh); // Now that vertex data is uploaded to GPU, we can free arrays // NOTE: We don't need CPU vertex data on OpenGL 3.3 or ES2 if (rlGetVersion() != OPENGL_11) { - free(vData.vertices); - free(vData.texcoords); - free(vData.normals); - free(vData.colors); + free(mesh.vertices); + free(mesh.texcoords); + free(mesh.normals); + free(mesh.colors); } return model; @@ -735,14 +735,14 @@ Model LoadHeightmap(Image heightmap, float maxHeight) // Load a map image as a 3d model (cubes based) Model LoadCubicmap(Image cubicmap) { - VertexData vData; + Mesh mesh; Color *cubicmapPixels = GetImageData(cubicmap); // Map cube size will be 1.0 float mapCubeSide = 1.0f; - int mapWidth = cubicmap.width * (int)mapCubeSide; - int mapHeight = cubicmap.height * (int)mapCubeSide; + int mapWidth = cubicmap.width*(int)mapCubeSide; + int mapHeight = cubicmap.height*(int)mapCubeSide; // NOTE: Max possible number of triangles numCubes * (12 triangles by cube) int maxTriangles = cubicmap.width*cubicmap.height*12; @@ -753,11 +753,11 @@ Model LoadCubicmap(Image cubicmap) float w = mapCubeSide; float h = mapCubeSide; - float h2 = mapCubeSide * 1.5; // TODO: Review walls height... + float h2 = mapCubeSide*1.5f; // TODO: Review walls height... - Vector3 *mapVertices = (Vector3 *)malloc(maxTriangles * 3 * sizeof(Vector3)); - Vector2 *mapTexcoords = (Vector2 *)malloc(maxTriangles * 3 * sizeof(Vector2)); - Vector3 *mapNormals = (Vector3 *)malloc(maxTriangles * 3 * sizeof(Vector3)); + Vector3 *mapVertices = (Vector3 *)malloc(maxTriangles*3*sizeof(Vector3)); + Vector2 *mapTexcoords = (Vector2 *)malloc(maxTriangles*3*sizeof(Vector2)); + Vector3 *mapNormals = (Vector3 *)malloc(maxTriangles*3*sizeof(Vector3)); // Define the 6 normals of the cube, we will combine them accordingly later... Vector3 n1 = { 1.0f, 0.0f, 0.0f }; @@ -775,12 +775,12 @@ Model LoadCubicmap(Image cubicmap) float height; } RectangleF; - RectangleF rightTexUV = { 0, 0, 0.5, 0.5 }; - RectangleF leftTexUV = { 0.5, 0, 0.5, 0.5 }; - RectangleF frontTexUV = { 0, 0, 0.5, 0.5 }; - RectangleF backTexUV = { 0.5, 0, 0.5, 0.5 }; - RectangleF topTexUV = { 0, 0.5, 0.5, 0.5 }; - RectangleF bottomTexUV = { 0.5, 0.5, 0.5, 0.5 }; + RectangleF rightTexUV = { 0.0f, 0.0f, 0.5f, 0.5f }; + RectangleF leftTexUV = { 0.5f, 0.0f, 0.5f, 0.5f }; + RectangleF frontTexUV = { 0.0f, 0.0f, 0.5f, 0.5f }; + RectangleF backTexUV = { 0.5f, 0.0f, 0.5f, 0.5f }; + RectangleF topTexUV = { 0.0f, 0.5f, 0.5f, 0.5f }; + RectangleF bottomTexUV = { 0.5f, 0.5f, 0.5f, 0.5f }; for (int z = 0; z < mapHeight; z += mapCubeSide) { @@ -1041,25 +1041,25 @@ Model LoadCubicmap(Image cubicmap) } // Move data from mapVertices temp arays to vertices float array - vData.vertexCount = vCounter; + mesh.vertexCount = vCounter; - vData.vertices = (float *)malloc(vData.vertexCount*3*sizeof(float)); - vData.normals = (float *)malloc(vData.vertexCount*3*sizeof(float)); - vData.texcoords = (float *)malloc(vData.vertexCount*2*sizeof(float)); - vData.colors = (unsigned char *)malloc(vData.vertexCount*4*sizeof(unsigned char)); // Not used... + mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float)); + mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float)); + mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float)); + mesh.colors = (unsigned char *)malloc(mesh.vertexCount*4*sizeof(unsigned char)); // Not used... // Fill color data // NOTE: Not used any more... just one plain color defined at DrawModel() - for (int i = 0; i < (4*vData.vertexCount); i++) vData.colors[i] = 255; + for (int i = 0; i < (4*mesh.vertexCount); i++) mesh.colors[i] = 255; int fCounter = 0; // Move vertices data for (int i = 0; i < vCounter; i++) { - vData.vertices[fCounter] = mapVertices[i].x; - vData.vertices[fCounter + 1] = mapVertices[i].y; - vData.vertices[fCounter + 2] = mapVertices[i].z; + mesh.vertices[fCounter] = mapVertices[i].x; + mesh.vertices[fCounter + 1] = mapVertices[i].y; + mesh.vertices[fCounter + 2] = mapVertices[i].z; fCounter += 3; } @@ -1068,9 +1068,9 @@ Model LoadCubicmap(Image cubicmap) // Move normals data for (int i = 0; i < nCounter; i++) { - vData.normals[fCounter] = mapNormals[i].x; - vData.normals[fCounter + 1] = mapNormals[i].y; - vData.normals[fCounter + 2] = mapNormals[i].z; + mesh.normals[fCounter] = mapNormals[i].x; + mesh.normals[fCounter + 1] = mapNormals[i].y; + mesh.normals[fCounter + 2] = mapNormals[i].z; fCounter += 3; } @@ -1079,8 +1079,8 @@ Model LoadCubicmap(Image cubicmap) // Move texcoords data for (int i = 0; i < tcCounter; i++) { - vData.texcoords[fCounter] = mapTexcoords[i].x; - vData.texcoords[fCounter + 1] = mapTexcoords[i].y; + mesh.texcoords[fCounter] = mapTexcoords[i].x; + mesh.texcoords[fCounter + 1] = mapTexcoords[i].y; fCounter += 2; } @@ -1090,18 +1090,18 @@ Model LoadCubicmap(Image cubicmap) free(cubicmapPixels); - // NOTE: At this point we have all vertex, texcoord, normal data for the model in vData struct + // NOTE: At this point we have all vertex, texcoord, normal data for the model in mesh struct - Model model = rlglLoadModel(vData); + Model model = rlglLoadModel(mesh); // Now that vertex data is uploaded to GPU, we can free arrays // NOTE: We don't need CPU vertex data on OpenGL 3.3 or ES2 if (rlGetVersion() != OPENGL_11) { - free(vData.vertices); - free(vData.texcoords); - free(vData.normals); - free(vData.colors); + free(mesh.vertices); + free(mesh.texcoords); + free(mesh.normals); + free(mesh.colors); } return model; @@ -1147,7 +1147,7 @@ void SetModelTexture(Model *model, Texture2D texture) void DrawModel(Model model, Vector3 position, float scale, Color tint) { Vector3 vScale = { scale, scale, scale }; - Vector3 rotationAxis = { 0, 0, 0 }; + Vector3 rotationAxis = { 0.0f, 0.0f, 0.0f }; DrawModelEx(model, position, 0.0f, rotationAxis, vScale, tint); } @@ -1163,7 +1163,7 @@ void DrawModelEx(Model model, Vector3 position, float rotationAngle, Vector3 rot void DrawModelWires(Model model, Vector3 position, float scale, Color color) { Vector3 vScale = { scale, scale, scale }; - Vector3 rotationAxis = { 0, 0, 0 }; + Vector3 rotationAxis = { 0.0f, 0.0f, 0.0f }; rlglDrawModel(model, position, 0.0f, rotationAxis, vScale, color, true); } @@ -1188,7 +1188,7 @@ void DrawBillboard(Camera camera, Texture2D texture, Vector3 center, float size, //Vector3 up = { viewMatrix.m1, viewMatrix.m5, viewMatrix.m9 }; // NOTE: Billboard locked to axis-Y - Vector3 up = { 0, 1, 0 }; + Vector3 up = { 0.0f, 1.0f, 0.0f }; /* a-------b | | @@ -1336,18 +1336,28 @@ bool CheckCollisionBoxSphere(Vector3 minBBox, Vector3 maxBBox, Vector3 centerSph return collision; } -// Detect collision between ray and box +// Detect collision between ray and sphere +bool CheckCollisionRaySphere(Ray ray, Vector3 spherePosition, float sphereRadius) +{ + bool collision = false; + + // TODO: implement collision... + + return collision; +} + +// Detect collision between ray and bounding box bool CheckCollisionRayBox(Ray ray, Vector3 minBBox, Vector3 maxBBox) { bool collision = false; float t[8]; - t[0] = (minBBox.x - ray.position.x) / ray.direction.x; - t[1] = (maxBBox.x - ray.position.x) / ray.direction.x; - t[2] = (minBBox.y - ray.position.y) / ray.direction.y; - t[3] = (maxBBox.y - ray.position.y) / ray.direction.y; - t[4] = (minBBox.z - ray.position.z) / ray.direction.z; - t[5] = (maxBBox.z - ray.position.z) / ray.direction.z; + t[0] = (minBBox.x - ray.position.x)/ray.direction.x; + t[1] = (maxBBox.x - ray.position.x)/ray.direction.x; + t[2] = (minBBox.y - ray.position.y)/ray.direction.y; + t[3] = (maxBBox.y - ray.position.y)/ray.direction.y; + t[4] = (minBBox.z - ray.position.z)/ray.direction.z; + t[5] = (maxBBox.z - ray.position.z)/ray.direction.z; t[6] = fmax(fmax(fmin(t[0], t[1]), fmin(t[2], t[3])), fmin(t[4], t[5])); t[7] = fmin(fmin(fmax(t[0], t[1]), fmax(t[2], t[3])), fmax(t[4], t[5])); @@ -1359,6 +1369,32 @@ bool CheckCollisionRayBox(Ray ray, Vector3 minBBox, Vector3 maxBBox) // TODO: Useful function to check collision area? //BoundingBox GetCollisionArea(BoundingBox box1, BoundingBox box2) +// Calculate mesh bounding box limits +BoundingBox CalculateBoundingBox(Mesh mesh) +{ + // Get min and max vertex to construct bounds (AABB) + Vector3 minVertex = mesh.vertices[0]; + Vector3 maxVertex = mesh.vertices[0]; + + for (int i = 1; i < mesh.vertexCount; i++) + { + // TODO: Compare min and max with previous vertex + //minVertex = Vector3.Min(minVertex, mesh.vertices[i]); + //maxVertex = Vector3.Max(maxVertex, mesh.vertices[i]); + } + + // NOTE: For OBB, transform mesh by model transform matrix + //minVertex = VectorTransform(meshMin, mesh.transform); + //maxVertex = VectorTransform(meshMax, mesh.transform); + + // Create the bounding box + BoundingBox box; + box.min = minVertex; + box.max = maxVertex; + + return box; +} + // Detect and resolve cubicmap collisions // NOTE: player position (or camera) is modified inside this function Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *playerPosition, float radius) @@ -1366,7 +1402,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p Color *cubicmapPixels = GetImageData(cubicmap); // Detect the cell where the player is located - Vector3 impactDirection = { 0, 0, 0 }; + Vector3 impactDirection = { 0.0f, 0.0f, 0.0f }; int locationCellX = 0; int locationCellY = 0; @@ -1389,7 +1425,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p { playerPosition->x = locationCellX + mapPosition.x - (CUBIC_MAP_HALF_BLOCK_SIZE - radius); playerPosition->z = locationCellY + mapPosition.z - (CUBIC_MAP_HALF_BLOCK_SIZE - radius); - impactDirection = (Vector3) { 1, 0, 1}; + impactDirection = (Vector3){ 1.0f, 0.0f, 1.0f }; } } } @@ -1405,7 +1441,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p { playerPosition->x = locationCellX + mapPosition.x - (CUBIC_MAP_HALF_BLOCK_SIZE - radius); playerPosition->z = locationCellY + mapPosition.z + (CUBIC_MAP_HALF_BLOCK_SIZE - radius); - impactDirection = (Vector3) { 1, 0, 1}; + impactDirection = (Vector3){ 1.0f, 0.0f, 1.0f }; } } } @@ -1421,7 +1457,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p { playerPosition->x = locationCellX + mapPosition.x + (CUBIC_MAP_HALF_BLOCK_SIZE - radius); playerPosition->z = locationCellY + mapPosition.z - (CUBIC_MAP_HALF_BLOCK_SIZE - radius); - impactDirection = (Vector3) { 1, 0, 1}; + impactDirection = (Vector3){ 1.0f, 0.0f, 1.0f }; } } } @@ -1437,7 +1473,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p { playerPosition->x = locationCellX + mapPosition.x + (CUBIC_MAP_HALF_BLOCK_SIZE - radius); playerPosition->z = locationCellY + mapPosition.z + (CUBIC_MAP_HALF_BLOCK_SIZE - radius); - impactDirection = (Vector3) { 1, 0, 1}; + impactDirection = (Vector3){ 1.0f, 0.0f, 1.0f }; } } } @@ -1452,7 +1488,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p if ((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX < radius) { playerPosition->x = locationCellX + mapPosition.x - (CUBIC_MAP_HALF_BLOCK_SIZE - radius); - impactDirection = (Vector3) { 1, 0, 0}; + impactDirection = (Vector3){ 1.0f, 0.0f, 0.0f }; } } } @@ -1464,7 +1500,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p if ((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX > 1 - radius) { playerPosition->x = locationCellX + mapPosition.x + (CUBIC_MAP_HALF_BLOCK_SIZE - radius); - impactDirection = (Vector3) { 1, 0, 0}; + impactDirection = (Vector3){ 1.0f, 0.0f, 0.0f }; } } } @@ -1476,7 +1512,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p if ((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY < radius) { playerPosition->z = locationCellY + mapPosition.z - (CUBIC_MAP_HALF_BLOCK_SIZE - radius); - impactDirection = (Vector3) { 0, 0, 1}; + impactDirection = (Vector3){ 0.0f, 0.0f, 1.0f }; } } } @@ -1488,7 +1524,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p if ((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY > 1 - radius) { playerPosition->z = locationCellY + mapPosition.z + (CUBIC_MAP_HALF_BLOCK_SIZE - radius); - impactDirection = (Vector3) { 0, 0, 1}; + impactDirection = (Vector3){ 0.0f, 0.0f, 1.0f }; } } } @@ -1512,7 +1548,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX < radius / 3) && ((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY < radius / 3)) { - impactDirection = (Vector3) { 1, 0, 1}; + impactDirection = (Vector3){ 1.0f, 0.0f, 1.0f }; } } } @@ -1535,7 +1571,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX < radius / 3) && ((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY > 1 - radius / 3)) { - impactDirection = (Vector3) { 1, 0, 1}; + impactDirection = (Vector3){ 1.0f, 0.0f, 1.0f }; } } } @@ -1558,7 +1594,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX > 1 - radius / 3) && ((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY < radius / 3)) { - impactDirection = (Vector3) { 1, 0, 1}; + impactDirection = (Vector3){ 1.0f, 0.0f, 1.0f }; } } } @@ -1581,7 +1617,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX > 1 - radius / 3) && ((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY > 1 - radius / 3)) { - impactDirection = (Vector3) { 1, 0, 1}; + impactDirection = (Vector3){ 1.0f, 0.0f, 1.0f }; } } } @@ -1591,13 +1627,13 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p // Floor collision if (playerPosition->y <= radius) { - playerPosition->y = radius + 0.01; + playerPosition->y = radius + 0.01f; impactDirection = (Vector3) { impactDirection.x, 1, impactDirection.z}; } // Roof collision - else if (playerPosition->y >= 1.5 - radius) + else if (playerPosition->y >= (1.5f - radius)) { - playerPosition->y = (1.5 - radius) - 0.01; + playerPosition->y = (1.5f - radius) - 0.01f; impactDirection = (Vector3) { impactDirection.x, 1, impactDirection.z}; } @@ -1617,9 +1653,9 @@ static float GetHeightValue(Color pixel) } // Load OBJ mesh data -static VertexData LoadOBJ(const char *fileName) +static Mesh LoadOBJ(const char *fileName) { - VertexData vData = { 0 }; + Mesh mesh = { 0 }; char dataType; char comments[200]; @@ -1636,7 +1672,7 @@ static VertexData LoadOBJ(const char *fileName) if (objFile == NULL) { TraceLog(WARNING, "[%s] OBJ file could not be opened", fileName); - return vData; + return mesh; } // First reading pass: Get numVertex, numNormals, numTexCoords, numTriangles @@ -1747,15 +1783,15 @@ static VertexData LoadOBJ(const char *fileName) } // At this point all vertex data (v, vt, vn) has been gathered on midVertices, midTexCoords, midNormals - // Now we can organize that data into our VertexData struct + // Now we can organize that data into our Mesh struct - vData.vertexCount = numTriangles*3; + mesh.vertexCount = numTriangles*3; // Additional arrays to store vertex data as floats - vData.vertices = (float *)malloc(vData.vertexCount*3*sizeof(float)); - vData.texcoords = (float *)malloc(vData.vertexCount*2*sizeof(float)); - vData.normals = (float *)malloc(vData.vertexCount*3*sizeof(float)); - vData.colors = (unsigned char *)malloc(vData.vertexCount*4*sizeof(unsigned char)); + mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float)); + mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float)); + mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float)); + mesh.colors = (unsigned char *)malloc(mesh.vertexCount*4*sizeof(unsigned char)); int vCounter = 0; // Used to count vertices float by float int tcCounter = 0; // Used to count texcoords float by float @@ -1783,32 +1819,32 @@ static VertexData LoadOBJ(const char *fileName) else if (numNormals == 0) fscanf(objFile, "%i/%i %i/%i %i/%i", &vNum[0], &vtNum[0], &vNum[1], &vtNum[1], &vNum[2], &vtNum[2]); else fscanf(objFile, "%i/%i/%i %i/%i/%i %i/%i/%i", &vNum[0], &vtNum[0], &vnNum[0], &vNum[1], &vtNum[1], &vnNum[1], &vNum[2], &vtNum[2], &vnNum[2]); - vData.vertices[vCounter] = midVertices[vNum[0]-1].x; - vData.vertices[vCounter + 1] = midVertices[vNum[0]-1].y; - vData.vertices[vCounter + 2] = midVertices[vNum[0]-1].z; + mesh.vertices[vCounter] = midVertices[vNum[0]-1].x; + mesh.vertices[vCounter + 1] = midVertices[vNum[0]-1].y; + mesh.vertices[vCounter + 2] = midVertices[vNum[0]-1].z; vCounter += 3; - vData.vertices[vCounter] = midVertices[vNum[1]-1].x; - vData.vertices[vCounter + 1] = midVertices[vNum[1]-1].y; - vData.vertices[vCounter + 2] = midVertices[vNum[1]-1].z; + mesh.vertices[vCounter] = midVertices[vNum[1]-1].x; + mesh.vertices[vCounter + 1] = midVertices[vNum[1]-1].y; + mesh.vertices[vCounter + 2] = midVertices[vNum[1]-1].z; vCounter += 3; - vData.vertices[vCounter] = midVertices[vNum[2]-1].x; - vData.vertices[vCounter + 1] = midVertices[vNum[2]-1].y; - vData.vertices[vCounter + 2] = midVertices[vNum[2]-1].z; + mesh.vertices[vCounter] = midVertices[vNum[2]-1].x; + mesh.vertices[vCounter + 1] = midVertices[vNum[2]-1].y; + mesh.vertices[vCounter + 2] = midVertices[vNum[2]-1].z; vCounter += 3; if (numNormals > 0) { - vData.normals[nCounter] = midNormals[vnNum[0]-1].x; - vData.normals[nCounter + 1] = midNormals[vnNum[0]-1].y; - vData.normals[nCounter + 2] = midNormals[vnNum[0]-1].z; + mesh.normals[nCounter] = midNormals[vnNum[0]-1].x; + mesh.normals[nCounter + 1] = midNormals[vnNum[0]-1].y; + mesh.normals[nCounter + 2] = midNormals[vnNum[0]-1].z; nCounter += 3; - vData.normals[nCounter] = midNormals[vnNum[1]-1].x; - vData.normals[nCounter + 1] = midNormals[vnNum[1]-1].y; - vData.normals[nCounter + 2] = midNormals[vnNum[1]-1].z; + mesh.normals[nCounter] = midNormals[vnNum[1]-1].x; + mesh.normals[nCounter + 1] = midNormals[vnNum[1]-1].y; + mesh.normals[nCounter + 2] = midNormals[vnNum[1]-1].z; nCounter += 3; - vData.normals[nCounter] = midNormals[vnNum[2]-1].x; - vData.normals[nCounter + 1] = midNormals[vnNum[2]-1].y; - vData.normals[nCounter + 2] = midNormals[vnNum[2]-1].z; + mesh.normals[nCounter] = midNormals[vnNum[2]-1].x; + mesh.normals[nCounter + 1] = midNormals[vnNum[2]-1].y; + mesh.normals[nCounter + 2] = midNormals[vnNum[2]-1].z; nCounter += 3; } else @@ -1817,17 +1853,17 @@ static VertexData LoadOBJ(const char *fileName) Vector3 norm = VectorCrossProduct(VectorSubtract(midVertices[vNum[1]-1], midVertices[vNum[0]-1]), VectorSubtract(midVertices[vNum[2]-1], midVertices[vNum[0]-1])); VectorNormalize(&norm); - vData.normals[nCounter] = norm.x; - vData.normals[nCounter + 1] = norm.y; - vData.normals[nCounter + 2] = norm.z; + mesh.normals[nCounter] = norm.x; + mesh.normals[nCounter + 1] = norm.y; + mesh.normals[nCounter + 2] = norm.z; nCounter += 3; - vData.normals[nCounter] = norm.x; - vData.normals[nCounter + 1] = norm.y; - vData.normals[nCounter + 2] = norm.z; + mesh.normals[nCounter] = norm.x; + mesh.normals[nCounter + 1] = norm.y; + mesh.normals[nCounter + 2] = norm.z; nCounter += 3; - vData.normals[nCounter] = norm.x; - vData.normals[nCounter + 1] = norm.y; - vData.normals[nCounter + 2] = norm.z; + mesh.normals[nCounter] = norm.x; + mesh.normals[nCounter + 1] = norm.y; + mesh.normals[nCounter + 2] = norm.z; nCounter += 3; } @@ -1835,14 +1871,14 @@ static VertexData LoadOBJ(const char *fileName) { // NOTE: If using negative texture coordinates with a texture filter of GL_CLAMP_TO_EDGE doesn't work! // NOTE: Texture coordinates are Y flipped upside-down - vData.texcoords[tcCounter] = midTexCoords[vtNum[0]-1].x; - vData.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[0]-1].y; + mesh.texcoords[tcCounter] = midTexCoords[vtNum[0]-1].x; + mesh.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[0]-1].y; tcCounter += 2; - vData.texcoords[tcCounter] = midTexCoords[vtNum[1]-1].x; - vData.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[1]-1].y; + mesh.texcoords[tcCounter] = midTexCoords[vtNum[1]-1].x; + mesh.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[1]-1].y; tcCounter += 2; - vData.texcoords[tcCounter] = midTexCoords[vtNum[2]-1].x; - vData.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[2]-1].y; + mesh.texcoords[tcCounter] = midTexCoords[vtNum[2]-1].x; + mesh.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[2]-1].y; tcCounter += 2; } } break; @@ -1853,19 +1889,19 @@ static VertexData LoadOBJ(const char *fileName) fclose(objFile); // Security check, just in case no normals or no texcoords defined in OBJ - if (numTexCoords == 0) for (int i = 0; i < (2*vData.vertexCount); i++) vData.texcoords[i] = 0.0f; + if (numTexCoords == 0) for (int i = 0; i < (2*mesh.vertexCount); i++) mesh.texcoords[i] = 0.0f; // NOTE: We set all vertex colors to white // NOTE: Not used any more... just one plain color defined at DrawModel() - for (int i = 0; i < (4*vData.vertexCount); i++) vData.colors[i] = 255; + for (int i = 0; i < (4*mesh.vertexCount); i++) mesh.colors[i] = 255; // Now we can free temp mid* arrays free(midVertices); free(midNormals); free(midTexCoords); - // NOTE: At this point we have all vertex, texcoord, normal data for the model in vData struct + // NOTE: At this point we have all vertex, texcoord, normal data for the model in mesh struct TraceLog(INFO, "[%s] Model loaded successfully in RAM (CPU)", fileName); - return vData; + return mesh; } |
