Java LWJGL / Slick-Util texture.bind() binds the same texture everytime - java

I've been programming for a few years now and wanted to try Java LWJGL, and people recommended using the Slick-Util library so I set all of it up, etc. and I've been working on several projects, never encountering this problem. However, I've encountered a problem where when binding a texture to openGL, the same incorrect texture is instead bound every time. I ran into this problem first with a project with a bunch of classes etc, and chose to try to condense the problem into one jar file.
So before I show the code, I wanna explain how the example file works. Basically, there are four PNG's within my project. They are contained in res/textures/character/texturesHere. The names of the PNG's consist as: char_d.png, char_u.png, char_l.png, char_r.png. I've checked all four and all four have different textures of the character's direction, as it should be. This is to prove that the resources of the project are NOT the problem. Here is the entire problem condensed into a "Main" class file.
package main_p;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import static org.lwjgl.opengl.GL11.*;
public class Main
{
public final int srcWidth = 1280;
public final int srcHeight = 720;
public float x = 150;
public float y = 150;
public float w = 40;
public float h = 40;
public Texture char_down;
public Texture char_right;
public Texture char_left;
public Texture char_up;
public Main()
{
try
{
Display.setTitle("Escape Alpha v0.0.1");
Display.setDisplayMode(new DisplayMode(srcWidth, srcHeight));
Display.create();
}
catch (LWJGLException e)
{
e.printStackTrace();
}
initGL();
initTextures();
loop();
}
public void loop()
{
glClear(GL_COLOR_BUFFER_BIT);
while (!Display.isCloseRequested())
{
glBegin(GL_QUADS);
char_left.bind();
glTexCoord2f(0, 0);
glVertex2f(x, y);
glTexCoord2f(1, 0);
glVertex2f(x + w, y);
glTexCoord2f(1, 1);
glVertex2f(x + w, y + h);
glTexCoord2f(0, 1);
glVertex2f(x, y + h);
glEnd();
Display.update();
Display.sync(60);
}
Display.destroy();
}
public void initGL()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, srcWidth, srcHeight, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
public void initTextures()
{
char_down = loadTextureFrom("character/char_d.png");
char_up = loadTextureFrom("character/char_u.png");
char_left = loadTextureFrom("character/char_l.png");
char_right = loadTextureFrom("character/char_r.png");
}
public Texture loadTextureFrom(String path)
{
try
{
return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/textures/" + path)));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
public static void write(Object ob)
{
System.out.println(ob);
}
public static void main(String[] args)
{
new Main();
}
}
One thing I've noticed is that within the initTextures() method, if I change the LAST loaded texture, ANY texture I bind will end up rendering the last texture loaded in initTextures()
In this example I bind Texture char_left, but instead it uses char_right, because it was the last one loaded in my initTextures() method. I can prove this because if I change whichever texture is LAST loaded in my initTextures() method, it ends up rendering that one instead, again disregarding the texture I tell it to bind. Like I said, I've checked all of my PNG's multiple times, and they all look good, and show the character facing the correct direction. My loadTextureFrom(String path) method is as any other texture loader. Nothing special about it really. I actually am using this exact method in 90% of my 2D projects for Java LWJGL / Slick-Util, and I've only recently started running into this problem. I'm at a loss for words, and have never ran into this problem before. Any help is greatly appreciated. :)
Edit 1:
I just did an important test. I checked the HashCode of all four textures, AFTER loading them within the initTextures() method.
public void initTextures()
{
char_left = loadTextureFrom("character/char_l.png");
char_right = loadTextureFrom("character/char_r.png");
char_down = loadTextureFrom("character/char_d.png");
char_up = loadTextureFrom("character/char_u.png");
write(System.identityHashCode(char_left));
write(System.identityHashCode(char_right));
write(System.identityHashCode(char_down));
write(System.identityHashCode(char_up));
}
It returned this:
1364345882
1878339755
1246651385
1619367563
I think this truly proves that the initTextures() method isn't the problem. Once again, any help is appreciated! This kind of 100% holds me back from doing any more work on any projects :/ Thanks! :)

I know this is an old post but I don't want anyone who sees this to go answerless. Basically, the texture bind call has to be outsde the "glBegin". That's all the problem is. You cannot bind textures inside of the glBegin call. If you don't do this, gl will just bind the last texture loaded.

I dont know if this will actually help but you have a problem in your loop method. Basically you are not clearing you screen. You do glClear(GL_COLOR_BUFFER_BIT); before your loop. Another thing that might cause this sort of problem is that you actually have to enable and disable GL_BLEND in a loop. Also almost forgot you didn't enable glEnable(GL_TEXTURE_2D); in your initGl() method.
public void loop()
{
while (!Display.isCloseRequested())
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_QUADS);
char_left.bind();
glTexCoord2f(0, 0);
glVertex2f(x, y);
glTexCoord2f(1, 0);
glVertex2f(x + w, y);
glTexCoord2f(1, 1);
glVertex2f(x + w, y + h);
glTexCoord2f(0, 1);
glVertex2f(x, y + h);
glEnd();
glDisable(GL_BLEND);
Display.update();
Display.sync(60);
}
Display.destroy();
}

