For some reason, once I add textures to my application, it causes everything else to be blue. If it’s normally white, green, or whatever color, it becomes blue. The screen also becomes unusually darkened, which I think is related to the fact that there are no longer any bright colors in the application because it is all blue. I cant seem to find out what is causing it to do this, and I hope someone can.
Below is the code I’m trying to use. This code is partially copied from various tutorials.
private GL gl;
private GLU glu;
private GLCanvas canvas;
private GLDrawable glDrawable;
private Camera camera;
Point framePosition;
private int texture;
private float[] lightAmbient = { 1.0f, 1.0f, 1.0f, 1.0f };
private float[] lightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
private float[] lightPosition = { 0.0f, 0.0f, 0.0f, 1.0f };
public Test(){
setSize(windowWidth, windowHeight);
GLCapabilities glCapabilities = new GLCapabilities();
glCapabilities.setRedBits(8);
glCapabilities.setBlueBits(8);
glCapabilities.setGreenBits(8);
glCapabilities.setAlphaBits(8);
GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(glCapabilities);
add(canvas);
camera = new Camera();
canvas.addGLEventListener(this);
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
setVisible(true);
canvas.addComponentListener(
new ComponentAdapter(){
public void componentMoved(ComponentEvent e){
Camera.framePosition = getLocationOnScreen();
}
}
);
canvas.addKeyListener(new KeyboardListener(camera));
canvas.addMouseMotionListener(new MousePositionListener(camera));
Camera.framePosition = getLocationOnScreen();
while(true){
canvas.display();
}
}
public void init(GLDrawable drawable){
this.glDrawable = drawable;
this.gl = glDrawable.getGL();
this.glu = glDrawable.getGLU();
glDrawable.setGL( new DebugGL(drawable.getGL() ));
glDrawable.setGLU(glu);
gl.glShadeModel(gl.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClearDepth(1.0f);
gl.glEnable(gl.GL_DEPTH_TEST);
gl.glDepthFunc(gl.GL_LEQUAL);
gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_NICEST);
camera.positionCamera(0.0, 1.5, 6.0, 0.0, 1.5, 0.0, 0.0, 1.0, 0.0);
System.out.println("Init GL is " + gl.getClass().getName());
// texture
gl.glEnable(gl.GL_TEXTURE_2D);
texture = genTexture(gl);
gl.glBindTexture(GL.GL_TEXTURE_2D, texture);
BufferedImage img = readPNGImage("NeHe.png");
makeRGBTexture(gl, glDrawable.getGLU(), img, GL.GL_TEXTURE_2D, false);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
//gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
//--
// lighting
gl.glLightfv(GL.GL_LIGHT1, GL.GL_AMBIENT, this.lightAmbient);
gl.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE, this.lightDiffuse);
gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, this.lightPosition);
gl.glEnable(GL.GL_LIGHT1);
gl.glEnable(GL.GL_LIGHTING);
//--
}
float angle = 0;
public void display(GLDrawable drawable){
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
gl.glLoadIdentity();
gl.glEnable(GL.GL_LIGHTING); // lighting
camera.update();
camera.look(glu);
gl.glBindTexture(GL.GL_TEXTURE_2D, texture);
gl.glPushMatrix();
gl.glNormal3f( 0.0f, 0.0f, -1.0f);
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glTranslatef(-1.5f, 0.0f, -19.0f);
gl.glBegin(gl.GL_TRIANGLES);
gl.glVertex3f( 0.0f, 1.9f, 0.0f);
gl.glVertex3f(-1.0f,-1.0f, 0.0f);
gl.glVertex3f( 1.0f,-1.0f, 0.0f);
gl.glEnd();
gl.glPopMatrix();
gl.glPushMatrix();
gl.glTranslatef(3.0f, 0.0f, -54.0f);
gl.glScalef(5.0f, 5.0f, 10.0f);
gl.glBegin(gl.GL_QUADS);
gl.glNormal3f( 0.0f, 0.0f, 1.0f);
gl.glColor3f(0.5f, 0.0f, 0.0f);
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 0.0f);
gl.glColor3f(0.5f, 0.0f, 0.0f);
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 0.0f);
gl.glColor3f(0.0f, 1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f,-1.0f, 0.0f);
gl.glColor3f(0.0f, 0.0f, 1.0f);
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f,-1.0f, 0.0f);
gl.glEnd();
gl.glPopMatrix();
angle += 1.0f;
if(angle >= 360){
angle = 0.0f;
}
FPS();
gl.glPushMatrix();
gl.glColor3f(0.0f, 1.0f, 0.0f);
for(float i = -50; i <= 50; i++){
gl.glBegin(gl.GL_LINES);
gl.glVertex3f(-50.0f, -5.0f, i);
gl.glVertex3f(50.0f, -5.0f, i);
gl.glVertex3f(i, -5.0f, -50.0f);
gl.glVertex3f(i, -5.0f, 50.0f);
gl.glEnd();
}
gl.glPopMatrix();
}
public void reshape(GLDrawable drawable, int x, int y, int width, int height){
if(height == 0){
height = 1;
}
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(gl.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0, (double)width / (double)height, 0.1, 100.0);
gl.glMatrixMode(gl.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void displayChanged(GLDrawable drawable, boolean modeChanged, boolean deviceChanged){
}
long oldTime = 0;
double averageFPS = 0;
int reps = 0;
public void FPS(){
if(oldTime == 0){
oldTime = System.nanoTime();
}
if(reps > 100){
reps = 0;
}
long echo = System.nanoTime();
double interval = ((echo - oldTime) / 1000000000.0);
oldTime = echo;
averageFPS = ((100 * averageFPS) + (1.0 / interval)) / 101.0;
reps++;
if(reps == 100){
System.out.println("FPS: " + (int)averageFPS);
}
}
private int genTexture(GL gl){
final int [] temp = new int[1];
gl.glGenTextures(1, temp);
return temp[0];
}
private BufferedImage readPNGImage(String filename){
try{
URL url = getResource(filename);
if(url == null){
throw new RuntimeException("Error reading resource " + filename);
}
BufferedImage img = ImageIO.read(url);
java.awt.geom.AffineTransform tx = java.awt.geom.AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -img.getHeight(null));
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
img = op.filter(img, null);
return img;
}catch(IOException e){
throw new RuntimeException(e);
}
}
private void makeRGBTexture(GL gl, GLU glu, BufferedImage img, int target, boolean mipmapped){
ByteBuffer dest = null;
if(img.getType() == BufferedImage.TYPE_3BYTE_BGR){}
if(img.getType() == BufferedImage.TYPE_CUSTOM){
byte [] data = ((DataBufferByte)(img.getRaster().getDataBuffer())).getData();
dest = ByteBuffer.allocateDirect(data.length);
dest.order(ByteOrder.nativeOrder());
dest.put(data, 0, data.length);
}else if(img.getType() == BufferedImage.TYPE_INT_RGB){
int [] data = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
dest = ByteBuffer.allocateDirect(data.length * BufferUtils.SIZEOF_INT);
dest.order(ByteOrder.nativeOrder());
dest.asIntBuffer().put(data, 0, data.length);
}else{
throw new RuntimeException("Unsupported image type " + img.getType());
}
if(mipmapped){
glu.gluBuild2DMipmaps(target, GL.GL_RGB8, img.getWidth(), img.getHeight(), GL.GL_RGB, GL.GL_UNSIGNED_BYTE, dest);
}else{
gl.glTexImage2D(target, 0, GL.GL_RGB, img.getWidth(), img.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, dest);
}
}
public final static URL getResource(final String filename){
URL url = ClassLoader.getSystemResource(filename);
if(url == null){
try{
url = new URL("file", "localhost", filename);
}catch(Exception e){}
}
return url;
}
public static int windowWidth = 1024;
public static int windowHeight = 768;