Hello guys ,
I have a problem , when I put texture on an object it doesn't texture the whole object , only a small portion of it , the code :
The class that creates the objects and creates the texture :
import static 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 Sprite {
public float sx;
public float sy;
String key="1";
Texture texture;
public Sprite(float sx,float sy,String key){
this.sx=sx;
this.sy=sy;
if(key!=null)
{
try
{
texture=TextureLoader.getTexture("JPG",ResourceLoader.getResourceAsStream("res/"+key+".jpg"));
}catch(Exception ex){}
}else{
this.key=key;
}
}
public void render(){
if(key!=null)
{
Color.white.bind(); // pt ca sa putem adauga pe obiect fara ai mai da culoare
texture.bind();
glBegin(GL_QUADS);
{
glTexCoord2f(0f,1f);
glVertex2f(0,0);
glTexCoord2f(1f,1f);
glVertex2f(0,sy);
glTexCoord2f(1f,0f);
glVertex2f(sx,sy);
glTexCoord2f(0f,0f);
glVertex2f(sx,0);
}
glEnd();
}else
{
glColor3f(0.3f,0.7f,0f);
glBegin(GL_QUADS);
{
glVertex2f(0,0);
glVertex2f(0,sy);
glVertex2f(sx,sy);
glVertex2f(sx,0);
}
glEnd();
}
}
}
the game object class , that has a method to creats a new sprite
import static org.lwjgl.opengl.GL11.*;
abstract public class gameObject {
protected float x;
protected float y;
protected float sx;
protected float sy;
protected Sprite spr;
public float getX(){
return x;
}
public void setX(float x){
this.x=x;
}
public float getY(){
return y;
}
public void setY(float y){
this.y=y;
}
public float getSX(){
return sx;
}
public float getSY(){
return sy;
}
public void getImput(){}
public void update(){}
public void render(){
glPushMatrix();
{
glTranslatef(x,y,0);
spr.render();
}
glPopMatrix();
}
public void init(float sx,float sy,String key)
{
spr=new Sprite(sx,sy,key);
}
}
the "child" of game object ... added the image (P1 ) as well
import static javax.swing.text.StyleConstants.Size;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
public class Player extends gameObject {
protected static final float SIZE=30;
public Player(float x,float y)
{
this.x=x;
this.y=y;
this.sx=SIZE;
this.sy=SIZE;
init(sx,sy,"P1");
}
public void getImput(){
if(Keyboard.isKeyDown(Keyboard.KEY_A))
{
move(-1,0);
}
if(Keyboard.isKeyDown(Keyboard.KEY_D))
{
move(1,0);
}
if(Keyboard.isKeyDown(Keyboard.KEY_W))
{
move(0,1);
}
if(Keyboard.isKeyDown(Keyboard.KEY_S))
{
move(0,-1);
}
}
public void setX(float x)
{
this.x=x;
}
public void move (int magX,int magY)
{
x=x+magX*4;
y=y+magY*4;
}
public void update(){
if(y<0)
{
y=0;
}
if(y>Display.getHeight()-SIZE)
{
y=Display.getHeight() - SIZE;
}
}
}
and the class which calls and adds othe objects to be updated ( in this projects are more classes but I didn't added all )
import java.util.ArrayList;
import org.lwjgl.opengl.Display;
public class Game {
private ArrayList<gameObject> objects;
private Player player;
private enemy[] enemys;
private Wall wall1;
private Wall wall2;
private EnemyCollision collision;
private Road road;
private int resetGame=1;
public Game(){
float x, y;
objects=new ArrayList<gameObject>();
player = new Player ((Display.getWidth()/2-Player.SIZE/2),Player.SIZE/2);
// enemy1=new enemy(Display.getWidth()/2,Display.getHeight()-enemy.SIZE,player);
wall1=new Wall(Display.getWidth()/4,player);
wall2=new Wall(Display.getWidth()*3/4,player);
road =new Road(wall1.getX()+1,0,(wall2.getX()-wall1.getX()-1),Display.getHeight());
enemys=new enemy[15];
for(int i=0;i<enemys.length;i++)
{
x=(float)(wall1.getX()+Math.random()*(wall2.getX()-wall1.getX()-enemy.SIZE));
y=(float)(Display.getHeight()+Math.random()*300);
enemys[i]=new enemy(x,y,player,wall1,wall2);
}
collision=new EnemyCollision(enemys);
objects.add(road);
objects.add(player);
// objects.add(enemy1);
for(int i=0;i<enemys.length;i++)
{
objects.add(enemys[i]);
}
objects.add(wall1);
objects.add(wall2);
}
public void getImput(){
for(gameObject go:objects)
go.getImput();
}
public void update(){
for(gameObject go:objects)
go.update();
collision.update();
}
public void render(){
for(gameObject go:objects)
{
go.render();
}
}
}
It's been a while since I've last touched fixed function OpenGL, but I think I found the issue.
Your texture mapping, past the first vertex, seems incorrect. Here's a handy image I keep around, as I forget how it goes all too often:
(http://ogldev.atspace.co.uk/www/tutorial16/txt_coords.png)
The vertices should go as such, assuming the top left of the display is the origin:
//top left
glTexCoord2f(0,1);
glVertex2f(0,0);
//bottom left
glTexCoord2f(0,0);
glVertex2f(0,sy);
//bottom right
glTexCoord2f(1,0);
glVertex2f(sx, sy);
//top right
glTexCoord2f(1,1);
glVertex2f(sx,0);
If this was not the source of the issue, a screenshot of the problem would help.
Managed to figure it out , eventually . The image size wasn't a power of 2 so it didn't rendered correctly ... Thank you for helping me :)