Hmm try this and see if it works. I use it in my 2d game it works there.
package game.utils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.util.ResourceLoader;
public class TextureLoader {
private static Map<URL, Texture> texMap = new HashMap<>();
public static void bindTexture(URL tex) {
try {
Texture texture = texMap.get(tex);
if (texture == null) {
texture = loadTexture(tex);
}
texture.bind();
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
} catch (IOException e) {
// TODO Auto-generated catch block.
e.printStackTrace();
}
}
public static void bindTexture(String tex) {
try {
bindTexture(ResourceLoader.getResource(tex));
} catch (Exception e) {
bindTexture("res/other/white.png");
}
}
private static Texture loadTexture(URL tex) throws FileNotFoundException, IOException {
System.out.printf("Loading texture %s (%s)%n", texMap.size(), tex.getFile());
Texture texture = org.newdawn.slick.opengl.TextureLoader.getTexture("PNG", tex.openStream());
texMap.put(tex, texture);
return texture;
}
}
You can change the "res/other/white.png" to whatever your missing texture file is. Also the glTexParameterf lines are for disabling mipmap/filtering/whatever it's called.

Related

Java Physics loop (repaint on interval) is choppy unless mouse is moving

I have a simple physics loop that does a calculation for a time interval, waits for the interval to pass, and then renders the results on the screen. It's very simple code (even though the timing is probably wrong, but that's exactly what I'm trying to learn about) and works well when I am moving the mouse around the screen.
package physicssim;
import java.awt.Graphics;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PhysicsSim extends JFrame {
private static class PhysicsObject {
public PhysicsObject(double x, double y, double v_x, double v_y) {
this.x = x;
this.y = y;
this.v_x = v_x;
this.v_y = v_y;
}
public double x;
public double y;
public double v_x;
public double v_y;
}
PhysicsObject particle;
boolean running = true;
DrawPane drawPane;
public PhysicsSim() {
particle = new PhysicsObject(10,10, .1, .2);
drawPane = new DrawPane(particle);
this.setSize(800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(drawPane);
this.setVisible(true);
}
private static class DrawPane extends JPanel {
PhysicsObject p;
public DrawPane(PhysicsObject p) {
this.p = p;
}
#Override
public void paint(Graphics g) {
super.paint(g); //To change body of generated methods, choose Tools | Templates.
g.fillOval((int)p.x, (int) p.y, 10, 10);
}
}
public void start() {
int FPS = 60;
long TIME_BETWEEN_FRAMES_NS = 1000000000/FPS;
// Initial draw
drawPane.repaint();
long lastDrawTime = System.nanoTime();
while(running) {
// Update physics
particle.x+=particle.v_x*(TIME_BETWEEN_FRAMES_NS*.0000001);
particle.y+=particle.v_y*(TIME_BETWEEN_FRAMES_NS*.0000001);
// While there is time until the next draw wait
while(TIME_BETWEEN_FRAMES_NS > (System.nanoTime()-lastDrawTime)) {
try {
Thread.sleep(1);
} catch (InterruptedException ex) {
Logger.getLogger(PhysicsSim.class.getName()).log(Level.SEVERE, null, ex);
}
}
drawPane.repaint();
long currentTime = System.nanoTime();
System.out.println(currentTime - lastDrawTime);
lastDrawTime = currentTime;
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
PhysicsSim sim = new PhysicsSim();
sim.start();
}
}
The last bit about printing the time difference was just a sanity check to make sure that it was in fact calling around the requested interval. The results are fairly consistent so I don't see why there should be any choppiness.
As I mentioned above, this code works great if I a moving the mouse around the screen, everything is smooth.
If I am not moving the mouse it becomes very choppy until I start moving the mouse over the application.
I assume this is something simple, but I hope that you guys can help me. Thank you.
Alright, it looks like my problem was I was drawing directly to g in paint(). After replacing with the following everything worked correctly.
#Override
public void paint(Graphics g) {
BufferedImage img = new BufferedImage(800, 600, BufferedImage.TYPE_3BYTE_BGR);
img.getGraphics().fillOval((int) p.x, (int) p.y, 10, 10);
g.drawImage(img, 0, 0, null);
}
I was considering deleting this code snippet because it's rough and shameful, but maybe it will help someone else. Happy coding.

Running LWJGL on a Raspberry Pi

I need to port something written in Java with LWJGL to a Raspberry. Im using Raspbian and tried oracles java version and some called "openjdk" or something like that.
With both versions I get this Exception:
java.lang.UnsatisfiedLinkError: org.lwjgl.opengl.GLContext.nLoadOpenGLLibrary()V
when creating the "Display".
I already searched for some solution, but they refer to OpenGLES and I did never use OpenGLES nor I found any download to use it.
I have no clue what to do and what information you might need, just comment if you need some more input.
edit:
Well you might want my current sourcecode too:
import java.io.File;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;
public class Main {
private int fps;
private long lastFPS, lastTime;
Main() {
createWindow();
init2D();
loop();
}
private void init2D() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 500, 400, 0, -1, 1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glDisable(GL11.GL_DEPTH_TEST);
lastFPS = getTime();
getDelta();
}
private long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
public void updateFPS() {
if (getTime() - lastFPS > 1000) {
Display.setTitle(Integer.toString(fps));
fps = 0;
lastFPS += 1000;
}
fps++;
}
private int getDelta() {
long time = getTime();
int delta = (int) (time - lastTime);
lastTime = time;
return delta;
}
private void loop() {
float rot = 0;
while(!Display.isCloseRequested()&&!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glLoadIdentity();
int x = Display.getDisplayMode().getWidth()/2;
int y = Display.getDisplayMode().getHeight()/2;
GL11.glTranslated(x, y, 0);
GL11.glRotated(rot, 0, 0, 1);
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
GL11.glColor3f(1, 0, 0);
} else {
GL11.glColor3f(1, 1, 1);
}
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2d(-50, -50);
GL11.glVertex2d(50, -50);
GL11.glVertex2d(50, 50);
GL11.glVertex2d(-50, 50);
GL11.glEnd();
rot+=0.05*getDelta();
Display.update();
updateFPS();
}
}
private void createWindow() {
try {
Display.setDisplayMode(new DisplayMode(500, 400));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
} // Window is created in this try-catch
try {
Assetloader.load();
} catch (Exception e) {
} // Load Assets
}
public static void main(String args[]) {
System.setProperty("org.lwjgl.librarypath", new File("native").getAbsolutePath());
new Main();
}
}
Raspberry Pi's only support OpenGL ES 2.0 and they're isn't any way around that.
OpenGL ES has no immediate mode rendering (glBegin, glVertex, glColor). You'll have to load your geometry into VBO's and render that way.
You'll also need to write basic shaders and pass your projection and modelview matrices in via glUniformMatrix.
Immediate mode rendering is pretty much only available on the desktop, iPhone's, Android's and Blackberry's use only OpenGL ES and pretty much any game console will use an API very similiar to OpenGL ES.
Bottom line: You'll need to ditch the immediate mode rendering and learn to use the ES spec.
These links should help...
http://www.khronos.org/registry/gles/specs/2.0/es_full_spec_2.0.25.pdf
http://www.khronos.org/files/opengles_shading_language.pdf

