Move 2d box along display boundaries - java

Currently, I am able to move the box to the left and right through the display boundary, but I am unable to figure out how to move the box up and down. I would like the box to navigate in a circle around the display, e.g move right, then down, then to the left, then up, then continuously moving throughout the display. Here is my code for my entity class:
import java.awt.Rectangle;
import org.lwjgl.opengl.GL11;
class Entity
{
private static enum State { START, LEFT, RIGHT, UP, DOWN};
private Rectangle box;
private State state;
private float speed; // pixels / ms
public Entity(float speed)
{
box = new Rectangle(10, 10, 10, 10);
state = State.START;
this.speed = speed;
}
public void draw()
{
float x = (float)box.getX();
float y = (float)box.getY();
float w = (float)box.getWidth();
float h = (float)box.getWidth();
// draw the square
GL11.glColor3f(0,1,0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x, y);
GL11.glVertex2f(x+w, y);
GL11.glVertex2f(x+w, y+w);
GL11.glVertex2f(x, y+w);
GL11.glEnd();
}
public void update(int delta)
{
switch (state)
{
case START:
state = State.RIGHT;
case RIGHT:
box.translate((int)(speed*delta), 0);
if (box.getX() >= 800)
{
state = State.LEFT;
}
break;
case LEFT:
box.translate((int)(-speed*delta), 0);
if (box.getX() <= 0)
{
state = State.RIGHT;
}
break;
}
}
}
Here is my code for GameLoop:
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.input.Keyboard;
import org.lwjgl.LWJGLException;
public class GameLoop
{
public static final int TARGET_FPS=100;
public static final int SCR_WIDTH=800;
public static final int SCR_HEIGHT=600;
public static void main(String[] args) throws LWJGLException
{
initGL(SCR_WIDTH, SCR_HEIGHT);
Entity e = new Entity(.1f);
long time = (Sys.getTime()*4000)/Sys.getTimerResolution(); // ms
while (! Display.isCloseRequested())
{
long time2 = (Sys.getTime()*4000)/
Sys.getTimerResolution(); // ms
int delta = (int)(time2-time);
System.out.println(delta);
e.update(delta);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
e.draw();
// UPDATE DISPLAY
Display.update();
Display.sync(TARGET_FPS);
time = time2;
}
Display.destroy();
}
public static void initGL(int width, int height) throws LWJGLException
{
// open window of appropriate size
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();
Display.setVSyncEnabled(true);
// enable 2D textures
GL11.glEnable(GL11.GL_TEXTURE_2D);
// set "clear" color to black
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// enable alpha blending
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
// set viewport to entire window
GL11.glViewport(0,0,width,height);
// set up orthographic projectionr
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, 1, -1);
// GLU.gluPerspective(90f, 1.333f, 2f, -2f);
// GL11.glTranslated(0, 0, -500);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
}
Any help would be appreciated. I also need to have the box change colors every time it changes direction.

Correct me if i'm wrong but i think you just need to use box.translate(0,value) to move your Rectangle up or down. Just adapt your left-right movement to the y value of the box. You could also use the State to determine which color it should be drawn in.

Related

How to make player get destroyed through camera?

