Hello Guest

Images darken after loading Textures in a different place?

  • 1 Replies
  • 6213 Views
I am new to LWJGL and I am trying to create a 3d simple game (making a floor, and some blocks I can collide with), but everytime I load more stuff the images get darker on screen.

Code: [Select]
package npe.dungeonfighter;

import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class DungeonFighter{
public static void main(String[] args){
initGame();
initList();
gameLoop();
cleanUp();
}
static Floor floor;
static Camera cam;
private static void gameLoop(){
cam = new Camera(70f,(float)Display.getWidth()/(float)Display.getHeight(),0.3f,1000f);

while(!Display.isCloseRequested()){
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
cam.useView();
glPushMatrix();{
glTranslatef(0,0,-10);
glCallList(1);
glEnd();
update();
Display.update();
}
glPopMatrix();
}
}
private static void draw(){

}
private static void update(){
cam.update();
}

private static void cleanUp(){
Display.destroy();
System.exit(0);
}
private static void initList(){

[b]//Images.init();[/b]

int list = glGenLists(1);
System.out.println(list);
glNewList(list,GL_COMPILE);{
glBegin(GL_QUADS);{
//Front face
glColor3f(1.0f,0,0);
glVertex3f(-1,-1,1);
glVertex3f(-1,1,1);
glVertex3f(1,1,1);
glVertex3f(1,-1,1);

//BackFace
glColor3f(0f,1f,0f);
glVertex3f(-1,-1,-1);
glVertex3f(-1,1,-1);
glVertex3f(1,1,-1);
glVertex3f(1,-1,-1);

//BottomFace
glColor3f(0f,0f,1f);
glVertex3f(-1,-1,-1);
glVertex3f(-1,-1,1);
glVertex3f(-1,1,1);
glVertex3f(-1,1,-1);

//TopFace
glColor3f(1.0f,1f,0f);
glVertex3f(1,-1,-1);
glVertex3f(1,-1,1);
glVertex3f(1,1,1);
glVertex3f(1,1,-1);

//LeftFace
glColor3f(0f,1f,1f);
glVertex3f(-1,-1,-1);
glVertex3f(1,-1,-1);
glVertex3f(1,-1,1);
glVertex3f(-1,-1,1);

//RightFace
glColor3f(1.0f,1f,1f);
glVertex3f(-1,1,-1);
glVertex3f(1,1,-1);
glVertex3f(1,1,1);
glVertex3f(-1,1,1);
}
glEnd();
}
glEndList();
}
private static void initGame(){
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.setTitle("Title!");
Display.create();
}catch (LWJGLException e){e.printStackTrace();}
}

}

This setup gives me this result:
http://imageshack.us/photo/my-images/849/3dgamep1.png/

But if I get rid of the "//" infront of //Images.init(); in the initLists() method, I get this result:
http://imageshack.us/photo/my-images/208/3dgamep2.png/

As you can see the cube becomes visibly darker.

The Images.init() method just runs a static method in a differant class that gets textures:
Code: [Select]
package npe.dungeonfighter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class Images{
static Texture floor;


public static void init(){
try {
floor=TextureLoader.getTexture("png",new FileInputStream(new File("res/floor.png")));
}catch(FileNotFoundException e){e.printStackTrace();}catch (IOException e){e.printStackTrace();}
}

}


I have no idea what I'm doing wrong. Thanks for any help!


EDIT: I found a post (after a bit of searching through the things- couldnt find one originaly) that said I needed to use glDisable(GL_TEXTURE2D);

I've done this and it works, but might I ask why this works?
« Last Edit: May 25, 2013, 08:39:23 by poo2thegeek »

Re: Images darken after loading Textures in a different place?
« Reply #1 on: July 27, 2013, 20:11:01 »
If that's glDisable(GL_TEXTURE_2D) before drawing the cube, I'd guess that the (0,0) coordinate of the texture (bottom left I think? not sure, could be top left) is a grey pixel that then gets multiplied with the color you're drawing in (the red, green etc. of the cube) to get the final cube color, resulting in a darker cube. Disabling textures disables this multiplication resulting in the expected bright cube.

Instead of disabling textures each frame (then enabling them after drawing your cube and before using textures), using glBindTexture(0) will work the same without disabling textures, then just bind a different texture when you want to switch back.