unable to load texture

Started by centaur2048, October 03, 2010, 00:46:50

Previous topic - Next topic

centaur2048

Hey, all,

I'm trying to get NewDawn's slick to load up my textures. Somehow, it's only applying a color to my quads instead of the texture (the color is taken from the png file). It's rather frustrating.

Any help would be greatly appreciate. Code below:

package redFox.engine;

import java.io.FileInputStream;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GLContext;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

import static org.lwjgl.opengl.GL11.*;

public class Renderer
{
	private Scene _Scene;
	
	private float view_rotx = 20.0f;
	private float view_roty = 30.0f;
	private float view_rotz;
	private int prevMouseX, prevMouseY;
	private boolean mouseButtonDown;
	private Texture _texture;
	
	public Renderer()
	{
		try
		{
			int currentBpp = Display.getDisplayMode().getBitsPerPixel();
			DisplayMode mode = findDisplayMode( 800, 600, currentBpp );
			Display.setTitle( "PrototypeEngine" );
			Display.setDisplayMode( mode );
			Display.setFullscreen( false );
			Display.create();
			
			_texture = TextureLoader.getTexture( "PNG", new FileInputStream( "img/stone.png" ) );
			
			_Scene = new Scene();
			
			init();
		}
		catch ( Exception e )
		{
			e.printStackTrace();
			Sys.alert( "Error", "Failed" + e.getMessage() );
		}
	}
	
	private DisplayMode findDisplayMode( int width, int height, int bpp)
	{
		DisplayMode mode = null;
		try
		{
			DisplayMode[] modes = Display.getAvailableDisplayModes();
			
			
			for ( int i = 0; i < modes.length; i++ )
			{
				if ( ( modes[i].getBitsPerPixel() == bpp ) || ( mode == null ) )
				{
					if ( ( modes[i].getWidth() == width ) && ( modes[i].getHeight() == height ) )
					{
						mode = modes[i];
					}
				}
			}
		}
		catch ( LWJGLException e )
		{
			e.printStackTrace();
			Sys.alert( "Error", "Failed" + e.getMessage() );
		}
		
		return mode;
	}

	private void init()
	{
		glLoadIdentity();
		glEnable(GL_TEXTURE_2D);
		glEnable(GL_CULL_FACE);
		glEnable(GL_DEPTH_TEST);
		glDepthFunc(GL_LEQUAL);
		glShadeModel(GL_SMOOTH);
		glEnable(GL_NORMALIZE);
		
		glMatrixMode(GL_PROJECTION);
		glFrustum(-1.0f, 1.0f, -0.75f, 0.75f, 5.0f, 60.0f );
		glMatrixMode(GL_MODELVIEW);
		glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
		glTranslatef(0.0f, 0.0f, -40.0f);
	}
	