I've been having some trouble making the player get destroyed through the camera. In my application, I made the camera follow the player(the ball). But the camera can only follow the ball upward. So what I want to accomplish is, when the player(the ball) reaches the bottom of the interface(the screen) it gets destroyed. After it gets destroyed it would be good, if a new activity(new screen) pops up, that says "Game over".
Thanks a lot for the great support.
the interface of the application
package com.luca.tuninga;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
public class MyGdxGame extends ApplicationAdapter {
public static float APP_FPS = 60f;
public static int V_WIDTH = 480;
public static int V_HEIGHT = 640;
Box2DDebugRenderer b2dr;
World world;
Body ballBody;
OrthographicCamera camera;
float cameraMaxY;
#Override
public void create() {
world = new World(new Vector2(0, -9.8f), false);
b2dr = new Box2DDebugRenderer();
camera = new OrthographicCamera();
camera.setToOrtho(false, V_WIDTH, V_HEIGHT);
cameraMaxY = camera.position.y;
ballBody = createBall();
createWalls();
}
private void update() {
world.step(1f / APP_FPS, 6, 2);
if (Gdx.input.isTouched()) {
ballBody.setLinearVelocity(0, MathUtils.clamp(ballBody.getLinearVelocity().y, 0, 3));
ballBody.applyForceToCenter(new Vector2(0, 650f), false);
}
if (ballBody.getPosition().y * 32 > cameraMaxY) {
camera.translate(0, (ballBody.getPosition().y * 32) - cameraMaxY);
camera.update();
cameraMaxY = camera.position.y;
}
}
#Override
public void render() {
Gdx.gl.glClearColor(.25f, .25f, .25f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
update();
b2dr.render(world, camera.combined.cpy().scl(32f));
}
#Override
public void dispose() {
super.dispose();
world.dispose();
}
private Body createBall() {
Body body;
BodyDef def = new BodyDef();
def.type = BodyDef.BodyType.DynamicBody;
def.fixedRotation = true;
def.position.set(camera.position.x/ 32 + .5f, camera.position.y/ 32);
def.gravityScale = 3;
CircleShape shape = new CircleShape();
shape.setRadius(.5f);
body = world.createBody(def);
body.createFixture(shape, 1.0f);
return body;
}
private void createWalls() {
Body body;
BodyDef def = new BodyDef();
def.type = BodyDef.BodyType.StaticBody;
def.fixedRotation = true;
PolygonShape shape = new PolygonShape();
shape.setAsBox(1, 200 / 32);
for(int i = 0; i < 20 ; i++) {
def.position.set(1.01f, i * (200 / 32));
body = world.createBody(def);
body.createFixture(shape, 1.0f);
def.position.set(V_WIDTH / 32 - 1, i * (200 / 32));
body = world.createBody(def);
body.createFixture(shape, 1.0f);
}
}
}
From what I understand, the camera will follow upwards only. Thus when the ball transitions into upwards position, camera will update and follow through, camera won't go down again.
So you can check when the ball is outside of the sight of camera; specifically the bottom of camera.
You can do something like this putting it at the end of update() function
if ((ballBody.getPosition().y + 0.5f) * 32 < ballBody.getPosition().y -
camera.getViewportHeight()/2) {
// destroy body
world.destroyBody(ballBody);
// TODO: switch to another screen (thus next frame update & draw loop of this screen won't be called anymore)
}
above is to check if when ball is completely out of camera's sight (thus I do + 0.5f which is its radius as you used to create the shape for such body) against camera's viewport height.
Then switch to another screen. This means the current screen won't be update or draw its content anymore, thus no need to have a flag to check. But if you need to do something else again in next frame, you better have a flag checking for the current screen to know that the game is now over, and thus you can check whether to do certain operations.

Rotate OrthographicCamera from a point (LibGdx)

I am studying the example of Orthographic Camera of wiki libgdx :
https://github.com/libgdx/libgdx/wiki/Orthographic-camera#description
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
public class OrthographicCameraExample implements ApplicationListener {
static final int WORLD_WIDTH = 100;
static final int WORLD_HEIGHT = 100;
private OrthographicCamera cam;
private SpriteBatch batch;
private Sprite mapSprite;
private float rotationSpeed;
#Override
public void create() {
rotationSpeed = 0.5f;
mapSprite = new Sprite(new Texture(Gdx.files.internal("sc_map.png")));
mapSprite.setPosition(0, 0);
mapSprite.setSize(WORLD_WIDTH, WORLD_HEIGHT);
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
// Constructs a new OrthographicCamera, using the given viewport width and height
// Height is multiplied by aspect ratio.
cam = new OrthographicCamera(30, 30 * (h / w));
cam.position.set(cam.viewportWidth / 2f, cam.viewportHeight / 2f, 0);
cam.update();
batch = new SpriteBatch();
}
#Override
public void render() {
handleInput();
cam.update();
batch.setProjectionMatrix(cam.combined);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
mapSprite.draw(batch);
batch.end();
}
private void handleInput() {
if (Gdx.input.isKeyPressed(Input.Keys.A)) {
cam.zoom += 0.02;
}
if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
cam.zoom -= 0.02;
}
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
cam.translate(-3, 0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
cam.translate(3, 0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
cam.translate(0, -3, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
cam.translate(0, 3, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.W)) {
cam.rotate(-rotationSpeed, 0, 0, 1);
}
if (Gdx.input.isKeyPressed(Input.Keys.E)) {
cam.rotate(rotationSpeed, 0, 0, 1);
}
cam.zoom = MathUtils.clamp(cam.zoom, 0.1f, 100/cam.viewportWidth);
float effectiveViewportWidth = cam.viewportWidth * cam.zoom;
float effectiveViewportHeight = cam.viewportHeight * cam.zoom;
cam.position.x = MathUtils.clamp(cam.position.x, effectiveViewportWidth / 2f, 100 - effectiveViewportWidth / 2f);
cam.position.y = MathUtils.clamp(cam.position.y, effectiveViewportHeight / 2f, 100 - effectiveViewportHeight / 2f);
}
#Override
public void resize(int width, int height) {
cam.viewportWidth = 30f;
cam.viewportHeight = 30f * height/width;
cam.update();
}
#Override
public void resume() {
}
#Override
public void dispose() {
mapSprite.getTexture().dispose();
batch.dispose();
}
#Override
public void pause() {
}
public static void main(String[] args) {
new LwjglApplication(new OrthographicCameraExample());
}
}
On the rotation of the camera, map is rotated with the camera at the midpoint of the screen. I would like to rotate the camera from a certain point. For example, the point 0,0 . I tried to use the method rotateAround ( Vector3 point , Vector3 axis , float angle) , I did not get the expected result.
cam.rotateAround(new Vector3(0,0,0), new Vector3(0,0,1), 1);
I know it's possible to move the camera to the point 0.0 and then rotate . But that's not what I want .
In the game I'm doing the player is in a fixed position at the bottom of the screen in the middle and I turned the screen around it , but without taking the player to the middle of the screen and then rotate .
I appreciate the help .
You just need to update camera after cam.rotateround :)
It works for me.
cam.rotateAround(new Vector3(270, 0, 0), new Vector3(0, 0, 1), 0.1f);
cam.update();
Here is a screenshot of my test. As you can see screen rotating around red dot. (Perspective camera is also rotating around same point that in its own coordinate.)
Also i suggest you to use
cam.setToOrtho(false, cam.viewportWidth, cam.viewportHeight);
instead of
cam.position.set(cam.viewportWidth / 2f, cam.viewportHeight / 2f, 0);

How to setup camera properly?

On my class I'm implementing ApplicationListener, so on my create method this is what I do:
public void create(){
camera=new Camera();
camera.setToOrtho(false,screenW,screenH);
}
//then on the render method:
public void render(){
camera.update();
}
But then when I use Gdx.input.getY() the result is reversed, when I go up the Y coordinate is less and when I go down the it gets higher.For better understanding:
Have a look at the camera class and the unproject method. That should translate between screen coordinates and world coordinates. (Probably more efficient way to do this, but for illustration):
float x = Gdx.input.getX();
float y = Gdx.input.getY();
float z = 0.0f;
Vector3 screenCoordinates = new Vector3(x, y, z);
Vector3 worldCoordinates = camera.unproject(screenCoordinates);
Then use the worldCoordinates vector for whatever you need it for.
EDIT: Added small working example and screenshot. My screen capture didn't capture the mouse, thus the red "star". But this simple app displays y coordinates in "initial" and "unprojected" coords as you move the mouse around the screen. Try it for yourself. I think this is what you are getting at, no?
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector3;
public class SimpleInputCoords implements ApplicationListener {
private OrthographicCamera camera;
#Override
public void create() {
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
}
#Override
public void render() {
int x = Gdx.input.getX();
int y = Gdx.input.getY();
int z = 0;
System.out.println("UnmodifiedYCoord:"+y);
Vector3 screenCoordinates = new Vector3(x, y, z);
Vector3 worldCoordinates = camera.unproject(screenCoordinates);
System.out.println("UnprojectedYCoord:"+worldCoordinates.y);
}
#Override
public void dispose() {
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
In Java coordinates start in the upper left corner (0,0). On a 100 x 100, (100,0) would be upper right, and (0, 100) would be lower left. So the behavior you are seeing is expected.
http://inetjava.sourceforge.net/lectures/part2_applets/InetJava-2.1-2.2-Introduction-to-AWT-and-Applets_files/image001.gif

Android game: Moving bitmap leaves blurry trail at ends

I am trying to create a background scrolling for an Android game. Lets say it is a set of images moving at a speed.
To test out few basic elements, I have taken a small, rectangular image and moving it linearly from top to bottom. It moves but I see a small trail, like a rocket going in clouds(haha!). I tried various options in Paint() class to rectify this, but I couldn't.
I will first post a screen shot and then add relevant code.
So, here the vertical line is a single png image:
Now all I am doing is moving this bitmap from a certain (x,y) by increasing y by 5 at a time. It moves, but notice the ends of the image while moving. Sort of a trail, but not a permanent one. I have tried but unable to remove this effect. I tried on Samsung Galaxy S5. I didnot test on any other model.
The code:
The moving vertical image is a Sprite object:
package com.src.*.*;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.Log;
public class Sprite {
private Rect sourceRect; // the rectangle to be drawn from the animation bitmap
private int frameNr; // number of frames in animation
private int currentFrame; // the current frame
private long frameTicker; // the time of the last frame update
private int framePeriod; // milliseconds between each frame (1000/fps)
private int spriteWidth; // the width of the sprite to calculate the cut out rectangle
private int spriteHeight; // the height of the sprite
private int x; // the X coordinate of the object (top left of the image)
private int y; // the Y coordinate of the object (top left of the image)
private Bitmap bitmap;
private float degrees;
private boolean rotate=false;
private Matrix matrix;
private Bitmap reversedBitmap;
private Paint paint;
public Sprite(Bitmap bitmap, int x, int y, int fps, int frameCount) {
this.bitmap = bitmap;
this.x = x;
this.y = y;
currentFrame = 0;
frameNr = frameCount;
spriteWidth = bitmap.getWidth() / frameCount;
spriteHeight = bitmap.getHeight();
sourceRect = new Rect(0, 0, spriteWidth, spriteHeight);
framePeriod = 1000 / fps;
frameTicker = 0l;
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG
| Paint.FILTER_BITMAP_FLAG);
paint.setDither(true);
}
public void animate(){
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void setReverse(Bitmap reverse){
this.reversedBitmap = reverse;
}
public void rorateSprite(float degrees){
if(degrees == 0){
rotate = false;
}else{
rotate = true;
}
this.degrees = degrees;
}
public void draw(Canvas canvas) {
// where to draw the sprite
Rect destRect = new Rect(getX(), getY(), getX() + spriteWidth, getY() + spriteHeight);
if(rotate){
canvas.drawBitmap(reversedBitmap, sourceRect, destRect, paint);
}else{
canvas.drawBitmap(bitmap, sourceRect, destRect, paint);
}
}
public void update(long gameTime){
if (gameTime > frameTicker + framePeriod) {
frameTicker = gameTime;
//Log.i("INFO", "In if loop" + currentFrame + " framenr:" + frameNr);
// increment the frame
currentFrame++;
if (currentFrame >= frameNr) {
currentFrame = 0;
}
}else{
//Log.i("INFO", "in else" + currentFrame + "");
}
// define the rectangle to cut out sprite
//Log.i("INFO", "sprite width: " + spriteWidth);
this.sourceRect.left = currentFrame * spriteWidth;
this.sourceRect.right = this.sourceRect.left + spriteWidth;
}
}
The bitmap is setup as follows:
wallAnimation = new Sprite(resizedBitmapWall, 0, 0, 30, 1);
wallAnimation.setX(margin);
The onDraw and Update for the sprite is as follows. It works without any exception or error. The only issue in all of this is the image having the blurry/tail effect at the ends. I dont even know the right word for it.
Code for onDraw and Update:
#Override
protected void onDraw(Canvas canvas) {
// fills the canvas with black
canvas.drawColor(Color.WHITE);
wallAnimation.draw(canvas);
paint.setColor(Color.BLACK);
for (int i = 0; i < 6; i++) {
canvas.drawLine(margin+laneWidth*i, 0, margin+laneWidth*i, laneHeight, paint);
}
canvas.drawText("High Score: "+ highScore, 10, 10, paint);
}
public void Update(long gameTime) {
// TODO Auto-generated method stub
if(wallState < laneHeight+laneHeight/6){
wallAnimation.setY(wallState);
//wallState+=laneHeight/80;
wallState+=20;
}else{
wallState = 0;
}
}

Textures not binding in OpenGL (LWJGL/Slick-util)

I have a problem with what seems to be binding the textures to a shape. When I do so, I get a white shape of the correct dimensions, the color given to it by Color.white.bind().
GraphicsManager.java
package com.jackwilsdon.spectrun;
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.lwjgl.util.vector.Vector2f;
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 GraphicsManager {
final static int WIDTH = 800;
final static int HEIGHT = 600;
private static Texture cloudTexture = null;
public static void initDisplay() throws LWJGLException
{
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.create();
}
public static void initOpenGL()
{
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, WIDTH, 0, HEIGHT, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
public static void loadTextures()
{
Texture currentObject = null;
try {
currentObject = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("./resources/cloud.png"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Texture loaded: "+currentObject);
System.out.println(">> Image width: "+currentObject.getImageWidth());
System.out.println(">> Image height: "+currentObject.getImageHeight());
System.out.println(">> Texture width: "+currentObject.getTextureWidth());
System.out.println(">> Texture height: "+currentObject.getTextureHeight());
System.out.println(">> Texture ID: "+currentObject.getTextureID());
cloudTexture = currentObject;
}
public static void DrawRectangle(int x, int y, int width, int height)
{
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x,y);
GL11.glVertex2f(x+width,y);
GL11.glVertex2f(x+width,y+height);
GL11.glVertex2f(x,y+height);
GL11.glEnd();
}
public static void DrawRectangle(int x, int y, int width, int height, int red, int green, int blue)
{
GL11.glColor3ub((byte)red, (byte)green, (byte)blue);
DrawRectangle(x, y, width, height);
}
public static Vector2f DrawCloud(int x, int y)
{
cloudTexture.bind();
int width = cloudTexture.getImageWidth();
int height = cloudTexture.getImageHeight();
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x,y);
GL11.glVertex2f(x+width,y);
GL11.glVertex2f(x+width,y+height);
GL11.glVertex2f(x,y+height);
GL11.glEnd();
return new Vector2f(cloudTexture.getImageWidth(), cloudTexture.getImageHeight());
}
}
I suspect it has something to do with my initOpenGL() method, as in an example I have looked at (http://ninjacave.com/slickutil1), theirs is very different. Just copying theirs into mine gives unexpected results (black screen). Is something missing from my initOpenGL() method?
EDIT: The loadTextures() method outputs the correct image dimensions, but the texture's dimensions are different, what does this mean?
EDIT 2: The PNG is not the problem, I've tried 2 others to find the same result.
A new problem has arisen after fixing my code, by setting the texture coordinates.
I now get a weird effect, like this; i.imgur.com/5lr79.png. The image is no longer full size, and has a black box to the bottom left of it.
GraphicsManager.java
package com.jackwilsdon.spectrun;
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.lwjgl.util.vector.Vector2f;
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 GraphicsManager {
final static int WIDTH = 800;
final static int HEIGHT = 600;
private static Texture cloudTexture = null;
public static void initDisplay() throws LWJGLException
{
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.create();
}
public static void initOpenGL()
{
GL11.glEnable(GL11.GL_TEXTURE);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, WIDTH, 0, HEIGHT, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
public static void loadTextures()
{
Texture currentObject = null;
try {
currentObject = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("./resources/cloud.png"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Texture loaded: "+currentObject);
System.out.println(">> Image width: "+currentObject.getImageWidth());
System.out.println(">> Image height: "+currentObject.getImageHeight());
System.out.println(">> Texture width: "+currentObject.getTextureWidth());
System.out.println(">> Texture height: "+currentObject.getTextureHeight());
System.out.println(">> Texture ID: "+currentObject.getTextureID());
cloudTexture = currentObject;
}
public static void DrawRectangle(int x, int y, int width, int height)
{
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x,y);
GL11.glVertex2f(x+width,y);
GL11.glVertex2f(x+width,y+height);
GL11.glVertex2f(x,y+height);
GL11.glEnd();
}
public static void DrawRectangle(int x, int y, int width, int height, int red, int green, int blue)
{
GL11.glColor3ub((byte)red, (byte)green, (byte)blue);
DrawRectangle(x, y, width, height);
}
public static Vector2f DrawCloud(int x, int y)
{
cloudTexture.bind();
int width = cloudTexture.getImageWidth();
int height = cloudTexture.getImageHeight();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(x,y);
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(x+width,y);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(x+width,y+height);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(x,y+height);
GL11.glEnd();
return new Vector2f(cloudTexture.getImageWidth(), cloudTexture.getImageHeight());
}
}
It doesn't help that you aren't calling GL11.glEnable( GL11.GL_TEXTURE ) and you also aren't setting any texture coordinates.
Both of these will rather mess up your ability to see a texture on the quad ...
I do believe the call to bind() needs to be between your glBegin() and glEnd() call to make it stick. The dimension problem is somewhat more odd - perhaps your texture dimensions are not a power of 2 and it is being translated by the driver?
You didnt enable gl_TEXTURE_2D first of all. Second, you need to call .bind() inside glBegin() and your glEnd(). Third, you need texture coordinates. So, for the top left edge it would be 'glTexCoord(0, 0)', the next would be '(1, 0)' and then '(1, 1)' and lastly '(0, 1)'
Don't forget to call TextureImpl.unbind when needed.

Categories