LWJGL - How to create a button to close the application

I have made it open a full screen window but now how can i create a button to have it exit the application?
Also, do you know any good tutorials to learn. I can't seem to find many?
Lastly can i use the opengl code that i learn to work with java in c++ or is that opengl completely different?
This is the code i have:
package game;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.*;
import org.lwjgl.*;
public class Main {
public Main() {
try {
Display.setDisplayMode(Display.getDesktopDisplayMode());
Display.setFullscreen(true);
Display.create();
} catch(LWJGLException e) {
e.printStackTrace();
}
}
}
lwjgl does not provide any high level widgets such as buttons. You'll need to draw the button using gl calls (use the button image as texture for a quad. Start with a colored rectangle before trying textures). Then you'll need to check for mouse click events in the button area. You may want to consider using a higher level library on top of lwjgl to simplify this.
Here is some code that I made that draws and handles buttons.
You can specify the X, Y and Texture of each button and when the button is clicked the variable isClicked becomes true. As for closing the application , use
if(EXITBUTTON.isClicked)
{
System.exit(0);
}
Button Class:
You need LWJGL and Slick Util.
import java.awt.Rectangle;
import java.io.IOException;
import org.lwjgl.input.Mouse;
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;
public class Button {
public int X;
public int Y;
public Texture buttonTexture;
public boolean isClicked=false;
Rectangle bounds = new Rectangle();
public void addButton(int x, int y , String TEXPATH){
X=x;
Y=y;
try {
buttonTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(TEXPATH));
System.out.println(buttonTexture.getTextureID());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bounds.x=X;
bounds.y=Y;
bounds.height=buttonTexture.getImageHeight();
bounds.width=buttonTexture.getImageWidth();
System.out.println(""+bounds.x+" "+bounds.y+" "+bounds.width+" "+bounds.height);
}
public void Draw(){
if(bounds.contains(Mouse.getX(),(600 - Mouse.getY()))&&Mouse.isButtonDown(0)){
isClicked=true;
}else{
isClicked=false;
}
Color.white.bind();
buttonTexture.bind(); // or GL11.glBind(texture.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(X,Y);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(X+buttonTexture.getTextureWidth(),Y);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(X+buttonTexture.getTextureWidth(),Y+buttonTexture.getTextureHeight());
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(X,Y+buttonTexture.getTextureHeight());
GL11.glEnd();
}
}

