I had a problem programming my first Minecraft Mod.
Here is the source code:
Events.java:
public class Events {
#SubscribeEvent
public void onRenderGameOverlay(RenderGameOverlayEvent event) {
if(!event.isCancelable() && event.type == ElementType.EXPERIENCE && !Minecraft.getMinecraft().thePlayer.capabilities.isCreativeMode) {
int posX = event.resolution.getScaledWidth() / 2 + 10;
int posY = event.resolution.getScaledHeight() - 48;
Minecraft.getMinecraft().renderEngine.bindTexture(new ResourceLocation("tc:textures/gui/thermo-icon.png"));
Minecraft.getMinecraft().ingameGUI.drawTexturedModalRect(posX + 9, posY + 3, 0, 9, 71, 3);
}
}
}
So, my problem is that in Minecraft it shows me this:
Note the red ellipse (I added it with GIMP), inside it there is a black rectangle (I haven't added it with GIMP)... it is too small and without texture...
I followed this tutorial (https://www.youtube.com/watch?v=oi41BAlRjtE), but still not working...
Any solution, please?
UPDATE - - - - - -
Thanks all guys for help, I understood that height = 3 pixel is too small... But now I've one more problem...
The light grey rectangle in the center, should be a
square, and the black part should be a "circle".
Does anyone know the cause of the wrong proportions? Thanks!
Too small?
What size were you expecting?
drawTexturedModalRect(posX + 9, posY + 3, 0, 9, 71, 3);
Is it not 71 pixels long and 3 tall... just what you provided? Its hard to tell. Maybe the texture is working but you are offseting it by 9 pixels(when you only draw 3 pixels of it), but it looks like you are using bindTexture wrong. It takes an int returned from getTexture.
int i = mc.renderEngine.getTexture("/Items/GUI/mixer.png");
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
mc.renderEngine.bindTexture(i);
There is a nice wiki / tutorial on doing the sort of mods you are doing that might help : http://www.minecraftforge.net/wiki/Gui_Overlay#Mod_Code
Related
I am required to write something similar to graphviz. I have already managed to draw the nodes so that they do not overlap and to connect their center with a quadratic Bezier curve, but I am facing the following problem with the directionality of a curved edge:
As you can see, the yellow little triangle is not quite nice. My code is the following:
g.setColor(Color.RED);
ArrowGraph arrow = graph.getArrows().get(0);
int x = (int)(arrow.getP1().getX() + arrow.getP2().getX()) / 2;
int y = (int)(arrow.getP1().getY() + arrow.getP2().getY()) / 2 + 50;
QuadCurve2D.Double curve = new QuadCurve2D.Double(arrow.getP1().getX() - 20, arrow.getP1().getY() - 20, x, y, arrow.getP2().getX() - 20, arrow.getP2().getY() - 20);
((Graphics2D)g).draw(curve);
int[] xPoints = new int[] {(int)arrow.getP2().getX() - 20, (int)arrow.getP2().getX() - 15, (int)arrow.getP2().getX() - 25};
int[] yPoints = new int[] {(int)arrow.getP2().getY() - 20, (int)arrow.getP2().getY() - 10, (int)arrow.getP2().getY() - 10};
g.setColor(Color.YELLOW);
g.fillPolygon(xPoints, yPoints, 3);
The coordinates that I know: the start and end point and the control point of the curve. The start and the end points are basically the centers of the nodes.
I would like to somehow adjust the triangle to the curve, so it's on the end of it (preferably at border of the node). Any idea how I could do that?
You can use FlatteningPathIterator passing your curve and get the last segment (last line on the path in fact).
The use the line to to build the arrow triangle.
I want to use Alpha/Blend mode for future stuff (transactions mainly and possible image blending).
Well, I can't get it to work using LWJGL (GL1.1), I already tried other blend modes but didn't worked, nor changing the background or anything like that...
Screenshots:
http://i.imgur.com/cHU4YGS.png - GL_BLEND always enabled, everything is transparent
http://i.imgur.com/sPmPqne.png - GL_BLEND enabled on QUAD and text, I can see the line that is on disabled GL_BLEND
i imgur com/nkda41v png - GL_BLEND disabled on everything but the text -> I need some reputation to post more than 2 links, sorry about that but I belive this image is important so i'll post it anyway. Just fill with dots
The results are the same with or without alpha argument on all these tests
Code:
` private void init() {
try {
Display.setDisplayMode(new DisplayMode(DEFAULT_WIDTH, DEFAULT_HEIGHT));
Display.setResizable(true);
Display.setVSyncEnabled(true);
Display.setTitle(DEFAULT_TITLE + " v" + VERSION);
Display.create();
updateMatrix();
} catch(LWJGLException e) {
e.printStackTrace();
}
Keyboard.enableRepeatEvents(true);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Font consolas = new Font("consolas", Font.PLAIN, 13);
font = new TrueTypeFont(consolas, antiAliasedFont);
}
private void updateMatrix() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, DEFAULT_WIDTH, DEFAULT_HEIGHT, 0, 1, -1);
//glScaled((double) DEFAULT_WIDTH / (double) Display.getWidth(), (double) DEFAULT_HEIGHT / (double) Display.getHeight(), 0);
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glMatrixMode(GL_MODELVIEW);
}
#Override
public void run() {
init();
Main main = Main.getMain();
while(!Display.isCloseRequested()) {
currentGraphicsTick++;
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0f, 0f, 0f, 1f);
if(Display.wasResized()) updateMatrix();
if(vsyncMode == 1) Display.setVSyncEnabled(true);
else if(vsyncMode == 2) Display.setVSyncEnabled(false);
if(Display.isActive()) {
glPushMatrix();
try { // Draw
float alpha = (float) Math.cos(Math.toRadians(currentGraphicsTick % 90));
System.out.println("Alpha: " + alpha);
glBegin(GL_LINE_STRIP);
{
float sin = (float) Math.abs(Math.sin(Math.toRadians(currentGraphicsTick % 360)));
new Color(0.7f, 0.7f, 0.7f, alpha).bind();
glVertex2f(DEFAULT_WIDTH * 0.03f, DEFAULT_HEIGHT * 0.05f);
glVertex2f(DEFAULT_WIDTH * 0.93f * sin, DEFAULT_HEIGHT * 0.95f * sin);
}
glEnd();
glBegin(GL_QUAD_STRIP);
{
new Color(0.5f, 0.5f, 0.5f, alpha).bind();
glVertex2i(0, 0);
glVertex2i(0, DEFAULT_HEIGHT);
glVertex2i(DEFAULT_WIDTH, 0);
glVertex2i(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
glEnd();
String[] split = main.getGameLoopThread().getDebugString().split("\n");
for(int i = 0; i < split.length; i++) {
font.drawString(1, 1 + (i * font.getLineHeight()), split[i], Color.white);
}
} catch(Throwable throwable) {
throwable.printStackTrace();
}
glPopMatrix();
}
Display.update();
Display.sync(TARGET_FPS);
}
}
Display.destroy();
closeRequested = true;
}
I already tried:
Removing the 'alpha' argument from the Slick's Color constructor
Using OpenGL's glColor with and without alpha argument
Disabling/Enabling GL_BLEND in part of the code (I know some things wouldn't work, but you never know, right?)
Used constants to the alpha variable (such as 0.3f, 0.5f, 0.7f, 1f) instead of making it variable through Math.sin/cos using the tick as the degree
Using glRect(...)
Changing the background
Removing the glClearColor
Removing glClear (nice effect, never did this lol)
What I expect to see was a fading moving LINE_STRIP:
On one side it moves from the (0, 0) to (width - 7%, height - 5%)
On the other it stand still on (width + 3%, height + 5%)
the rectangle would make it fade (the original idea would use the same color as the background, but it didn't on my tests because I want to see the rectangle)
I've had a similar(in terms of what I had to tackle) when doing a 2D game using my own engine. LWJGL's blend functions always have a few issues and there aren't really that many concrete answers for them as it really boils down to your code and how you placed things.
I presume you're using SlickUtil, and to that I would say write your own (or of course search around) your own methods for this. Util's methods were always somewhat wonky with blending.
Removing alpha shouldn't have changed much in terms of getting
what you want
The only time I explicitly enable GL_BLEND is
when rendering lights(always presuming you're doing 3D) and when I would render textures
I suggest not removing glClearColor, that doesn't directly affect your situation as
long as you put it in the correct place
Like I said this bug/problem could be due to quite a few things that I can't quite pin-point from what you've posted.
My suggestions are:
Organise the whole project well to be able to point bugs out quickly
Make alot of stuff generic ASAP(Doesn't matter if you just want to quickly write something)
Avoid SlickUtil, it's great and I LOVE it but it does get it's issues
Sorry I can't help too much but tracking down your issue from the jumbled up code you posted is a bit difficult. I'll keep my eye on this question in case I may be of help in the future.
Besides the tips from #Juxhin, what fixed my problem with alpha blend was TextureImpl.bindNone(); from SlickUtils (check openGL's similar below)
This method is similar to glDisable(GL_TEXTURE_2D) before rendering the thing that needs to be blent (I know that searching and checking Slick's source)
I have this very simple code:
private static final int SKY_COLOR = Color.rgb(161, 210, 241);
private static final int GROUND_COLOR = Color.rgb(171, 131, 35);
#Override
public void init() {
/* Draw the sky */
SurfaceImage backgroundTopImage = graphics().createSurface(graphics().width(), graphics().height() / 2);
backgroundTopImage.surface().setFillColor(SKY_COLOR);
//--> THE NEXT LINE
backgroundTopImage.surface().fillRect(0, 0, backgroundTopImage.width(), backgroundTopImage.height());
ImageLayer backgroundTop = graphics().createImageLayer(backgroundTopImage);
graphics().rootLayer().addAt(backgroundTop, 0, 0);
/*Draw the ground */
SurfaceImage groundBottomImage = graphics().createSurface(graphics().width(), graphics().height() / 4);
groundBottomImage.surface().setFillColor(GROUND_COLOR);
PlayN.log().debug("groundBottomImage.height()=" + groundBottomImage.height());
groundBottomImage.surface().fillRect(0, 0, groundBottomImage.width(), groundBottomImage.height());
ImageLayer groundBottom = graphics().createImageLayer(groundBottomImage);
PlayN.log().debug("graphics().height() * (3 / 4)=" + graphics().height() * (3f / 4f));
graphics().rootLayer().addAt(groundBottom, 0, graphics().height() * (3f / 4f));
}
The first part is supposed to make a sky on the top half of the screen, and the second part is a background color on the bottom quarter.
I have the following result instead :
And when I comment the first fillRect(), I get the expected result, only for the ground part:
The console shows in both cases:
Updating display mode: 1136 x 640 x 0 #0Hz, fullscreen: false
groundBottomImage.height()=160.0
graphics().height() * (3 / 4)=480.0
Playn 1.8
---- EDIT ----
I reverted to PlayN 1.7, and the same code worked fine.
That's a bug in the 1.8 release, since fixed. I'll make a 1.8.1 release because there are a few pesky bugs that have cropped up.
I have found some code which recognize circles in particular image and I was able to convert 90% of that code it in to javacv. But unfortunately I couldn't able to convert following lines in to javacv. So please can some one help me to convert this lines into javacv ?
CvSeq circles = cvHoughCircles(gry, mem, CV_HOUGH_GRADIENT, 1, 40.0, 100, 100,0,0);
cvCvtColor(canny, rgbcanny, CV_GRAY2BGR);
for (int i = 0; i < circles->total; i++)
{
// round the floats to an int
float* p = (float*)cvGetSeqElem(circles, i);
cv::Point center(cvRound(p[0]), cvRound(p[1]));
int radius = cvRound(p[2]);
// draw the circle center
cvCircle(img, center, 3, CV_RGB(0,255,0), -1, 8, 0 );
// draw the circle outline
cvCircle(img, center, radius+1, CV_RGB(0,0,255), 2, 8, 0 );
printf("x: %d y: %d r: %d\n",center.x,center.y, radius);
}
I just need to know how to convert 5 code lines which inside the for loop. Please be kind enough to share your experience. Thanks.
Well, I'm not going to convert that code for you (I don't know JavaCV) but here's some useful links for you:
Hints for Converting OpenCV C/C++ code to JavaCV - this is the very first thing you should read.
JavaCV source code - in this archive there's file samples/HoughLines.java. It's very similar to HoughCircles.java wich was deleted from repo.
JavaCV examples - this archive contains file OpenCV2_Cookbook/src/opencv2_cookbook/chapter07/ex4HoughCircles.scala. And here's some part of code from this file:
// Draw lines on the canny contour image
val colorDst = cvCreateImage(cvGetSize(src), src.depth(), 3)
cvCvtColor(src, colorDst, CV_GRAY2BGR)
for (i <- 0 until circles.total) {
val point = new CvPoint3D32f(cvGetSeqElem(circles, i))
val center = cvPointFrom32f(new CvPoint2D32f(point.x, point.y))
val radius = math.round(point.z)
cvCircle(colorDst, center, radius, CV_RGB(255, 0, 0), 1, CV_AA, 0)
print(point)
}
show(colorDst, "Hough Circles")
This is exactly what you're looking for.
I am trying to draw a 3D tile grid that can be rotated and elevated.
The grid though, when rotated sometimes, does not show the graphics as it should, and mutilates them. The regular grid should look nothing like this.
The top half should not be present.
Can anyone help explain or give examples as to what may be causing this issue?
Just found something else: When you see the spike appear, on one side of your screen, on the opposite, that tile is missing. :
I found that if you set azimuth to 133, elevation to 324, zmod to -0.9, leave xmod at 0 and change the grid to 7 × 7 instead of 10 × 10, then only one single point 'misbehaves'. For this one misbehaving point, we have:
i = 48, j = 3,
x0 = 7.0, y0 = 0.0, z0 = 6.1,
x1 = -0.31273127, y1 = 5.4544506, z1 = -7.5074077,
near = 6.0 and nearToObj = 1.5,
just before the following two lines:
x1 = x1 * near / (z1 + near + nearToObj);
y1 = y1 * near / (z1 + near + nearToObj);
The critical thing here is that z1 + near + nearToObj has crossed below zero. As it crossed below zero, it caused the sign of the values of x1 and y1 calculated by the above two lines to change. This sign change is what causes the grid to appear wrong.
I'm no expert in 3D graphics, but I believe this outcome suggests that you can't plot the point in question because it has gone behind the camera. I'm afraid I'm not sure what the best way to solve this problem would be - that would require more knowledge of 3D graphics than I have.
As for my other answer, it was totally wrong, so I've deleted it. If I was going to claim that the Java 2D graphics API was any doing wrapping using 16-bit integers, I could at least have tried to verify that
g.drawLine(30, 30, 60, 60);
and
g.drawLine(30, 30, 60 + 65536, 60 + 65536);
produced the same output. They do not.
What's (new BasicStroke(6 / 5)) supposed to do?
6 / 5 is a fancy way to write 1.
$ cat Int.java
class Int {
public static void main(String args[]) {
System.out.println("6 / 5 == " + (6 / 5));
return;
}
}
$ make Int.class
javac Int.java
$ java Int
6 / 5 == 1
$