Jump to content

Line drawing function?


tf2_mandeokyi

Recommended Posts

I made a function that can draw a line between pos1 and pos2 when the mouse is clicked, but sometimes Minecraft crashes. or instead of crashing, it doesn't draw a line when I click the mouse.

 

import java.awt.Color;

import org.lwjgl.opengl.GL11;

import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.math.Vec3d;

public class draw3dObjects {
	
	public static void drawLine(Vec3d pos1, Vec3d pos2, Color c, boolean smooth, float width) { // This causes crash (or maybe?)
      
		GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
		GL11.glDisable(GL11.GL_CULL_FACE);
		GL11.glDisable(GL11.GL_LIGHTING);
		GL11.glDisable(GL11.GL_TEXTURE_2D);
		
		GL11.glEnable(GL11.GL_BLEND);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

		GL11.glColor4d(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
		GL11.glLineWidth(width);
		GL11.glDepthMask(false);
		Tessellator tessellator = Tessellator.getInstance();
		BufferBuilder bufferBuilder = tessellator.getBuffer();
		bufferBuilder.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION_COLOR);
		
		int r = c.getRed(), g = c.getGreen(), b = c.getBlue(), a = c.getAlpha();
		
		bufferBuilder.pos(pos1.x, pos1.y, pos1.z).color(r, g, b, a).endVertex();
		bufferBuilder.pos(pos2.x, pos2.y, pos2.z).color(r, g, b, a).endVertex();
		
		tessellator.draw();
		
		GL11.glDepthMask(true);
		GL11.glPopAttrib();
      
	}
  
}

 

import java.awt.Color;

import /* Some files... */.draw3dObjects;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.LeftClickEmpty;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class mouseClickEvents {

	@SubscribeEvent
	public void onPlayerClickedLeftButtonAtAir(LeftClickEmpty event) {

		if(event.getEntity() instanceof EntityPlayer) {

			EntityPlayer player = event.getEntityPlayer();
			draw3dObjects.drawLine(player.getPositionVector(), new Vec3d(1,1,1), new Color(255,0,0,100), true, 3);

		}

	}

}

 

Can I put them in a function? What's the problem if I can?

Link to comment
Share on other sites

Ok, so as you said, I changed GL11 to GlStateManager (There was GlStateManager, instead of GLStateManager), and placed that function at RenderGameOverlayEvent (Not at PlayerInteractEvent because I don't know how to make a loop at the inside of it).

But Minecraft still crashes when I open the map. Did I miss something?

 

import java.awt.Color;

import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.math.Vec3d;

public class draw3dObjects {
	
	public static void drawLine(Vec3d pos1, Vec3d pos2, Color c, boolean smooth, float width) {
		GlStateManager.pushMatrix();
		
		int r = c.getRed(), g = c.getGreen(), b = c.getBlue(), a = c.getAlpha();
		
		GlStateManager.color(r, g, b, a);
		GlStateManager.glLineWidth(width);
		GlStateManager.depthMask(smooth);
		
		Tessellator t = Tessellator.getInstance();
		BufferBuilder bBuilder = t.getBuffer();
		//bBuilder.begin(GlStateManager.GL_LINES, DefaultVertexFormats.POSITION_COLOR);  // Is this function useful?
		
		bBuilder.pos(pos1.x, pos1.y, pos1.z).color(r, g, b, a).endVertex();
		bBuilder.pos(pos2.x, pos2.y, pos2.z).color(r, g, b, a).endVertex();
		
		t.draw();
		
		GlStateManager.depthMask(true);
		GlStateManager.popMatrix();
	}
}
import java.awt.Color;

import /* Some files... */.draw3dObjects;

import net.minecraft.util.math.Vec3d;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class gameOverlayEvents {
	
	@SubscribeEvent
	public void renderGame(RenderGameOverlayEvent event) {
		draw3dObjects.drawLine(new Vec3d(5,10,5), new Vec3d(10,10,10), new Color(255,0,0,100), false, 4);
	}
	
}

 

Link to comment
Share on other sites

As you said, I added .Pre after RenderGameOverlayEvent. (but I don't get ElementType what you're going to say is.)

And I found why Minecraft crashed. It's because that I separated functions at other classes.

But the problem now is that function draws line at HUD, not 3D space. 

Any solution?

(btw, GL11 also works well instead of GlStateManager (And for other peoples too), so I changed GlStateManager to GL11.)

import java.awt.Color;

import org.lwjgl.opengl.GL11;

import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class gameOverlayEvents {
	
	@SubscribeEvent
	public void renderGame(RenderGameOverlayEvent.Pre event) {
		
		drawBoundingBox(new Vec3d(5,5,5), new Vec3d(5,10,5), new Color(255,0,0,100), false, 1);
		
	}
	
	public static void drawBoundingBox(Vec3d pos1, Vec3d pos2, Color c, boolean smooth, float width) {
		GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
		GL11.glDisable(GL11.GL_CULL_FACE);
		GL11.glDisable(GL11.GL_LIGHTING);
		GL11.glDisable(GL11.GL_TEXTURE_2D);

		GL11.glEnable(GL11.GL_BLEND);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
		GL11.glTranslated(0, 0, 0);
		
		int r = c.getRed(), g = c.getGreen(), b = c.getBlue(), a = c.getAlpha();
		
		GL11.glColor4d(r, g, b, a);
		GL11.glLineWidth(width);
		GL11.glDepthMask(false);
		Tessellator tessellator = Tessellator.getInstance();
		BufferBuilder bufferBuilder = tessellator.getBuffer();
		bufferBuilder.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION_COLOR);
		
		bufferBuilder.pos(pos1.x, pos1.y, pos1.z).color(r, g, b, a).endVertex(); 
		bufferBuilder.pos(pos2.x, pos2.y, pos2.z).color(r, g, b, a).endVertex(); 
		
		tessellator.draw();
		
		GL11.glDepthMask(true);
		GL11.glPopAttrib();
	}
	
} // I copied this code at "http://www.minecraftforge.net/forum/topic/60740-1121-solved-help-to-draw-line/"

 

