markaren / threepp

C++20 port of three.js (r129)
MIT License
629 stars 61 forks source link

depth test not working with custom projection matrix #208

Closed longsamaa closed 9 months ago

longsamaa commented 10 months ago

I already have a projection matrix. For each render I want to use threepp to render the model with that projection matrix. I reassigned the camera's projection matrix like this:

main_camera->projectionMatrix = matrix;
main_render->render(*main_scene, *main_camera);

Untitled

My model was rendering properly on screen. But it seems the depth test doesn't work. Is there any way for me to fix this ? Thanks for help!

markaren commented 10 months ago

Are you sure the issue is related the projectionMatrix? I can't immediately see anything in the code that should affect this based on that value. Could it be the material settings? How does it look with the PerspectiveCamera?

longsamaa commented 10 months ago

Are you sure the issue is related the projectionMatrix? I can't immediately see anything in the code that should affect this based on that value. Could it be the material settings? How does it look with the PerspectiveCamera?

I just reassigned the projection matrix to the camera. Let threepp render my model like this: https://docs.mapbox.com/mapbox-gl-js/example/add-3d-model/ Do I need to edit anything to make threepp render correctly? Thank you!

markaren commented 10 months ago

I really can't say. The example provided isn't plain three.js either (external library / custom shaders etc.), so you'd have to share your code I think.

longsamaa commented 10 months ago

yep sure!! i use threepp with maplibre native with my custom layer. I initialize scene and camera in my layer like this:

main_render = std::make_shared<threepp::GLRenderer>(window_size); // size of maplibre native window 
main_render->autoClear = false; 
main_render->checkShaderErrors = true; 
main_camera = std::make_shared<threepp::Camera>(); 
main_scene = threepp::Scene::create(); 
main_obj = loader.load("\\data\\models\\collada\\youbot.dae");
main_obj->rotateX(M_PI * 0.5); 

and when rendering I use this:

 auto matrix = par.matrix // matrix of tile maplibre
 threepp::Matrix4 m; 
 m.fromArray(matrix.data()); 
 main_render->resetState(); 
 main_camera->projectionMatrix = m;
 main_render->render(*main_scene, *main_camera);

My model has the correct position on the tile. very interesting but it seems the dept test has an error like this: image

markaren commented 10 months ago

You have autoClear = false, but I don't see any clearing of the threepp rendering?

longsamaa commented 10 months ago

yeah I tried clearing of the threepp rendering before render:

main_render->clearDepth(); 
main_render->clearStencil();

But it still doesn't work

markaren commented 10 months ago

Could you share more complete example code (or complete proeject) that highlights the issue?

longsamaa commented 10 months ago

Yes, how can I share source code with you?

markaren commented 10 months ago

You could zip the project to me by mail (visible on my profile)

longsamaa commented 9 months ago

sorry for the late reply!!. I get a error depth when when I use threepp with 1 available gl context. When i use Threepp::Canvas to create opengl context then threepp renders correctly.

markaren commented 9 months ago

Are you saying the issue was related to using a custom Window/OpenGL context?

Might I ask what you used?

longsamaa commented 9 months ago

tks for help!. I can fix it by turning off glCullface when render. My scene can render correctly. Nice project!!