OpenGL not rendering as expected - java

If I have the screen setup with the following
gl.glViewport(0,0,width,height); //Reset the current viewport
gl.glMatrixMode(GL10.GL_PROJECTION); //select the projection matrix
gl.glLoadIdentity(); //Reset the Projection Matrix
gl.glOrthof(0f,1f,0f,1f,-1f,1f);
and the vertices set up as follows
this._vertices=new float[]{
0.0f,0.5f,0.0f, //V1 bottom left
0.0f,1.0f,0.0f, //V2 top left
0.5f,0.5f,0.0f, //V3 Bottom right
0.5f,1.0f,0.0f //V4 top right
};
then when drawing I do
gl.glTranslatef(0.0f,0.0f,0.0f);
it places the square in the top left (i thought 0,0 was bottom left?)
and then
gl.glTranslatef(0.5f,-0.5f,0.0f);
places the square in the middle of the screen (suggesting the bottom is actually -1.0f rather than 0.0f)
How do I make the bottom left of my screen start from 0,0?
Update------
I have found that if I change the line
gl.glOrthof(0f,1f,0f,1f,-1f,1f);
to
gl.glOrthof(0f,1f,1f,0f,-1f,1f);
then nothing changes (ie the top left is still 0,0 and bottom left is 0,-1)
however if I leave the line out completely then the origin is in the centre of the screen (ie top left is -1,-1)

The glOrthof is used to create your screen coordinate system, since its input parameters are left, right, top and bottom I can not understand where the confusion lies. If you wish your top left corner is the (0,0) point then just set the top and left parameters to zero and bottom and right to some positive values so that positive X is to the right and positive Y is downwards.
Some very common cases of usage of glOrthof are:
View coordinate system where you insert (0,0) as top-left and (screenHeight , screenWidth) as bottom-right. In quite a few cases the bottom-left is at (0,0) though.
Graph like coordinate system where screen centre is at (0,0) and then top-left is at (1,-1) and bottom-right at (-1,1)
Normalized view system which has the screen centre at (0,0) but height is relative to width depending on the screen ratio. So top-left is at (-1,-(screenHeight/screenWidth)) and bottom-right at (1, screenHeight/screenWidth).
You should note that only in 1st and 3rd cases your square will always be drawn as a square and not as a rectangle (where a/b = screenWidth/screenHeight). So choose which suits your application best.
You should know that glOrthof is also very useful for panning and zooming. Instead of doing some strange translations and scales you can simply change glOrthof parameters so that you display a certain part of your scene.

Related

3D rotation ratios via mouse placement on screen - Java

I am making a 3D game in which the player can rotate their view point via the mouse to look around the environment. I firstly just did x and y rotation via vertical and horizontal movement of the mouse and z via another control. But after playing the game I realised it did not rotate correctly. NOTE: I have a global variable matrix which represents the player's angle (3x1), at 0,0,0 it seems to work correctly as up or down is a direct x axis rotation and right or left is a direct y axis rotation, but if I move my camera diagonally for example then left doesn't directly correlate to a y axis rotation anymore.
Visually on a unit circle the players viewpoint wouldn't travel the full circumference anymore and would travel in a circle that is smaller that the circumference. This is the current code (x and yRateOfRot is the ratio of how far away from the centre the cursor is in each direction between -1 and 1):
private static void changeRotation(){
angle.set(Matrix.add(angle.matrix,new double[][]{
{ROTATION_SPEED * camera.xRateOfRot()},
{ROTATION_SPEED * camera.yRateOfRot()},
{ROTATION_SPEED * camera.zRateOfRot()}}));
}
I have looked at this source http://paulbourke.net/geometry/rotate/ and understand how to rotate via an arbitrary axis which I could do but I am not sure how to correlate this into getting a ratio to find out what the x,y and z change would be for looking in a specific direction i.e. at 0,0,0 the ratio of looking up would be x:1, y:0, z:0 but then at another angle the ratios would be different as looking up no longer means only an x rotation. Any information would be appreciated, thanks!

Why is my pointer y coordinate reversed?