Link to comment
Share on other sites

11 hours ago, diesieben07 said:

The methods in GlStateManager are the same as in GL11.

GlStateManager does a bunch of caching and optimisation of the gl methods. If you don’t use it, those caches stop being accurate and you break all other rendering. Aside from this they’re the exact same. If you want to draw in world space you need to do the proper translations. I would use a different event too

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

Ok... I changed (again) GL11 to GlStateManager.

But Minecraft crashes when I use GlStateManager (when opening a map). What is the problem with this code?

package com.tf2_mandeokyi.TF2Mod.events;

import java.awt.Color;

import org.lwjgl.opengl.GL11;

import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class gameOverlayEvents {
	
	@SubscribeEvent
	public void renderGame(RenderWorldLastEvent event) {

		drawBoundingBox(new Vec3d(5,5,5), new Vec3d(5,10,5), new Color(255,0,0,100), false, 1);
		
	}
	
	public static void drawBoundingBox(Vec3d pos1, Vec3d pos2, Color c, boolean smooth, float width) {
		
		GlStateManager.pushAttrib();
		GlStateManager.disableCull();
		GlStateManager.disableLighting();
		GlStateManager.disableTexture2D();
		
		GlStateManager.enableBlend();
		GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
		
		int r = c.getRed(), g = c.getGreen(), b = c.getBlue(), a = c.getAlpha();
		
		GlStateManager.color(r, g, b, a);
		GlStateManager.glLineWidth(width);
		GlStateManager.depthMask(false);
		
		Tessellator t = Tessellator.getInstance();
		BufferBuilder bb = t.getBuffer();
		bb.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION_COLOR);
		
		bb.pos(pos1.x, pos1.y, pos1.z).color(r, g, b, a).endVertex();
		bb.pos(pos2.x, pos2.y, pos2.z).color(r, g, b, a).endVertex();
		
		GlStateManager.depthMask(true);
		GlStateManager.popAttrib();
		
	}
	
}

 

log:

Spoiler

[11:20:03] [main/INFO] [STDOUT]: [net.minecraft.init.Bootstrap:printToSYSOUT:629]: ---- Minecraft Crash Report ----
// Would you like a cupcake?

Time: 2/12/19 11:20 AM
Description: Unexpected error

