Improved LoadHeightmap()
This commit is contained in:
parent
15cd4dce4e
commit
685273675b
@ -21,13 +21,13 @@ int main()
|
|||||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - heightmap loading and drawing");
|
InitWindow(screenWidth, screenHeight, "raylib [models] example - heightmap loading and drawing");
|
||||||
|
|
||||||
// Define our custom camera to look into our 3d world
|
// Define our custom camera to look into our 3d world
|
||||||
Camera camera = {{ 24.0f, 18.0f, 24.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
|
Camera camera = {{ 18.0f, 16.0f, 18.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
|
||||||
|
|
||||||
Image image = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM)
|
Image image = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM)
|
||||||
Texture2D texture = LoadTextureFromImage(image); // Convert image to texture (VRAM)
|
Texture2D texture = LoadTextureFromImage(image); // Convert image to texture (VRAM)
|
||||||
Model map = LoadHeightmap(image, 32); // Load heightmap model
|
Model map = LoadHeightmap(image, (Vector3){ 16, 8, 16 }); // Load heightmap model with defined size
|
||||||
SetModelTexture(&map, texture); // Bind texture to model
|
SetModelTexture(&map, texture); // Bind texture to model
|
||||||
Vector3 mapPosition = { -16.0f, 0.0f, -16.0f }; // Set model position (depends on model scaling!)
|
Vector3 mapPosition = { -8.0f, 0.0f, -8.0f }; // Set model position (depends on model scaling!)
|
||||||
|
|
||||||
UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM
|
UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM
|
||||||
|
|
||||||
@ -54,7 +54,9 @@ int main()
|
|||||||
Begin3dMode(camera);
|
Begin3dMode(camera);
|
||||||
|
|
||||||
// NOTE: Model is scaled to 1/4 of its original size (128x128 units)
|
// NOTE: Model is scaled to 1/4 of its original size (128x128 units)
|
||||||
DrawModel(map, mapPosition, 1/4.0f, RED);
|
DrawModel(map, mapPosition, 1.0f, RED);
|
||||||
|
|
||||||
|
DrawGrid(20, 1.0f);
|
||||||
|
|
||||||
End3dMode();
|
End3dMode();
|
||||||
|
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 121 KiB After Width: | Height: | Size: 95 KiB |
60
src/models.c
60
src/models.c
@ -55,7 +55,6 @@ extern unsigned int whiteTexture;
|
|||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Module specific Functions Declaration
|
// Module specific Functions Declaration
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
static float GetHeightValue(Color pixel);
|
|
||||||
static Mesh LoadOBJ(const char *fileName);
|
static Mesh LoadOBJ(const char *fileName);
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
@ -608,17 +607,19 @@ Model LoadModelEx(Mesh data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load a heightmap image as a 3d model
|
// Load a heightmap image as a 3d model
|
||||||
Model LoadHeightmap(Image heightmap, float maxHeight)
|
// NOTE: model map size is defined in generic units
|
||||||
|
Model LoadHeightmap(Image heightmap, Vector3 size)
|
||||||
{
|
{
|
||||||
|
#define GRAY_VALUE(c) ((c.r+c.g+c.b)/3)
|
||||||
|
|
||||||
Mesh mesh;
|
Mesh mesh;
|
||||||
|
|
||||||
int mapX = heightmap.width;
|
int mapX = heightmap.width;
|
||||||
int mapZ = heightmap.height;
|
int mapZ = heightmap.height;
|
||||||
|
|
||||||
Color *heightmapPixels = GetImageData(heightmap);
|
Color *pixels = GetImageData(heightmap);
|
||||||
|
|
||||||
// NOTE: One vertex per pixel
|
// NOTE: One vertex per pixel
|
||||||
// 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
|
||||||
|
|
||||||
mesh.vertexCount = numTriangles*3;
|
mesh.vertexCount = numTriangles*3;
|
||||||
@ -634,7 +635,7 @@ Model LoadHeightmap(Image heightmap, float maxHeight)
|
|||||||
|
|
||||||
int trisCounter = 0;
|
int trisCounter = 0;
|
||||||
|
|
||||||
float scaleFactor = maxHeight/255; // TODO: Review scaleFactor calculation
|
Vector3 scaleFactor = { size.x/mapX, size.y/255.0f, size.z/mapZ };
|
||||||
|
|
||||||
for(int z = 0; z < mapZ-1; z++)
|
for(int z = 0; z < mapZ-1; z++)
|
||||||
{
|
{
|
||||||
@ -644,17 +645,17 @@ Model LoadHeightmap(Image heightmap, float maxHeight)
|
|||||||
//----------------------------------------------------------
|
//----------------------------------------------------------
|
||||||
|
|
||||||
// one triangle - 3 vertex
|
// one triangle - 3 vertex
|
||||||
mesh.vertices[vCounter] = x;
|
mesh.vertices[vCounter] = (float)x*scaleFactor.x;
|
||||||
mesh.vertices[vCounter + 1] = GetHeightValue(heightmapPixels[x + z*mapX])*scaleFactor;
|
mesh.vertices[vCounter + 1] = (float)GRAY_VALUE(pixels[x + z*mapX])*scaleFactor.y;
|
||||||
mesh.vertices[vCounter + 2] = z;
|
mesh.vertices[vCounter + 2] = (float)z*scaleFactor.z;
|
||||||
|
|
||||||
mesh.vertices[vCounter + 3] = x;
|
mesh.vertices[vCounter + 3] = (float)x*scaleFactor.x;
|
||||||
mesh.vertices[vCounter + 4] = GetHeightValue(heightmapPixels[x + (z+1)*mapX])*scaleFactor;
|
mesh.vertices[vCounter + 4] = (float)GRAY_VALUE(pixels[x + (z + 1)*mapX])*scaleFactor.y;
|
||||||
mesh.vertices[vCounter + 5] = z+1;
|
mesh.vertices[vCounter + 5] = (float)(z + 1)*scaleFactor.z;
|
||||||
|
|
||||||
mesh.vertices[vCounter + 6] = x+1;
|
mesh.vertices[vCounter + 6] = (float)(x + 1)*scaleFactor.x;
|
||||||
mesh.vertices[vCounter + 7] = GetHeightValue(heightmapPixels[(x+1) + z*mapX])*scaleFactor;
|
mesh.vertices[vCounter + 7] = (float)GRAY_VALUE(pixels[(x + 1) + z*mapX])*scaleFactor.y;
|
||||||
mesh.vertices[vCounter + 8] = z;
|
mesh.vertices[vCounter + 8] = (float)z*scaleFactor.z;
|
||||||
|
|
||||||
// another triangle - 3 vertex
|
// another triangle - 3 vertex
|
||||||
mesh.vertices[vCounter + 9] = mesh.vertices[vCounter + 6];
|
mesh.vertices[vCounter + 9] = mesh.vertices[vCounter + 6];
|
||||||
@ -665,21 +666,21 @@ Model LoadHeightmap(Image heightmap, float maxHeight)
|
|||||||
mesh.vertices[vCounter + 13] = mesh.vertices[vCounter + 4];
|
mesh.vertices[vCounter + 13] = mesh.vertices[vCounter + 4];
|
||||||
mesh.vertices[vCounter + 14] = mesh.vertices[vCounter + 5];
|
mesh.vertices[vCounter + 14] = mesh.vertices[vCounter + 5];
|
||||||
|
|
||||||
mesh.vertices[vCounter + 15] = x+1;
|
mesh.vertices[vCounter + 15] = (float)(x + 1)*scaleFactor.x;
|
||||||
mesh.vertices[vCounter + 16] = GetHeightValue(heightmapPixels[(x+1) + (z+1)*mapX])*scaleFactor;
|
mesh.vertices[vCounter + 16] = (float)GRAY_VALUE(pixels[(x + 1) + (z + 1)*mapX])*scaleFactor.y;
|
||||||
mesh.vertices[vCounter + 17] = z+1;
|
mesh.vertices[vCounter + 17] = (float)(z + 1)*scaleFactor.z;
|
||||||
vCounter += 18; // 6 vertex, 18 floats
|
vCounter += 18; // 6 vertex, 18 floats
|
||||||
|
|
||||||
// Fill texcoords array with data
|
// Fill texcoords array with data
|
||||||
//--------------------------------------------------------------
|
//--------------------------------------------------------------
|
||||||
mesh.texcoords[tcCounter] = (float)x / (mapX-1);
|
mesh.texcoords[tcCounter] = (float)x/(mapX - 1);
|
||||||
mesh.texcoords[tcCounter + 1] = (float)z / (mapZ-1);
|
mesh.texcoords[tcCounter + 1] = (float)z/(mapZ - 1);
|
||||||
|
|
||||||
mesh.texcoords[tcCounter + 2] = (float)x / (mapX-1);
|
mesh.texcoords[tcCounter + 2] = (float)x/(mapX - 1);
|
||||||
mesh.texcoords[tcCounter + 3] = (float)(z+1) / (mapZ-1);
|
mesh.texcoords[tcCounter + 3] = (float)(z + 1)/(mapZ - 1);
|
||||||
|
|
||||||
mesh.texcoords[tcCounter + 4] = (float)(x+1) / (mapX-1);
|
mesh.texcoords[tcCounter + 4] = (float)(x + 1)/(mapX - 1);
|
||||||
mesh.texcoords[tcCounter + 5] = (float)z / (mapZ-1);
|
mesh.texcoords[tcCounter + 5] = (float)z/(mapZ - 1);
|
||||||
|
|
||||||
mesh.texcoords[tcCounter + 6] = mesh.texcoords[tcCounter + 4];
|
mesh.texcoords[tcCounter + 6] = mesh.texcoords[tcCounter + 4];
|
||||||
mesh.texcoords[tcCounter + 7] = mesh.texcoords[tcCounter + 5];
|
mesh.texcoords[tcCounter + 7] = mesh.texcoords[tcCounter + 5];
|
||||||
@ -687,13 +688,12 @@ Model LoadHeightmap(Image heightmap, float maxHeight)
|
|||||||
mesh.texcoords[tcCounter + 8] = mesh.texcoords[tcCounter + 2];
|
mesh.texcoords[tcCounter + 8] = mesh.texcoords[tcCounter + 2];
|
||||||
mesh.texcoords[tcCounter + 9] = mesh.texcoords[tcCounter + 3];
|
mesh.texcoords[tcCounter + 9] = mesh.texcoords[tcCounter + 3];
|
||||||
|
|
||||||
mesh.texcoords[tcCounter + 10] = (float)(x+1) / (mapX-1);
|
mesh.texcoords[tcCounter + 10] = (float)(x + 1)/(mapX - 1);
|
||||||
mesh.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
|
||||||
//--------------------------------------------------------------
|
//--------------------------------------------------------------
|
||||||
// NOTE: Current Model implementation doe not use normals!
|
|
||||||
for (int i = 0; i < 18; i += 3)
|
for (int i = 0; i < 18; i += 3)
|
||||||
{
|
{
|
||||||
mesh.normals[nCounter + i] = 0.0f;
|
mesh.normals[nCounter + i] = 0.0f;
|
||||||
@ -709,7 +709,7 @@ Model LoadHeightmap(Image heightmap, float maxHeight)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
free(heightmapPixels);
|
free(pixels);
|
||||||
|
|
||||||
// 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()
|
||||||
@ -1688,12 +1688,6 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p
|
|||||||
// Module specific Functions Definition
|
// Module specific Functions Definition
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
// Get current vertex y altitude (proportional to pixel colors in grayscale)
|
|
||||||
static float GetHeightValue(Color pixel)
|
|
||||||
{
|
|
||||||
return (((float)pixel.r + (float)pixel.g + (float)pixel.b)/3);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load OBJ mesh data
|
// Load OBJ mesh data
|
||||||
static Mesh LoadOBJ(const char *fileName)
|
static Mesh LoadOBJ(const char *fileName)
|
||||||
{
|
{
|
||||||
|
@ -759,7 +759,7 @@ void DrawGizmo(Vector3 position);
|
|||||||
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 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, 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)
|
||||||
void UnloadModel(Model model); // Unload 3d model from memory
|
void UnloadModel(Model model); // Unload 3d model from memory
|
||||||
void SetModelTexture(Model *model, Texture2D texture); // Link a texture to a model
|
void SetModelTexture(Model *model, Texture2D texture); // Link a texture to a model
|
||||||
|
Loading…
Reference in New Issue
Block a user