Complete and Utter Noob Needs Help

Started by Moocman, March 29, 2011, 10:25:33

Previous topic - Next topic

Moocman

This is my first attempts at coding ever, and I'm having a little bit of trouble. I'm trying to get a texture loaded, but whenever I try and do it, I get about 3 errors. I'm using the following image: http://dl.dropbox.com/u/6448609/Arrow.png as my texture. Could someone please tell me what I'm doing wrong?

CODE: http://pastebin.com/NzyYqhnQ


CodeBunny

Um, you should probably follow a tutorial?

Are you new to java? Then I'd recommend not using LWJGL - go with something easier and less finicky than OpenGL to start out with.

Moocman

I have followed tutorials, but I thoroughly dislike them. All I want to do do is load a texture, and I simply can't do it. Yes, I'm new to Java, but I've only had one problem that I can't get past and this is it.

CodeBunny

Well, then this code should work if you're using Slick-Util:

String type; // The file type you're loading: PNG, TGA, JPEG, BMP, etc.
InputStream in; // The input stream to the file you're loading.
texture = TextureLoader.getTexture(type, in);


If you don't use Slick-Util, I don't know what methods you should use, although PNGDecoder has been touted.

Which tutorials? The NinjaCave ones on the LWJGL wiki are very good, and they give you source code examples.

Moocman

It loads the texture fine, but getting it to use the texture is what I'm having trouble with. It doesn't like line 533, which is arrow.bind();. If I delete that, I start getting errors at line 539, which is GL11.glVertex2f(100+arrow.getTextureWidth(),100);. I know there's something I'm missing, but I'm not sure what.

CodeBunny

...Alright. No offense, but a lot of your code indicates you really don't have a clue about how OpenGL works.

First off, if you put up code, please make sure it's properly formatted. This code is a mess and it's hard to discern the structure, which makes it difficult to understand, and makes it harder for us to help you.

More importantly, your GL code is really bad in a lot of cases.

Example:
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glMatrixMode(GL11.GL_MODELVIEW);


What do you think this does???  You pop back and forth between matrix modes and don't do anything in between. This is just a waste of code.

Another:
glClearColor(0.0f,0.0f,0.0f,0.0f);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glClearColor(0.33f, 0.48f, 0.09f, 0.0f);


glClearColor defines what color the background is cleared to when you call glClear(GL_COLOR_BUFFER_BIT). Just set it to your desired color to begin with.

glLoadIdentity();
glPushMatrix();


There is no need to do this. None whatsoever.

Look - despite your prejudices against tutorials, you obviously need to follow some. Please swallow your pride and learn how to do this properly - you'll finish it faster and get better results.

I don't see anything obviously wrong with your texture bind as such, but my guess is that if this program is cleaned up and better thought out it will either resolve itself or the bug will become apparent.

Moocman

On second thought, I have used the NinjaCave tutorials, and they're actually pretty good. I just get carried away and the result is a screwed up program that doesn't work. I TRIED to fix it up, and this is the result. There was a lot of crap in there, mainly because I've asked for the help of many different people.
import org.lwjgl.LWJGLException;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import javax.swing.*;
import java.awt.*;
import java.lang.Math.*;
import java.lang.Object.*;
import java.util.Random.*;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import java.io.FileInputStream;
import java.io.IOException;
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;


/**
 * @author D34N0 and Maxibon
 * @see
 */
public class Main {
  public static final int DISPLAY_HEIGHT = 640; //Applet size
  public static final int DISPLAY_WIDTH = 960;
  public static final Logger LOGGER = Logger.getLogger(Main.class.getName());

  private int squareSize;  
  private int squareX;
  private int squareY;
  private float squareZ;
  private int squareSpeed;
  private int mouseX;
  private int mouseY;
  private float CalculateX;
  private float CalculateY;
  private float dotX;
  private float dotY;
  private float squareXCalc;
  private float squareYCalc;
  private float dotYSpeed;
  private float dotXSpeed;
  private long count;
  private int life = 1;
  private int begin;
  private float deltaX;
  private float deltaY;
  private float squareZDouble;
  private float squareZCalc;
  private int leftButtonDown;
  private int rightButtonDown;
  private Texture arrow;
  private double dotSpeed;


