Résolu Rendu 3D dans le monde
-
Bonjour,
J’aimerais faire un rendu d’une forme 3D dans le monde.
Seulement elle ne s’affiche pas.
Voilà les classes/fonctions en question:
@SubscribeEvent public void onRenderWorld(RenderWorldLastEvent event) { IRenderTypeBuffer.Impl buffer = Minecraft.getInstance().renderBuffers().bufferSource(); ActiveRenderInfo activerenderinfo = Minecraft.getInstance().gameRenderer.getMainCamera(); Vector3d cameraPosition = activerenderinfo.getPosition(); BlockPos pos = new BlockPos(0, 0, 0); event.getMatrixStack().pushPose(); event.getMatrixStack().translate( cameraPosition.x - pos.getX(), cameraPosition.y - pos.getY(), cameraPosition.z - pos.getZ() ); torusRenderer.render(event.getMatrixStack(), buffer, event.getPartialTicks()); event.getMatrixStack().popPose(); buffer.endBatch(RenderType.lines()); }
Classe de la forme 3D
public class TorusRenderer extends WorldShapeRenderer { @Override public void render(MatrixStack matrices, IRenderTypeBuffer renderTypeBuffer, float partialTicks) { IVertexBuilder vertexBuilderLines = renderTypeBuffer.getBuffer(RenderType.lines()); Vector3d cameraPosition = Minecraft.getInstance().gameRenderer.getMainCamera().getPosition(); BlockPos worldPosition = new BlockPos(10, 10, 10); Vector3f shapePosition = new Vector3f( (float) cameraPosition.x + (float) worldPosition.getX(), (float) cameraPosition.y + (float) worldPosition.getY(), (float) cameraPosition.z + (float) worldPosition.getZ() ); this.setPosition(worldPosition); matrices.pushPose(); //matrices.last().pose().translate(shapePosition); putTorus(Math.abs(Math.cos((float) 90 / 2 + partialTicks * Math.PI / 100)) + 0.5, 10, 32, vertexBuilderLines ); matrices.popPose(); } void putTorus(double R, int nsides, int rings, IVertexBuilder buffer) { double r, nextR; double ringDelta = (Math.PI * 2) / rings; double sideDelta = (Math.PI * 2) / nsides; double theta = 0.0f, cosTheta = 1.0f, sinTheta = 0.0f; for (int i = rings - 1; i >= 0; i--) { double theta1 = theta + ringDelta; double cosTheta1 = Math.cos(theta1); double sinTheta1 = Math.sin(theta1); double phi = 0.0f; r = R; nextR = R; for (int j = nsides; j >= 0; j--) { phi += sideDelta; double cosPhi = Math.cos(phi); double sinPhi = Math.sin(phi); double dist = R + nextR * cosPhi; buffer.vertex(getPosition().getX() + cosTheta1 * dist, getPosition().getY() + nextR * sinPhi, getPosition().getZ() - sinTheta1 * dist) .color(255, 0, 0, 255) .endVertex(); dist = R + r * cosPhi; buffer.vertex(getPosition().getX() + cosTheta * dist, getPosition().getY() + r * sinPhi, getPosition().getZ() - sinTheta * dist) .color(255, 0, 0, 255) .endVertex(); } theta = theta1; cosTheta = cosTheta1; sinTheta = sinTheta1; } } }
||
Merci pour votre aide par avance.
-
Salut, j’ai essayé quelques manips’ de mon côté qui n’ont été que très peu concluantes.
Au final j’ai réussi à faire marcher le truc sans utiliser la MatrixStack.
Et je me suis un petit peu permis de modifier quelques trucs par-ci par-là…
La classe de l’appel de l’event :
@SubscribeEvent public void onRenderWorld(RenderWorldLastEvent event) { RenderSystem.pushMatrix(); // VIEW MATRIX MULT ActiveRenderInfo activerenderinfo = Minecraft.getInstance().gameRenderer.getMainCamera(); RenderSystem.rotatef(activerenderinfo.getXRot(), 1.0f, 0.0f, 0.0f); RenderSystem.rotatef(activerenderinfo.getYRot() + 180.0f, 0.0f, 1.0f, 0.0f); Vector3d view = activerenderinfo.getPosition().reverse(); RenderSystem.translated(view.x, view.y, view.z); RenderSystem.enableBlend(); RenderSystem.defaultBlendFunc(); RenderSystem.enableAlphaTest(); RenderSystem.defaultAlphaFunc(); RenderSystem.enableDepthTest(); RenderSystem.disableTexture(); Tessellator tessellator = Tessellator.getInstance(); IRenderTypeBuffer buffer = IRenderTypeBuffer.immediate(tessellator.getBuilder()); this.torusRenderer.render(new MatrixStack(), buffer, event.getPartialTicks()); tessellator.end(); RenderSystem.disableBlend(); RenderSystem.disableAlphaTest(); RenderSystem.enableTexture(); RenderSystem.popMatrix(); }
La classe du rendu : (j’ai par cherché à comprendre l’algorithme mais franchement c’est plutôt joli !)
public class TorusRenderer { @Override public void render(MatrixStack matrices, IRenderTypeBuffer renderTypeBuffer, float partialTicks) { Vector3d pos = Minecraft.getInstance().gameRenderer.getMainCamera().getPosition(); IVertexBuilder vertexBuilderLines = renderTypeBuffer.getBuffer(RenderType.lines()); BlockPos worldPosition = new BlockPos(10, 10, 10); Vector3d wpvec3 = new Vector3d(worldPosition.getX(), worldPosition.getY(), worldPosition.getZ()); if(pos.distanceTo(wpvec3) > Minecraft.getInstance().gameRenderer.getRenderDistance()) return; this.setPosition(worldPosition); putTorus(Math.abs(Math.cos((float) 90 / 2 * Math.PI / 100)) + 0.5, 10, 32, vertexBuilderLines ); } private void putTorus(double R, int nsides, int rings, IVertexBuilder buffer) { double r, nextR; double ringDelta = (Math.PI * 2) / rings; double sideDelta = (Math.PI * 2) / nsides; double theta = 0.0f, cosTheta = 1.0f, sinTheta = 0.0f; for (int i = rings - 1; i >= 0; i--) { double theta1 = theta + ringDelta; double cosTheta1 = Math.cos(theta1); double sinTheta1 = Math.sin(theta1); double phi = 0.0f; r = R; nextR = R; for (int j = nsides; j >= 0; j--) { phi += sideDelta; double cosPhi = Math.cos(phi); double sinPhi = Math.sin(phi); double dist = R + nextR * cosPhi; buffer.vertex( getPosition().getX() + cosTheta1 * dist, getPosition().getY() + nextR * sinPhi, getPosition().getZ() + sinTheta1 * dist) .color(255, 0, 0, 255) .endVertex(); dist = R + r * cosPhi; buffer.vertex(getPosition().getX() + cosTheta * dist, getPosition().getY() + r * sinPhi, getPosition().getZ() + sinTheta * dist) .color(255, 0, 0, 255) .endVertex(); } theta = theta1; cosTheta = cosTheta1; sinTheta = sinTheta1; } } }
-
Salut, j’ai essayé quelques manips’ de mon côté qui n’ont été que très peu concluantes.
Au final j’ai réussi à faire marcher le truc sans utiliser la MatrixStack.
Et je me suis un petit peu permis de modifier quelques trucs par-ci par-là…
La classe de l’appel de l’event :
@SubscribeEvent public void onRenderWorld(RenderWorldLastEvent event) { RenderSystem.pushMatrix(); // VIEW MATRIX MULT ActiveRenderInfo activerenderinfo = Minecraft.getInstance().gameRenderer.getMainCamera(); RenderSystem.rotatef(activerenderinfo.getXRot(), 1.0f, 0.0f, 0.0f); RenderSystem.rotatef(activerenderinfo.getYRot() + 180.0f, 0.0f, 1.0f, 0.0f); Vector3d view = activerenderinfo.getPosition().reverse(); RenderSystem.translated(view.x, view.y, view.z); RenderSystem.enableBlend(); RenderSystem.defaultBlendFunc(); RenderSystem.enableAlphaTest(); RenderSystem.defaultAlphaFunc(); RenderSystem.enableDepthTest(); RenderSystem.disableTexture(); Tessellator tessellator = Tessellator.getInstance(); IRenderTypeBuffer buffer = IRenderTypeBuffer.immediate(tessellator.getBuilder()); this.torusRenderer.render(new MatrixStack(), buffer, event.getPartialTicks()); tessellator.end(); RenderSystem.disableBlend(); RenderSystem.disableAlphaTest(); RenderSystem.enableTexture(); RenderSystem.popMatrix(); }
La classe du rendu : (j’ai par cherché à comprendre l’algorithme mais franchement c’est plutôt joli !)
public class TorusRenderer { @Override public void render(MatrixStack matrices, IRenderTypeBuffer renderTypeBuffer, float partialTicks) { Vector3d pos = Minecraft.getInstance().gameRenderer.getMainCamera().getPosition(); IVertexBuilder vertexBuilderLines = renderTypeBuffer.getBuffer(RenderType.lines()); BlockPos worldPosition = new BlockPos(10, 10, 10); Vector3d wpvec3 = new Vector3d(worldPosition.getX(), worldPosition.getY(), worldPosition.getZ()); if(pos.distanceTo(wpvec3) > Minecraft.getInstance().gameRenderer.getRenderDistance()) return; this.setPosition(worldPosition); putTorus(Math.abs(Math.cos((float) 90 / 2 * Math.PI / 100)) + 0.5, 10, 32, vertexBuilderLines ); } private void putTorus(double R, int nsides, int rings, IVertexBuilder buffer) { double r, nextR; double ringDelta = (Math.PI * 2) / rings; double sideDelta = (Math.PI * 2) / nsides; double theta = 0.0f, cosTheta = 1.0f, sinTheta = 0.0f; for (int i = rings - 1; i >= 0; i--) { double theta1 = theta + ringDelta; double cosTheta1 = Math.cos(theta1); double sinTheta1 = Math.sin(theta1); double phi = 0.0f; r = R; nextR = R; for (int j = nsides; j >= 0; j--) { phi += sideDelta; double cosPhi = Math.cos(phi); double sinPhi = Math.sin(phi); double dist = R + nextR * cosPhi; buffer.vertex( getPosition().getX() + cosTheta1 * dist, getPosition().getY() + nextR * sinPhi, getPosition().getZ() + sinTheta1 * dist) .color(255, 0, 0, 255) .endVertex(); dist = R + r * cosPhi; buffer.vertex(getPosition().getX() + cosTheta * dist, getPosition().getY() + r * sinPhi, getPosition().getZ() + sinTheta * dist) .color(255, 0, 0, 255) .endVertex(); } theta = theta1; cosTheta = cosTheta1; sinTheta = sinTheta1; } } }
-
@twiguinou Nickel ! Un grand merci !
Bon maintenant faut que j’apprenne les maths, et que je fasse ça proprement !
Encore merci poulet !
-