LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: TCIAL on October 21, 2012, 21:34:29

Title: Textures with glOrtho(...)
Post by: TCIAL on October 21, 2012, 21:34:29
Hi,
i don't work very long with Java & LWJGL. I come from C# & OpenTK and this is my first app that uses glOrtho(...) because it is a 2D snake game. So the game itself works and when using only colors then the rendering works pretty well. Now i want to use textures but it only renders gray quads without any textures. Code is below, maybe you can find out what's the problem :)


package CDG.SnakeGame;
import java.io.*;
import java.nio.*;
import java.util.*;
import org.lwjgl.*;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
import de.matthiasmann.twl.utils.*;
import de.matthiasmann.twl.utils.PNGDecoder.Format;

public class Main
{
private boolean interrupted = false;
private int actCount = 0;
private int[] textures;
private String textureFolder = "/textures/";
private String[] textureFiles = {"body_texture.png","head_texture_0.png","head_texture_1.png","head_texture_2.png","head_texture_3.png","apple.png"};

private void init()
{
WorldManager.setWorld(new SnakeWorld(40,30,20));
try
{
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();
Display.setTitle("CDGSnake");
}
catch(LWJGLException e)
{
e.printStackTrace();
System.exit(-1);
}

try
{
        textures = new int[6];
loadTextures();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(-1);
}

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, -1, 1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
GL11.glEnable(GL11.GL_TEXTURE_2D);

initWorldObjects();
}

private void initWorldObjects()
{
for(int y = 0; y < WorldManager.getWorld().getHeight(); y++)
{
for(int x = 0; x < WorldManager.getWorld().getWidth(); x++)
{
if(y == 0 || y == WorldManager.getWorld().getHeight()-1 || x == 0 || x == WorldManager.getWorld().getWidth()-1)
{
Wall w = new Wall();
WorldManager.getWorld().addObject(w, x, y);
System.out.println("Creating wall @"+x+"/"+y);
}
}
}
SnakeHead h = new SnakeHead();
WorldManager.getWorld().addObject(h,new Random().nextInt(WorldManager.getWorld().getWidth()-2)+1,new Random().nextInt(WorldManager.getWorld().getHeight()-2)+1);
WorldManager.getWorld().spawnFood();
}

private void renderFrame()
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
List<Entity> objects = WorldManager.getWorld().getObjects(null);

for(int i = 0; i < objects.size(); i++)
{
//objects.get(i).act();
if(objects.get(i).getClass() == Wall.class)
{
GL11.glColor3f(0.65f, 0.65f, 0.65f);
}
else if(objects.get(i).getClass() == SnakeHead.class)
{
int dir = ((SnakeHead)objects.get(i)).getDirection();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textures[dir+1]);
//GL11.glColor3f(1.0f, 1.0f, 1.0f);
}
else if(objects.get(i).getClass() == SnakeBody.class)
{
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textures[0]);
//GL11.glColor3f(1.0f, 1.0f, 1.0f);
}
else
{
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textures[5]);
//GL11.glColor3f(1.0f, 1.0f, 1.0f);
}
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex2i(((objects.get(i).getX())*WorldManager.getWorld().getTileSize()), ((objects.get(i).getY())*WorldManager.getWorld().getTileSize()));
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex2i(((objects.get(i).getX())*WorldManager.getWorld().getTileSize())+WorldManager.getWorld().getTileSize(), ((objects.get(i).getY())*WorldManager.getWorld().getTileSize()));
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex2i(((objects.get(i).getX())*WorldManager.getWorld().getTileSize())+WorldManager.getWorld().getTileSize(), ((objects.get(i).getY())*WorldManager.getWorld().getTileSize())+WorldManager.getWorld().getTileSize());
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex2i(((objects.get(i).getX())*WorldManager.getWorld().getTileSize()), ((objects.get(i).getY())*WorldManager.getWorld().getTileSize())+WorldManager.getWorld().getTileSize());

GL11.glEnd();

}
}

private void loadTextures() throws IOException
{

for(int i = 0; i < 6; i++)
{
textures[i] = GL11.glGenTextures();
GL13.glActiveTexture(GL13.GL_TEXTURE0);
System.out.println(i);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textures[i]);
InputStream in = new FileInputStream(new File(".").getAbsolutePath()+textureFolder+textureFiles[i]);
ByteBuffer buf = null;
int th = 0;
int tw = 0;
try
{
PNGDecoder decoder = new PNGDecoder(in);
buf = ByteBuffer.allocateDirect(4*decoder.getWidth()*decoder.getHeight());
decoder.decode(buf, decoder.getWidth()*4, Format.RGBA);
th = decoder.getHeight();
tw = decoder.getWidth();
System.out.println(tw+"/"+th);
buf.flip();
}
finally
{
in.close();
}
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, tw, th, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
System.out.println(GL11.glGetError()==GL11.GL_NO_ERROR);
}
}

private boolean checkExitKeys()
{
return Keyboard.isKeyDown(Keyboard.KEY_ESCAPE);
}

private void loop()
{
init();
Keyboard.enableRepeatEvents(true);
while(!interrupted)
{

if(Keyboard.getNumKeyboardEvents() > 0)
{
while(Keyboard.next())
{
char key = Keyboard.getEventCharacter();
switch(key)
{
case 'w':
WorldManager.setKey('w',true);
break;
case 'd':
WorldManager.setKey('d',true);
break;
case 's':
WorldManager.setKey('s',true);
break;
case 'a':
WorldManager.setKey('a',true);
break;
}
}

}

if(actCount == 6)
{
List<Entity> objects = WorldManager.getWorld().getObjects(null);
for(int i = 0; i < objects.size(); i++)
{
objects.get(i).act();
}
actCount = 0;
}
Display.sync(60);
renderFrame();
interrupted = checkExitKeys() || Display.isCloseRequested();
Display.update();
actCount++;
}
}

public static final void main(String[] argv)
{
new Main().loop();
}

public SnakeWorld getWorld()
{
if(WorldManager.getWorld() == null)
System.out.println("WTF?!");
return WorldManager.getWorld();
}
}



~Josh