admin管理员组文章数量:1417549
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
1 Answer
Reset to default 2Alpha 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);
本文标签: cDraw transparent 3d object in raylibStack Overflow
版权声明:本文标题:c++ - Draw transparent 3d object in raylib - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745272459a2650976.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论