问题描述:

I use eclipse. so in my workspace, under my project, i create a new folder "res" with a subfolder "images" that have all my png's for use as textures. so here is the method im using for loading the textures:

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.FileNotFoundException;

import org.lwjgl.LWJGLException;

import org.lwjgl.opengl.Display;

import org.lwjgl.opengl.DisplayMode;

import org.lwjgl.opengl.GL11;

import org.newdawn.slick.Color;

import org.newdawn.slick.opengl.Texture;

import org.newdawn.slick.opengl.TextureLoader;

import org.newdawn.slick.util.ResourceLoader;

import org.lwjgl.input.Mouse;

import java.util.Random;

public class TextureDemo

{

public int count = 0;

private static Texture wood;

Random random = new Random();

public TextureDemo()

{

initGL(640, 480, "SLICK TEXTURES");

loadTexture("mozilla");

int x = 100, y = 100, count = 0, width = 0, height = 0, counter = 10;

while(true)

{

count++;

if(count == counter)

{

x--; y--; width++; height++; counter += random.nextInt(50) + 1;

}

render(x, y, width, height);

Display.update();

Display.sync(60);

if(Display.isCloseRequested())

{

Display.destroy();

System.exit(0);

}

}

}

private void initGL(int width, int height, String title)

{

try

{

Display.setDisplayMode(new DisplayMode(width, height));

Display.setTitle(title);

Display.create();

}

catch(LWJGLException e)

{

e.printStackTrace();

Display.destroy();

System.exit(1);

}

GL11.glMatrixMode(GL11.GL_PROJECTION);

GL11.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);

GL11.glDisable(GL11.GL_COLOR_MATERIAL);

GL11.glLoadIdentity();

GL11.glOrtho(0, width, height, 0, 1, -1);

GL11.glMatrixMode(GL11.GL_MODELVIEW);

GL11.glEnable(GL11.GL_BLEND);

GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

}

public void loadTexture(String key)

{

try

{

wood = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("./res/images/"+key+".png"));

System.out.println("working." + wood.getTextureID());

}

catch(FileNotFoundException e)

{

e.printStackTrace();

Display.destroy();

System.exit(1);

}

catch(IOException e)

{

e.printStackTrace();

}

}

public void render(int x, int y, int width, int height)

{

GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

GL11.glEnable(GL11.GL_BLEND);

GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

System.out.println("working." + wood.getTextureID());

GL11.glBindTexture(GL11.GL_TEXTURE_2D, wood.getTextureID());

GL11.glBegin(GL11.GL_QUADS);

GL11.glTexCoord2f(0, 0);

GL11.glVertex2f(x, y);

GL11.glTexCoord2f(1, 0);

GL11.glVertex2f(x + wood.getImageWidth(), y);

GL11.glTexCoord2f(1, 1);

GL11.glVertex2f(x + wood.getImageWidth(), y + wood.getImageHeight());

GL11.glTexCoord2f(0, 1);

GL11.glVertex2f(x, y + wood.getImageHeight());

GL11.glEnd();

GL11.glDisable(GL11.GL_BLEND);

System.out.println(wood.getHeight()+ " " +wood.getWidth());

}

public static void main (String[] args)

{

new TextureDemo();

}

}

WHY WHY WHY are my textures black lol? I really don't understand how my code did this. Is it possible that my computer could have problems that are causing that?

网友答案:

Asking the same question twice won't help. However, you should make sure you first call glBindTexture, before you start glBegin.

网友答案:

I'm sorry, however there are many problems with your code. I would recommend taking a look at some opengl tutorials. The arcsynthesis one is very in-depth.

Your problem is that the texture "wood" is never bound to opengl.

In order to bind your texture to the opengl context you must call

glBindTexture(GL_TEXTURE_2D, id);

before glbegin()

The id is the texture id generated by glGentextures or in your case the slick util method. Also what is wood? In the first code block it is the texture id but in the second block you are calling 'getHeght()' on type wood. I am confused as to what 'wood' is.

Either way, you need to bind the texture for opengl to use it and I again recommending looking at a few tutorials on the opengl basics.

网友答案:

You haven't enabled textures! In your initGL method call GL11.glEnable(GL11.GL_TEXTURE_2D).

相关阅读:
Top