LWJGL Texture Unbinding - java

I'm currently working on a simple textured rectangle class using LWJGL (GL11). This is what I have so far in the draw method.
glEnable(GL_TEXTURE_2D);
color.bind();
glBegin(GL_QUADS);
t.bind();
if (centered)
{
glTexCoord2d(0, 0);
glVertex2d(x - (width / 2), y - (height / 2));
glTexCoord2d(t.getWidth(), 0);
glVertex2d(x + (width / 2), y - (height / 2));
glTexCoord2d(t.getWidth(), t.getHeight());
glVertex2d(x + (width / 2), y + (height / 2));
glTexCoord2d(0, t.getHeight());
glVertex2d(x - (width / 2), y + (height / 2));
}
else
{
glTexCoord2d(0, 0);
glVertex2d(x, y);
glTexCoord2d(t.getWidth(), 0);
glVertex2d(x + width, y);
glTexCoord2d(t.getWidth(), t.getHeight());
glVertex2d(x + width, y + height);
glTexCoord2d(0, t.getHeight());
glVertex2d(x, y + height);
}
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
The problem is with the second to last line (glBindTexture(GL_TEXTURE_2D, 0)). If I leave this on, the image will display for a split second (probably one frame) and then turn solid white. If I comment this out, it will display, but always displays the last texture that was loaded. Any ideas?

I think it's because you are binding your texture between the glBegin() and glEnd() calls. This is invalid. I'm guessing when you leave the glBindTexture(GL_TEXTURE_2D, 0) call off, then the binding is taking effect the next frame. Not sure why it would work for one frame, though.
You can use glGetError to see if there are any errors generated.

Related

How do I get my camera to update the same frame as my player sprite?

In LibGDX I'm trying to have my OrthographicCamera follow the playersprite when I move it around. Currently, the playersprite gets updated first and then in the next frame the camera updates to the position of the playersprite in the previous frame. Here is an example:
https://i.imgur.com/wxJUizU.gifv
I have tried moving gameMap.render(cam) to the bottom of the method, but not only does it not solve my problem, it also places the map textures above the playersprite, so you won't be able to see the playersprite anymore when it moves under the map.
Here is the code for rendering the playersprite and camera:
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.viewportHeight = Gdx.graphics.getHeight() / 3;
cam.viewportWidth = Gdx.graphics.getWidth() / 3;
gameMap.render(cam);
gameMap.getTiledMapRenderer().getBatch().begin();
player.draw(gameMap.getTiledMapRenderer().getBatch());
gameMap.getTiledMapRenderer().getBatch().end();
cam.position.x = player.getX() + (player.getWidth() / 2);
cam.position.y = player.getY() + (player.getHeight() / 2);
cam.update();
}
Typically, you want to break your logic into two sections:
Update
Render
So, before each frame you want to perform any pertinent calculations.
Your camera is currently updating late, because it isn't performing logic until after the gameMap has been updated.
For example:
// Perform logic and update the state
public void Update () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.viewportHeight = Gdx.graphics.getHeight() / 3;
cam.viewportWidth = Gdx.graphics.getWidth() / 3;
gameMap.getTiledMapRenderer().getBatch().begin();
cam.position.x = player.getX() + (player.getWidth() / 2);
cam.position.y = player.getY() + (player.getHeight() / 2);
}
// Draw map
public void Render () {
gameMap.render(cam);
player.draw(gameMap.getTiledMapRenderer().getBatch());
gameMap.getTiledMapRenderer().getBatch().end();
cam.update();
}
I'm not familiar with the framework you're using, so I can't tell if this would run or not, but when working with frames in any game development situation, this is how you want to handle things.
Like Bradley says you should split your logic into two parts: update and render
Create a udpate() method where you place all your things to update and call this method first in the render() method:
public void update(){
//Update camera position
cam.position.x = player.getX() + (player.getWidth() / 2);
cam.position.y = player.getY() + (player.getHeight() / 2);
//apply all updates to the camera before this the changed position won't apply to the camera
cam.update();
}
#Override
public void render() {
//First call the update method we created
update();
//Clear the Screen
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//Render
gameMap.render(cam);
gameMap.getTiledMapRenderer().getBatch().begin();
player.draw(gameMap.getTiledMapRenderer().getBatch());
gameMap.getTiledMapRenderer().getBatch().end();
}
You don't need to call:
cam.viewportHeight = Gdx.graphics.getHeight() / 3;
cam.viewportWidth = Gdx.graphics.getWidth() / 3;
every frame. It's enough to call it ones in the create() method

How to rotate libgdx

