I have the weirdest bug right now [solved]

This is honestly one of the most perplexing bugs I’ve ever faced.
(For reference, I’m using a standard LWJGL/OpenGL set up)
So let me explain what I’m trying to make: a panel. Like the bottom of this image:
http://eportfolio.lagcc.cuny.edu/scholars/doc_sp10/eP_sp10/Gerard_Irizarry/Projects/pastoria_city/images/pkmn_images/battle%20screen%201.JPG
It’s a rectangle with a pretty border. (Two in this image.) Seems simple.
So I’m made a class for these panels. There’s not even any text drawn on them yet, just the border and the 1-colored rectangle to fill the middle

So I started off by making the border. This is accomplished with a texture, and my process of doing this is unimportant. Just know that I bind one texture and draw a bunch of quads from it. I got the border working first. I thought the hard part was over.

But then I tried to draw the colored rectangle that the border frames. And… nothing happened.
As is, here’s what the draw method looked like:

public void draw(){
		glColor3f(red, green, blue);
		glRecti(x, y, x+length, y+height);
		glColor3f(1f, 1f, 1f);

		someTexture.bind();
		//an implementation for drawing the border which definitely works
}

And… Nothing showed up. I mean, the border was still being drawn correctly, so I’m definitely CALLING the draw() method, but I couldn’t see the 1 color rectangle.
I played around with this for a while. I tried drawing a quad by specifying 4 vertexes, and nothing worked. Then I tried this:

public void draw(){
		someTexture.bind();
		//an implementation for drawing the border which definitely works

		glColor3f(red, green, blue);
		glRecti(x, y, x+length, y+height);
		glColor3f(1f, 1f, 1f);
}

And I saw something where the rectangle was supposed to be. It was a single-colored rectangle, but it was gray and transparent.

…?

I made a separate Java Project, and drew some rectangles there with no problem. Then a though crosses my mind:
“Am I still drawing a rectangle from a texture?”
So I changed the texture that makes up my border into pure red texture, and sure enough with the second implementation the panel was being filled with pure red. With the first implementation, there was once again no visible box.

So… What gives? How can I just draw a single colored rectangle when I’m also binding sprites? (I think that’s where the problem lies)
I hope I was clear enough.

Thanks

The problem is that glRecti doesn’t generate the texcoords for you.

Right, I know that. I’m trying to draw a rectangle with one color. As in, a rectangle that’s all white, all red, etc. There’s no texture for it, and no texture coords. I want to draw a rectangle independent of any textures. So how do I do that?

a) Slick’s texture.bind() will enable texturing. When texturing is enabled, texture coordinates (glTexCoord) will be expected. If none are specified, you may end up with some unusual results.

b) To draw a rectangle without texturing, you need to first disable texturing – glDisable(GL_TEXTURE_2D). Since you are using SlickUtil, it’s better to let it manage the state for you – so instead of calling glEnable/glDisable directly, you use bind() to enable texturing, and the following to disable it:

TextureImpl.bindNone();

c) Instead of rendering multiple rectangles just to achieve a border, you should try using a sprite sheet. It will be more efficient, easier, and more flexible.

d) glRect is deprecated. Ideally you should try learning some more modern OpenGL concepts – they will give you much more insight into graphics programming. If you would rather not learn OpenGL, and you just want to render some sprites, I’d suggest going with LibGDX or Slick2D.

Ah! Yes, thank you! It works like a charm!
(Also, in case your’e wondering, I don’t have a sprite that looks like a picture frame that I draw a rectangle within. I do have a kind of sprite sheet. It’s a texture that contains a copy of the border at a corner and at a straight edge. I draw the different parts of it (the corner and the edges) flipped differently to acheive the border)

(I am trying to lean OpenGL but I have enough on my plate as it is. The project I’m making is too far underway to switch libraries now, but I’ll be sure to look into them later.)

Thanks again

Far too underway? It looks like you haven’t even started rendering anything yet… ::slight_smile:

There is no “switching libraries” – SlickUtil is Slick2D. Just grab the latest version of Slick2D and write the following:

... inside init() ...
spriteSheet = TextureLoader.getTexture(... load texture as usual...);

//alternatively you could use SpriteSheet, XMLPackedSheet, etc.
sheet = new Image(spriteSheet);
spriteA = sheet.getSubImage(0, 0, 32, 32);


... inside draw() ...
//assuming orthographic projection is set..

//draws the sprite at (50, 50)
spriteA.draw(50, 50);