Crop camera view in libgdx - java

I'm making an Android game using LibGDX. I want to do something hard to explain but I'll try nonetheless.
Look at this camera view (it's from 3ds max):
I can get same exact view in LibGDX without any problems:
perspCam = new PerspectiveCamera(40, screenHeight, screenWidth * h / w);
perspCam.position.set(1, 6, 18f);
perspCam.lookAt(1, 3, -1f);
perspCam.update();
Now the tricky part. What I actually want to see in-game is part inside the red rectangle but filling the whole screen.
I have managed (using GlScissors) to make it look like this:
But I have no idea how to make it fit the whole screen.
Gdx.gl20.glEnable(Gdx.gl20.GL_SCISSOR_TEST);
Gdx.gl20.glScissor(0, 0, 450, 550);
modelBatch.render(instance);
modelBatch.flush();
Gdx.gl20.glDisable(Gdx.gl20.GL_SCISSOR_TEST);
modelBatch.end();

In stead of using glScissor, you should change camera position.
Make it nearer and shift it sideways too. The exact coordinates can be calculated properly.
Hope this helps.

Related

libgdx distorted texture when using camera.setToOrtho(false, GAME_WIDTH, GAME_HEIGHT);

I'am trying to make libgdx game, adn i've got 2 problems now:
1. When im using camera.setToOrtho(false, GAME_WIDTH, GAME_HEIGHT); my textrure of player is distored, one eye is bigger than another.
2. Im using this camera and viweport:
camera= new OrthographicCamera(GAME_WIDTH,GAME_HEIGHT);
camera.setToOrtho(false, GAME_WIDTH, GAME_HEIGHT);
viewport = new StretchViewport(GAME_WIDTH,GAME_HEIGHT,camera);
and when i do :
touchPos.set(Gdx.input.getX(1),Gdx.input.getY(1),0);
game.camera.unproject(touchPos);
System.out.println(touchPos.x+" "+ touchPos.y);
I get 0 0 in right top corner but my character witch cords 0 0 is drawing in left bottom corner.
When i use:
game.camera = new OrthographicCamera(820,480);
game.viewport = new FillViewport(820,480,game.camera);
i must use game.batch.setProjectionMatrix(game.camera.combined);
when i use this code i've got corectly working unproject method and coordinate system but i've got distored texture.
When i use:
camera = new PerspectiveCamera();
viewport = new StretchViewport(900, 470, camera);
i've got distored texture and bad coords system. and i can 't use :game.batch.setProjectionMatrix(game.camera.combined);
It is distorted because you are using a fixed width and height for the camera, so it simply stretches to fit whatever size window/screen you're putting it on.
Incidentally, StretchViewport does the same thing (distorts the scene to fit the dimensions you gave it to the window). But you just created a viewport without actually calling update() on it, so it's not doing anything.
You most likely want to use ExtendViewport or FillViewport to avoid stretching, but other options are available. Read about them here.
Your viewport won't properly respond to screen rotations (or desktop window resize) unless you put viewport.update(width,height) into the resize method. And you have to call update on it at least once regardless, using the actual screen dimensions. This is most convenient by simply putting viewport.update(width,height) into the resize method.

How does one rotate relative to the camera in openGL or more specifically LWJGL?

I am making a 3d game with LWJGL. In this game, whenever I press an arrow key, I want the screen to rotate in that direction, regardless of the current orientation. I am struggling to implement this in code. Using three glRotatef functions based off of a rotation vector3f, does not accomplish this. Rotating up and down work because glRotatef(rotation.x, 1, 0, 0) is called first, but left and right only work when your not looking up or down. If you are, you rotate around a universal y axis, and camera spins. I saw that another implementation could use gluLookAt(), but I imagine I would encounter the same problem.
EDIT
I thought I solved my issue by changing the order by which glRotatef()'s where called depending on the direction I want to rotate. I thought this would work because in my game, I will only be rotating one axis at a time. It worked somewhat but in some orientations it doesn't.
if(updown){
glRotatef(rotation.x, 1, 0, 0);
glRotatef(rotation.y, 0, 1, 0);
} else if(leftright){
glRotatef(rotation.y, 0, 1, 0);
glRotatef(rotation.x, 1, 0, 0);
}
glTranslatef(position.x, position.y, position.z);
gluLookAt would probably get you there much quicker, but you would need to manually rotate the eye coordinate about the origin coordinate. With gluLookAt you also need to calculate an up vector if you plan to rotate around all 3 axes.
I have a hunch you just need to add a glTranslate before you do your glRotate so that the camera has something to orbit around.
If you show some code you might be able to get more help.
I am over this issue and have moved on. Because I am only going to rotate one axis 90 degrees at a time, I went through all 64 possible orientations and applied the necesary transforations manualy.

Can't get to move libgdx OrthographicCamera

I'm having issues with the Camera class in libGDX, I just can't visually move it even though its position changes.
This is how I setup the camera:
camera = new OrthographicCamera(frustumWidth, frustumHeight);
This is how I change its position:
world.onUpdate(deltaTime, camera);
renderer.render(world);
camera.position.set(MathUtils.random(0, 800), MathUtils.random(0, 480), 0);
//camera.position.set(
// world.dynamicObjects.get(GameWorld.MainPgID).pos.x * GameWorld.frustumToWorldRatio,
//world.dynamicObjects.get(GameWorld.MainPgID).pos.y * GameWorld.frustumToWorldRatio, 0);
I submit the changes at the beginning of the .render function:
camera.update();
batcher.setProjectionMatrix(camera.combined);
batcher.begin();
polygonBatcher.begin();
As I've said the position does change, and yet of the many combinations I've tried none works.
Perhaps I misunderstood how the Camera works and I need to move the objects and not the camera? Seems to be stupid to me, after all it's called camera for a reason.
You have to apply the combined projection matrix on every batch/renderer you want it to affect.
But besides of that it seems to be fine. Try to debug it step by step.
Setup a simple project with only new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); and some lines (ShapeRenderer) or sprites (SpriteBatch), set the projection matrix to camera.combined, change camera.position and update() the camera. That should do it.
Then when you change camera.position all the renderer with the camera's frustum should offset accordingly without changing the offsets of the sprites/geometry.
btw. the viewport is not 0 - 800, but rather -400 - 400 for x and respectively for y;