I am trying to rotate a sprite in an android app using LibGDX. I have used sprite.rotate and sprite.setRotation in an attempt to rotate the sprite, but none have worked.
public Cannon(){
cannend = new Texture("cannonend.png");
cannon = new Sprite(cannend, (Gdx.graphics.getWidth() / 2) - (cannend.getWidth() / 2) + 3, (Gdx.graphics.getHeight() / 2) + (Gdx.graphics.getHeight() / 6), (cannend.getWidth()), (cannend.getHeight()));
cannon.setPosition((Gdx.graphics.getWidth()/ 2) - (cannend.getWidth() / 2) + 3, (Gdx.graphics.getHeight() / 2) + (Gdx.graphics.getHeight() / 6));
cannon.setRotation(70);
cannon.setCenter((Gdx.graphics.getWidth()/ 2) - (cannend.getWidth() / 2), (Gdx.graphics.getHeight() / 2) + (Gdx.graphics.getHeight() / 6) - (cannend.getWidth() / 2));
}
When I draw this sprite in my Playstate class this is the code that I use.
public PlayState(GameStateManager gsm) {
super(gsm);
cannon = new Cannon();
cannontop = new Texture("Cannon.png");
man = new Man(0, (Gdx.graphics.getHeight() / 4));
bg = new Texture("background.png");
cam.setToOrtho(false, 2160, 3840);
}
//skipped some irrelevant code
#Override
public void render(SpriteBatch sb) {
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(bg, cam.position.x - (cam.viewportWidth / 2), 0);
sb.draw(man.getTexture(), man.getPosition().x, man.getPosition().y);
sb.draw(cannontop, (bg.getWidth()/ 2) - (cannon.getTextureWidth() / 2) - 23, (bg.getHeight() / 2) + (bg.getHeight() / 5));
sb.draw(cannon.getTexture(), (bg.getWidth()/ 2) - (cannon.getTextureWidth() / 2), (bg.getHeight() / 2) + (bg.getHeight() / 5) - (cannontop.getWidth() / 2) + 8);
sb.end();
}
I have just learnt Java, so please forgive me if some of my code is bad. I have also added and taken away single pixels for the widths and heights when drawing things - I'm not sure whether this is ok, as I normally use ratios because they dont seem to affect gameplay when screen sizes change. Anyway, what could I use to rotate the sprite a certain amount every frame? Also, what does sprite.getX() do?
Thanks.
You are not drawing sprite you are drawing texture of sprite.
sprite.draw(batch);

OPENGL - Maintain object's original aspect ratio

i'm reading and loading .obj files, i want to have 4 different view ports all with different perspectives, my main problem is trying to keep the original aspect ration of the object and prevent it from changing with window re-size or transformations like rotation.
This is what i'm trying to get:
This is what i'm getting (in fullscreen):
A portion of my display code:
GL2 gl = drawable.getGL().getGL3bc();
gl.glEnable(GL_DEPTH_TEST);
gl.glDepthFunc(GL_LEQUAL);
gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gl.glOrtho(xMin - (0.1 * (xMax - xMin)), xMax
+ (0.1 * (xMax - xMin)), yMin - (0.1 * (yMax - yMin)), yMax
+ (0.1 * (yMax - yMin)), zMin - (0.1 * (zMax - zMin)), zMax
+ (0.1 * (zMax - zMin)));
gl.glMatrixMode(GL_PROJECTION);
gl.glViewport(0, 0, width / 2, height / 2);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glRotatef(-90f, 1f, 0f, 0f);
draw(gl);
gl.glPopMatrix();
gl.glViewport(width / 2, height / 2, width / 2, height / 2);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glRotated(-90, 0, 1, 0);
draw(gl);
gl.glPopMatrix();
gl.glViewport(0, height / 2, width / 2, height / 2);
gl.glLoadIdentity();
gl.glPushMatrix();
draw(gl);
gl.glPopMatrix();
xMax,yMin are the maximum and minimum values of the x axis in the points given in the .obj file.
height and width are the current dimensions of the window.
xMax,yMin are the maximum and minimum values of the x axis in the points given in the .obj file. height and width are the current dimensions of the window.
Well, this is most likely your problem. By setting an orthogonal view that relies on the minimum and maximum values of each coordinate, you will be actually distorting the object, because such a region doesn't have the same aspect ratio as the window. To make it clearer, if you were to put the entire model inside a box and take a picture of it, the application would stretch it so that the Y and X coordinates (assuming Y as the up-down axis) make part of the entire viewport. This would be seen as enlarging the "picture"'s width, or squeezing the height.
So you should adjust these parameters to remove the distortion. Since the model is tall (larger in Y than X), I suggest applying this trick before setting the orthogonal view:
xMin = xMin * width / height;
xMax = xMax * width / height;

Problems dynamically moving VBO

I am making a 3D game that has a player with the follow cam. Before I started using real models I used the cube and I rendered it using displaylist and it everything moved fine. However, now that I am importing full 3D models with many more vertices, I looked into VBOs. I have a full structure setup for my VBOs and I can see the model drawn initially but it is drawn at the center of the game world. When I move the player, the model doesn't translate as it should. The model doesn't move its position.
Here is the code that I used initially to draw the player as a rectangle (which works):
public static void drawRectPrism(float centerx, float centery, float centerz, float length, float height, float width, float rx, float ry, float rz)
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
{
glTranslatef(centerx, centery, centerz);
glRotatef(rx, 1, 0, 0);
glRotatef(ry, 0, 1, 0);
glRotatef(rz, 0, 0, 1);
glTranslatef(-centerx, -centery, -centerz);
glTranslatef(-length/2f, -height/2f, -width/2f);
glBegin(GL_QUADS);
{
glColor3f(1.0f, 0, 0);
glVertex3f(centerx, centery, centerz);
glVertex3f(centerx + length, centery, centerz);
glVertex3f(centerx + length, centery + height, centerz);
glVertex3f(centerx, centery + height, centerz);
glColor3f(0, 1.0f, 0);
glVertex3f(centerx, centery, centerz + width);
glVertex3f(centerx + length, centery, centerz + width);
glVertex3f(centerx + length, centery + height, centerz + width);
glVertex3f(centerx, centery + height, centerz + width);
glColor3f(0, 0, 1.0f);
glVertex3f(centerx, centery, centerz);
glVertex3f(centerx, centery + height, centerz);
glVertex3f(centerx, centery + height, centerz + width);
glVertex3f(centerx, centery, centerz + width);
glColor3f(0, 1.0f, 1.0f);
glVertex3f(centerx + length, centery, centerz);
glVertex3f(centerx + length, centery + height, centerz);
glVertex3f(centerx + length, centery + height, centerz + width);
glVertex3f(centerx + length, centery, centerz + width);
glColor3f(1.0f, 1.0f, 0);
glVertex3f(centerx, centery, centerz);
glVertex3f(centerx + length, centery, centerz);
glVertex3f(centerx + length, centery, centerz + width);
glVertex3f(centerx, centery, centerz + width);
glColor3f(1.0f, 0, 1.0f);
glVertex3f(centerx, centery + height, centerz);
glVertex3f(centerx + length, centery + height, centerz);
glVertex3f(centerx + length, centery + height, centerz + width);
glVertex3f(centerx, centery + height, centerz + width);
}
glEnd();
}
glPopMatrix();
}
I tried a couple of different ways somewhere better than others and probably implemented terrible programming structure, but I figured it should still work.
First attempt: to adapt the rectangle code to load my vertices and models instead of specific rectangle verticies:
public void translate(float x, float y, float z, float rx, float ry, float rz)
{
File f = new File("graveDigga.obj");
try{
m = OBJLoader.loadModel(f);
}
catch(FileNotFoundException e)
{
e.printStackTrace();
Display.destroy();
System.exit(1);
}
catch(IOException e)
{
e.printStackTrace();
Display.destroy();
System.exit(1);
}
displayListChar = glGenLists(1);
glNewList(displayListChar, GL_COMPILE);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
{
glTranslatef(x, y, z);
glRotatef(rx, 1, 0, 0);
glRotatef(ry, 0, 1, 0);
glRotatef(rz, 0, 0, 1);
//glTranslatef(-x, -y, -z);
//glTranslatef(-length/2f, -height/2f, -width/2f);
glBegin(GL_TRIANGLES);
for(Face face : m.faces)
{
Vector2f t1 = m.textures.get((int) face.textures.x - 1);
glTexCoord2f(t1.x +x ,1-(t1.y +y ));
Vector3f n1 = m.normals.get((int) face.normal.x-1);
glNormal3f(n1.x +x ,n1.y+y,n1.z +z);
Vector3f v1 = m.vertices.get((int) face.vertex.x-1);
glVertex3f(v1.x +x,v1.y+y,v1.z+z);
Vector2f t2 = m.textures.get((int) face.textures.y - 1);
glTexCoord2f(t2.x +x, 1 - (t2.y+y ));
Vector3f n2 = m.normals.get((int) face.normal.y-1);
glNormal3f(n2.x+x,n2.y+y ,n2.z+z);
Vector3f v2 = m.vertices.get((int) face.vertex.y-1);
glVertex3f(v2.x+x,v2.y+y ,v2.z+z);
Vector2f t3 = m.textures.get((int) face.textures.z - 1);
glTexCoord2f(t3.x +x, 1 - (t3.y +y));
Vector3f n3 = m.normals.get((int) face.normal.z-1);
glNormal3f(n3.x+x,n3.y+y,n3.z +z);
Vector3f v3 = m.vertices.get((int) face.vertex.z-1);
glVertex3f(v3.x+x,v3.y+y,v3.z +z);
}
glEnd();
}
glPopMatrix();
//}
//glPopMatrix();
build();
}
I next tried to display model by creating a VBO from this data and call a rander method in my game loop. Before calling render I would run through the code to attempt to translate the position of VBO but nothing was happening.
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
{
//glLoadIdentity();
glTranslatef(x,y,z);
glRotatef(rx, 1, 0, 0);
glRotatef(ry, 0, 1, 0);
glRotatef(rz, 0, 0, 1);
glTranslatef(-x,-y,-z);
glDrawArrays(GL_TRIANGLES, 0, m.faces.size() * 3);
}
I am not sure if I should be using shaders for this or not, but part of me is questioning why it is hard to move a 3D model in world space? Is there a messy way that is easier to implement for a temporary basis?
Considering you're a new person with 1 rep, have an upvote. Nicely written question :).
I am not sure if I should be using shaders for this or not, but part of me is questioning why it is hard to move a 3D model in world space? Is there a messy way that is easier to implement for a temporary basis?
A simple glTranslatef() call should suffice to move an object, and simple glRotatef() call will rotate the object.
For example, using glRotatef(90, 1, 0, 0) returns this result:
Wheras without the line, the grass is not rotated at all:
Same with the ship. Using glRotatef(40, -1, 0, 0) returns this result:
Wheras without the line it just returns flat:
Obviously that is just the pitch. glRotatef(AmountToRotate, PITCH, YAW, ROLL) can roll the ship onto its side, or rotate the ship around.
Enough about rotation.
For rendering the grass, I just use this:
public void render(){
Main.TerrainDemo.shader.start();
glPushMatrix();
glDisable(GL_LIGHTING);
glTranslatef(location.x * TerrainDemo.scale, location.y, location.z * TerrainDemo.scale);
TexturedModel texturedModel = TerrainDemo.texModel;
RawModel model = texturedModel.getRawModel();
GL30.glBindVertexArray(model.getVaoID());
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturedModel.getTexture().getID());
glScalef(10f, 10f, 10f);
glColor4f(0, 0, 0, 0.5f);
glDrawElements(GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL30.glBindVertexArray(0);
glEnable(GL_LIGHTING);
glPopMatrix();
Main.TerrainDemo.shader.stop();
}
To cut through code which you will probably not recognize/understand, I am basically just saying that glTranslatef(location.x * TerrainDemo.scale, location.y, location.z * TerrainDemo.scale) is the only thing that is setting the location. After translating, I simple set the scale (the size), and then draw it.
Wheras if I remove the glTranslatef() line, all the grass will just render in the same location:
So to answer your question:
use something like this (Psuedocode)
PUSH MATRIX HERE (glPushMatrix) SO TO NOT SAVE THE CURRENT TRANSLATION
USE GLTRANSLATEF ON LOCATION OF CURRENT OBJECT
RENDER/DRAW OBJECT
POP THE MATRIX
Unfortunately, looking through your code I could'nt find the specific issue that is actually causing it not to draw, meaning I cannot just say "Have xxx code and it will work", but I hope that it helped on how to move/rotate an object.
I am using VBOs just like you are for rendering the grass, ship, and trees (though I use a display list for terrain because I am lazy).
My skype is joehot200 if you wish to discuss anything further.

Texture not rotating LWJGL

I am using the LWJGL and Slick-Util libraries in an attempt to rotate a texture.
Here is my code I'm using to try and rotate a texture.
GL11.glBegin(GL11.GL_QUADS);
GL11.glPushMatrix();
GL11.glTranslatef(x, y, 0);
GL11.glRotatef(r, 1, 1, 0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(x, y);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(x + texture.getTextureWidth(), y);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(x + texture.getTextureWidth(), y + texture.getTextureHeight());
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(x, y + texture.getTextureHeight());
GL11.glPopMatrix();
GL11.glEnd();
It draws the texture perfectly fine at the x and y locations given, but when I pass in a degree, it does not rotate and just draws as if the rotation was zero.
Does anyone know what I'm doing wrong? Thank you in advance.
Edit:
When I run this code with an 'r' of 180 or any other degree. This is what my screen looks like.
The red circle image looks exactly like that as a regular un-rotated image.
I know it is a bit egotistical to answer your own questions, but after much research I have finally found my answer. Here is a working method that will rotate a slick-util texture according to an 'r' given in degrees.
public static void drawTexture(Texture texture, int x, int y, int r)
{
int width = texture.getTextureWidth();
int height = texture.getTextureHeight();
GL11.glTranslatef(x + width / 2, y + height / 2, 0);
GL11.glRotatef(r, 0, 0, 1);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(-width / 2, -height / 2, 0);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(width / 2, -height / 2, 0);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(width / 2, height / 2, 0);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(-width / 2, height / 2, 0);
GL11.glEnd();
GL11.glLoadIdentity();
}

Categories

Resources