Add function DrawCubeTextureRec
This commit is contained in:
parent
7d995d95eb
commit
4817ab46f3
74
examples/models/models_cube_texture_region.c
Normal file
74
examples/models/models_cube_texture_region.c
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
/*******************************************************************************************
|
||||||
|
*
|
||||||
|
* raylib [core] example - Draw cube with texture regions
|
||||||
|
*
|
||||||
|
* This example has been created using raylib 4.0 (www.raylib.com)
|
||||||
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Timon de Groot (@tdgroot) and Ramon Santamaria (@raysan5)
|
||||||
|
*
|
||||||
|
********************************************************************************************/
|
||||||
|
|
||||||
|
#include "raylib.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
const int screenWidth = 800;
|
||||||
|
const int screenHeight = 450;
|
||||||
|
|
||||||
|
InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d cube texture region");
|
||||||
|
|
||||||
|
// Define the camera to look into our 3d world
|
||||||
|
Camera3D camera = { 0 };
|
||||||
|
camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
|
||||||
|
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
|
||||||
|
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
|
||||||
|
camera.fovy = 45.0f; // Camera field-of-view Y
|
||||||
|
camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
|
||||||
|
|
||||||
|
SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
|
||||||
|
|
||||||
|
// Load texture and define texture regions
|
||||||
|
Texture2D texture = LoadTexture("resources/sheet.png"); // Load texture sheet
|
||||||
|
Rectangle sprite1 = (Rectangle){ 0.0f, 0.0f, 32.0f, 32.0f }; // Define texture region for sprite 1
|
||||||
|
Rectangle sprite2 = (Rectangle){ 32.0f, 0.0f, 32.0f, 32.0f }; // Define texture region for sprite 2
|
||||||
|
|
||||||
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Main game loop
|
||||||
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
|
{
|
||||||
|
// Update
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
UpdateCamera(&camera); // Update camera
|
||||||
|
|
||||||
|
if (IsKeyDown('Z')) camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Draw
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
BeginDrawing();
|
||||||
|
|
||||||
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
|
BeginMode3D(camera);
|
||||||
|
|
||||||
|
DrawCubeTextureRec(texture, sprite1, (Vector3){4.0f, 0.0f, -1.0f }, 2.0f, 2.0f, 2.0f, WHITE);
|
||||||
|
DrawCubeTextureRec(texture, sprite2, (Vector3){-4.0f, 0.0f, 1.0f }, 2.0f, 2.0f, 2.0f, BLUE);
|
||||||
|
|
||||||
|
EndMode3D();
|
||||||
|
|
||||||
|
EndDrawing();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
}
|
||||||
|
|
||||||
|
// De-Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
CloseWindow(); // Close window and OpenGL context
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
BIN
examples/models/resources/sheet.png
Normal file
BIN
examples/models/resources/sheet.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 355 B |
161
src/models.c
161
src/models.c
|
|
@ -9,7 +9,7 @@
|
||||||
* #define SUPPORT_FILEFORMAT_IQM
|
* #define SUPPORT_FILEFORMAT_IQM
|
||||||
* #define SUPPORT_FILEFORMAT_GLTF
|
* #define SUPPORT_FILEFORMAT_GLTF
|
||||||
* #define SUPPORT_FILEFORMAT_VOX
|
* #define SUPPORT_FILEFORMAT_VOX
|
||||||
*
|
*
|
||||||
* Selected desired fileformats to be supported for model data loading.
|
* Selected desired fileformats to be supported for model data loading.
|
||||||
*
|
*
|
||||||
* #define SUPPORT_MESH_GENERATION
|
* #define SUPPORT_MESH_GENERATION
|
||||||
|
|
@ -464,6 +464,157 @@ void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float hei
|
||||||
rlSetTexture(0);
|
rlSetTexture(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DrawCubeTextureRec(Texture2D texture, Rectangle source, Vector3 position, float width, float height, float length, Color color)
|
||||||
|
{
|
||||||
|
float x = position.x;
|
||||||
|
float y = position.y;
|
||||||
|
float z = position.z;
|
||||||
|
float texture_width = (float)texture.width;
|
||||||
|
float texture_height = (float)texture.height;
|
||||||
|
|
||||||
|
rlCheckRenderBatchLimit(36);
|
||||||
|
|
||||||
|
rlSetTexture(texture.id);
|
||||||
|
|
||||||
|
rlBegin(RL_QUADS);
|
||||||
|
rlColor4ub(color.r, color.g, color.b, color.a);
|
||||||
|
|
||||||
|
// Front Face
|
||||||
|
{
|
||||||
|
// Normal Pointing Towards Viewer
|
||||||
|
rlNormal3f(0.0f, 0.0f, 1.0f);
|
||||||
|
|
||||||
|
// Bottom Left Of The Texture and Quad
|
||||||
|
rlTexCoord2f(source.x / texture_width, (source.y + source.height) / texture_height);
|
||||||
|
rlVertex3f(x - width/2, y - height/2, z + length/2);
|
||||||
|
|
||||||
|
// Bottom Right Of The Texture and Quad
|
||||||
|
rlTexCoord2f((source.x + source.width) / texture_width, (source.y + source.height) / texture_height);
|
||||||
|
rlVertex3f(x + width/2, y - height/2, z + length/2);
|
||||||
|
|
||||||
|
// Top Right Of The Texture and Quad
|
||||||
|
rlTexCoord2f((source.x + source.width) / texture_width, source.y / texture_height);
|
||||||
|
rlVertex3f(x + width/2, y + height/2, z + length/2);
|
||||||
|
|
||||||
|
// Top Left Of The Texture and Quad
|
||||||
|
rlTexCoord2f(source.x / texture_width, source.y / texture_height);
|
||||||
|
rlVertex3f(x - width/2, y + height/2, z + length/2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Back Face
|
||||||
|
{
|
||||||
|
// Normal Pointing Away From Viewer
|
||||||
|
rlNormal3f(0.0f, 0.0f, - 1.0f);
|
||||||
|
|
||||||
|
// Bottom Right Of The Texture and Quad
|
||||||
|
rlTexCoord2f((source.x + source.width) / texture_width, (source.y + source.height) / texture_height);
|
||||||
|
rlVertex3f(x - width/2, y - height/2, z - length/2);
|
||||||
|
|
||||||
|
// Top Right Of The Texture and Quad
|
||||||
|
rlTexCoord2f((source.x + source.width) / texture_width, source.y / texture_height);
|
||||||
|
rlVertex3f(x - width/2, y + height/2, z - length/2);
|
||||||
|
|
||||||
|
// Top Left Of The Texture and Quad
|
||||||
|
rlTexCoord2f(source.x / texture_width, source.y / texture_height);
|
||||||
|
rlVertex3f(x + width/2, y + height/2, z - length/2);
|
||||||
|
|
||||||
|
// Bottom Left Of The Texture and Quad
|
||||||
|
rlTexCoord2f(source.x / texture_width, (source.y + source.height) / texture_height);
|
||||||
|
rlVertex3f(x + width/2, y - height/2, z - length/2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Top Face
|
||||||
|
{
|
||||||
|
// Normal Pointing Up
|
||||||
|
rlNormal3f(0.0f, 1.0f, 0.0f);
|
||||||
|
|
||||||
|
// Top Left Of The Texture and Quad
|
||||||
|
rlTexCoord2f(source.x / texture_width, source.y / texture_height);
|
||||||
|
rlVertex3f(x - width/2, y + height/2, z - length/2);
|
||||||
|
|
||||||
|
// Bottom Left Of The Texture and Quad
|
||||||
|
rlTexCoord2f(source.x / texture_width, (source.y + source.height) / texture_height);
|
||||||
|
rlVertex3f(x - width/2, y + height/2, z + length/2);
|
||||||
|
|
||||||
|
// Bottom Right Of The Texture and Quad
|
||||||
|
rlTexCoord2f((source.x + source.width) / texture_width, (source.y + source.height) / texture_height);
|
||||||
|
rlVertex3f(x + width/2, y + height/2, z + length/2);
|
||||||
|
|
||||||
|
// Top Right Of The Texture and Quad
|
||||||
|
rlTexCoord2f((source.x + source.width) / texture_width, source.y / texture_height);
|
||||||
|
rlVertex3f(x + width/2, y + height/2, z - length/2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bottom Face
|
||||||
|
{
|
||||||
|
// Normal Pointing Down
|
||||||
|
rlNormal3f(0.0f, - 1.0f, 0.0f);
|
||||||
|
|
||||||
|
// Top Right Of The Texture and Quad
|
||||||
|
rlTexCoord2f((source.x + source.width) / texture_width, source.y / texture_height);
|
||||||
|
rlVertex3f(x - width/2, y - height/2, z - length/2);
|
||||||
|
|
||||||
|
// Top Left Of The Texture and Quad
|
||||||
|
rlTexCoord2f(source.x / texture_width, source.y / texture_height);
|
||||||
|
rlVertex3f(x + width/2, y - height/2, z - length/2);
|
||||||
|
|
||||||
|
// Bottom Left Of The Texture and Quad
|
||||||
|
rlTexCoord2f(source.x / texture_width, (source.y + source.height) / texture_height);
|
||||||
|
rlVertex3f(x + width/2, y - height/2, z + length/2);
|
||||||
|
|
||||||
|
// Bottom Right Of The Texture and Quad
|
||||||
|
rlTexCoord2f((source.x + source.width) / texture_width, (source.y + source.height) / texture_height);
|
||||||
|
rlVertex3f(x - width/2, y - height/2, z + length/2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Right face
|
||||||
|
{
|
||||||
|
// Normal Pointing Right
|
||||||
|
rlNormal3f(1.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
|
// Bottom Right Of The Texture and Quad
|
||||||
|
rlTexCoord2f((source.x + source.width) / texture_width, (source.y + source.height) / texture_height);
|
||||||
|
rlVertex3f(x + width/2, y - height/2, z - length/2);
|
||||||
|
|
||||||
|
// Top Right Of The Texture and Quad
|
||||||
|
rlTexCoord2f((source.x + source.width) / texture_width, source.y / texture_height);
|
||||||
|
rlVertex3f(x + width/2, y + height/2, z - length/2);
|
||||||
|
|
||||||
|
// Top Left Of The Texture and Quad
|
||||||
|
rlTexCoord2f(source.x / texture_width, source.y / texture_height);
|
||||||
|
rlVertex3f(x + width/2, y + height/2, z + length/2);
|
||||||
|
|
||||||
|
// Bottom Left Of The Texture and Quad
|
||||||
|
rlTexCoord2f(source.x / texture_width, (source.y + source.height) / texture_height);
|
||||||
|
rlVertex3f(x + width/2, y - height/2, z + length/2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Left Face
|
||||||
|
{
|
||||||
|
// Normal Pointing Left
|
||||||
|
rlNormal3f( - 1.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
|
// Bottom Left Of The Texture and Quad
|
||||||
|
rlTexCoord2f(source.x / texture_width, (source.y + source.height) / texture_height);
|
||||||
|
rlVertex3f(x - width/2, y - height/2, z - length/2);
|
||||||
|
|
||||||
|
// Bottom Right Of The Texture and Quad
|
||||||
|
rlTexCoord2f((source.x + source.width) / texture_width, (source.y + source.height) / texture_height);
|
||||||
|
rlVertex3f(x - width/2, y - height/2, z + length/2);
|
||||||
|
|
||||||
|
// Top Right Of The Texture and Quad
|
||||||
|
rlTexCoord2f((source.x + source.width) / texture_width, source.y / texture_height);
|
||||||
|
rlVertex3f(x - width/2, y + height/2, z + length/2);
|
||||||
|
|
||||||
|
// Top Left Of The Texture and Quad
|
||||||
|
rlTexCoord2f(source.x / texture_width, source.y / texture_height);
|
||||||
|
rlVertex3f(x - width/2, y + height/2, z - length/2);
|
||||||
|
}
|
||||||
|
rlEnd();
|
||||||
|
|
||||||
|
rlSetTexture(0);
|
||||||
|
}
|
||||||
|
|
||||||
// Draw sphere
|
// Draw sphere
|
||||||
void DrawSphere(Vector3 centerPos, float radius, Color color)
|
void DrawSphere(Vector3 centerPos, float radius, Color color)
|
||||||
{
|
{
|
||||||
|
|
@ -2424,7 +2575,7 @@ Mesh GenMeshHeightmap(Image heightmap, Vector3 size)
|
||||||
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
|
||||||
int nCounter = 0; // Used to count normals float by float
|
int nCounter = 0; // Used to count normals float by float
|
||||||
|
|
||||||
int trisCounter = 0;
|
int trisCounter = 0;
|
||||||
|
|
||||||
Vector3 scaleFactor = { size.x/mapX, size.y/255.0f, size.z/mapZ };
|
Vector3 scaleFactor = { size.x/mapX, size.y/255.0f, size.z/mapZ };
|
||||||
|
|
@ -3100,7 +3251,7 @@ void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle source, Vector
|
||||||
{
|
{
|
||||||
// NOTE: Billboard locked on axis-Y
|
// NOTE: Billboard locked on axis-Y
|
||||||
Vector3 up = { 0.0f, 1.0f, 0.0f };
|
Vector3 up = { 0.0f, 1.0f, 0.0f };
|
||||||
|
|
||||||
DrawBillboardPro(camera, texture, source, position, up, size, Vector2Zero(), 0.0f, tint);
|
DrawBillboardPro(camera, texture, source, position, up, size, Vector2Zero(), 0.0f, tint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -5540,7 +5691,7 @@ static Model LoadVOX(const char *fileName)
|
||||||
int meshescount = 0;
|
int meshescount = 0;
|
||||||
unsigned int readed = 0;
|
unsigned int readed = 0;
|
||||||
unsigned char* fileData;
|
unsigned char* fileData;
|
||||||
|
|
||||||
//Read vox file into buffer
|
//Read vox file into buffer
|
||||||
fileData = LoadFileData(fileName, &readed);
|
fileData = LoadFileData(fileName, &readed);
|
||||||
if (fileData == 0)
|
if (fileData == 0)
|
||||||
|
|
@ -5604,7 +5755,7 @@ static Model LoadVOX(const char *fileName)
|
||||||
pmesh->vertices = MemAlloc(size);
|
pmesh->vertices = MemAlloc(size);
|
||||||
memcpy(pmesh->vertices, pvertices, size);
|
memcpy(pmesh->vertices, pvertices, size);
|
||||||
|
|
||||||
// Copy indices
|
// Copy indices
|
||||||
// TODO: compute globals indices array
|
// TODO: compute globals indices array
|
||||||
size = voxarray.indices.used * sizeof(unsigned short);
|
size = voxarray.indices.used * sizeof(unsigned short);
|
||||||
pmesh->indices = MemAlloc(size);
|
pmesh->indices = MemAlloc(size);
|
||||||
|
|
|
||||||
|
|
@ -1374,6 +1374,7 @@ RLAPI void DrawCubeV(Vector3 position, Vector3 size, Color color);
|
||||||
RLAPI void DrawCubeWires(Vector3 position, float width, float height, float length, Color color); // Draw cube wires
|
RLAPI void DrawCubeWires(Vector3 position, float width, float height, float length, Color color); // Draw cube wires
|
||||||
RLAPI void DrawCubeWiresV(Vector3 position, Vector3 size, Color color); // Draw cube wires (Vector version)
|
RLAPI void DrawCubeWiresV(Vector3 position, Vector3 size, Color color); // Draw cube wires (Vector version)
|
||||||
RLAPI void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float height, float length, Color color); // Draw cube textured
|
RLAPI void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float height, float length, Color color); // Draw cube textured
|
||||||
|
RLAPI void DrawCubeTextureRec(Texture2D texture, Rectangle source, Vector3 position, float width, float height, float length, Color color); // Draw cube with a region of a texture
|
||||||
RLAPI void DrawSphere(Vector3 centerPos, float radius, Color color); // Draw sphere
|
RLAPI void DrawSphere(Vector3 centerPos, float radius, Color color); // Draw sphere
|
||||||
RLAPI void DrawSphereEx(Vector3 centerPos, float radius, int rings, int slices, Color color); // Draw sphere with extended parameters
|
RLAPI void DrawSphereEx(Vector3 centerPos, float radius, int rings, int slices, Color color); // Draw sphere with extended parameters
|
||||||
RLAPI void DrawSphereWires(Vector3 centerPos, float radius, int rings, int slices, Color color); // Draw sphere wires
|
RLAPI void DrawSphereWires(Vector3 centerPos, float radius, int rings, int slices, Color color); // Draw sphere wires
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user