I am doing something with JOGL libraries (forced to) and I can't figure out how to offset the center zero coordinates. I would like to offset them to the bottom of my viewport, in the method
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
but I can't google any way to translate int height into any meaningfull offset float coordinates.
edit:
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0, width / (float) height, 0.1, 100.0);
gl.glMatrixMode(GL_MODELVIEW);
gl.glLoadIdentity();
Solved using
glu.gluLookAt(0, 0, 1, 0, 0.42, 0, 0, 1, 0);
Didn't realize the offset wasn't relative.
Related
Hello Everyone,
I am working on a 2D tile game and I want to work on light so at night it'll be darker and at night brighter. If I can't change the brightness, I could have it fade from a brighter texture to a darker. Here is the image drawing function I have:
public static void drawRectTexRot(Texture tex, float x, float y, float width, float height, float angle) {
tex.bind();
glTranslatef(x + width / 2, y + height / 2, 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(- width / 2, - height / 2, 0);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(0,0);
glTexCoord2f(1,0);
glVertex2f(width,0);
glTexCoord2f(1,1);
glVertex2f(width, height);
glTexCoord2f(0, 1);
glVertex2f(0, height);
glEnd();
glLoadIdentity();
}
Any help would be great. :)
-Ekrcoaster
In java you cange transparency of Graphics2D object by method :
float opacity = 0.5f;
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
Value 0 is for transparent and 1 is opaque. In this case whatever you draw next
will be half transparent.
To change brightness of Image use RescaleOp. Example :
RescaleOp rescaleOp = new RescaleOp(1.3f, 0, null);
rescaleOp.filter(image, image);
The first argument of RescaleOp constructor determines brightness scale. Value of 1f means the same brightness, value 0.75f means image is darker by 25%. Value of 1.3f makes image brighter by 30 %.
I'm coding for a game and want the background to repeat itself.
xOffset = (int) (camera.getX() % WIDTH);
g.drawImage(bgInv, xOffset - WIDTH, 0, WIDTH, HEIGHT, null);
g.translate(xOffset, 0);
g.drawImage(bg, 0, 0, WIDTH, HEIGHT, null);
g.translate(-xOffset, 0);
g.drawImage(bgInv, xOffset + WIDTH, 0, WIDTH, HEIGHT, null);
The first drawImage draws when the camera's X is negative
and the third when the camera's X is positive.
bg is the normal background
bgInv is the background inverted
The problem is that when I'm moving and the xOffset goes from WIDTH to 0, it seems like there's a "wrap".
Click here to see
The console is outputting xOffset
I know it is because I'm using modulo to get xOffset but I didn't figure out a better way...
Thanks in advance
If I understood correctly, what you want to repeat is a 2 * WIDTH by HEIGHT image, where the left half is the background image and the right half is the same image horizontally inverted.
So what you can do is the following:
xOffset = (int) (camera.getX() % (2 * WIDTH));
// draw the background image at x = xOffset - 2 * WIDTH
g.drawImage(bg, xOffset - 2 * WIDTH, 0, WIDTH, HEIGHT, null);
g.drawImage(bgInv, xOffset - WIDTH, 0, WIDTH, HEIGHT, null);
// draw the background image at x = xOffset
g.drawImage(bg, xOffset, 0, WIDTH, HEIGHT, null);
g.drawImage(bgInv, xOffset + WIDTH, 0, WIDTH, HEIGHT, null);
I have a basic LWJGL window set up and I am trying to draw a square using the glBegin(GL_QUADS) method. Square square = new Square(25, 25, 25), is the way I am calling my Square class to draw the square... but it is a rectangle. When I call it I pass in all 25's as the parameters. the first two are the starting coordinates and the last 25 is the side length, as seen below. What am I doing wrong to produce a rectangle?
public Square(float x,float y,float sl) {
GL11.glColor3f(0.5F, 0.0F, 0.7F);
glBegin(GL11.GL_QUADS);
glVertex2f(x, y);
glVertex2f(x, y+sl);
glVertex2f(x+sl, y+sl);
glVertex2f(x+sl, y);
glEnd();
}
My Viewport code
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Resets any previous projection matrices
glOrtho(0, 640, 0, 480, 1, -1);
glMatrixMode(GL_MODELVIEW);
Using glOrtho(0, 640, 0, 480, 1, -1); constructs a non-square viewport. That means that the rendered output is more than likely going to be skewed if your window is not the same size as your viewport (or at least the same aspect ratio).
Consider the following comparison:
If your viewport is the same size as your window, then it should remain square. I'm using JOGL, but in my resize function, I reshape my viewport to be the new size of my window.
glcanvas.addGLEventListener(new GLEventListener() {
#Override
public void reshape(GLAutoDrawable glautodrawable, int x, int y, int width, int height) {
GL2 gl = glautodrawable.getGL().getGL2();
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity(); // Resets any previous projection matrices
gl.glOrtho(0, width, 0, height, 1, -1);
gl.glMatrixMode(GL2.GL_MODELVIEW);
}
... Other methods
}
To draw a square around the point (x | y) you can calculate the four points that represent the corners of your square.
First you'll need your width to height ratio
float ratio = width / height
I will use a defaultSize for the length of the shortest path from the midpoint to any of the sides.
Then you can calculate four values like so:
float a = x + defaultSize
float b = ratio * (y + defaultSize)
float c = x - defaultSize
float d = ratio * (y - defaultSize)
with which you can represent all four corners to draw your square with. Since GL_SQUAD is deprecated I'll use GL_TRIANGLE.
glBegin(GL_TRIANGLES);
glColor3f(red, green, blue);
// upper left triangle
glVertex2f(a, b);
glVertex2f(c, b);
glVertex2f(c, d);
// lower right triangle
glVertex2f(a, b);
glVertex2f(c, d);
glVertex2f(a, d);
glEnd();
I don't know if this is the most performant or idiomatic way to do this since I just started exploring LWJGL.
How should the x, y, z, sizeX, sizeY, sizeZ values be put to the vertices to make a cube?
public static void cube(float x, float y, float z, float sx, float sy, float sz){
glPushMatrix();
{
glTranslatef(x, y, z);
//Just one side of the cube is given due to too much unnecessary code.
glBegin(GL_QUADS);
glVertex3f(-1, -1, 1);
glVertex3f(1, -1, 1);
glVertex3f(1, 1, 1);
glVertex3f(-1, 1, 1);
glEnd();
}
glPopMatrix();
}
Thanks.
Wherever in your code you have e.g. glVertex3f(-1, -1, 1); mulitply them with the corresponding value of sx, sy, sz divided by 2 e.g. glVertex3f(-sx/2, -sy/2, sz/2);
For the position you can issue a glTranslatef(x, y, z) before drawing the cube. If you insist on hardcoding this into the vertices then you should write the above statement as glVertex3f(x - sx/2, y - sy/2, z + sz/2);
I tried the code below, which draws a good approximation of a circle if the rectangle's width is the same as its height; but it doesn't draw a great oval, the "corners" are very pointed. Any suggestions?
float width = rect.width();
float height = rect.height();
float centerX = rect.width() / 2;
float centerY = rect.height() / 2;
float diameter = Math.min(width, height);
float length = (float) (0.5522847498 * diameter/2);
path.moveTo(0, centerY);
path.cubicTo(0, centerY - length, 0, centerX - length, 0, centerX, 0);
path.cubicTo(centerX + length, 0, width, centerY - length, height, centerY);
path.cubicTo(width, centerY + length, centerX + length, height, centerX, height);
path.cubicTo(centerX - length, height, 0, centerY + length, 0, centerY);
You should scale length according to which axis it's on, so that the distance from each arc endpoint to the adjacent control points is (not fixed but) a fixed fraction of the axis you're moving parallel to at that point.
If it's a true rectangle, with right angles, then it should be easy. The major and minor axes of the ellipse equal the lengths of the sides of the rectangle, and the center of the ellipse is located at the intersection of the rectangle's diagonals.
I don't know how to express it as Bezier splines off the top of my head, but the classic equation for an ellipse should be easy enough to write, as long as you transform to the appropriate coordinate system first (e.g. x-axis along the major axis of the rectangle/ellipse, y-axis along the minor axis of the rectangle/ellipse).