  static {
    try {
      LOGGER.addHandler(new FileHandler("errors.log",true));
    }
    catch(IOException ex) {
      LOGGER.log(Level.WARNING,ex.toString(),ex);
    }
  }

  public static void main(String[] args) { 
    Main main = null;
    try {

      main = new Main();
      main.create();
      main.run();
    }
    catch(Exception ex) {
      LOGGER.log(Level.SEVERE,ex.toString(),ex);
    }
    finally {
      if(main != null) {
        main.destroy();
      }
    }
  }


public void init() throws IOException {
try {
      arrow = TextureLoader.getTexture("PNG", new FileInputStream("res/ttl/Arrow.png"));

}
catch (IOException e) {
			e.printStackTrace();
		}

}

  public Main() { 
    squareSize = 5; 
    squareX = 480;
    squareY = 320;
    squareZ = 0;
    dotX = 600;
    dotY = 600;
    leftButtonDown = 0;
    dotSpeed = 0.15;
      
  }

  public void create() throws LWJGLException {

    Display.setDisplayMode(new DisplayMode(DISPLAY_WIDTH,DISPLAY_HEIGHT));
    Display.setFullscreen(false);
    Display.setTitle("TTL");
    Display.create();
    Keyboard.create();
    Mouse.setGrabbed(false);
    Mouse.create();
    initGL();
    resizeGL();
  }

  public void destroy() {

    Mouse.destroy();
    Keyboard.destroy();
    Display.destroy();
  }

  public void initGL() {

    glDisable(GL_DEPTH_TEST);
    glDisable(GL_LIGHTING);
    GL11.glEnable(GL11.GL_TEXTURE_2D);
		GL11.glClearColor(0.33f, 0.48f, 0.09f, 0.0f); //BACKGROUND (0.33f, 0.48f, 0.09f, 0.0f)
        	GL11.glEnable(GL11.GL_BLEND);
        	GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);

  }
  

  public void processKeyboard() {

    
    if(Keyboard.isKeyDown(Keyboard.KEY_A)){
             squareX = squareX - squareSpeed;
        
  }

    if(Keyboard.isKeyDown(Keyboard.KEY_D)){
        squareX = squareX + squareSpeed;


    }

       if(Keyboard.isKeyDown(Keyboard.KEY_W)){
        squareY = squareY + squareSpeed;
      /*
       if(Keyboard.isKeyDown(Keyboard.KEY_A)){
                squareX = squareX + squareSpeed/2;
                squareY = squareY + squareSpeed/2;
                }

       if(Keyboard.isKeyDown(Keyboard.KEY_D)){
   
                     squareX = squareX + squareSpeed/2;
                     squareY = squareY + squareSpeed/2;
               }
      }
    */
      }
        if(Keyboard.isKeyDown(Keyboard.KEY_S)){
        squareY = squareY - squareSpeed;

 
        /*if(Keyboard.isKeyDown(Keyboard.KEY_D)){
                squareX = squareX - squareSpeed/2;
                squareY = squareY - squareSpeed/2;
         
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_A)){ // GAH! It no recognizey!
              
                squareX = squareX - squareSpeed/2;
                squareY = squareY - squareSpeed/2;
             
        }

*/

        }


    if(Keyboard.isKeyDown(Keyboard.KEY_R)){

        ++begin; 
        
      }

    }


