Using more than one texture

Started by pyritebomb, March 30, 2011, 15:59:05

Previous topic - Next topic

pyritebomb

When I load textures, it only binds the most recently loaded texture. I use the slick_util method of getting the texture. I think this is something that I just haven't found how to work rather than sorting through my code.

Fool Running

Any time you want to use a texture, you need to bind it by calling the bind() method.
See http://slick.cokeandcode.com/wiki/doku.php?id=loading_and_binding_opengl_textures_with_slick-util for an example.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

pyritebomb

Im using bind() already. I will post both my loading and my drawing snippets.

blocks = new Texture[2];
			blocks[0] = TextureLoader.getTexture("png", new FileInputStream("data/terrain-bark.png"));
			blocks[1] = TextureLoader.getTexture("png", new FileInputStream("data/terrain-stump.png"));

                 game.blocks[0].bind();
                  //glVertex()to make the sides of a cube
                 glEnd();
		
		glBegin(GL_QUADS);
		game.blocks[1].bind();
                //glVertex() to make the top and bottom



Fool Running

Ah, sorry, I misunderstood your problem.
So no matter which texture you bind you are only getting the "stump" image? if you would reverse the two TextureLoader.getTexture() calls (but keep the same indexes), you would only get the "bark" image? That is really strange...

Would it be possible to paste all of your code (or at least whatever class the variable "game" is)?
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

pyritebomb

package org.minecraftclone;

import org.lwjgl.BufferUtils;
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 org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.util.glu.GLU;
import org.lwjgl.util.vector.Vector2f;
import org.minecraftclone.core.*;
import org.minecraftclone.entity.Player;
import org.minecraftclone.world.MapManager;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

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

public class Game {

	public boolean closeRequested = false;
		
	public int displayList;
	
	public Player player;
	public MapManager mapManager;
	InputManager inputManager;
	
	public Texture[] blocks;
	public Texture tex1;
	public Texture tex2;
	
	int shader;
	
	FloatBuffer lightAmbient = BufferUtils.createFloatBuffer(4).put(new float[] {1.0f, 1.0f, 1.0f, 1.0f });
	FloatBuffer lightDiffuse = BufferUtils.createFloatBuffer(4).put(new float[] {0.2f, 0.2f, 0.2f, 1.0f });
	FloatBuffer lightPosition = BufferUtils.createFloatBuffer(4).put(new float[] {0.0f, 5.0f, 0.0f, 20.0f });
	
	
	public Game()
	{
		initGL();
		init();
		
		while(!closeRequested)
		{
			inputManager.checkInput();
			inputManager.updateLogic();
			render();
			
			Display.update();
		}
		
		deInit();
	}
	
	public void initGL()
	{
		try {
			Display.setDisplayMode(new DisplayMode(800,600)); 
			Display.setVSyncEnabled(true);
			Display.create();
		} catch (LWJGLException e) {
			System.exit(0);
		}
		
		int height = Display.getDisplayMode().getHeight();
		int width = Display.getDisplayMode().getWidth();
		
    	glEnable(GL_BLEND);
    	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    	glViewport(0,0,width,height);
		
		glMatrixMode(GL_PROJECTION);					
		glLoadIdentity();					

		GLU.gluPerspective(45.0f,(float)width/(float)height,0.1f,100.0f);

		glMatrixMode(GL_MODELVIEW);					
		glLoadIdentity();	
		
		glEnable(GL_TEXTURE_2D);    
		
		glShadeModel(GL_SMOOTH);						
		glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
		glClearDepth(1.0f);							
		glEnable(GL_DEPTH_TEST);						
		glDepthFunc(GL_LEQUAL);
		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	
		
		glEnable(GL_CULL_FACE);
		
		GL20.glCreateShader(shader);
	}
	
	public void init()
	{		
		player = new Player();
		inputManager = new InputManager(this);
		mapManager = new MapManager(this);
		
		Mouse.setCursorPosition(400, 300);
		
		try {
			blocks = new Texture[2];
			blocks[0] = TextureLoader.getTexture("png", new FileInputStream("data/terrain-bark.png"));
			blocks[1] = TextureLoader.getTexture("png", new FileInputStream("data/terrain-stump.png"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void deInit()
	{
		Display.destroy();
		Keyboard.destroy();
	}
	
	public void render()
	{		
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
		glLoadIdentity();	
		
		mapManager.renderMap();
		
		glCallList(displayList);
	}
	
	public static void main(String[] args)
	{
		new Game();
	}
	
}


Sure. I don't personally think i've done anything wrong but after a while of looking through things, it's starting to get on my nerves when I have little experience with openGL

avm1979

Maybe it's a problem in TextureLoader?  If you were to keep using the same texture name instead of the integer value you should get back from glGenTextures, you'd see this behavior.  For example, if you're always loading the texture with name 0 instead of using the name returned from glGenTextures.


Edit: never mind, I see that that's from slick. /facepalm

Fool Running

I think I see the problem (I should have seen it from your first code post):
You are attempting to bind a texture in the middle of a glBegin(GL_QUADS) and glEnd(). This is not allowed (or maybe supported is a better word since I think the call is ignored). Try binding the texture before the glBegin() call.
      
      
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

avm1979

That's strange, though - doing that should generate an invalid operation error.

At least, that's what the docs say...

pyritebomb

Ahh yes. That worked. Thanks for the help, I appreciate it :). I had a feeling it would be something trivial