Unexpected results implementing simple motion blur in Libgdx

In the two attached pictures, the desktop screenshot of libgdx functions as expected. The screenshot from my Galaxy Nexus is unfortunately not as expected. I am attempting to create a simple motion blur or trail effect.
Rendering as I expected on my desktop.
Not rendering as I expected on my Galaxy nexus.
The circle textures are drawn in a for loop during rendering and the effect is achieved with a pixmap using the RGBA of 0, 0, 0, 0.1f that is drawn before the circles.
screenClearSprite creation
Pixmap screenClearPixmap = new Pixmap(256, 256, Format.RGBA8888);
screenClearPixmap.setColor(Color.rgba8888(0, 0, 0, 0.1f));
screenClearPixmap.fillRectangle(0, 0, 256, 256);
screenClearTexture = new Texture(screenClearPixmap);
screenClearSprite = new Sprite(screenClearTexture);
screenClearSprite.setSize(screenWidth, screenHeight);
screenClearPixmap.dispose();
Render
batch.begin();
font.draw(batch, "fps:" + Gdx.graphics.getFramesPerSecond(), 0, 20);
screenClearSprite.draw(batch);
for (int i = 0; i < circleBodies.size(); i++) {
tempPos = circleBodies.get(i).getPosition();
batch.draw(circleTexture, (tempPos.x * SCALE) + screenWidthHalf
- circleSizeHalf, (tempPos.y * SCALE) + screenHeightHalf
- circleSizeHalf);
}
batch.end();
So, what did I do wrong? Perhaps there is a better way to get the 'motion blur' effect of movement?
Here is a different approach, where you clear your screen each time with solid color and no alpha.
This means that you will have to modify your code some. The good thing about this, is that the way you are doing it has some flaws: It will blur everything in motion, not just the balls. And can quickly produce ugly results/artefacts unless you are careful.
Do the same as you are doing now, but instead of drawing the balls to the batch, draw them onto a texture/bitmap/whatever. Then each frame add an alpha-blended image over the balls-image, and then draw the balls in their current position on top of that. Then add that image to your screen. Very much like you are doing now, except you draw to something else and keep it. This way you don't have to rely on the viewport you are drawing onto, and can keep everything separated.
This method is similar to drawing to an accumulation buffer.
Instead of doing it the way you are doing, you can keep track of the n latest positions of each ball. And then draw all of them each frame, with different alpha. This is very easy to implement. Can result in many drawing calls if you have many balls or a large n, but if it's not too much it shouldn't limit your fps and gives nice control.
Perhaps there is a better way to get the 'motion blur' effect of
movement?
in order to make motion blur in my game i use another approch "The particle effect" it works realy fine with me and i didn't have Android/Desktop problems or with different android devices
all you have to do is to use "Particle Effect Editor" of Libgdx and make your effect then load it in your project finally draw it at the same position you draw your object (and alos draw your object)
Tips to make the right effect file with Paticle Editor :
set (use) the same image of the object that you want to blur it motion in the particle effect
try to limit the count : the max number of particle allowed
Disable the "velocity" and "Angle"
parameter
Particle effect help to do motion effect
Hope this will help someone !

