Hello Guest

lwjgl box color leaking to background

  • 3 Replies
  • 549 Views
lwjgl box color leaking to background
« on: December 21, 2019, 22:07:54 »
I have a color that keeps leaking to the background whenever I try to draw a rectangle to the screen with that color. Here is what it looks like before: see first image (sorry don't know how to use the img tags) and here is what it looks like after swapping the screen: see second image

Here is the code for the class that draws the rectangle:

Code: [Select]
package com.programwings.wofrpg.display.drawable.components;

import com.programwings.wofrpg.display.screens.MainMenu;
import com.programwings.wofrpg.util.Color;
import com.programwings.wofrpg.util.Coordinate;
import com.programwings.wofrpg.util.Dimension;
import com.programwings.wofrpg.util.logcat.LogCat;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.BufferUtils;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.stb.STBImage.stbi_image_free;
import static org.lwjgl.stb.STBImage.stbi_load;

public class TextBoxComponent {

private int picturei;
public int inputType;
private double width, height;
private Color boxColor;
private String hint, picture;
private Coordinate topLeft;

public TextBoxComponent(Coordinate topLeft,
double width,
double height,
String hint,
String picture,
int inputType,
Color boxColor) {
this.boxColor = boxColor;
this.height = height;
this.hint = hint;
this.picture = picture;
this.inputType = inputType;
this.topLeft = topLeft;
this.width = width;
if(picture != null) {
IntBuffer wi = BufferUtils.createIntBuffer(1);
IntBuffer hi = BufferUtils.createIntBuffer(1);
IntBuffer con= BufferUtils.createIntBuffer(1);

ByteBuffer data = stbi_load(picture, wi, hi, con, 3);
this.picturei = glGenTextures();

glBindTexture(GL_TEXTURE_2D, this.picturei);
int wid = wi.get();
int hei = hi.get();

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA,  wid, hei, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
try {
stbi_image_free(data);
} catch(NullPointerException e) {
new LogCat("components").log("An error has occoured while freeing images. [[java.lang.NullPointerException::ln57]]", LogCat.STDERR);
e.printStackTrace();
}
}
}

public Dimension getDimensions() {
return new Dimension(width, height);
}

public Coordinate getTopLeftCornerPoint() {
return topLeft;
}

public String getHint() {
return hint;
}

public void setBoxColor(Color c) {
boxColor = c;
}

Coordinate[] vertices;

public void draw(@Nullable String text) {
try {
if (picture == null) {
vertices = new Coordinate[]{topLeft, new Coordinate(topLeft.x + width, topLeft.y), new Coordinate(topLeft.x + width, topLeft.y - height), new Coordinate(topLeft.x, topLeft.y - height)};
glBegin(GL_QUADS);
glColor4d(boxColor.r, boxColor.g, boxColor.b, boxColor.a);
glVertex2d(topLeft.x, topLeft.y);
glColor4d(boxColor.r, boxColor.g, boxColor.b, boxColor.a);
glVertex2d(topLeft.x + width, topLeft.y);
glColor4d(boxColor.r, boxColor.g, boxColor.b, boxColor.a);
glVertex2d(topLeft.x + width, topLeft.y - height);
glColor4d(boxColor.r, boxColor.g, boxColor.b, boxColor.a);
glVertex2d(topLeft.x, topLeft.y - height);
glEnd();
} else if (boxColor == null) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, picturei);
glBegin(GL_QUADS);
glTexCoord2d(1, 0);
glVertex2d(topLeft.x, topLeft.y);
glTexCoord2d(0, 0);
glVertex2d(topLeft.x + width, topLeft.y);
glTexCoord2d(0, 1);
glVertex2d(topLeft.x + width, topLeft.y - height);
glTexCoord2d(1, 1);
glVertex2d(topLeft.x, topLeft.y - height);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
} else {
throw new IllegalStateException("An error has occoured within the system.");
}
} catch(NullPointerException e) {
new LogCat("components").log("Something happened while trying to draw a com.programwings.wofrpg.display.drawable.components.TextBoxComponent to a screen. Unable to continue. [[java.lang.NullPointerException::unknown line number]]\n" +
"", LogCat.STDERR);
MainMenu.displayMainMenu();
}
}

