Rename VertexData struct to Mesh

Reviewed vertex type variables
This commit is contained in:
raysan5 2016-01-18 13:36:18 +01:00
parent a640503119
commit fd05d3e353
4 changed files with 171 additions and 156 deletions

View file

@ -56,7 +56,7 @@ extern unsigned int whiteTexture;
// Module specific Functions Declaration // Module specific Functions Declaration
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
static float GetHeightValue(Color pixel); static float GetHeightValue(Color pixel);
static VertexData LoadOBJ(const char *fileName); static Mesh LoadOBJ(const char *fileName);
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module Functions Definition // Module Functions Definition
@ -558,23 +558,23 @@ void DrawGizmo(Vector3 position)
Model LoadModel(const char *fileName) Model LoadModel(const char *fileName)
{ {
Model model; Model model;
VertexData vData = { 0 }; Mesh mesh = { 0 };
// NOTE: Initialize default data for model in case loading fails, maybe a cube? // 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); 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"); TraceLog(WARNING, "Model could not be loaded");
} }
else else
{ {
// NOTE: model properties (transform, texture, shader) are initialized inside rlglLoadModel() // 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 // 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... // 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) if (rlGetVersion() != OPENGL_11)
{ {
free(vData.vertices); free(mesh.vertices);
free(vData.texcoords); free(mesh.texcoords);
free(vData.normals); free(mesh.normals);
free(vData.colors); free(mesh.colors);
} }
*/ */
} }
@ -595,7 +595,7 @@ Model LoadModel(const char *fileName)
} }
// Load a 3d model (from vertex data) // Load a 3d model (from vertex data)
Model LoadModelEx(VertexData data) Model LoadModelEx(Mesh data)
{ {
Model model; Model model;
@ -610,7 +610,7 @@ Model LoadModelEx(VertexData data)
// Load a heightmap image as a 3d model // Load a heightmap image as a 3d model
Model LoadHeightmap(Image heightmap, float maxHeight) Model LoadHeightmap(Image heightmap, float maxHeight)
{ {
VertexData vData; Mesh mesh;
int mapX = heightmap.width; int mapX = heightmap.width;
int mapZ = heightmap.height; int mapZ = heightmap.height;
@ -621,12 +621,12 @@ Model LoadHeightmap(Image heightmap, float maxHeight)
// TODO: Consider resolution when generating model data? // TODO: Consider resolution when generating model data?
int numTriangles = (mapX-1)*(mapZ-1)*2; // One quad every four pixels 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)); mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float));
vData.normals = (float *)malloc(vData.vertexCount*3*sizeof(float)); mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float));
vData.texcoords = (float *)malloc(vData.vertexCount*2*sizeof(float)); mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float));
vData.colors = (unsigned char *)malloc(vData.vertexCount*4*sizeof(unsigned char)); // Not used... mesh.colors = (unsigned char *)malloc(mesh.vertexCount*4*sizeof(unsigned char)); // Not used...
int vCounter = 0; // Used to count vertices float by float int vCounter = 0; // Used to count vertices float by float
int tcCounter = 0; // Used to count texcoords 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 // one triangle - 3 vertex
vData.vertices[vCounter] = x; mesh.vertices[vCounter] = x;
vData.vertices[vCounter + 1] = GetHeightValue(heightmapPixels[x + z*mapX])*scaleFactor; mesh.vertices[vCounter + 1] = GetHeightValue(heightmapPixels[x + z*mapX])*scaleFactor;
vData.vertices[vCounter + 2] = z; mesh.vertices[vCounter + 2] = z;
vData.vertices[vCounter + 3] = x; mesh.vertices[vCounter + 3] = x;
vData.vertices[vCounter + 4] = GetHeightValue(heightmapPixels[x + (z+1)*mapX])*scaleFactor; mesh.vertices[vCounter + 4] = GetHeightValue(heightmapPixels[x + (z+1)*mapX])*scaleFactor;
vData.vertices[vCounter + 5] = z+1; mesh.vertices[vCounter + 5] = z+1;
vData.vertices[vCounter + 6] = x+1; mesh.vertices[vCounter + 6] = x+1;
vData.vertices[vCounter + 7] = GetHeightValue(heightmapPixels[(x+1) + z*mapX])*scaleFactor; mesh.vertices[vCounter + 7] = GetHeightValue(heightmapPixels[(x+1) + z*mapX])*scaleFactor;
vData.vertices[vCounter + 8] = z; mesh.vertices[vCounter + 8] = z;
// another triangle - 3 vertex // another triangle - 3 vertex
vData.vertices[vCounter + 9] = vData.vertices[vCounter + 6]; mesh.vertices[vCounter + 9] = mesh.vertices[vCounter + 6];
vData.vertices[vCounter + 10] = vData.vertices[vCounter + 7]; mesh.vertices[vCounter + 10] = mesh.vertices[vCounter + 7];
vData.vertices[vCounter + 11] = vData.vertices[vCounter + 8]; mesh.vertices[vCounter + 11] = mesh.vertices[vCounter + 8];
vData.vertices[vCounter + 12] = vData.vertices[vCounter + 3]; mesh.vertices[vCounter + 12] = mesh.vertices[vCounter + 3];
vData.vertices[vCounter + 13] = vData.vertices[vCounter + 4]; mesh.vertices[vCounter + 13] = mesh.vertices[vCounter + 4];
vData.vertices[vCounter + 14] = vData.vertices[vCounter + 5]; mesh.vertices[vCounter + 14] = mesh.vertices[vCounter + 5];
vData.vertices[vCounter + 15] = x+1; mesh.vertices[vCounter + 15] = x+1;
vData.vertices[vCounter + 16] = GetHeightValue(heightmapPixels[(x+1) + (z+1)*mapX])*scaleFactor; mesh.vertices[vCounter + 16] = GetHeightValue(heightmapPixels[(x+1) + (z+1)*mapX])*scaleFactor;
vData.vertices[vCounter + 17] = z+1; mesh.vertices[vCounter + 17] = z+1;
vCounter += 18; // 6 vertex, 18 floats vCounter += 18; // 6 vertex, 18 floats
// Fill texcoords array with data // Fill texcoords array with data
//-------------------------------------------------------------- //--------------------------------------------------------------
vData.texcoords[tcCounter] = (float)x / (mapX-1); mesh.texcoords[tcCounter] = (float)x / (mapX-1);
vData.texcoords[tcCounter + 1] = (float)z / (mapZ-1); mesh.texcoords[tcCounter + 1] = (float)z / (mapZ-1);
vData.texcoords[tcCounter + 2] = (float)x / (mapX-1); mesh.texcoords[tcCounter + 2] = (float)x / (mapX-1);
vData.texcoords[tcCounter + 3] = (float)(z+1) / (mapZ-1); mesh.texcoords[tcCounter + 3] = (float)(z+1) / (mapZ-1);
vData.texcoords[tcCounter + 4] = (float)(x+1) / (mapX-1); mesh.texcoords[tcCounter + 4] = (float)(x+1) / (mapX-1);
vData.texcoords[tcCounter + 5] = (float)z / (mapZ-1); mesh.texcoords[tcCounter + 5] = (float)z / (mapZ-1);
vData.texcoords[tcCounter + 6] = vData.texcoords[tcCounter + 4]; mesh.texcoords[tcCounter + 6] = mesh.texcoords[tcCounter + 4];
vData.texcoords[tcCounter + 7] = vData.texcoords[tcCounter + 5]; mesh.texcoords[tcCounter + 7] = mesh.texcoords[tcCounter + 5];
vData.texcoords[tcCounter + 8] = vData.texcoords[tcCounter + 2]; mesh.texcoords[tcCounter + 8] = mesh.texcoords[tcCounter + 2];
vData.texcoords[tcCounter + 9] = vData.texcoords[tcCounter + 3]; mesh.texcoords[tcCounter + 9] = mesh.texcoords[tcCounter + 3];
vData.texcoords[tcCounter + 10] = (float)(x+1) / (mapX-1); mesh.texcoords[tcCounter + 10] = (float)(x+1) / (mapX-1);
vData.texcoords[tcCounter + 11] = (float)(z+1) / (mapZ-1); mesh.texcoords[tcCounter + 11] = (float)(z+1) / (mapZ-1);
tcCounter += 12; // 6 texcoords, 12 floats tcCounter += 12; // 6 texcoords, 12 floats
// Fill normals array with data // Fill normals array with data
@ -696,9 +696,9 @@ Model LoadHeightmap(Image heightmap, float maxHeight)
// NOTE: Current Model implementation doe not use normals! // NOTE: Current Model implementation doe not use normals!
for (int i = 0; i < 18; i += 3) for (int i = 0; i < 18; i += 3)
{ {
vData.normals[nCounter + i] = 0.0f; mesh.normals[nCounter + i] = 0.0f;
vData.normals[nCounter + i + 1] = 1.0f; mesh.normals[nCounter + i + 1] = 1.0f;
vData.normals[nCounter + i + 2] = 0.0f; mesh.normals[nCounter + i + 2] = 0.0f;
} }
// TODO: Calculate normals in an efficient way // TODO: Calculate normals in an efficient way
@ -713,20 +713,20 @@ Model LoadHeightmap(Image heightmap, float maxHeight)
// Fill color data // Fill color data
// NOTE: Not used any more... just one plain color defined at DrawModel() // 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 // 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 // NOTE: We don't need CPU vertex data on OpenGL 3.3 or ES2
if (rlGetVersion() != OPENGL_11) if (rlGetVersion() != OPENGL_11)
{ {
free(vData.vertices); free(mesh.vertices);
free(vData.texcoords); free(mesh.texcoords);
free(vData.normals); free(mesh.normals);
free(vData.colors); free(mesh.colors);
} }
return model; return model;
@ -735,7 +735,7 @@ Model LoadHeightmap(Image heightmap, float maxHeight)
// Load a map image as a 3d model (cubes based) // Load a map image as a 3d model (cubes based)
Model LoadCubicmap(Image cubicmap) Model LoadCubicmap(Image cubicmap)
{ {
VertexData vData; Mesh mesh;
Color *cubicmapPixels = GetImageData(cubicmap); Color *cubicmapPixels = GetImageData(cubicmap);
@ -1041,25 +1041,25 @@ Model LoadCubicmap(Image cubicmap)
} }
// Move data from mapVertices temp arays to vertices float array // 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)); mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float));
vData.normals = (float *)malloc(vData.vertexCount*3*sizeof(float)); mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float));
vData.texcoords = (float *)malloc(vData.vertexCount*2*sizeof(float)); mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float));
vData.colors = (unsigned char *)malloc(vData.vertexCount*4*sizeof(unsigned char)); // Not used... mesh.colors = (unsigned char *)malloc(mesh.vertexCount*4*sizeof(unsigned char)); // Not used...
// Fill color data // Fill color data
// NOTE: Not used any more... just one plain color defined at DrawModel() // 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; int fCounter = 0;
// Move vertices data // Move vertices data
for (int i = 0; i < vCounter; i++) for (int i = 0; i < vCounter; i++)
{ {
vData.vertices[fCounter] = mapVertices[i].x; mesh.vertices[fCounter] = mapVertices[i].x;
vData.vertices[fCounter + 1] = mapVertices[i].y; mesh.vertices[fCounter + 1] = mapVertices[i].y;
vData.vertices[fCounter + 2] = mapVertices[i].z; mesh.vertices[fCounter + 2] = mapVertices[i].z;
fCounter += 3; fCounter += 3;
} }
@ -1068,9 +1068,9 @@ Model LoadCubicmap(Image cubicmap)
// Move normals data // Move normals data
for (int i = 0; i < nCounter; i++) for (int i = 0; i < nCounter; i++)
{ {
vData.normals[fCounter] = mapNormals[i].x; mesh.normals[fCounter] = mapNormals[i].x;
vData.normals[fCounter + 1] = mapNormals[i].y; mesh.normals[fCounter + 1] = mapNormals[i].y;
vData.normals[fCounter + 2] = mapNormals[i].z; mesh.normals[fCounter + 2] = mapNormals[i].z;
fCounter += 3; fCounter += 3;
} }
@ -1079,8 +1079,8 @@ Model LoadCubicmap(Image cubicmap)
// Move texcoords data // Move texcoords data
for (int i = 0; i < tcCounter; i++) for (int i = 0; i < tcCounter; i++)
{ {
vData.texcoords[fCounter] = mapTexcoords[i].x; mesh.texcoords[fCounter] = mapTexcoords[i].x;
vData.texcoords[fCounter + 1] = mapTexcoords[i].y; mesh.texcoords[fCounter + 1] = mapTexcoords[i].y;
fCounter += 2; fCounter += 2;
} }
@ -1090,18 +1090,18 @@ Model LoadCubicmap(Image cubicmap)
free(cubicmapPixels); 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 // 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 // NOTE: We don't need CPU vertex data on OpenGL 3.3 or ES2
if (rlGetVersion() != OPENGL_11) if (rlGetVersion() != OPENGL_11)
{ {
free(vData.vertices); free(mesh.vertices);
free(vData.texcoords); free(mesh.texcoords);
free(vData.normals); free(mesh.normals);
free(vData.colors); free(mesh.colors);
} }
return model; return model;
@ -1617,9 +1617,9 @@ static float GetHeightValue(Color pixel)
} }
// Load OBJ mesh data // 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 dataType;
char comments[200]; char comments[200];
@ -1636,7 +1636,7 @@ static VertexData LoadOBJ(const char *fileName)
if (objFile == NULL) if (objFile == NULL)
{ {
TraceLog(WARNING, "[%s] OBJ file could not be opened", fileName); TraceLog(WARNING, "[%s] OBJ file could not be opened", fileName);
return vData; return mesh;
} }
// First reading pass: Get numVertex, numNormals, numTexCoords, numTriangles // First reading pass: Get numVertex, numNormals, numTexCoords, numTriangles
@ -1747,15 +1747,15 @@ static VertexData LoadOBJ(const char *fileName)
} }
// At this point all vertex data (v, vt, vn) has been gathered on midVertices, midTexCoords, midNormals // 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 // Additional arrays to store vertex data as floats
vData.vertices = (float *)malloc(vData.vertexCount*3*sizeof(float)); mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float));
vData.texcoords = (float *)malloc(vData.vertexCount*2*sizeof(float)); mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float));
vData.normals = (float *)malloc(vData.vertexCount*3*sizeof(float)); mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float));
vData.colors = (unsigned char *)malloc(vData.vertexCount*4*sizeof(unsigned char)); mesh.colors = (unsigned char *)malloc(mesh.vertexCount*4*sizeof(unsigned char));
int vCounter = 0; // Used to count vertices float by float int vCounter = 0; // Used to count vertices float by float
int tcCounter = 0; // Used to count texcoords float by float int tcCounter = 0; // Used to count texcoords float by float
@ -1783,32 +1783,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 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]); 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; mesh.vertices[vCounter] = midVertices[vNum[0]-1].x;
vData.vertices[vCounter + 1] = midVertices[vNum[0]-1].y; mesh.vertices[vCounter + 1] = midVertices[vNum[0]-1].y;
vData.vertices[vCounter + 2] = midVertices[vNum[0]-1].z; mesh.vertices[vCounter + 2] = midVertices[vNum[0]-1].z;
vCounter += 3; vCounter += 3;
vData.vertices[vCounter] = midVertices[vNum[1]-1].x; mesh.vertices[vCounter] = midVertices[vNum[1]-1].x;
vData.vertices[vCounter + 1] = midVertices[vNum[1]-1].y; mesh.vertices[vCounter + 1] = midVertices[vNum[1]-1].y;
vData.vertices[vCounter + 2] = midVertices[vNum[1]-1].z; mesh.vertices[vCounter + 2] = midVertices[vNum[1]-1].z;
vCounter += 3; vCounter += 3;
vData.vertices[vCounter] = midVertices[vNum[2]-1].x; mesh.vertices[vCounter] = midVertices[vNum[2]-1].x;
vData.vertices[vCounter + 1] = midVertices[vNum[2]-1].y; mesh.vertices[vCounter + 1] = midVertices[vNum[2]-1].y;
vData.vertices[vCounter + 2] = midVertices[vNum[2]-1].z; mesh.vertices[vCounter + 2] = midVertices[vNum[2]-1].z;
vCounter += 3; vCounter += 3;
if (numNormals > 0) if (numNormals > 0)
{ {
vData.normals[nCounter] = midNormals[vnNum[0]-1].x; mesh.normals[nCounter] = midNormals[vnNum[0]-1].x;
vData.normals[nCounter + 1] = midNormals[vnNum[0]-1].y; mesh.normals[nCounter + 1] = midNormals[vnNum[0]-1].y;
vData.normals[nCounter + 2] = midNormals[vnNum[0]-1].z; mesh.normals[nCounter + 2] = midNormals[vnNum[0]-1].z;
nCounter += 3; nCounter += 3;
vData.normals[nCounter] = midNormals[vnNum[1]-1].x; mesh.normals[nCounter] = midNormals[vnNum[1]-1].x;
vData.normals[nCounter + 1] = midNormals[vnNum[1]-1].y; mesh.normals[nCounter + 1] = midNormals[vnNum[1]-1].y;
vData.normals[nCounter + 2] = midNormals[vnNum[1]-1].z; mesh.normals[nCounter + 2] = midNormals[vnNum[1]-1].z;
nCounter += 3; nCounter += 3;
vData.normals[nCounter] = midNormals[vnNum[2]-1].x; mesh.normals[nCounter] = midNormals[vnNum[2]-1].x;
vData.normals[nCounter + 1] = midNormals[vnNum[2]-1].y; mesh.normals[nCounter + 1] = midNormals[vnNum[2]-1].y;
vData.normals[nCounter + 2] = midNormals[vnNum[2]-1].z; mesh.normals[nCounter + 2] = midNormals[vnNum[2]-1].z;
nCounter += 3; nCounter += 3;
} }
else else
@ -1817,17 +1817,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])); Vector3 norm = VectorCrossProduct(VectorSubtract(midVertices[vNum[1]-1], midVertices[vNum[0]-1]), VectorSubtract(midVertices[vNum[2]-1], midVertices[vNum[0]-1]));
VectorNormalize(&norm); VectorNormalize(&norm);
vData.normals[nCounter] = norm.x; mesh.normals[nCounter] = norm.x;
vData.normals[nCounter + 1] = norm.y; mesh.normals[nCounter + 1] = norm.y;
vData.normals[nCounter + 2] = norm.z; mesh.normals[nCounter + 2] = norm.z;
nCounter += 3; nCounter += 3;
vData.normals[nCounter] = norm.x; mesh.normals[nCounter] = norm.x;
vData.normals[nCounter + 1] = norm.y; mesh.normals[nCounter + 1] = norm.y;
vData.normals[nCounter + 2] = norm.z; mesh.normals[nCounter + 2] = norm.z;
nCounter += 3; nCounter += 3;
vData.normals[nCounter] = norm.x; mesh.normals[nCounter] = norm.x;
vData.normals[nCounter + 1] = norm.y; mesh.normals[nCounter + 1] = norm.y;
vData.normals[nCounter + 2] = norm.z; mesh.normals[nCounter + 2] = norm.z;
nCounter += 3; nCounter += 3;
} }
@ -1835,14 +1835,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: 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 // NOTE: Texture coordinates are Y flipped upside-down
vData.texcoords[tcCounter] = midTexCoords[vtNum[0]-1].x; mesh.texcoords[tcCounter] = midTexCoords[vtNum[0]-1].x;
vData.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[0]-1].y; mesh.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[0]-1].y;
tcCounter += 2; tcCounter += 2;
vData.texcoords[tcCounter] = midTexCoords[vtNum[1]-1].x; mesh.texcoords[tcCounter] = midTexCoords[vtNum[1]-1].x;
vData.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[1]-1].y; mesh.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[1]-1].y;
tcCounter += 2; tcCounter += 2;
vData.texcoords[tcCounter] = midTexCoords[vtNum[2]-1].x; mesh.texcoords[tcCounter] = midTexCoords[vtNum[2]-1].x;
vData.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[2]-1].y; mesh.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[2]-1].y;
tcCounter += 2; tcCounter += 2;
} }
} break; } break;
@ -1853,19 +1853,19 @@ static VertexData LoadOBJ(const char *fileName)
fclose(objFile); fclose(objFile);
// Security check, just in case no normals or no texcoords defined in OBJ // 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: We set all vertex colors to white
// NOTE: Not used any more... just one plain color defined at DrawModel() // 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 // Now we can free temp mid* arrays
free(midVertices); free(midVertices);
free(midNormals); free(midNormals);
free(midTexCoords); 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); TraceLog(INFO, "[%s] Model loaded successfully in RAM (CPU)", fileName);
return vData; return mesh;
} }

View file

@ -308,17 +308,27 @@ typedef struct Camera {
Vector3 up; Vector3 up;
} Camera; } Camera;
// Bounding box type
typedef struct BoundingBox {
Vector3 min;
Vector3 max;
} BoundingBox;
// Vertex data definning a mesh // Vertex data definning a mesh
// NOTE: If using OpenGL 1.1, data loaded in CPU; if OpenGL 3.3+ data loaded in GPU (vaoId) typedef struct Mesh {
typedef struct VertexData { int vertexCount; // num vertices
int vertexCount; float *vertices; // vertex position (XYZ - 3 components per vertex)
float *vertices; // 3 components per vertex float *texcoords; // vertex texture coordinates (UV - 2 components per vertex)
float *texcoords; // 2 components per vertex float *texcoords2; // vertex second texture coordinates (useful for lightmaps)
float *normals; // 3 components per vertex float *normals; // vertex normals (XYZ - 3 components per vertex)
unsigned char *colors; // 4 components per vertex float *tangents; // vertex tangents (XYZ - 3 components per vertex)
unsigned int vaoId; unsigned char *colors; // vertex colors (RGBA - 4 components per vertex)
unsigned int vboId[4];
} VertexData; BoundingBox bounds; // mesh limits defined by min and max points
unsigned int vaoId; // OpenGL Vertex Array Object id
unsigned int vboId[6]; // OpenGL Vertex Buffer Objects id (6 types of vertex data)
} Mesh;
// Shader type (generic shader) // Shader type (generic shader)
typedef struct Shader { typedef struct Shader {
@ -349,7 +359,7 @@ typedef struct Shader {
// 3d Model type // 3d Model type
typedef struct Model { typedef struct Model {
VertexData mesh; Mesh mesh;
Matrix transform; Matrix transform;
Texture2D texture; // Only for OpenGL 1.1, on newer versions this should be in the shader Texture2D texture; // Only for OpenGL 1.1, on newer versions this should be in the shader
Shader shader; Shader shader;
@ -742,7 +752,7 @@ void DrawGizmo(Vector3 position);
// Model 3d Loading and Drawing Functions (Module: models) // Model 3d Loading and Drawing Functions (Module: models)
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
Model LoadModel(const char *fileName); // Load a 3d model (.OBJ) Model LoadModel(const char *fileName); // Load a 3d model (.OBJ)
Model LoadModelEx(VertexData data); // Load a 3d model (from vertex data) Model LoadModelEx(Mesh data); // Load a 3d model (from vertex data)
//Model LoadModelFromRES(const char *rresName, int resId); // TODO: Load a 3d model from rRES file (raylib Resource) //Model LoadModelFromRES(const char *rresName, int resId); // TODO: Load a 3d model from rRES file (raylib Resource)
Model LoadHeightmap(Image heightmap, float maxHeight); // Load a heightmap image as a 3d model Model LoadHeightmap(Image heightmap, float maxHeight); // Load a heightmap image as a 3d model
Model LoadCubicmap(Image cubicmap); // Load a map image as a 3d model (cubes based) Model LoadCubicmap(Image cubicmap); // Load a map image as a 3d model (cubes based)

View file

@ -1100,9 +1100,9 @@ void rlglInitPostpro(void)
if (postproFbo.id > 0) if (postproFbo.id > 0)
{ {
// Create a simple quad model to render fbo texture // Create a simple quad model to render fbo texture
VertexData quadData; Mesh quad;
quadData.vertexCount = 6; quad.vertexCount = 6;
float w = (float)screenWidth; float w = (float)screenWidth;
float h = (float)screenHeight; float h = (float)screenHeight;
@ -1112,12 +1112,12 @@ void rlglInitPostpro(void)
float quadNormals[6*3] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f }; float quadNormals[6*3] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f };
unsigned char quadColors[6*4] = { 255 }; unsigned char quadColors[6*4] = { 255 };
quadData.vertices = quadPositions; quad.vertices = quadPositions;
quadData.texcoords = quadTexcoords; quad.texcoords = quadTexcoords;
quadData.normals = quadNormals; quad.normals = quadNormals;
quadData.colors = quadColors; quad.colors = quadColors;
postproQuad = rlglLoadModel(quadData); postproQuad = rlglLoadModel(quad);
// NOTE: postproFbo.colorTextureId must be assigned to postproQuad model shader // NOTE: postproFbo.colorTextureId must be assigned to postproQuad model shader
} }
@ -1982,7 +1982,7 @@ void rlglGenerateMipmaps(Texture2D texture)
} }
// Load vertex data into a VAO (if supported) and VBO // Load vertex data into a VAO (if supported) and VBO
Model rlglLoadModel(VertexData mesh) Model rlglLoadModel(Mesh mesh)
{ {
Model model; Model model;

View file

@ -131,17 +131,22 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion;
COMPRESSED_ASTC_8x8_RGBA // 2 bpp COMPRESSED_ASTC_8x8_RGBA // 2 bpp
} TextureFormat; } TextureFormat;
// VertexData type // Mesh with vertex data type
// NOTE: If using OpenGL 1.1, data loaded in CPU; if OpenGL 3.3+ data loaded in GPU (vaoId) // NOTE: If using OpenGL 1.1, data loaded in CPU; if OpenGL 3.3+ data loaded in GPU (vaoId)
typedef struct VertexData { typedef struct Mesh {
int vertexCount; int vertexCount; // num vertices
float *vertices; // 3 components per vertex float *vertices; // vertex position (XYZ - 3 components per vertex)
float *texcoords; // 2 components per vertex float *texcoords; // vertex texture coordinates (UV - 2 components per vertex)
float *normals; // 3 components per vertex float *texcoords2; // vertex second texture coordinates (useful for lightmaps)
unsigned char *colors; float *normals; // vertex normals (XYZ - 3 components per vertex)
unsigned int vaoId; float *tangents; // vertex tangents (XYZ - 3 components per vertex)
unsigned int vboId[4]; unsigned char *colors; // vertex colors (RGBA - 4 components per vertex)
} VertexData;
BoundingBox bounds; // mesh limits defined by min and max points
unsigned int vaoId; // OpenGL Vertex Array Object id
unsigned int vboId[6]; // OpenGL Vertex Buffer Objects id (6 types of vertex data)
} Mesh;
// Shader type // Shader type
typedef struct Shader { typedef struct Shader {
@ -179,7 +184,7 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion;
// 3d Model type // 3d Model type
typedef struct Model { typedef struct Model {
VertexData mesh; Mesh mesh;
Matrix transform; Matrix transform;
Texture2D texture; Texture2D texture;
Shader shader; Shader shader;
@ -254,7 +259,7 @@ void rlglGenerateMipmaps(Texture2D texture); // Gene
void rlglInitPostpro(void); // Initialize postprocessing system void rlglInitPostpro(void); // Initialize postprocessing system
void rlglDrawPostpro(void); // Draw with postprocessing shader void rlglDrawPostpro(void); // Draw with postprocessing shader
Model rlglLoadModel(VertexData mesh); // Upload vertex data into GPU and provided VAO/VBO ids Model rlglLoadModel(Mesh mesh); // Upload vertex data into GPU and provided VAO/VBO ids
void rlglDrawModel(Model model, Vector3 position, float rotationAngle, Vector3 rotationAxis, Vector3 scale, Color color, bool wires); void rlglDrawModel(Model model, Vector3 position, float rotationAngle, Vector3 rotationAxis, Vector3 scale, Color color, bool wires);
Vector3 rlglUnproject(Vector3 source, Matrix proj, Matrix view); // Get world coordinates from screen coordinates Vector3 rlglUnproject(Vector3 source, Matrix proj, Matrix view); // Get world coordinates from screen coordinates