When I click on the top left corner it gives me 0,0 but I want the opposite result. I want to have a 0,0 coordinate when I click on the bottom left corner.
Basically when I go down the y coordinate is higher and when I go up the y coordinate is lower.It only happens when I do Gdx.input.getY();
This is true for almost any computer language or function.
The reason for this goes back. In earlier computers Cathode Ray Tubes (CRTs) would "draw" the image with a cathode ray from the upper left corner to the lower right.
To ease the interface between the graphics card memory and the CRT, the memory was read from the beginning and the image was drawn from the top left (with the lowest memory address) to the lower right (with the highest memory address).
The reason for this order likely originates from the writing style in countries where the computer was invented.
The tradition of computing from left to right and top to bottom persists today, thus the origin for the plane field in in the upper left corner.
Use the Camera's unproject method, which takes a Vector3 holding screen coordinates and converts them to camera (or world) coordinates. Try something like:
camera.unproject(screenCoords.set(screenX, screenY, 0);
Note, this will modify screenCoords to hold world coordinates (in your case... flipping the y, and taking into account any translation of the camera).

Position Vector relative to origin

I have a little tech game I am messing around with and I can't figure out the formula to position 1 object given another objects origin.
So I have a Spaceship and a Cannon. I have the game setup to use units, so 1 unit = 16 pixels (pixel art).
Basically my cannon should be placed 0.5625 units on the X and 0 on the Y relative to the origin of the Spaceship, which is located at 0, 0 (bottom left corner).
The cannon should is independent on the angle of the spaceship, it can aim in different directions rather than being fixed to aim the way of the spaceship.
I have it constantly following the cursor, which works fine. Now when I rotate the Spaceship, obviously the origin of the Spaceship is changing in world coordinates, so my formula to place the cannon is all messed up, like so:
protected Vector2 weaponMount = new Vector2();
weaponMount.set(getBody().getPosition().x + 0.5625f, getBody()
.getPosition().y);
Obviously if I position the ship at a 90° angle, X is going to be different and the cannon would be waaaayyy off the ship. Here is a screenshot example of what I mean:
What would be the formula for this? I have tried using cos/sin but that does not work.
Any ideas?
weaponMount.set(0.5625f,0).setAngle(SpaceshipAngle).add(getBody().getPosition());
Where SpaceshipAngle is the angle of your Spaceship.
The origin of the spaceship is the point, arround which the spaceship will rotate and scale (the Texture of it). The position instead is always the lower left corner of the Texture and does not depend on the rotation.
Your problem is, that your offset does not depent on the rotation of your spaceship.
To take care about this rotation you should store a Vector2 offset, which describes your weapons offset (in your case it is a Vector2(0.5625f, 0)).
Next store a float angle describing your spaceships rotation.
Then you can rotate the offset by using: offset.setAngle(rotation).
The last thing is to set the weapons position. The code for this did not change so much:
weaponMount.set(getBody().getPosition().x + offset.x, getBody()
.getPosition().y + offset.y);

Java - Scaling a canvas about a particular point

I am more or less finished a very simple planetary gravity simulator using Newtonian physics. It can transform and scale the planets for pan and zoom. This works fine, mouse input and everything. The problem I have is more aesthetic than anything else. Since the origin of the canvas is at the top left corner of the window (within a JPanel, within a JFrame), everything scales about that point. I was wondering is there any way to either set the origin to the center of the screen, or to scale about a particular point? (Even though AffineTransform.scale() only has one constructor, with scaleX and scaleY as the args). I have tried setting the bounds of the canvas as negative numbers as such:
canvas.setBounds(-width/2, -height/2, width/2, height/2);
(Width and height are the screen size).
This obviously doesn't work as negative numbers are outside the co-ords of the panel.
So does anyone know anyway of accomplishing this? Either setting the centre of the screen as the origin or scaling about a particular point rather than the origin?
The trick here is to concatenate instances of AffineTransform.
Move the entire rendering so the center is at the origin.
Scale the rendering.
Move the center of the rendering (now at the origin) back to the center of the view.

Changing image node to another corner

So basically i decided to start a small project but i noticed the node of the image is at the upper left corner. So for example, the x and y values are from that corner. I'd like them to be starting at the bottom left corner. How can i change that?
Sorry if node isn't the right name.
Java Component uses the top left as the origin of the axis. So if you it to be at the bottom left, you need to add the height of the component to the y coordonate.
You should set the location to x, y+ component.getHeight()

Categories