Jump to content

[Solved] Rendering a quad that always faces the camera


Melonslise

Recommended Posts

7 hours ago, Melonslise said:

I'm trying to render a quad that always faces the player in the RenderWorldLastEvent. The code either fails to render completely or sometimes flickers in and out of view whenever the player moves. Can someone help?

Look at render snowball

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

8 hours ago, Cadiboo said:

Look at render snowball

I tried adding and playing around with the code from RenderSnowball, but it gave really weird results. I also feel like the rotation code from RenderSnowball won't work with a long beam of sorts that I'm trying to make.

Here's my code:

 

 


public static void drawLine(double startX, double startY, double startZ, double endX, double endY, double endZ, int red, int green, int blue, int alpha, double width, float partialTick)
{
	Minecraft mc = Minecraft.getMinecraft();

	// TODO RenderPos from RenderManager
	double
	originX = mc.player.lastTickPosX + (mc.player.posX - mc.player.lastTickPosX) * (double) partialTick,
	originY = mc.player.lastTickPosY + (mc.player.posY - mc.player.lastTickPosY) * (double) partialTick,
	originZ = mc.player.lastTickPosZ + (mc.player.posZ - mc.player.lastTickPosZ) * (double) partialTick;

	Tessellator tessellator = Tessellator.getInstance();
	BufferBuilder buffer = tessellator.getBuffer();

	GlStateManager.pushMatrix();
	GlStateManager.disableCull();
	GlStateManager.disableTexture2D();

	GlStateManager.rotate(-mc.getRenderManager().playerViewY, 0F, 1F, 0F);
	GlStateManager.rotate(mc.getRenderManager().options.thirdPersonView == 2 ? -1F : 1F * mc.getRenderManager().playerViewX, 1F, 0F, 0F);
	GlStateManager.rotate(180F, 0F, 1F, 0F);

	buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_COLOR);
	buffer.pos(startX - originX, startY - originY - width / 2D, startZ - originZ).color(red, green, blue, alpha).endVertex();
	buffer.pos(endX - originX, endY - originY - width / 2D, endZ - originZ).color(red, green, blue, alpha).endVertex();
	buffer.pos(endX- originX, endY - originY + width / 2D, endZ - originZ).color(red, green, blue, alpha).endVertex();
	buffer.pos(startX - originX, startY - originY + width / 2D, startZ - originZ).color(red, green, blue, alpha).endVertex();
	tessellator.draw();

	GlStateManager.enableTexture2D();
	GlStateManager.enableCull();

	GlStateManager.popMatrix();
}

 

Edited by Melonslise
Link to comment
Share on other sites

What exactly are you trying to do?

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

I got it!

Here's the code if anyone is interested:

 

public static void drawLine(Vec3d start, Vec3d end, int red, int green, int blue, int alpha, double width, float partialTick)
{
	Minecraft mc = Minecraft.getMinecraft();

	// TODO RenderPos from RenderManager?
	Vec3d origin = new Vec3d(mc.player.lastTickPosX + (mc.player.posX - mc.player.lastTickPosX) * (double) partialTick, mc.player.lastTickPosY + (mc.player.posY - mc.player.lastTickPosY) * (double) partialTick, mc.player.lastTickPosZ + (mc.player.posZ - mc.player.lastTickPosZ) * (double) partialTick);

	Vec3d camera = mc.player.getPositionEyes(partialTick);
	Vec3d direction = end.subtract(start).normalize();
	Vec3d startUp = direction.crossProduct(start.subtract(camera)).normalize().scale(width);
	Vec3d endUp = direction.crossProduct(end.subtract(camera)).normalize().scale(width);

	Tessellator tessellator = Tessellator.getInstance();
	BufferBuilder buffer = tessellator.getBuffer();

	GlStateManager.pushMatrix();
	GlStateManager.disableCull();
	GlStateManager.disableTexture2D();

	buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_COLOR);
	buffer.pos(start.x + startUp.x - origin.x, start.y + startUp.y - origin.y, start.z + startUp.z - origin.z).color(red, green, blue, alpha).endVertex();
	buffer.pos(start.x - startUp.x - origin.x, start.y - startUp.y - origin.y, start.z - startUp.z - origin.z).color(red, green, blue, alpha).endVertex();
	buffer.pos(end.x - endUp.x - origin.x, end.y - endUp.y - origin.y, end.z - endUp.z - origin.z).color(red, green, blue, alpha).endVertex();
	buffer.pos(end.x + endUp.x - origin.x, end.y + endUp.y - origin.y, end.z + endUp.z - origin.z).color(red, green, blue, alpha).endVertex();
	tessellator.draw();

	GlStateManager.enableTexture2D();
	GlStateManager.enableCull();

	GlStateManager.popMatrix();
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.