Java LWJGL Slick UnicodeFont acting up

I'm having some problems displaying text on the screen, in the past I have just used sprite based text, however this time I want to use UnicodeFont. TrueTypeFonts draw perfectly however its deprecated.
When I try to draw the UnicodeFont it seems to be affected by the characters I use. for example If I draw the string "stackoverflow" the text and the box will draw, if I try "stackoverflowcom" the box will not draw.
A barebones version of my source code is below. On line ~74 I call uniFont.drawString(0, 0,"stackoverflow"); , if the com (or anything really) the box will not be drawn.
edit. > You can use the boolean tryUnicode to swap between true and unicode.
Ive tried sticking them in to seperate display lists but it made no difference.
Could anyone offer an insight in to why this is happening?
Thanks
import java.awt.Font;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import static org.lwjgl.opengl.GL11.*;
public class Game {
private UnicodeFont uniFont;
private TrueTypeFont truFont;
public static void main(String[] argv) {
Game game = new Game();
game.start();
}
public void start()
{
initGL(600, 600);
initFonts();
while(!Display.isCloseRequested()) //display not closed
{
render();
Display.update();
Display.sync(60);
}
Display.destroy();
System.exit(0);
}
private void render()
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(-0.25f,0.7f,0);
glScalef(0.001f,-0.001f,0.001f);
glEnable(GL_BLEND);
boolean tryUnicode = false;
if(tryUnicode)
{
uniFont.drawString(0, 0,"stackoverflow");
//EDIT.. glDisable texture is required here.
}else
{
glScalef(1.1f,1.1f,1f);
truFont.drawString(0, 0, "stackoverflow truFont");
}
glDisable(GL_BLEND);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.25f,0,0);
glColor3f(0.5f, 0f, 0f);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(0, 0,0.0f);
glVertex3f(0.5f, 0,0f);
glVertex3f(0f,0.5f,0f);
glVertex3f(0.5f, 0.5f,0f);
glEnd();
glPopMatrix();
}
private void initGL(int width, int height) {
try {
Display.setDisplayMode(new DisplayMode(width,height));
Display.create();
//Display.setVSyncEnabled(true);
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
glEnable(GL11.GL_TEXTURE_2D);
glShadeModel(GL11.GL_SMOOTH);
glEnable(GL11.GL_DEPTH_TEST);
glDisable(GL11.GL_LIGHTING);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1);
glEnable(GL_BLEND);
glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_BLEND);
glMatrixMode(GL11.GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
glMatrixMode(GL11.GL_MODELVIEW);
}
private void initFonts() {
Font awtFont = new Font("", Font.PLAIN,55);
truFont = new TrueTypeFont(awtFont, true);
uniFont = new UnicodeFont(awtFont, 128, false, false);
uniFont.addAsciiGlyphs();
uniFont.addGlyphs(400,600); // Setting the unicode Range
uniFont.getEffects().add(new ColorEffect(java.awt.Color.white));
try {
uniFont.loadGlyphs();
} catch (SlickException e) {};
}
}
It seems I have this error now, it will only draw the outlined rectangle before using the font. This seems to be a Slick error, and is a major problem so they should fix it before adding new features.
The only work-around is to render fonts last on your frame.
EDIT: Problem Solved!
add this line after you render your font:
GL11.glDisable(GL11.GL_TEXTURE_2D);
The Slick people should really add that line in as it causes so many bugs!
This is how I create fonts:
Font font = ...; //Your loaded font
float size = 20.0F;
UnicodeFont f = new UnicodeFont(font.deriveFont(0 /*normal*/, size));
f.addAsciiGlyphs();
ColorEffect e = new ColorEffect();
e.setColor(java.awt.Color.white);
f.getEffects().add(e);
try {
f.loadGlyphs();
} catch (SlickException e1) {
e1.printStackTrace();
}
return f;
Hope this helped!

