c++ - Draw transparent 3d object in raylib - Stack Overflow

I am new to c++ and raylib. The problem I am having is that when I try to draw a transparent cube or an

I am new to c++ and raylib. The problem I am having is that when I try to draw a transparent cube or any 3d objects, it will use the ClearBackground color instead.

Here is a simple example:

#include <raylib.h>

int main() {

    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera first person");

    Camera3D cam = { -2 ,2 ,0 };
    cam.fovy = 90;
    cam.up = (Vector3){ 0 ,1 ,0 };
    cam.target = (Vector3){ 0, 0, 0 };

    while (!WindowShouldClose())
    {
        // --- Update ---
        UpdateCamera(&cam, CAMERA_ORBITAL);

        // --- Draw ---
        BeginDrawing();

            ClearBackground(GRAY);

            BeginMode3D(cam);

                DrawSphere((Vector3){ -1, 0 ,0 }, .5, BLANK);
                DrawCubeWiresV((Vector3){ -1, 0 ,0 }, (Vector3){ 1, 1, 1 }, RED);
                DrawCubeWiresV((Vector3){ 1, 0 ,0 }, (Vector3){ 1, 1, 1 }, RED);

            EndMode3D();

        EndDrawing();
    }
    
    CloseWindow();

    return 0;
}

As you can see there is a "Sphere" with "BLANK" color. If you run the project you can see that the sphere is clearing every object behind it, so you can say "The sphere is using the background color instead of not being drawn".

NOTE: I want this because I want to use a semi-transparent color for glass in my game

I am new to c++ and raylib. The problem I am having is that when I try to draw a transparent cube or any 3d objects, it will use the ClearBackground color instead.

Here is a simple example:

#include <raylib.h>

int main() {

    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera first person");

    Camera3D cam = { -2 ,2 ,0 };
    cam.fovy = 90;
    cam.up = (Vector3){ 0 ,1 ,0 };
    cam.target = (Vector3){ 0, 0, 0 };

    while (!WindowShouldClose())
    {
        // --- Update ---
        UpdateCamera(&cam, CAMERA_ORBITAL);

        // --- Draw ---
        BeginDrawing();

            ClearBackground(GRAY);

            BeginMode3D(cam);

                DrawSphere((Vector3){ -1, 0 ,0 }, .5, BLANK);
                DrawCubeWiresV((Vector3){ -1, 0 ,0 }, (Vector3){ 1, 1, 1 }, RED);
                DrawCubeWiresV((Vector3){ 1, 0 ,0 }, (Vector3){ 1, 1, 1 }, RED);

            EndMode3D();

        EndDrawing();
    }
    
    CloseWindow();

    return 0;
}

As you can see there is a "Sphere" with "BLANK" color. If you run the project you can see that the sphere is clearing every object behind it, so you can say "The sphere is using the background color instead of not being drawn".

NOTE: I want this because I want to use a semi-transparent color for glass in my game

Share Improve this question edited Jan 31 at 11:28 Christoph Rackwitz 15.9k5 gold badges39 silver badges51 bronze badges asked Jan 31 at 9:17 sina kasesazsina kasesaz 356 bronze badges 1
  • I imagine you need to enable alpha blending – Alan Birtles Commented Jan 31 at 9:45
Add a comment  | 

1 Answer 1

Reset to default 2

Alpha blending is already enabled in raylib (check the BeginBlendMode function).

To fix your problem you can just move the sphere (or any transparent object) later in the draw order. I'm not sure why this works but it might be that raylib bases the transparency on what has been already been drawn instead on the depth buffer.

So instead of

DrawSphere((Vector3){ -1, 0 ,0 }, .5, BLANK);
DrawCubeWiresV((Vector3){ -1, 0 ,0 }, (Vector3){ 1, 1, 1 }, RED);
DrawCubeWiresV((Vector3){ 1, 0 ,0 }, (Vector3){ 1, 1, 1 }, RED);

Do

DrawCubeWiresV((Vector3){ -1, 0 ,0 }, (Vector3){ 1, 1, 1 }, RED);
DrawCubeWiresV((Vector3){ 1, 0 ,0 }, (Vector3){ 1, 1, 1 }, RED);
DrawSphere((Vector3){ -1, 0 ,0 }, .5, BLANK);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745272460a4619831.html

相关推荐

  • c++ - Draw transparent 3d object in raylib - Stack Overflow

    I am new to c++ and raylib. The problem I am having is that when I try to draw a transparent cube or an

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信