mirror of
				https://github.com/raysan5/raylib.git
				synced 2025-10-26 12:27:01 +00:00 
			
		
		
		
	Support DYNAMIC_DRAW mesh loading
This commit is contained in:
		
							
								
								
									
										10
									
								
								src/models.c
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								src/models.c
									
									
									
									
									
								
							| @@ -553,7 +553,7 @@ Model LoadModel(const char *fileName) | |||||||
|     if (model.mesh.vertexCount == 0) TraceLog(WARNING, "Model could not be loaded"); |     if (model.mesh.vertexCount == 0) TraceLog(WARNING, "Model could not be loaded"); | ||||||
|     else |     else | ||||||
|     { |     { | ||||||
|         rlglLoadMesh(&model.mesh);  // Upload vertex data to GPU |         rlglLoadMesh(&model.mesh, false);  // Upload vertex data to GPU (static model) | ||||||
|          |          | ||||||
|         model.transform = MatrixIdentity(); |         model.transform = MatrixIdentity(); | ||||||
|         model.material = LoadDefaultMaterial(); |         model.material = LoadDefaultMaterial(); | ||||||
| @@ -563,13 +563,13 @@ Model LoadModel(const char *fileName) | |||||||
| } | } | ||||||
|  |  | ||||||
| // Load a 3d model (from vertex data) | // Load a 3d model (from vertex data) | ||||||
| Model LoadModelEx(Mesh data) | Model LoadModelEx(Mesh data, bool dynamic) | ||||||
| { | { | ||||||
|     Model model = { 0 }; |     Model model = { 0 }; | ||||||
|  |  | ||||||
|     model.mesh = data; |     model.mesh = data; | ||||||
|      |      | ||||||
|     rlglLoadMesh(&model.mesh);      // Upload vertex data to GPU |     rlglLoadMesh(&model.mesh, dynamic);  // Upload vertex data to GPU | ||||||
|      |      | ||||||
|     model.transform = MatrixIdentity(); |     model.transform = MatrixIdentity(); | ||||||
|     model.material = LoadDefaultMaterial(); |     model.material = LoadDefaultMaterial(); | ||||||
| @@ -668,7 +668,7 @@ Model LoadHeightmap(Image heightmap, Vector3 size) | |||||||
|      |      | ||||||
|     model.mesh = GenMeshHeightmap(heightmap, size); |     model.mesh = GenMeshHeightmap(heightmap, size); | ||||||
|      |      | ||||||
|     rlglLoadMesh(&model.mesh); |     rlglLoadMesh(&model.mesh, false);  // Upload vertex data to GPU (static model) | ||||||
|      |      | ||||||
|     model.transform = MatrixIdentity(); |     model.transform = MatrixIdentity(); | ||||||
|     model.material = LoadDefaultMaterial(); |     model.material = LoadDefaultMaterial(); | ||||||
| @@ -683,7 +683,7 @@ Model LoadCubicmap(Image cubicmap) | |||||||
|      |      | ||||||
|     model.mesh = GenMeshCubicmap(cubicmap, (Vector3){ 1.0, 1.0, 1.5f }); |     model.mesh = GenMeshCubicmap(cubicmap, (Vector3){ 1.0, 1.0, 1.5f }); | ||||||
|      |      | ||||||
|     rlglLoadMesh(&model.mesh); |     rlglLoadMesh(&model.mesh, false);  // Upload vertex data to GPU (static model) | ||||||
|      |      | ||||||
|     model.transform = MatrixIdentity(); |     model.transform = MatrixIdentity(); | ||||||
|     model.material = LoadDefaultMaterial(); |     model.material = LoadDefaultMaterial(); | ||||||
|   | |||||||
| @@ -803,7 +803,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(Mesh data);                                   // Load a 3d model (from mesh data) | Model LoadModelEx(Mesh data, bool dynamic);                     // Load a 3d model (from mesh data) | ||||||
| Model LoadModelFromRES(const char *rresName, int resId);        // Load a 3d model from rRES file (raylib Resource) | Model LoadModelFromRES(const char *rresName, int resId);        // Load a 3d model from rRES file (raylib Resource) | ||||||
| Model LoadHeightmap(Image heightmap, Vector3 size);             // Load a heightmap image as a 3d model | Model LoadHeightmap(Image heightmap, Vector3 size);             // 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) | ||||||
|   | |||||||
							
								
								
									
										17
									
								
								src/rlgl.c
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								src/rlgl.c
									
									
									
									
									
								
							| @@ -1500,7 +1500,7 @@ void rlglGenerateMipmaps(Texture2D texture) | |||||||
| } | } | ||||||
|  |  | ||||||
| // Upload vertex data into a VAO (if supported) and VBO | // Upload vertex data into a VAO (if supported) and VBO | ||||||
| void rlglLoadMesh(Mesh *mesh) | void rlglLoadMesh(Mesh *mesh, bool dynamic) | ||||||
| { | { | ||||||
|     mesh->vaoId = 0;        // Vertex Array Object |     mesh->vaoId = 0;        // Vertex Array Object | ||||||
|     mesh->vboId[0] = 0;     // Vertex positions VBO |     mesh->vboId[0] = 0;     // Vertex positions VBO | ||||||
| @@ -1511,6 +1511,9 @@ void rlglLoadMesh(Mesh *mesh) | |||||||
|     mesh->vboId[5] = 0;     // Vertex texcoords2 VBO |     mesh->vboId[5] = 0;     // Vertex texcoords2 VBO | ||||||
|     mesh->vboId[6] = 0;     // Vertex indices VBO |     mesh->vboId[6] = 0;     // Vertex indices VBO | ||||||
|      |      | ||||||
|  |     int drawHint = GL_STATIC_DRAW; | ||||||
|  |     if (dynamic) drawHint = GL_DYNAMIC_DRAW; | ||||||
|  |  | ||||||
| #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) | ||||||
|     GLuint vaoId = 0;       // Vertex Array Objects (VAO) |     GLuint vaoId = 0;       // Vertex Array Objects (VAO) | ||||||
|     GLuint vboId[7];        // Vertex Buffer Objects (VBOs) |     GLuint vboId[7];        // Vertex Buffer Objects (VBOs) | ||||||
| @@ -1527,14 +1530,14 @@ void rlglLoadMesh(Mesh *mesh) | |||||||
|     // Enable vertex attributes: position (shader-location = 0) |     // Enable vertex attributes: position (shader-location = 0) | ||||||
|     glGenBuffers(1, &vboId[0]); |     glGenBuffers(1, &vboId[0]); | ||||||
|     glBindBuffer(GL_ARRAY_BUFFER, vboId[0]); |     glBindBuffer(GL_ARRAY_BUFFER, vboId[0]); | ||||||
|     glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->vertices, GL_STATIC_DRAW); |     glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->vertices, drawHint); | ||||||
|     glVertexAttribPointer(0, 3, GL_FLOAT, 0, 0, 0); |     glVertexAttribPointer(0, 3, GL_FLOAT, 0, 0, 0); | ||||||
|     glEnableVertexAttribArray(0); |     glEnableVertexAttribArray(0); | ||||||
|  |  | ||||||
|     // Enable vertex attributes: texcoords (shader-location = 1) |     // Enable vertex attributes: texcoords (shader-location = 1) | ||||||
|     glGenBuffers(1, &vboId[1]); |     glGenBuffers(1, &vboId[1]); | ||||||
|     glBindBuffer(GL_ARRAY_BUFFER, vboId[1]); |     glBindBuffer(GL_ARRAY_BUFFER, vboId[1]); | ||||||
|     glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh->vertexCount, mesh->texcoords, GL_STATIC_DRAW); |     glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh->vertexCount, mesh->texcoords, drawHint); | ||||||
|     glVertexAttribPointer(1, 2, GL_FLOAT, 0, 0, 0); |     glVertexAttribPointer(1, 2, GL_FLOAT, 0, 0, 0); | ||||||
|     glEnableVertexAttribArray(1); |     glEnableVertexAttribArray(1); | ||||||
|  |  | ||||||
| @@ -1543,7 +1546,7 @@ void rlglLoadMesh(Mesh *mesh) | |||||||
|     { |     { | ||||||
|         glGenBuffers(1, &vboId[2]); |         glGenBuffers(1, &vboId[2]); | ||||||
|         glBindBuffer(GL_ARRAY_BUFFER, vboId[2]); |         glBindBuffer(GL_ARRAY_BUFFER, vboId[2]); | ||||||
|         glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->normals, GL_STATIC_DRAW); |         glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->normals, drawHint); | ||||||
|         glVertexAttribPointer(2, 3, GL_FLOAT, 0, 0, 0); |         glVertexAttribPointer(2, 3, GL_FLOAT, 0, 0, 0); | ||||||
|         glEnableVertexAttribArray(2); |         glEnableVertexAttribArray(2); | ||||||
|     } |     } | ||||||
| @@ -1559,7 +1562,7 @@ void rlglLoadMesh(Mesh *mesh) | |||||||
|     { |     { | ||||||
|         glGenBuffers(1, &vboId[3]); |         glGenBuffers(1, &vboId[3]); | ||||||
|         glBindBuffer(GL_ARRAY_BUFFER, vboId[3]); |         glBindBuffer(GL_ARRAY_BUFFER, vboId[3]); | ||||||
|         glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*mesh->vertexCount, mesh->colors, GL_STATIC_DRAW); |         glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*mesh->vertexCount, mesh->colors, drawHint); | ||||||
|         glVertexAttribPointer(3, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); |         glVertexAttribPointer(3, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); | ||||||
|         glEnableVertexAttribArray(3); |         glEnableVertexAttribArray(3); | ||||||
|     } |     } | ||||||
| @@ -1575,7 +1578,7 @@ void rlglLoadMesh(Mesh *mesh) | |||||||
|     { |     { | ||||||
|         glGenBuffers(1, &vboId[4]); |         glGenBuffers(1, &vboId[4]); | ||||||
|         glBindBuffer(GL_ARRAY_BUFFER, vboId[4]); |         glBindBuffer(GL_ARRAY_BUFFER, vboId[4]); | ||||||
|         glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->tangents, GL_STATIC_DRAW); |         glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->tangents, drawHint); | ||||||
|         glVertexAttribPointer(4, 3, GL_FLOAT, 0, 0, 0); |         glVertexAttribPointer(4, 3, GL_FLOAT, 0, 0, 0); | ||||||
|         glEnableVertexAttribArray(4); |         glEnableVertexAttribArray(4); | ||||||
|     } |     } | ||||||
| @@ -1591,7 +1594,7 @@ void rlglLoadMesh(Mesh *mesh) | |||||||
|     { |     { | ||||||
|         glGenBuffers(1, &vboId[5]); |         glGenBuffers(1, &vboId[5]); | ||||||
|         glBindBuffer(GL_ARRAY_BUFFER, vboId[5]); |         glBindBuffer(GL_ARRAY_BUFFER, vboId[5]); | ||||||
|         glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh->vertexCount, mesh->texcoords2, GL_STATIC_DRAW); |         glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh->vertexCount, mesh->texcoords2, drawHint); | ||||||
|         glVertexAttribPointer(5, 2, GL_FLOAT, 0, 0, 0); |         glVertexAttribPointer(5, 2, GL_FLOAT, 0, 0, 0); | ||||||
|         glEnableVertexAttribArray(5); |         glEnableVertexAttribArray(5); | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -280,7 +280,7 @@ RenderTexture2D rlglLoadRenderTexture(int width, int height);   // Load a textur | |||||||
| void rlglUpdateTexture(unsigned int id, int width, int height, int format, void *data);         // Update GPU texture with new data | void rlglUpdateTexture(unsigned int id, int width, int height, int format, void *data);         // Update GPU texture with new data | ||||||
| void rlglGenerateMipmaps(Texture2D texture);                             // Generate mipmap data for selected texture | void rlglGenerateMipmaps(Texture2D texture);                             // Generate mipmap data for selected texture | ||||||
|  |  | ||||||
| void rlglLoadMesh(Mesh *mesh);                                      // Upload vertex data into GPU and provided VAO/VBO ids | void rlglLoadMesh(Mesh *mesh, bool dynamic);                        // Upload vertex data into GPU and provided VAO/VBO ids | ||||||
| void rlglUpdateMesh(Mesh mesh, int buffer, int numVertex);          // Update vertex data on GPU (upload new data to one buffer) | void rlglUpdateMesh(Mesh mesh, int buffer, int numVertex);          // Update vertex data on GPU (upload new data to one buffer) | ||||||
| void rlglDrawMesh(Mesh mesh, Material material, Matrix transform);  // Draw a 3d mesh with material and transform | void rlglDrawMesh(Mesh mesh, Material material, Matrix transform);  // Draw a 3d mesh with material and transform | ||||||
| void rlglUnloadMesh(Mesh *mesh);                                    // Unload mesh data from CPU and GPU | void rlglUnloadMesh(Mesh *mesh);                                    // Unload mesh data from CPU and GPU | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 raysan5
					raysan5