Loading Obj files in Libgdx not working on android

The following code shows a torus slowly revolving and coming into display:
package com.objloader.example;
import ...
public class ObjLoaderProg implements ApplicationListener{
String torus;
Mesh model;
private PerspectiveCamera camera;
#Override
public void create() {
InputStream stream=null;
try {
stream = new FileInputStream(Gdx.files.internal("data/torus.obj").path());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
model = ObjLoader.loadObj(stream, true);
Gdx.gl.glEnable(GL10.GL_DEPTH_TEST);
Gdx.gl10.glTranslatef(0.0f,0.0f,-3.0f);
}
#Override
public void dispose() {
}
#Override
public void pause() {
}
protected int lastTouchX;
protected int lastTouchY;
protected float rotateZ=0.01f;
protected float increment=0.01f;
#Override
public void render() {
Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
camera.update();
camera.apply(Gdx.gl10);
Gdx.gl10.glTranslatef(0.0f,0.0f,-3.0f);
Gdx.gl10.glRotatef(rotateZ, rotateZ, 5.0f, rotateZ);
model.render(GL10.GL_TRIANGLES);
if (Gdx.input.justTouched()) {
lastTouchX = Gdx.input.getX();
lastTouchY = Gdx.input.getY();
} else if (Gdx.input.isTouched()) {
camera.rotate(0.2f * (lastTouchX - Gdx.input.getX()), 0, 1.0f, 0);
camera.rotate(0.2f * (lastTouchY - Gdx.input.getY()), 1.0f, 0, 0);
lastTouchX = Gdx.input.getX();
lastTouchY = Gdx.input.getY();
}
rotateZ+=increment;
System.out.println(""+rotateZ);
}
#Override
public void resize(int arg0, int arg1) {
float aspectRatio = (float) arg0 / (float) arg1;
camera = new PerspectiveCamera(67, 2f * aspectRatio, 2f);
camera.near=0.1f;
camera.translate(0, 0, 0);
}
#Override
public void resume() {
}
}
It renders a torus obj that's saved in the data folder, and by clicking and dragging on the screen the user can rotate the camera.
This works fine on the desktop, but when I try to run it on android, I get a NullPointerException at:
model.render(GL10.GL_TRIANGLES);
I've tried placing torus.obj just inside assets, and within assets/data. I'm using libgdx 0.9.2.
I assume you are referring to model.render(GL10.GL_TRIANGLES);. I believe you have two problems. First, model is null because you are catching a FileNotFoundException and ignoring it. I suggest you don't catch the FileNotFoundException right now, let it crash, and look at the stack trace. That will give you a better indication of why this failing. Note that e.printStackTrace() is not useful for debugging on android, try using the gdx log.
The second problem is that I suspect the path/stream is actually going to the wrong place. Instead of creating a FileInputStream, use the FileHandle.read() function. It returns a java.io.InputStream that you can pass to ObjLoader.loadObj().
in = Gdx.files.internal("data/torus.obj").read();
model ObjLoader.loadObj(in);
in.close();
The difference between this and your code is that FileHandle.read() in libgdx's android backend uses the android AssetManager to open files that are bundled with the program.
Finally, copy the torus.obj file to <YourAndroidProject>/assets/data/torus.obj.
An aside: if you specify the line number, please provide the full file otherwise the line will be off. Line 52 of the code you have provided is: lastTouchY = Gdx.input.getY();. Note the "import ..." at the beginning of your code. Those imports affect the line number.

Categories