public void pollInput()
    
    {
    if (Mouse.isButtonDown(0)) {
	    if (Mouse.isButtonDown(1)){
        rightButtonDown = 5;
        leftButtonDown = 5;
        squareSpeed = 7;

            }
 else {
        leftButtonDown = 5;
        rightButtonDown = 0;
        squareSpeed = 15;
        }
  }
 else {
        if (Mouse.isButtonDown(1))
        {
         leftButtonDown = 0;
                 rightButtonDown = 5;
                 squareSpeed = 4;
        }
 else {
          leftButtonDown = 0;
          rightButtonDown = 0;
          squareSpeed = 7;
        }
 }

    }

   public void processTrackingX(){
    if (dotX < squareX){
        squareXCalc = squareX - dotX;
       }

    else if (dotX > squareX) {
    
        squareXCalc = dotX - squareX;
    }

    dotSpeed = dotSpeed + 0.001;

  if (dotSpeed > 12)
      dotSpeed = 12;
   }

      public void processTrackingY(){
    if (dotY < squareY){
        squareYCalc = squareY - dotY;
       }

    else if (dotY > squareY)
    {
        squareYCalc = dotY - squareY;
    }

   }

    @SuppressWarnings("SleepWhileHoldingLock")
   public void processDot(){



if (squareYCalc + squareXCalc > dotSpeed){
        while (squareYCalc + squareXCalc > dotSpeed){
        

        squareXCalc = squareXCalc * 0.99f;
        squareYCalc = squareYCalc * 0.99f;

       }
        }

 else if (squareYCalc + squareXCalc < dotSpeed){
     
     //LOOP GOES HERE
     squareXCalc = squareXCalc * 1.01f;
        squareYCalc = squareYCalc * 1.01f;

 
}

       dotXSpeed = squareXCalc;
       dotYSpeed = squareYCalc;
       processDotX();
       processDotY();
       processTrackingX();
       processTrackingY();

    }

   public void processDotX(){
    if (dotX < squareX){
        dotX = dotX + dotXSpeed;

       }

    else if (dotX > squareX)
    {
        dotX = dotX - dotXSpeed;
    }

   }

      public void processDotY(){
    if (dotY < squareY){
        dotY = dotY + dotYSpeed;

       }

    else if (dotY > squareY)
    {
        dotY = dotY - dotYSpeed;

    }

   }

      public void processCollision(){ 

          if  (squareX - dotX < 5)
          {
              if (squareX - dotX >= -5){
                 if (squareY - dotY < 5){
                     if (squareY - dotY >= -5){

                        // -- life;

                         life = 0;
                     }
                  }}}



      if (squareX > 950){
            squareX = 950;
  }
        if (squareX < 20){
            squareX = 20;
  }
        if (squareY > 630){
            squareY = 630;
  }
        if (squareY < 20){
            squareY = 20;
  }

      }


       public void processMouse() {
    mouseX = Mouse.getX();
    mouseY = Mouse.getY();
  
  }

public void calculateAngleSecondary(){
    CalculateX = squareX;
            CalculateY = squareY;

}

public void calculateAnglePrimary(){
    deltaX = mouseX - CalculateX;
    deltaY = CalculateY - mouseY;
    squareZCalc =   (float) (squareZDouble * 180/Math.PI + 90);
    squareZ = 360 - squareZCalc;
    }

    public void calculateAngleTertiary(){
        squareZDouble = (float) Math.atan2(deltaY,deltaX);

}

  public void render() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
   glTranslatef(squareX,squareY,0.0f);
    glRotatef(squareZ,0.0f,0.0f,1.0f);
    glColor3f(1f,1f,1f);

    /*
    glBegin(GL_TRIANGLES);
		glVertex3f( 0.0f, 30.0f, 0.0f);	
		glVertex3f(-15.0f,-8.0f, 0.0f); 
		glVertex3f( 15.0f,-8.0f, 0.0f);
	glEnd();


        */