java.lang.IllegalStateException: Already building!
	at net.minecraft.client.renderer.BufferBuilder.begin(BufferBuilder.java:188)
	at net.minecraft.client.model.TexturedQuad.draw(TexturedQuad.java:66)
	at net.minecraft.client.model.ModelBox.render(ModelBox.java:96)
	at net.minecraft.client.model.ModelRenderer.compileDisplayList(ModelRenderer.java:297)
	at net.minecraft.client.model.ModelRenderer.render(ModelRenderer.java:131)
	at net.minecraft.client.renderer.entity.RenderPlayer.renderRightArm(RenderPlayer.java:210)
	at net.minecraft.client.renderer.ItemRenderer.renderArmFirstPerson(ItemRenderer.java:262)
	at net.minecraft.client.renderer.ItemRenderer.renderItemInFirstPerson(ItemRenderer.java:368)
	at net.minecraft.client.renderer.ItemRenderer.renderItemInFirstPerson(ItemRenderer.java:343)
	at net.minecraft.client.renderer.EntityRenderer.renderHand(EntityRenderer.java:826)
	at net.minecraft.client.renderer.EntityRenderer.renderWorldPass(EntityRenderer.java:1492)
	at net.minecraft.client.renderer.EntityRenderer.renderWorld(EntityRenderer.java:1312)
	at net.minecraft.client.renderer.EntityRenderer.updateCameraAndRender(EntityRenderer.java:1115)
	at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1208)
	at net.minecraft.client.Minecraft.run(Minecraft.java:441)
	at net.minecraft.client.main.Main.main(Main.java:118)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
	at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
	at GradleStart.main(GradleStart.java:25)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Thread: Client thread
Stacktrace:
	at net.minecraft.client.renderer.BufferBuilder.begin(BufferBuilder.java:188)
	at net.minecraft.client.model.TexturedQuad.draw(TexturedQuad.java:66)
	at net.minecraft.client.model.ModelBox.render(ModelBox.java:96)
	at net.minecraft.client.model.ModelRenderer.compileDisplayList(ModelRenderer.java:297)
	at net.minecraft.client.model.ModelRenderer.render(ModelRenderer.java:131)
	at net.minecraft.client.renderer.entity.RenderPlayer.renderRightArm(RenderPlayer.java:210)
	at net.minecraft.client.renderer.ItemRenderer.renderArmFirstPerson(ItemRenderer.java:262)
	at net.minecraft.client.renderer.ItemRenderer.renderItemInFirstPerson(ItemRenderer.java:368)
	at net.minecraft.client.renderer.ItemRenderer.renderItemInFirstPerson(ItemRenderer.java:343)
	at net.minecraft.client.renderer.EntityRenderer.renderHand(EntityRenderer.java:826)
	at net.minecraft.client.renderer.EntityRenderer.renderWorldPass(EntityRenderer.java:1492)
	at net.minecraft.client.renderer.EntityRenderer.renderWorld(EntityRenderer.java:1312)

-- Affected level --
Details:
	Level name: MpServer
	All players: 1 total; [EntityPlayerSP['Player543'/1, l='MpServer', x=8.50, y=65.00, z=8.50]]
	Chunk stats: MultiplayerChunkCache: 0, 0
	Level seed: 0
	Level generator: ID 01 - flat, ver 0. Features enabled: false
	Level generator options: 
	Level spawn location: World: (8,64,8), Chunk: (at 8,4,8 in 0,0; contains blocks 0,0,0 to 15,255,15), Region: (0,0; contains chunks 0,0 to 31,31, blocks 0,0,0 to 511,255,511)
	Level time: 0 game time, 0 day time
	Level dimension: 0
	Level storage version: 0x00000 - Unknown?
	Level weather: Rain time: 0 (now: false), thunder time: 0 (now: false)
	Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: false
	Forced entities: 1 total; [EntityPlayerSP['Player543'/1, l='MpServer', x=8.50, y=65.00, z=8.50]]
	Retry entities: 0 total; []
	Server brand: fml,forge
	Server type: Integrated singleplayer server
Stacktrace:
	at net.minecraft.client.multiplayer.WorldClient.addWorldInfoToCrashReport(WorldClient.java:461)
	at net.minecraft.client.Minecraft.addGraphicsAndWorldToCrashReport(Minecraft.java:2888)
	at net.minecraft.client.Minecraft.run(Minecraft.java:470)
	at net.minecraft.client.main.Main.main(Main.java:118)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
	at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
	at GradleStart.main(GradleStart.java:25)