public boolean isClicked(double px, double py) {
//System.out.println((px >= topLeft.x && px <= vertices[1].x) && (py >= vertices[2].y && py <= topLeft.y));
return (px >= topLeft.x && px <= vertices[1].x) && (py >= vertices[2].y && py <= topLeft.y);
}

}

where a Coordinate is a point on the screen in OpenGL units (two doubles, one for x and one for y), and a Color is a 4 double constructor that forms the box color.

If it helps, here is the class for the screen that is being drawn:

Code: [Select]
package com.programwings.wofrpg.display.screens;

import com.programwings.wofrpg.display.Primary;
import com.programwings.wofrpg.game.handlers.MouseHandler;
import com.programwings.wofrpg.util.Color;
import com.programwings.wofrpg.util.Coordinate;

import java.util.concurrent.atomic.AtomicBoolean;

import static com.programwings.wofrpg.display.vars.Images.*;
import static org.lwjgl.glfw.GLFW.*;

public class NewCharMenu {

public static void drawNewCharMenu() {

AtomicBoolean isShowing = new AtomicBoolean(true);

glfwSetKeyCallback(Primary.w, ((window, key, scancode, action, mods) -> {
if(key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
isShowing.set(false);
}
}));

while(isShowing.get()) {
glfwPollEvents();

if(glfwWindowShouldClose(Primary.w)) break;

//mouseInput
if(glfwGetMouseButton(Primary.w, GLFW_MOUSE_BUTTON_LEFT) == 1) {
Coordinate mouseCoord = MouseHandler.getMouseCoords(Primary.w);
//if(testTextBox.isClicked(mouseCoord.x, mouseCoord.y)) {
// testTextBox.setBoxColor(new Color(1, 0, 0, 1));
//} else {
// testTextBox.setBoxColor(new Color(0, 1, 1, 1));
//}
}

mmbkg.draw();

testTextBox.draw("test i guess");

glfwSwapBuffers(Primary.w);
}
MainMenu.displayMainMenu();
}

}

Can someone help me?
Thanks

-Surge

EDIT 1: I also ran it with lwjglx debugger and got this following warning stacktrace:
Code: [Select]
[warn ][1] OpenGL debug message
  ID: 0xD
  Source: API
  Type: DEPRECATED BEHAVIOR
  Severity: MEDIUM
  Message: API_ID_DEPRECATED_TARGET deprecated behavior warning has been generated. Enable of GL_LIGHTING capability is deprecated.
  Stacktrace: org.lwjgl.system.JNI.invokePV(Native Method)
              org.lwjgl.glfw.GLFW.glfwSwapBuffers(GLFW.java:4565)
              org.lwjglx.debug.glfw.GLFW.glfwSwapBuffers(GLFW.java:507)
              com.programwings.wofrpg.display.screens.NewCharMenu.drawNewCharMenu(NewCharMenu.java:43)
              com.programwings.wofrpg.display.screens.MainMenu.displayMainMenu(MainMenu.java:25)
              com.programwings.wofrpg.display.Primary.loop(Primary.java:143)
              com.programwings.wofrpg.display.Primary.run(Primary.java:39)
              com.programwings.wofrpg.Runnable.run(Runnable.java:21)
              com.programwings.wofrpg.Runnable.main(Runnable.java:26)

It points to the
Code: [Select]
glfwSwapBuffers() method in the second class that I gave.
« Last Edit: December 21, 2019, 22:31:10 by Animus_Surge »

*

Offline abcdef

  • ****
  • 335
Re: lwjgl box color leaking to background
« Reply #1 on: December 25, 2019, 01:19:29 »
On your stacktrace you need to read the error message

API_ID_DEPRECATED_TARGET deprecated behavior warning has been generated. Enable of GL_LIGHTING capability is deprecated.

Re: lwjgl box color leaking to background
« Reply #2 on: December 25, 2019, 06:47:07 »
Rrrrrright... but I don't understand it. GL_LIGHTING isn't even being called at all. All that I'm calling beforehand is GL_BLEND_MODE, and that's to help render transparent images.

*

Offline Danjb

  • *
  • 18
Re: lwjgl box color leaking to background
« Reply #3 on: February 26, 2020, 14:53:34 »
When you call:

Code: [Select]
glColor4d(boxColor.r, boxColor.g, boxColor.b, boxColor.a);
That changes the draw colour permanently. If you want to reset it, you need to call:

Code: [Select]
glColor4d(1.0, 1.0, 1.0, 1.0);