Jump to content

[1.12.2] Drawing Cube Through TileEntitySpecialRenderer


unassigned

Recommended Posts

Hello, I have a TESR and I'd like to draw a cube (eventual a sphere, thought I'd start easy) at the location of the TE. I have very limited knowledge of opengl and how minecraft handles it through GlStateManager. I know how to do very basic things, but when it comes down to drawing vertices, I am lost. Here is what I have so far, as well as what I think should go where:

            Tessellator tessy = Tessellator.getInstance();
            BufferBuilder buf = tessy.getBuffer();

            GlStateManager.pushMatrix();

            //translate matrix to te pos
            //set color / rot

            //begin drawing
            //set vertices to bufferbuilder

            //draw tessellator

            GlStateManager.popMatrix();

 

Any pointers would be nice.

 

Thanks.  

Link to comment
Share on other sites

6 hours ago, unassigned said:

opengl and how minecraft handles it through GlStateManager.

GlStateManager just manages the various state parameters of opengl like blend, culling, light, etc. The drawing is not done through GlStateManager.

 

To draw vertices you need to understand that a vertex is just a collection of data in a certain arrangement that defines the properties of a point. Several points make a polygon of a kind(a quad for example).

To draw vertices you first need to start drawing using BufferBuilder#begin. The first parameter is the type of polygons you will be drawing. You can get the constants directly from GL11. The second parameter is the format of your vertices - the order in which the data is arranged. You can get the formats from DefaultVertexFormats and their names are pretty descriptive apart from BLOCK(pos vec3, color vec4b, uv vec2, lightmap vec2s) and ITEM(pos vec3, color vec4b, uv vec2, normal vec3).

Then you can start defining your vertices. They must match the format and the amount must match the amount per polygon * number of polygons(note that you can draw multiple polygons in one draw call). To define a vertex use the methods in BufferBuilder. For example, if you want to draw a simple quad it would need 4 vertices and would look like this(example with the POSITION_TEX format)

bb.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX);
bb.pos(xs, ys, z).tex(us, vs).endVertex();
bb.pos(xs, ye, z).tex(us, ve).endVertex();
bb.pos(xe, ye, z).tex(ue, ve).endVertex();
bb.pos(xe, ys, z).tex(ue, ve).endVertex();
Tessellator.getInstance().draw();

The order in which you define your vertices also matters, because opengl will not render everything that doesn't match the order to save on rendering time - that's culling(if it is enabled). Minecraft usually defines vertices counter-clockwise but you can change that using GlStateManager. Just don't forget to reset it back after you are done.

Link to comment
Share on other sites

 

Okay, thanks for this valuable information. I can now properly draw a square, and I assume I must draw six of them to draw a cube? (Implying that there is no other 'shortcut'). If so, I think I have it down, but I'm getting some weird culling issues and I do not know how I would fix. I tried uploading a gif to demonstrate it, but I guess they're not supported, so here are 2 images of this issue:

 

Here is what it should look like:

06202d6662bddb40dadb07394a312b57.png

 

And here is what it looks like from a small distance:

657bcd3f4c64c64e760744a6833a1dbc.png

 