Color.white.bind();
        // or GL11.glBind(texture.getTextureID());

		GL11.glBegin(GL11.GL_QUADS);
			GL11.glTexCoord2f(0,0);
			GL11.glVertex2f(100,100);
			GL11.glTexCoord2f(1,0);
			GL11.glVertex2f(100+arrow.getTextureWidth(),100);
			GL11.glTexCoord2f(1,1);
			GL11.glVertex2f(100+arrow.getTextureWidth(),100+arrow.getTextureHeight());
			GL11.glTexCoord2f(0,1);
			GL11.glVertex2f(100,100+arrow.getTextureHeight());
		GL11.glEnd();


 

    glLoadIdentity();
    glColor3f(1f,0f,0f);
    glTranslatef(dotX,dotY,0.0f);
    glRotatef(/*dotZ*/0.0f,0.0f,0.0f,1.0f);
           glBegin(GL_QUADS); 
          glVertex2f(-14f,-14f); 
      glVertex2f(14f,-14f);
      glVertex2f(14f,14f); 
      glVertex2f(-14f,14f); 
      glEnd();

  }


  public void resizeGL() {
    //2D Scene
    glViewport(0,0,DISPLAY_WIDTH,DISPLAY_HEIGHT); 

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0f,DISPLAY_WIDTH,0.0f,DISPLAY_HEIGHT);
    glPushMatrix();

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glPushMatrix();
  }

  public void run() { 
    while(!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
      if(Display.isVisible()) {
        processKeyboard();
        processDot();
        processLife();
        processCollision();
        calculateAngleTertiary();
        calculateAngleSecondary();
        calculateAnglePrimary();
        processMouse();
        update();
        render();
        pollInput();
         

      }
      else {
        if(Display.isDirty()) {
          render();
          processDot();

        }
        try {
          Thread.sleep(100);
        }
        catch(InterruptedException ex) {
        }
      }
      Display.update();
      Display.sync(60);
    }
  }

  public void update() { 
    }

  public void processLife() {
      if (life == 0){
        System.exit(1);
      }
  }
}


CodeBunny

Okay. Now, do you ever actually call init()? That defines your texture. If it isn't called, you texture doesn't reference anything, and bind(), getTextureHeight(), etc. all crash immediately. I don't see the call, so I think you just forgot to load your texture.

Java tip: If you ever get an exception with Null in the stack trace, you haven't initialized the variable the stack trace talks about.

Okay, a couple of things:

Why do you have that try-catch-finally in your main? I don't think you need it in any way, but I might be missing something.

A couple of your GL calls don't have GL11, they're just glClear(), GL_COLOR_BUFFER_BIT, etc. Does that actually compile?

And: Your life would be easier if you made this object oriented. Put your input into one class, your math into another, and your rendering into a third at the very least. Then, just have your Main call those classes and make them interact.

Get used to doing that. It makes everything about everything in Java easier - easier to understand, easier to edit without causing your program to crash, easier to detect bugs, easier to fix bugs, easier to finish work quickly, easier to develop the structure of your code... it's a good thing.

Fool Running

Quote from: CodeBunny on March 31, 2011, 11:18:20
A couple of your GL calls don't have GL11, they're just glClear(), GL_COLOR_BUFFER_BIT, etc. Does that actually compile?
He has "import static org.lwjgl.opengl.GL11.*;" so they compile and run just fine. ;D
He actually doesn't need any of the GL11 calls.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

CodeBunny


Moocman

Quote from: CodeBunny on March 31, 2011, 11:18:20

And: Your life would be easier if you made this object oriented. Put your input into one class, your math into another, and your rendering into a third at the very least. Then, just have your Main call those classes and make them interact.

Get used to doing that. It makes everything about everything in Java easier - easier to understand, easier to edit without causing your program to crash, easier to detect bugs, easier to fix bugs, easier to finish work quickly, easier to develop the structure of your code... it's a good thing.

How would I do that? I simply haven't found a tutorial that explains this.

CodeBunny

That's one of the reasons I suggested learning Java before OpenGL earlier - if you don't know how to program in an object oriented way, then everything is far more difficult (and usually a lot less efficient and scalable).

If you're serious about programming, you need to learn this. Here:

Wikipedia tends to have decent to good tech articles:
http://en.wikipedia.org/wiki/Object-oriented_programming
http://en.wikipedia.org/wiki/Object-oriented_design

And in any case, you should follow the Java Tutorials:
http://download.oracle.com/javase/tutorial/java/index.html

And, actually, the tutorial on object-oriented programming is the very first one:
http://download.oracle.com/javase/tutorial/java/concepts/

It depends on how serious about programming you are. This is an area where it might actually be a good idea to buy a book on Java.