OpenGL: Set position instead of translating?

Can I set the current render position to be an arbitrary value, instead of just giving it an offset from the current location?
This is what I'm doing currently:
gl.glTranslatef(3.0f, 0.0f, 2.0f);
It allows me to say "I want to move left" but not "I want to move to point (2, 1, 2)". Is there a way to do the latter?
I'm using OpenGL with JOGL.
Update:
#Bahbar suggests the following:
gl.glLoadIdentity();
gl.glTranslatef(...);
When I do this, everything except six lines disappears. I'm not sure why. I'm having a problem with the far clipping plane being too close, so perhaps they're too far away to be rendered.
Yes. Just start from the identity matrix.
gl.glLoadIdentity();
gl.glTranslatef(...);
Yes, you can set your view position using the gluLookAt command. If you look at the OpenGL FAQ, there is a question that has the line most relevant to your problem: 8.060 How do I make the camera "orbit" around a point in my scene?
You can simulate an orbit by
translating/rotating the scene/object
and leaving your camera in the same
place. For example, to orbit an object
placed somewhere on the Y axis, while
continuously looking at the origin,
you might do this:
gluLookAt(camera[0], camera[1], camera[2], /* look from camera XYZ */
0, 0, 0, /* look at the origin */
0, 1, 0); /* positive Y up vector */
glRotatef(orbitDegrees, 0.f, 1.f, 0.f); /* orbit the Y axis */
/* ...where orbitDegrees is derived from mouse motion */
glCallList(SCENE); /* draw the scene */
If you insist on physically orbiting the camera position, you'll
need to transform the current camera
position vector before using it in
your viewing transformations.
In either event, I recommend you
investigate gluLookAt() (if you aren't
using this routine already).
Note that gluLookAt call: the author is storing the camera position in a 3 value array. If you do the same, you will be able to specify your viewpoint in absolute coordinates exactly as you wanted.
NOTE: if you move the eye position, it's likely that you're going to want to specify the view direction as well. In this example, the author has decided to keep the eye focused on the point (0, 0, 0).
There's a good chance that this question is related to what you're trying to do as well: GLU.gluLookAt in Java OpenGL bindings seems to do nothing.

Categories

Resources