Here is the code I have, I draw each square separately, which creates for some messy code, but I wanted to visualize each squares vertices.

            Tessellator tessy = Tessellator.getInstance();
            BufferBuilder buf = tessy.getBuffer();

            GlStateManager.pushMatrix();
            GlStateManager.disableCull();
            GlStateManager.disableLighting();

            GlStateManager.alphaFunc(GL11.GL_ALWAYS, 0);
            GlStateManager.translate(x+.5f, y+.5f, z+.5f);

            buf.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION);

            //south side [pos z] [parent x]
            buf.pos(x-0.5f, y+0.5f, z+0.5f).endVertex();
            buf.pos(x-0.5f, y-0.5f, z+0.5f).endVertex();
            buf.pos(x+0.5f, y-0.5f, z+0.5f).endVertex();
            buf.pos(x+0.5f, y+0.5f, z+0.5f).endVertex();

            //north side [neg z] [parent x]
            buf.pos(x-0.5f, y+0.5f, z-0.5f).endVertex();
            buf.pos(x-0.5f, y-0.5f, z-0.5f).endVertex();
            buf.pos(x+0.5f, y-0.5f, z-0.5f).endVertex();
            buf.pos(x+0.5f, y+0.5f, z-0.5f).endVertex();

            //east side [pos x] [parent z]
            buf.pos(x+0.5f, y+0.5f, z-0.5f).endVertex();
            buf.pos(x+0.5f, y-0.5f, z-0.5f).endVertex();
            buf.pos(x+0.5f, y-0.5f, z+0.5f).endVertex();
            buf.pos(x+0.5f, y+0.5f, z+0.5f).endVertex();

            //west side [neg x] [parent z]
            buf.pos(x-0.5f, y-0.5f, z+0.5f).endVertex();
            buf.pos(x-0.5f, y-0.5f, z-0.5f).endVertex();
            buf.pos(x-0.5f, y+0.5f, z-0.5f).endVertex();
            buf.pos(x-0.5f, y+0.5f, z+0.5f).endVertex();

            //top [pos y] [parent x & y]
            buf.pos(x+0.5f, y+0.5f, z-0.5f).endVertex();
            buf.pos(x+0.5f, y+0.5f, z+0.5f).endVertex();
            buf.pos(x-0.5f, y+0.5f, z+0.5f).endVertex();
            buf.pos(x-0.5f, y+0.5f, z-0.5f).endVertex();

            //bottom [neg y] [parent x & y]
            buf.pos(x+0.5f, y-0.5f, z-0.5f).endVertex();
            buf.pos(x+0.5f, y-0.5f, z+0.5f).endVertex();
            buf.pos(x-0.5f, y-0.5f, z+0.5f).endVertex();
            buf.pos(x-0.5f, y-0.5f, z-0.5f).endVertex();

            tessy.draw();

            GlStateManager.enableLighting();
            GlStateManager.enableCull();
            GlStateManager.disableAlpha();
            GlStateManager.popMatrix();

 

Thanks!

 

- edit - I cannot get rid of this video player thing. I guess this happened when I tried to upload the gif.

Edited by unassigned
bug
Link to comment
Share on other sites

2 hours ago, unassigned said:

GlStateManager.translate(x+.5f, y+.5f, z+.5f);

 

2 hours ago, unassigned said:

buf.pos(x-0.5f, y+0.5f, z+0.5f).endVertex();

If you are already translating to x,y,z then there is no need to draw the vertices from x,y,z since they are already at x,y,z.

Link to comment
Share on other sites

16 hours ago, V0idWa1k3r said:

 

If you are already translating to x,y,z then there is no need to draw the vertices from x,y,z since they are already at x,y,z.

Huh, that has fixed it. Why would something like this cause an issue like that? I'd imagine it would just translate it twice.

 

Now, how would I draw a sphere? Would I have to create a high count polygon? Or is there an easier way? As I see no glMode for the bufferbuilder that has anything to do with spherical objects. Also, I have no idea where to even start with drawing vertices for s

Link to comment
Share on other sites

You could generate the sphere's mesh on startup and render it. That would require you to write an algorythm that does that though.

Another option is to create your sphere in any kind of 3d model editor, export it to something that's easy to parse like wavefront, parse it into a collection of vertices and render that.

Link to comment
Share on other sites

7 hours ago, unassigned said:

Now, how would I draw a sphere? Would I have to create a high count polygon? Or is there an easier way? As I see no glMode for the bufferbuilder that has anything to do with spherical objects. Also, I have no idea where to even start with drawing vertices for s

There’s a tutorial out there somewhere on how to draw a sphere with GLStateManager, however it’s pretty bad for performance as it uses OpenGL instead of the BufferBuilder

 

Heres a link, I’ll try and find the actual tutorial https://www.minecraftforum.net/forums/mapping-and-modding-java-edition/minecraft-mods/modification-development/2241469-creating-a-sphere

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

  • 1 year later...
Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

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