-- System Details --
Details:
	Minecraft Version: 1.12.2
	Operating System: Windows 10 (amd64) version 10.0
	Java Version: 1.8.0_181, Oracle Corporation
	Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
	Memory: 792185640 bytes (755 MB) / 1037959168 bytes (989 MB) up to 1037959168 bytes (989 MB)
	JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M
	IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0
	FML: MCP 9.42 Powered by Forge 14.23.5.2768 5 mods loaded, 5 mods active
	States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored

	| State     | ID        | Version      | Source                           | Signature |
	|:--------- |:--------- |:------------ |:-------------------------------- |:--------- |
	| UCHIJAAAA | minecraft | 1.12.2       | minecraft.jar                    | None      |
	| UCHIJAAAA | mcp       | 9.42         | minecraft.jar                    | None      |
	| UCHIJAAAA | FML       | 8.0.99.99    | forgeSrc-1.12.2-14.23.5.2768.jar | None      |
	| UCHIJAAAA | forge     | 14.23.5.2768 | forgeSrc-1.12.2-14.23.5.2768.jar | None      |
	| UCHIJAAAA | tf2m      | 1.0          | bin                              | None      |

	Loaded coremods (and transformers): 
	GL info: ' Vendor: 'NVIDIA Corporation' Version: '4.6.0 NVIDIA 391.01' Renderer: 'GeForce GTX 1050 Ti/PCIe/SSE2'
	Launched Version: 1.12.2
	LWJGL: 2.9.4
	OpenGL: GeForce GTX 1050 Ti/PCIe/SSE2 GL version 4.6.0 NVIDIA 391.01, NVIDIA Corporation
	GL Caps: Using GL 1.3 multitexturing.
Using GL 1.3 texture combiners.
Using framebuffer objects because OpenGL 3.0 is supported and separate blending is supported.
Shaders are available because OpenGL 2.1 is supported.
VBOs are available because OpenGL 1.5 is supported.

	Using VBOs: Yes
	Is Modded: Definitely; Client brand changed to 'fml,forge'
	Type: Client (map_client.txt)
	Resource Packs: 
	Current Language: English (US)
	Profiler Position: N/A (disabled)
	CPU: 8x Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
[11:20:03] [Server thread/INFO] [minecraft/MinecraftServer]: Stopping server
[11:20:03] [Server thread/INFO] [minecraft/MinecraftServer]: Saving players
[11:20:03] [main/INFO] [STDOUT]: [net.minecraft.init.Bootstrap:printToSYSOUT:629]: #@!@# Game crashed! Crash report saved to: #@!@# D:\Desktop\some mods\minecraft\TF2 mod\run\.\crash-reports\crash-2019-02-12_11.20.03-client.txt
[11:20:03] [main/INFO] [FML]: Waiting for the server to terminate/save.
[11:20:03] [Server thread/INFO] [minecraft/MinecraftServer]: Saving worlds
[11:20:03] [Server thread/INFO] [minecraft/MinecraftServer]: Saving chunks for level 'tf2 mod'/overworld
[11:20:04] [Server thread/INFO] [FML]: Unloading dimension 0
[11:20:04] [Server thread/INFO] [FML]: Applying holder lookups
[11:20:04] [Server thread/INFO] [FML]: Holder lookups applied
[11:20:04] [main/INFO] [FML]: Server terminated.
[11:20:04] [Client Shutdown Thread/INFO] [minecraft/MinecraftServer]: Stopping server

 

 
Edited by tf2_mandeokyi
Didn't included something
Link to comment
Share on other sites

You never tell the BufferBuilder Tessellator to draw

Edited by Cadiboo

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

Then how do I draw?

 

[Edit] (

I got a code that can draw a line. Thankfully it doesn't crash the game, but it doesn't draw a line...

Before:

Tessellator t = Tessellator.getInstance();
BufferBuilder bb = t.getBuffer();
bb.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION_COLOR);

bb.pos(pos1.x, pos1.y, pos1.z).color(r, g, b, a).endVertex();
bb.pos(pos2.x, pos2.y, pos2.z).color(r, g, b, a).endVertex();

After:

GlStateManager.glBegin(GL11.GL_LINES);
		
GlStateManager.glVertex3f((float)pos1.x, (float)pos1.y, (float)pos1.z);
GlStateManager.glVertex3f((float)pos2.x, (float)pos2.y, (float)pos2.z);

GlStateManager.glEnd();

)

Edited by tf2_mandeokyi
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.



×
×
  • Create New...

Important Information

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