	public void update( float deltaTime )
	{	
		if ( Mouse.isButtonDown(0) )
		{
			if ( !mouseButtonDown )
			{
				prevMouseX = Mouse.getX();
				prevMouseY = Mouse.getY();
			}
			
			mouseButtonDown = true;
		}
		else
		{
			mouseButtonDown = false;
		}
		
		if ( mouseButtonDown )
		{
			int x = Mouse.getX();
		    int y = Mouse.getY();

			float thetaY = 360.0f * ( (float)(x-prevMouseX)/800.0f);
		    float thetaX = 360.0f * ( (float)(prevMouseY-y)/600.0f);

		    prevMouseX = x;
		    prevMouseY = y;

		    view_rotx += thetaX;
		    view_roty += thetaY;
		}
		
		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
		
		glPushMatrix();
		glRotatef(view_rotx, 1.0f, 0.0f, 0.0f);
		glRotatef(view_roty, 0.0f, 1.0f, 0.0f);
		glRotatef(view_rotz, 0.0f, 0.0f, 1.0f);

		//_Scene.update( 0.0f );
		_texture.bind();
        glBegin(GL_QUADS);                        // Draw A Quad
        	glTexCoord2f( 0.0f, 0.0f );
            glVertex3f( 1.0f, 1.0f,-1.0f);         // Top Right Of The Quad (Top)
            glVertex3f(-1.0f, 1.0f,-1.0f);         // Top Left Of The Quad (Top)
            glVertex3f(-1.0f, 1.0f, 1.0f);         // Bottom Left Of The Quad (Top)
            glVertex3f( 1.0f, 1.0f, 1.0f);         // Bottom Right Of The Quad (Top)
            
            glTexCoord2f( 1.0f, 0.0f );
            glVertex3f( 1.0f,-1.0f, 1.0f);         // Top Right Of The Quad (Bottom)
            glVertex3f(-1.0f,-1.0f, 1.0f);         // Top Left Of The Quad (Bottom)
            glVertex3f(-1.0f,-1.0f,-1.0f);         // Bottom Left Of The Quad (Bottom)
            glVertex3f( 1.0f,-1.0f,-1.0f);         // Bottom Right Of The Quad (Bottom)
            
            glTexCoord2f( 0.0f, 1.0f );
            glVertex3f( 1.0f, 1.0f, 1.0f);         // Top Right Of The Quad (Front)
            glVertex3f(-1.0f, 1.0f, 1.0f);         // Top Left Of The Quad (Front)
            glVertex3f(-1.0f,-1.0f, 1.0f);         // Bottom Left Of The Quad (Front)
            glVertex3f( 1.0f,-1.0f, 1.0f);         // Bottom Right Of The Quad (Front)

            glTexCoord2f( 1.0f, 1.0f );
            glVertex3f( 1.0f,-1.0f,-1.0f);         // Bottom Left Of The Quad (Back)
            glVertex3f(-1.0f,-1.0f,-1.0f);         // Bottom Right Of The Quad (Back)
            glVertex3f(-1.0f, 1.0f,-1.0f);         // Top Right Of The Quad (Back)
            glVertex3f( 1.0f, 1.0f,-1.0f);         // Top Left Of The Quad (Back)

            glTexCoord2f( 1.0f, 0.0f );
            glVertex3f(-1.0f, 1.0f, 1.0f);         // Top Right Of The Quad (Left)
            glVertex3f(-1.0f, 1.0f,-1.0f);         // Top Left Of The Quad (Left)
            glVertex3f(-1.0f,-1.0f,-1.0f);         // Bottom Left Of The Quad (Left)
            glVertex3f(-1.0f,-1.0f, 1.0f);         // Bottom Right Of The Quad (Left)

            glTexCoord2f( 0.0f, 0.0f );
            glVertex3f( 1.0f, 1.0f,-1.0f);         // Top Right Of The Quad (Right)
            glVertex3f( 1.0f, 1.0f, 1.0f);         // Top Left Of The Quad (Right)
            glVertex3f( 1.0f,-1.0f, 1.0f);         // Bottom Left Of The Quad (Right)
            glVertex3f( 1.0f,-1.0f,-1.0f);         // Bottom Right Of The Quad (Right)
        glEnd();                       
        
		glPopMatrix();
		
		Display.update();
	}
	
	// Accessors
	public boolean shouldExit() { return Display.isCloseRequested(); }
}


Texture [stored locally as a png]:


Result:


bobjob

you need to set the Texture coordinate for each vertex cordinate.

eg
glTexCoord2f( 0.0f, 0.0f );
 glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
 glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
 glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
 glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)


should be:
eg
glTexCoord2f( 0.0f, 0.0f );
 glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
glTexCoord2f( 0.0f, 1.0f );
 glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glTexCoord2f( 1.0f, 1.0f );
 glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glTexCoord2f( 1.0f, 0.0f );
 glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)

centaur2048

Oh. Wow. Duh. Yeah.

That would make sense :) It's been a couple of years since I coded up anything with OpenGL... shows, doesn't it?

Thank you so much!!