LWJGL Forum

Programming => OpenGL => Topic started by: Glorfindel22 on November 03, 2011, 02:13:23

Title: Following Tutorial: Can't get Texture to display on screen
Post by: Glorfindel22 on November 03, 2011, 02:13:23
Okay I am following a youtube tutorial. The goal of the tutorial was to get it so that when you used the WASD keys the texture moved around the screen using the X, Y coordinates. For some reason the player texture is not rendered on the screen I think I missed something but I am not sure what and there is no source code. Here is my code:
package DragonCraft;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class DragonCraftMenu {


        private static boolean running = true;
        Player player = new Player(100, 100, 32, 32);
               
        public static void main(String[] args) {
            DragonCraftMenu example = new DragonCraftMenu();
          example.run();
     }

        private static void init(int width, int height) throws LWJGLException {
                DisplayMode[] dm = Display.getAvailableDisplayModes();
                for (DisplayMode mode : dm) {
                       if (mode.getHeight() == height && mode.getWidth() == width
                                        && mode.getBitsPerPixel() == 32) {
                                Display.setDisplayMode(mode);
                    }
                }
                Display.setTitle("DragonCraft");
                Display.create();

                GL11.glMatrixMode(GL11.GL_PROJECTION);
                GL11.glLoadIdentity();

                GL11.glOrtho(0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight(), 0, -1, 1);

                GL11.glMatrixMode(GL11.GL_MODELVIEW);
                GL11.glLoadIdentity();

                GL11.glEnable(GL11.GL_ALPHA_TEST);
                GL11.glAlphaFunc(GL11.GL_GREATER, 0.4f);
               
                GL11.glEnable(GL11.GL_TEXTURE_2D);
               
                GL11.glClearColor(0.2f, 0.4f, 0.95f, 1.0f);
               
                System.out.println("loadin tex");
                TextureBank.Instance().loadTexture("Images/player.png", "player");
        }

        private void run() {
                try {
                        init(1024,768);
                       
                        while(running)
                        {
                                exitLogic();                       
                                input();
                                update();
                                render();
                                Display.update();
                        }
                       
                } catch (LWJGLException e) {
                        e.printStackTrace();
                }
                cleanup();

        }

       

        private void input()
        {
        player.Input();
        }

        private void update() {
                // TODO Auto-generated method stub
               
        }

        private void render() {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
            GL11.glLoadIdentity();
           
                player.Render();
                Display.update();

        }


         //Just having the exit logic stored away

        private void exitLogic() {
                if(Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
                {
                        running=false;
                }
        }
       
        private void cleanup() {
                Display.destroy();
                System.exit(0);
        }
}

And the TextureBank:

package DragonCraft;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;

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

public class TextureBank {
private HashMap<String, Texture> textures = new HashMap<String, Texture>();

private static TextureBank textureBank = new TextureBank();

private TextureBank()
{

}

public static TextureBank Instance()
{
return textureBank;
}


public boolean loadTexture(String path, String name)
{
         Texture texture;
         
         try {
                if((texture = TextureLoader.getTexture("PNG", new FileInputStream(path)))!=null)
                {
                        System.out.println(texture);
                        textures.put(name, texture);
                        return true;
                }
        } catch (FileNotFoundException e) {
                e.printStackTrace();
        } catch (IOException e) {
                e.printStackTrace();
        }
         return false;
}

public Texture getTexture(String name)
{
         if(textures.containsKey(name))
         {
                 return textures.get(name);
         }
         return null;
}
}

And Player

package DragonCraft;
import org.lwjgl.input.*;
import org.lwjgl.opengl.GL11;

public class Player {
private float x,y;
private int w,h;

public Player(float X, float Y, int H, int W) {
X=x;
Y=y;
W=w;
H=h;
}
   
public void Input() {
if(Keyboard.isKeyDown(Keyboard.KEY_W))
{
y -= 1;
}
else if(Keyboard.isKeyDown(Keyboard.KEY_S))
{
y += 1;
}
else if(Keyboard.isKeyDown(Keyboard.KEY_A))
{
x -= 1;
}
else if(Keyboard.isKeyDown(Keyboard.KEY_D))
{
         x += 1;
}
}

public void Render()
{
GL11.glBindTexture(GL11.GL_TEXTURE_2D, TextureBank.Instance().getTexture("player").getTextureID());
GL11.glTranslatef(x, y, 0.0f);
GL11.glBegin(GL11.GL_QUADS);

GL11.glTexCoord2d(0.0f, 0.0f);
GL11.glVertex2f(0.0f, 0.0f);
GL11.glTexCoord2d(1.0f, 0.0f);
GL11.glVertex2f(w, 0.0f);
GL11.glTexCoord2d(1.0f, 1.0f);
GL11.glVertex2f(w, h);
GL11.glTexCoord2d(0.0f, 1.0f);
GL11.glVertex2f(0.0f, h);

GL11.glEnd();
GL11.glTranslatef(-x, -y, 0.0f);
}
}
Title: Re: Following Tutorial: Can't get Texture to display on screen
Post by: abcdef on November 03, 2011, 11:10:38
Try looking at

http://nehe.gamedev.net/tutorial/texture_mapping/12038/

There is a lwjgl port, should help you debug things a bit. Also check that

TextureBank.Instance().getTexture("player").getTextureID()

doesn't return -1 which shows its not actually bound the texture successfully