Skip to content

Instantly share code, notes, and snippets.

@chrisdill
Created January 19, 2021 22:30
Show Gist options
  • Save chrisdill/09de7c818bc8618e07d8d41174704fee to your computer and use it in GitHub Desktop.
Save chrisdill/09de7c818bc8618e07d8d41174704fee to your computer and use it in GitHub Desktop.
Drawing a cube with Rlgl using 2 textures
using System.Numerics;
using Raylib_cs;
namespace Testing
{
public class CubeTexture1
{
// Draw cube with multiple textures for different faces
// NOTE: Cube position is the center position
static void DrawCubeCustom(Texture2D texture1, Texture2D texture2, Vector3 position, float width, float height, float length, Color color)
{
float x = position.X;
float y = position.Y;
float z = position.Z;
if (Rlgl.rlCheckBufferLimit(36))
{
Rlgl.rlglDraw();
}
Rlgl.rlBegin(Rlgl.RL_QUADS);
Rlgl.rlColor4ub(color.r, color.g, color.b, color.a);
// NOTE: Enable texture 1 for Front, Back
Rlgl.rlEnableTexture(texture1.id);
// Front Face
// Normal Pointing Towards Viewer
Rlgl.rlNormal3f(0.0f, 0.0f, 1.0f);
// Bottom Left Of The Texture and Quad
Rlgl.rlTexCoord2f(0.0f, 0.0f);
Rlgl.rlVertex3f(x - width / 2, y - height / 2, z + length / 2);
// Bottom Right Of The Texture and Quad
Rlgl.rlTexCoord2f(1.0f, 0.0f);
Rlgl.rlVertex3f(x + width / 2, y - height / 2, z + length / 2);
// Top Right Of The Texture and Quad
Rlgl.rlTexCoord2f(1.0f, 1.0f);
Rlgl.rlVertex3f(x + width / 2, y + height / 2, z + length / 2);
// Top Left Of The Texture and Quad
Rlgl.rlTexCoord2f(0.0f, 1.0f);
Rlgl.rlVertex3f(x - width / 2, y + height / 2, z + length / 2);
// Back Face
// Normal Pointing Away From Viewer
Rlgl.rlNormal3f(0.0f, 0.0f, -1.0f);
// Bottom Right Of The Texture and Quad
Rlgl.rlTexCoord2f(1.0f, 0.0f);
Rlgl.rlVertex3f(x - width / 2, y - height / 2, z - length / 2);
// Top Right Of The Texture and Quad
Rlgl.rlTexCoord2f(1.0f, 1.0f);
Rlgl.rlVertex3f(x - width / 2, y + height / 2, z - length / 2);
// Top Left Of The Texture and Quad
Rlgl.rlTexCoord2f(0.0f, 1.0f);
Rlgl.rlVertex3f(x + width / 2, y + height / 2, z - length / 2);
// Bottom Left Of The Texture and Quad
Rlgl.rlTexCoord2f(0.0f, 0.0f);
Rlgl.rlVertex3f(x + width / 2, y - height / 2, z - length / 2);
// NOTE: Enable texture 2 for Top, Bottom, Left and Right
Rlgl.rlEnableTexture(texture2.id);
// Top Face
// Normal Pointing Up
Rlgl.rlNormal3f(0.0f, 1.0f, 0.0f);
// Top Left Of The Texture and Quad
Rlgl.rlTexCoord2f(0.0f, 1.0f);
Rlgl.rlVertex3f(x - width / 2, y + height / 2, z - length / 2);
// Bottom Left Of The Texture and Quad
Rlgl.rlTexCoord2f(0.0f, 0.0f);
Rlgl.rlVertex3f(x - width / 2, y + height / 2, z + length / 2);
// Bottom Right Of The Texture and Quad
Rlgl.rlTexCoord2f(1.0f, 0.0f);
Rlgl.rlVertex3f(x + width / 2, y + height / 2, z + length / 2);
// Top Right Of The Texture and Quad
Rlgl.rlTexCoord2f(1.0f, 1.0f);
Rlgl.rlVertex3f(x + width / 2, y + height / 2, z - length / 2);
// Bottom Face
// Normal Pointing Down
Rlgl.rlNormal3f(0.0f, -1.0f, 0.0f);
// Top Right Of The Texture and Quad
Rlgl.rlTexCoord2f(1.0f, 1.0f);
Rlgl.rlVertex3f(x - width / 2, y - height / 2, z - length / 2);
// Top Left Of The Texture and Quad
Rlgl.rlTexCoord2f(0.0f, 1.0f);
Rlgl.rlVertex3f(x + width / 2, y - height / 2, z - length / 2);
// Bottom Left Of The Texture and Quad
Rlgl.rlTexCoord2f(0.0f, 0.0f);
Rlgl.rlVertex3f(x + width / 2, y - height / 2, z + length / 2);
// Bottom Right Of The Texture and Quad
Rlgl.rlTexCoord2f(1.0f, 0.0f);
Rlgl.rlVertex3f(x - width / 2, y - height / 2, z + length / 2);
// Right face
// Normal Pointing Right
Rlgl.rlNormal3f(1.0f, 0.0f, 0.0f);
// Bottom Right Of The Texture and Quad
Rlgl.rlTexCoord2f(1.0f, 0.0f);
Rlgl.rlVertex3f(x + width / 2, y - height / 2, z - length / 2);
// Top Right Of The Texture and Quad
Rlgl.rlTexCoord2f(1.0f, 1.0f);
Rlgl.rlVertex3f(x + width / 2, y + height / 2, z - length / 2);
// Top Left Of The Texture and Quad
Rlgl.rlTexCoord2f(0.0f, 1.0f);
Rlgl.rlVertex3f(x + width / 2, y + height / 2, z + length / 2);
// Bottom Left Of The Texture and Quad
Rlgl.rlTexCoord2f(0.0f, 0.0f);
Rlgl.rlVertex3f(x + width / 2, y - height / 2, z + length / 2);
// Left Face
// Normal Pointing Left
Rlgl.rlNormal3f(-1.0f, 0.0f, 0.0f);
// Bottom Left Of The Texture and Quad
Rlgl.rlTexCoord2f(0.0f, 0.0f);
Rlgl.rlVertex3f(x - width / 2, y - height / 2, z - length / 2);
// Bottom Right Of The Texture and Quad
Rlgl.rlTexCoord2f(1.0f, 0.0f);
Rlgl.rlVertex3f(x - width / 2, y - height / 2, z + length / 2);
// Top Right Of The Texture and Quad
Rlgl.rlTexCoord2f(1.0f, 1.0f);
Rlgl.rlVertex3f(x - width / 2, y + height / 2, z + length / 2);
// Top Left Of The Texture and Quad
Rlgl.rlTexCoord2f(0.0f, 1.0f);
Rlgl.rlVertex3f(x - width / 2, y + height / 2, z - length / 2);
Rlgl.rlEnd();
Rlgl.rlDisableTexture();
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
Raylib.SetConfigFlags(ConfigFlag.FLAG_MSAA_4X_HINT);
Raylib.InitWindow(800, 450, "CubemapTexture1");
// Define the camera to look into our 3d world
Camera3D camera = new Camera3D
{
position = new Vector3(10.0f, 10.0f, 10.0f),
target = new Vector3(0.0f, 0.0f, 0.0f),
up = new Vector3(0.0f, 1.0f, 0.0f),
fovy = 45.0f,
type = CameraType.CAMERA_PERSPECTIVE,
};
Vector3 cubePosition = new Vector3(0.0f, 0.0f, 0.0f);
Texture2D texture1 = Raylib.LoadTexture("data/container2.png");
Texture2D texture2 = Raylib.LoadTexture("data/awesomeface.png");
Raylib.SetCameraMode(camera, CameraMode.CAMERA_ORBITAL); // Set a free camera mode
Raylib.SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!Raylib.WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
Raylib.UpdateCamera(ref camera); // Update camera
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
Raylib.BeginDrawing();
Raylib.ClearBackground(new Color(51, 76, 76, 255));
Raylib.BeginMode3D(camera);
DrawCubeCustom(texture1, texture2, cubePosition, 2.0f, 2.0f, 2.0f, Color.WHITE);
Raylib.DrawGrid(10, 1.0f);
Raylib.EndMode3D();
Raylib.EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
Raylib.UnloadTexture(texture1);
Raylib.UnloadTexture(texture2);
Raylib.CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
}
}
@chrisdill
Copy link
Author

CubeTexture1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment