LWJGL Jumping Down - java

I'm currently building a little game using LWJGL, and I got a Game class which contains my game loop, instances etc... And a Player class which contains a constructor (for x and y coords), logic method for calculations, key presses etc... and a render method for drawing the player on the screen.
For some reason, when I press UP (Key.UP) the player jumps down the floor, and the goes up to the floor (inverted, instead of jumping up and then falling down to the floor).
I would really appreciate if you could tell me what the problem is.
Game.java:
import static org.lwjgl.opengl.GL11.*;
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class Game {
static Texture bgTexture;
static Player player;
public static void main(String[] args) throws LWJGLException, IOException {
// Initialize
Display.setDisplayMode(new DisplayMode(640, 480));
Display.create();
glEnable(GL_TEXTURE_2D);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glViewport(0, 0, 640, 480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
bgTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/background.png"));
player = new Player(50, 200 - 64);
while (!Display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT);
drawBackground();
player.render();
Display.update();
Display.sync(60);
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
Display.destroy();
System.exit(0);
}
}
}
public static void drawBackground() {
bgTexture.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(1, 0);
glVertex2f(bgTexture.getTextureWidth(), 0);
glTexCoord2f(1, 1);
glVertex2f(bgTexture.getTextureWidth(), bgTexture.getTextureHeight());
glTexCoord2f(0, 1);
glVertex2f(0, bgTexture.getTextureHeight());
glEnd();
}
}
Player.java
import static org.lwjgl.opengl.GL11.*;
import java.io.IOException;
import org.lwjgl.input.Keyboard;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class Player {
public double x, y, xspeed, yspeed;
private Texture pTexture;
public Player(double x, double y) throws IOException {
this.x = x;
this.y = y;
pTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/player.png"));
}
public void logic() {
x += xspeed;
y += yspeed;
yspeed -= 0.4;
// Collision detection
if (y <= 480 - 64) {
y = 480 - 64;
yspeed = 0;
if (!Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && xspeed > 0) xspeed = xspeed * 0.7;
if (!Keyboard.isKeyDown(Keyboard.KEY_LEFT) && xspeed < 0) xspeed = xspeed * 0.7;
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) yspeed = 8;
}
// Moving
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) xspeed = 4;
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) xspeed = -4;
}
public void render() {
logic();
glPushMatrix();
pTexture.bind();
glTranslated(x, y, 0);
glBegin(GL_QUADS);
glTexCoord2d(0, 0);
glVertex2d(0, 0);
glTexCoord2d(1, 0);
glVertex2d(pTexture.getTextureWidth(), 0);
glTexCoord2d(1, 1);
glVertex2d(pTexture.getTextureWidth(), pTexture.getTextureHeight());
glTexCoord2d(0, 1);
glVertex2d(0, pTexture.getTextureHeight());
glEnd();
glPopMatrix();
}
}
Thank you so much.

You are calling glOrtho(0, 640, 480, 0, 1, -1); which sets 480 to the bottom of the screen and 0 to the top. if you instead use glOrtho(0, 640, 0, 480, 1, -1); A larger y value will mean a higher point on the screen, which is what you want I think.

Related

OpenGL transparency not working

I am trying to draw my font texture atlas to the screen using LWJGL, but OpenGL instead draws a solid white square.
A working example using my drawing code:
import java.awt.image.*;
import java.io.*;
import java.nio.*;
import javax.imageio.*;
import org.lwjgl.*;
import org.lwjgl.opengl.*;
public class OpenGLImageTest
{
private static int textureID;
public static void main(String[] args) throws Exception
{
Display.setTitle("OpenGL Image Test");
Display.setDisplayMode(new DisplayMode(640, 480));
Display.create();
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 640, 0, 480, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
textureID = bindTextureFile("textures/font.png");
while(!Display.isCloseRequested())
{
Display.sync(60);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glClearColor(0, 0, 0, 1);
GL11.glColor4f(1, 0, 0, 1);
drawFontAtlas(0, 0);
Display.update();
}
Display.destroy();
}
private static void drawFontAtlas(int x, int y)
{
GL11.glPushMatrix();
GL11.glTranslatef(x, y, 0);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2i(0, 0);
GL11.glVertex2i(0, 256);
GL11.glVertex2i(256, 256);
GL11.glVertex2i(256, 0);
GL11.glEnd();
GL11.glDisable(GL11.GL_BLEND);
GL11.glDisable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glPopMatrix();
}
private static int bindTextureFile(String file)
{
try
{
BufferedImage image = ImageIO.read(new FileInputStream(file));
int[] pixels = new int[image.getWidth() * image.getHeight()];
image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);
for(int y = 0; y < image.getWidth(); y++)
{
for(int x = 0; x < image.getHeight(); x++)
{
int pixel = pixels[y * image.getWidth() + x];
buffer.put((byte)((pixel >> 16) & 0xFF));
buffer.put((byte)((pixel >> 8) & 0xFF));
buffer.put((byte)(pixel & 0xFF));
buffer.put((byte)((pixel >> 24) & 0xFF));
}
}
buffer.flip();
int textureID = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB8, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
return textureID;
}
catch(Exception e)
{
e.printStackTrace();
}
return -1;
}
}
Can anyone tell me what I am doing wrong and how to fix it?
Edit: font.png is white on transparent white. It's the default Minecraft font for testing purposes.
Your problem can be fixed by adding texture coordinates:
{
GL11.glTexCoord2f(0, 1); // added texture coordinate
GL11.glVertex2i(0, 0);
GL11.glTexCoord2f(0, 0); // added texture coordinate
GL11.glVertex2i(0, 256);
GL11.glTexCoord2f(1, 0); // added texture coordinate
GL11.glVertex2i(256, 256);
GL11.glTexCoord2f(1, 1); // added texture coordinate
GL11.glVertex2i(256, 0);
}
I actually used slick-util.jar to load your texture, but it will work with your code just the same... you can find a working example of my code bellow... slick-util.jar can be found here
In order to get actual transparency, you also need to change this line in your code: GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB8, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer); and what needs to be changed is GL11.GL_RGB8 into GL11.GL_RGBA
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
public class OpenGLImageTest {
private static Texture tex; //private static int textureID;
public static void main(String[] args) throws Exception {
Display.setTitle("OpenGL Image Test");
Display.setDisplayMode(new DisplayMode(640, 480));
Display.create();
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 640, 0, 480, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
tex = loadTexture("textures/font.png"); //textureID = bindTextureFile("textures/font.png");
while (!Display.isCloseRequested()) {
Display.sync(60);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glClearColor(0, 0, 0, 1);
GL11.glColor4f(1, 1, 1, 1);
drawFontAtlas(0, 0);
Display.update();
}
Display.destroy();
}
private static void drawFontAtlas(int x, int y) {
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glPushMatrix();
GL11.glTranslatef(x, y, 0);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_BLEND);
tex.bind(); //GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
GL11.glBegin(GL11.GL_QUADS);
{
GL11.glTexCoord2f(0, 1); // added texture coordinate
GL11.glVertex2i(0, 0);
GL11.glTexCoord2f(0, 0); // added texture coordinate
GL11.glVertex2i(0, 256);
GL11.glTexCoord2f(1, 0); // added texture coordinate
GL11.glVertex2i(256, 256);
GL11.glTexCoord2f(1, 1); // added texture coordinate
GL11.glVertex2i(256, 0);
}
GL11.glEnd();
GL11.glDisable(GL11.GL_BLEND);
GL11.glDisable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glPopMatrix();
}
public static Texture loadTexture(String texturePath) {
try {
return TextureLoader.getTexture("png", new FileInputStream(new File(texturePath)));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

Can not draw anything after text rendering initialization code in lwjgl (Java)

Hello I have two seperate initialization codes to switch between rendering 2d shapes and (2d) text in lwjgl. If the initialization code for rendering text is executed, the 2d shapes will not be drawn. I tried everything, and I found the problem line: GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
After I have done something with the glBlendFunc, I can only render tekst, and switching to the initialization code for rendering 2d shapes won't work anymore.
Here are my 2 codes:
Simple 2d rendering:
GL11.glEnable(GL_BLEND);
GL11.glMatrixMode(GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
GL11.glMatrixMode(GL_MODELVIEW);
GL11.glLoadIdentity();
Code for rendering text:
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1);
GL11.glEnable(GL11.GL_BLEND);
// Problem line
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0, 800, 600);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
I think the problem is just a wrong OpenGl state, but how can I put the states right?
It's hard to find out the issue without access to your whole code, thus I can just post some guesses:
Do you clear the colour and depth buffer (glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);)?
Do you enable GL_TEXTURE_2D when drawing textures and disable it otherwise?
I use the same blend function and can draw images just fine (it's the most used blend function for drawing images that have transparency).
PS: This are the only settings I use to draw any kind of 2D stuff (I'm using vertex array objects and shaders for rendering though):
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_MULTISAMPLE);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glDisable(GL_DEPTH_TEST);
Here is more code
Renderer.java
package game;
import static org.lwjgl.opengl.GL11.GL_BLEND;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.util.glu.GLU.gluPerspective;
import static org.lwjgl.opengl.GL11.GL_CONSTANT_COLOR;
import static org.lwjgl.opengl.GL11.GL_ONE;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
public class Renderer {
private static boolean in3d = false;
public static void initText2D() {
in3d = false;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0, 800, 600);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
public static void init2D() {
in3d = false;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
GL11.glEnable(GL_BLEND);
GL11.glMatrixMode(GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
GL11.glMatrixMode(GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glDisable(GL_TEXTURE_2D);
// Test
//GL11.glBlendFunc(GL_CONSTANT_COLOR, GL_ONE);
}
public static void init3D(float fov, float aspect, float near, float far) {
in3d = true;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, aspect, near, far);
GL11.glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
}
public static void begin(int shape) {
GL11.glBegin(shape);
}
public static void end() {
GL11.glEnd();
}
public static void setVertex3(float x, float y, float z) {
if(!in3d) {
System.out.println("[WARNING] > Adding 3d vertex but there is no 3d context");
}
GL11.glVertex3f(x, y, z);
}
public static void setColor(float r, float g, float b) {
GL11.glColor3f(r, g, b);
}
public static void setVertex2(float x, float y) {
if(in3d) {
System.out.println("[WARNING] > Adding 2d vertex while in 3d context");
}
GL11.glVertex2f(x, y);
}
}
Button.java (example of how I draw a button in lwjgl)
package gui;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glVertex2f;
import game.Renderer;
import game.TextRenderer;
import org.lwjgl.opengl.GL11;
public class Button extends AbstractButton {
private String text;
public Button(int x, int y, int w, int h, String text) {
this.setX(x);
this.setY(y);
this.setWidth(w);
this.setHeight(h);
this.text = text;
paintElement();
}
public Button(String text) {
this.text = text;
paintElement();
}
public Button() {
paintElement();
}
public void paintElement() {
Renderer.init2D();
Renderer.begin(GL11.GL_QUADS);
Renderer.setColor(this.getColorR(), this.getColorG(), this.getColorB());
Renderer.setVertex2(this.getX(), this.getY());
Renderer.setVertex2(this.getX() + this.getWidth(), this.getY());
Renderer.setVertex2(this.getX() + this.getWidth(), this.getY() + this.getHeight());
Renderer.setVertex2(this.getX(), this.getY() + this.getHeight());
Renderer.end();
Renderer.initText2D();
TextRenderer.drawString(this.getX() + 10, this.getY() + 10, this.text);
}
}
Main.java
package game;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.glVertex2f;
import java.awt.Font;
import gui.Button;
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.TrueTypeFont;
public class Main {
private int gamestate;
private boolean closeRequested;
private static Camera cam;
private static User user;
public Main() {
createUser();
createDisplay();
createCamera();
gameLoop();
cleanUp();
}
private void createUser() {
}
private void createDisplay() {
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.setResizable(true);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
}
private void createCamera() {
}
private void gameLoop() {
while(!closeRequested) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Renderer.init2D();
/*
// Test 2d line
Renderer.init2D();
Renderer.setColor(1.0f, 1.0f, 1.0f);
Renderer.begin(GL11.GL_LINE);
Renderer.setVertex2(0, 0);
Renderer.setVertex2(100, 100);
Renderer.end();
*/
// set the color of the quad (R,G,B,A)
glColor3f(0.7f, 0.5f, 1.0f);
// draw quad
glBegin(GL_QUADS);
glVertex2f(100,100);
glVertex2f(100+200,100);
glVertex2f(100+200,100+200);
glVertex2f(100,100+200);
glEnd();
checkInput();
Button b = new Button(100, 100, 100, 30, "Test");
Display.update();
Display.sync(10);
}
return;
}
private void checkInput() {
// Check keyboard, mouse and other input
if(Display.isCloseRequested()) {
closeRequested = true;
}
return;
}
private void cleanUp() {
}
public static void main(String[] args) {
new Main();
}
}
I got it working!
This is my new (working) code:
init2d
GL11.glEnable(GL_BLEND);
GL11.glMatrixMode(GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
GL11.glMatrixMode(GL_MODELVIEW);
GL11.glLoadIdentity();
// solution
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
initText2d
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0, 800, 600);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
I think the problem was that slick uses textures for text rendering, and I had to enable the textures or bind them to 0.
See this: http://lwjgl.org/forum/index.php?topic=4019.0

LWJGL and Slick textures display as black...?

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).

Java lwjgl and slick are not displaying my textures

I have tried a lot of different things, but nothing worked so far. at first, the problem was that there was just a blank screen when the program was run. I finally got it to display a blank white square. My images are 128x128 and the other is 512x512.
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
{
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())
{
wood.release();
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.glEnable(GL11.GL_TEXTURE);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, 0, height, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
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);
System.out.println("working." + wood.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, wood.getTextureID());
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(x, y);
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(x + wood.getImageWidth(), y);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(x + wood.getImageWidth(), y + wood.getImageHeight());
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(x, y + wood.getImageHeight());
GL11.glEnd();
System.out.println(wood.getHeight()+ " " +wood.getWidth());
}
public static void main (String[] args)
{
new TextureDemo();
}
}
I just want to be able to see the png's i have in the program. Soany ideas why i'm getting white images? how important is the order of the initGL method?
Not sure about it, but I think you had to glEnable(GL_TEXTURE_2D); in this old deprecated OpenGL style. (Instead of GL_TEXTURE)
Update: I really think you should do glEnable(GL_TEXTURE_2D);. Try using a gray clear color. Maybe they just turned into black squares instead of whites.
Slick has a function to bind the texture (I'm not sure what getTextureID returns).
Update: Make sure you first glBindTexture and then glBegin.
I think your texturecoords are wrong. Try replacing with this:
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());

opengl textures not rendering correctly

The problem i am encountering is that i cannot get my texture to render in the correct proportions and for some reason the texture is also repeating itself with a space inbetween.
this is the texture im using(i am trzing to fill the screen with this texture):
When rendered it looks like this(red outline is the entire screen and texture is rendered with a 1 px border):
and here is the code:
package game;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.Timer;
import org.lwjgl.util.glu.GLU;
import org.newdawn.slick.opengl.TextureLoader;
public class WarZone {
private boolean done = false;
private String windowTitle = "War Zone";
private DisplayMode displayMode;
private Timer timer;
private float dt;
public static void main(String[] args) {
new WarZone().run(false);
}
public void run(boolean fullscreen) {
try {
init();
switchToOrtho();
while (!done) {
timer.tick();
update();
render();
Display.update();
}
cleanup();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
private void update() {
// Exit if Escape is pressed or window is closed
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) || Display.isCloseRequested()) {
done = true;
return;
}
}
private boolean render() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear the screen and the depth buffer
GL11.glLoadIdentity(); // Reset the current modelview matrix
int w = displayMode.getWidth();
int h = displayMode.getHeight();
GL11.glColor3f(1, 0, 0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2i(0, 0);
GL11.glVertex2i(w, 0);
GL11.glVertex2i(w, h);
GL11.glVertex2i(0, h);
GL11.glEnd();
//if(true)return false;
GL11.glColor3f(0, 1, 1);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 1);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2i(1, 1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2i(w - 1, 1);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2i(w - 1, h - 1);
GL11.glTexCoord2f(0, 1);
GL11.glVertex2i(1, h - 1);
GL11.glEnd();
return true; // Rendered correctly
}
public static void switchToOrtho() {
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPushMatrix();
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getDisplayMode().getWidth(), 0, Display.getDisplayMode().getHeight(), -1, 1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
}
public static void switchToFrustum() {
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPopMatrix();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
private void init() throws Exception {
createWindow();
initGL();
load();
}
private void load() throws FileNotFoundException, IOException {
TextureLoader.getTexture("BMP", new FileInputStream("res/temp/Main_Menu_Play_Button.bmp"), true).getTextureID();
}
private void createWindow() throws Exception {
DisplayMode availibleDisplayModes[] = Display.getAvailableDisplayModes();
for (DisplayMode d:availibleDisplayModes) {
if (d.getWidth() == 640 && d.getHeight() == 480 && d.getBitsPerPixel() == 32) {
displayMode = d;
break;
}
}
Display.setDisplayMode(displayMode);
Display.setTitle(windowTitle);
Display.create();
}
private void initGL() {
GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable texture mapping
GL11.glShadeModel(GL11.GL_SMOOTH); // Enable smooth shading
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black background
GL11.glClearDepth(1.0f); // Depth buffer setup
GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables depth testing
GL11.glDepthFunc(GL11.GL_LEQUAL); // Type of depth testing
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select projection matrix
GL11.glLoadIdentity(); // Reset the projection matrix
// Calculate the aspect ratio of the window
GLU.gluPerspective(45.0f, (float)displayMode.getWidth() / (float)displayMode.getHeight(), 0.1f, 100.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);// Select the modelview matrix
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);// Most precise perspective calculations
}
public void requestFinish() {
done = true;
}
private void cleanup() {
Display.destroy();
}
}
I would reallz appreciate it if someone could tell me what i had done wrong.
First, I don't know what TextureLoader or newdawn.slick.opengl are, so there is only limited information I can give about this.
However, it is very possible that your texture loading code does not know how to handle non-power-of-two textures. Which means it is likely padding the texture's size out to the nearest power of two.
More importantly is this:
GL11.glColor3f(0, 1, 1);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 1);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2i(1, 1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2i(w - 1, 1);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2i(w - 1, h - 1);
GL11.glTexCoord2f(0, 1);
GL11.glVertex2i(1, h - 1);
GL11.glEnd();
This will draw a screen-sized quad (assuming that w and h are screen sizes). This quad maps the entire area of the texture to this quad. OpenGL is only doing what you told it to do: take the texture and map it to the quad.
If you want to draw a texture with pixel accuracy (1:1 texel to pixel), then you need to provide a width and height to the vertex positions that is equal to the texture's size, not the screen size.
Also, you set the color to (0, 1, 1). The default texture environment will multiply the per-vertex color by the texel values fetched from the texture. So you should either set the color to white, or change the texture environment.
OpenGL Doesn't like textures that are not powers of two if I recall. Is your texture a power of 2 for both height and width?
http://www.gamedev.net/topic/466904-opengl-textures-only-power-of-two/

Categories

Resources