LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: adrienm001 on September 25, 2013, 14:43:24

Title: Weird bug while using glColor
Post by: adrienm001 on September 25, 2013, 14:43:24
Hi everybody,

I'm currently developping a game and I got a weird bug. I'm busy to implement all the menus but I got that bug when I want to implement the "load" screen. So to explain you my implementation, I used one class to draw all the menu and one class to manage the keyboard event. When the user wants to generate a new game, those 2 classes are remplaced by 2 others classes. The one for the keyboard does nothing and the other simply draw a loading bar. There is the part where I meet that bug. I want to draw a black rectangle with a border but I can't change the colors of those elements. Here is the code of this class :


public void render() {
GraphicController.getWindow().initGL2D();

int step = loader.getStep();
float ratio = ((float) step) / loader.getFullStep();

GL11.glColor3f(1.0f, 1.0f, 1.0f);

GraphicController.getTextureLoader().nebulaMenuTexture.bind();
Texture texture = GraphicController.getTextureLoader().nebulaMenuTexture;

GL11.glBegin(GL11.GL_QUADS);
{
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(-(texture.getImageWidth() - Data.getInstance()
.getActifWidth()) / 2, -(texture.getImageHeight() - Data
.getInstance().getActifHeight()) / 2);
GL11.glTexCoord2f(texture.getWidth(), 0);
GL11.glVertex2f((texture.getImageWidth() + Data.getInstance()
.getActifWidth()) / 2, -(texture.getImageHeight() - Data
.getInstance().getActifHeight()) / 2);
GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
GL11.glVertex2f((texture.getImageWidth() + Data.getInstance()
.getActifWidth()) / 2, (texture.getImageHeight() + Data
.getInstance().getActifHeight()) / 2);
GL11.glTexCoord2f(0, texture.getHeight());
GL11.glVertex2f(-(texture.getImageWidth() - Data.getInstance()
.getActifWidth()) / 2, (texture.getImageHeight() + Data
.getInstance().getActifHeight()) / 2);
}
GL11.glEnd();

GL11.glColor3f(0.0f, 0.0f, 0.0f);
GL11.glBegin(GL11.GL_QUADS);
{
GL11.glVertex2f(Data.getInstance().getActifWidth() * 0.25f, Data
.getInstance().getActifHeight() * 0.5f - 20);
GL11.glVertex2f(Data.getInstance().getActifWidth() * 0.75f, Data
.getInstance().getActifHeight() * 0.5f - 20);
GL11.glVertex2f(Data.getInstance().getActifWidth() * 0.75f, Data
.getInstance().getActifHeight() * 0.5f + 20);
GL11.glVertex2f(Data.getInstance().getActifWidth() * 0.25f, Data
.getInstance().getActifHeight() * 0.5f + 20);

GL11.glColor3f(1.0f, 1.0f, 1.0f);

GL11.glVertex2f(Data.getInstance().getActifWidth() * 0.25f + 5,
Data.getInstance().getActifHeight() * 0.5f - 15);
GL11.glVertex2f((float) (Data.getInstance().getActifWidth()
* (0.25 + 0.5 * ratio) - 10 * ratio + 5), Data
.getInstance().getActifHeight() * 0.5f - 15);
GL11.glVertex2f((float) (Data.getInstance().getActifWidth()
* (0.25 + 0.5 * ratio) - 10 * ratio + 5), Data
.getInstance().getActifHeight() * 0.5f + 15);
GL11.glVertex2f(Data.getInstance().getActifWidth() * 0.25f + 5,
Data.getInstance().getActifHeight() * 0.5f + 15);

}
GL11.glEnd();


GL11.glBegin(GL11.GL_LINES);
{
GL11.glVertex2f(Data.getInstance().getActifWidth() * 0.15f, Data
.getInstance().getActifHeight() * 0.5f - 20);
GL11.glVertex2f(Data.getInstance().getActifWidth() * 0.85f, Data
.getInstance().getActifHeight() * 0.5f - 20);
GL11.glVertex2f(Data.getInstance().getActifWidth() * 0.85f, Data
.getInstance().getActifHeight() * 0.5f - 20);
GL11.glVertex2f(Data.getInstance().getActifWidth() * 0.85f, Data
.getInstance().getActifHeight() * 0.5f + 20);
GL11.glVertex2f(Data.getInstance().getActifWidth() * 0.85f, Data
.getInstance().getActifHeight() * 0.5f + 20);
GL11.glVertex2f(Data.getInstance().getActifWidth() * 0.15f, Data
.getInstance().getActifHeight() * 0.5f + 20);
GL11.glVertex2f(Data.getInstance().getActifWidth() * 0.15f, Data
.getInstance().getActifHeight() * 0.5f + 20);
GL11.glVertex2f(Data.getInstance().getActifWidth() * 0.15f, Data
.getInstance().getActifHeight() * 0.5f - 20);
}
GL11.glEnd();

GraphicController.getTextRender().drawString(Constante.stepText[step],
Data.getInstance().getActifWidth() / 4,
Data.getInstance().getActifHeight() / 2 - 25, 6, 1);
}


And my initGL2D() :
public void initGL2D() {
GL11.glShadeModel(GL11.GL_SMOOTH);
//GL11.glClearDepth(1.0); // Depth Buffer Setup
GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
GL11.glLoadIdentity(); // Reset The Projection Matrix

GLU.gluOrtho2D(0.0f, Data.getInstance().getActifWidth(), Data.getInstance().getActifHeight(), 0.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
GL11.glLoadIdentity();

// Really Nice Perspective Calculations
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
}


Does someone see why I got this bug ?

Thanks in advance

PS : sorry for my English, it's not my mother tong
Title: Re: Weird bug while using glColor
Post by: quew8 on September 25, 2013, 16:58:22
I think your problem is with textures. You don't disable them after drawing the first quad (which I'm guessing is some kind of background?). After drawing the first quad, call glDisable(GL_TEXTURE_2D) and then after you've finished drawing the lines call glEnable(GL_TEXTURE_2D) so that the other textured stuff you draw is textured.

If you specify a colour other than white whilst texturing is enabled, then the texture gets blended with the colour. In your case the texture coord is that of the last vertex in the first quad, so that pixel gets blended with the colour you specify.

Also, your English was perfect until you apologized for it being poor, where you misspelt "tongue" as "tong". Between you and me I think your spelling is better. 
Title: Re: Weird bug while using glColor
Post by: adrienm001 on September 26, 2013, 00:06:18
Thank you so much, it works perfectly with your modifications !!