Can I use glScale on GL_POINTS? - java

I am programming a GUI framework in lwjgl (opengl for java). I've recently implemented rounded rectangles by rendering a couple of normal rectangles surrounded by circles. To render the circles I used GL11.GL_POINTS. I now reached the point, where I am trying to implement animations and for a window open animation, I decided to GL11.glScaled() it from small to normal. That works fine, but unfortunately my circles don't get resized.
I tried changing my GL_POINTS circle render method against a method that uses TRIANGLE_FANs and that worked fine. My problem there was, that the circles didn't look smooth and round at all and if I increase the rendered triangles it starts to lag very quick. Even though my computer isn't bad at all.
This is the code I've used to render circles with GL_POINTS.
GL11.glEnable(GL11.GL_POINT_SMOOTH);
GL11.glHint(GL11.GL_POINT_SMOOTH_HINT, GL11.GL_NICEST);
GL11.glPointSize(radius);
GL11.glBegin(GL11.GL_POINTS);
GL11.glVertex2d(x, y);
GL11.glEnd();
GL11.glDisable(GL11.GL_POINT_SMOOTH);
This is the code I've used to scale the circles
GL11.glPushMatrix();
GL11.glTranslated(x, y, 0);
GL11.glScaled(2.0f, 2.0f, 1);
GL11.glTranslated(-x, -y, 0);
render circles
GL11.glPopMatrix();
I expect the circles to scale accordingly to the number I've put into glScaled()
Currently they aren't rescaling at all, just rendered at their normal size.

Here's a demonstration of how to properly render a circle using triangle fans:
public void render() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// Coordinate system starts out as screen space coordinates
glOrtho(0, 400, 300, 0, 1, -1);
glColor3d(1, 0.5, 0.5);
renderCircle(120, 120, 100);
glColor3d(0.5, 1, 0.5);
renderCircle(300, 200, 50);
glColor3d(0.5, 0.5, 1);
renderCircle(200, 250, 30);
}
private void renderCircle(double centerX, double centerY, double radius) {
glPushMatrix();
glTranslated(centerX, centerY, 0);
glScaled(radius, radius, 1);
// Another translation here would be wrong
renderUnitCircle();
glPopMatrix();
}
private void renderUnitCircle() {
glBegin(GL_TRIANGLE_FAN);
int numVertices = 100;
double angle = 2 * Math.PI / numVertices;
for (int i = 0; i < numVertices; ++i) {
glVertex2d(Math.cos(i*angle), Math.sin(i*angle));
}
glEnd();
}
Output image:
The GL_POINT_SIZE value is actually the size of the point in pixels onscreen, not current coordinate units. For that reason your circles were unaffected by GL_SCALE. That's one reason not to use GL_POINTS to render circles. The other (arguably more important) reason being that GL_POINT_SIZE is severely deprecated and unsupported in newer OpenGL profiles.

Related

Using 3D Layers in Processing

I'm working on a method of visualizing solids of revolution in 3D using Processing. Currently, I'm able to draw all 3 axes, revolving around the Y as well as the shape I want to revolve around one of the axes.
I want to clear the axes from the screen each time so that they look like they're rotating in each frame, but I don't want to clear the shape from rotating as that is needed to create the solid of revolution visual. Not sure how to do this - any ideas?
Here's my current draw loop:
void draw() {
background(0);
camera(width/2.0, map(mouseY,0,height,-height/2 - PADDING ,height/2 + PADDING), (height/2.0) / tan(PI*30.0 / 180.0),
width/2.0, height/2.0, 0, 0, 1, 1);
translate(width/2,height/2);
rotateY(inc);
pushMatrix();
rotateY(inc2);
shape(plotFunc());
popMatrix();
plotAxes();
inc2 += 0.1;
inc += 0.010;
} //draw;

LWJGL/OpenGL scaling textures weirdly

I am making a platformer which I started developing with default java functions, but now am switching to OpenGL for everything. I have done everything like I always do with OpenGL, and what I did works fine in my other OpenGL projects. Now my problem is that LWJGL/OpenGL is scaling my textures in a very strange way.
It seems to be related to my screen's aspect ratio. (8:5)
I already had to flip the screen to make it the right way round, but as you can see the text is working fine, it's just the textured rect, and it isn't even straight on the bottom.
Here are the most important snippets from the two classes which actually use OpenGL:
Metamorph.java (main class)
public static void initGL()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), 0, Display.getHeight(), 1, -1);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_MODELVIEW);
glClearColor(0, 0, 0, 1);
glDisable(GL_DEPTH_TEST);
}
public void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glScalef(1.0f, -1.0f, 1.0f);
glTranslatef(0f, -720f, 0f);
//glScalef(1280f/800, 720f/500, 1f);
renderer.render();
Display.update();
Display.sync(60);
}
Renderer.java (rendering stuff)
private void renderMainMenuWithGL()
{
//System.out.println("Main Menu!");
glColor4f(1, 1, 1, 1);
try
{
Texture bg = loadTexture("mockery");
bg.bind();
} catch (Exception e)
{
e.printStackTrace();
}
//drawQuad(0, 0, 1280, 720, 0, 0, 1280, 720);
glPushMatrix();
{
glBegin(GL_QUADS);
glTexCoord2f(0, 0);glVertex2f(0, 0);
glTexCoord2f(0, 1);glVertex2f(0, 720);
glTexCoord2f(1, 1);glVertex2f(1280, 720);
glTexCoord2f(1, 0);glVertex2f(1280, 0);
glEnd();
}
glPopMatrix();
TrueTypeFont f = loadFont(MAINFONT, Font.PLAIN, 50);
TrueTypeFont fb = loadFont(MAINFONT, Font.PLAIN, 48);
int sel = -1;
if(Mouse.getX() > 1000 && Mouse.getX() < 1240 && Mouse.getY() > 282.5F && Mouse.getY()< 737.5F)
sel = Math.round((Mouse.getY() - 337.5F)/75F);
if(sel == 0)
drawStringRight(fb, 1240, 350, "Story", new Color(0xff516b6b));
else
drawStringRight(f, 1240, 350, "Story", new Color(0xff516b6b));
}
private void drawStringRight(TrueTypeFont f, int x, int y, String s, Color c)
{
glPushMatrix();
f.drawString(x-f.getWidth(s), y, s, c);
glPopMatrix();
}
I am also open to advice on file structure/what I did wrong elsewhere, but keep in mind this is heavily WIP
The only possible problem I can see from what you have posted is the scaling and translating you are doing prior to rendering. You should not need to do this with your projection matrix setup the way it is. Other possibilities are that either the dimensions are not really 1280x720 as you think or you have modified one of the matrices further in the code you have not posted. I would try setting both the modelview and projection matrices to the identity matrix and then use glOrtho as you have above immediatly before drawing your quad, and use Display.getWidth and Display.getHeight instead of 1280/720 for the vertex coords.
This works fine for me:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), 0, Display.getHeight(), 1, -1);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2i(0, 0);
glTexCoord2f(1,0);
glVertex2i(Display.getWidth(), 0);
glTexCoord2f(1,1);
glVertex2i(Display.getWidth(), Display.getHeight());
glTexCoord2f(0,1);
glVertex2i(0, Display.getHeight());
glEnd();
If this still doesnt work, make sure that the viewport is also set to the entire display: glViewport(0,0,Display.getWidth(),Display.getHeight()).
Aswell, I notice that you are using glPushMatrix() and glPopMatrix before and after drawing, which does nothing and is not needed. Push and pop are used to save the current projection or modelview matrix and then reload it later, so that you can apply transformations inbetween and undo them when needed.
You're setting the projection matrix such that the coordinates of the corners of the window are (0,0) through (1680, 1050), and those are mapped into an area that covers 1280x800 pixels. Then you're drawing a 1280x720 image into it, so the screen coordinates of the image come out to span only 800x500 pixels. If you use 1280x720 in your glOrtho() call, I believe it will fix the issue. That is, you want the size of the window, not the size of the display in that call.

Drawn Image In OpenGL Comes Out Wrong Size And With Black Boxes In It

Im currently trying to work on my Java project which includes LWJGL.
I have come so far with my "game" that im now drawing an image to the screen.
The problem im getting is that the drawn image gets drawn with black boxes in it and its not the correct size.
here is an image of how it looks visually.
Here is how the actualy red square image should look like:
Here is the code i use for rendering with OpenGL, I cannot figure out what im doing wrong.
public class Renderer {
//Integers used for player cordinates, Taken from player class by using Static variables
int playerX;
int playerY;
SpriteSheetLoader spriteLoader;
Texture player;
public Renderer(){
}
public void initRenderer(){
//Initialize OpenGL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity(); // Resets any previous projection matrices
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
try {
player = TextureLoader.getTexture("PNG",ResourceLoader.getResourceAsStream("res/PaddleTemp.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void update(){
GL11.glClear( GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT );
playerX = Player.playerX; //Gets player x and y from the player class using static variables
playerY = Player.playerY;
player.bind();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); //top left
GL11.glVertex2f(playerX, playerY);
GL11.glTexCoord2f(1,0); //Top right
GL11.glVertex2f(playerX + 50, playerY);
GL11.glTexCoord2f(1, 1); //Bottom right
GL11.glVertex2f(playerX + 50, playerY + 150);
GL11.glTexCoord2f(0, 1); //bottom left
GL11.glVertex2f(playerX, playerY + 150);
GL11.glEnd();
}
Sorry, I'm not absolutely sure what the problem is. I can't seem to reproduce it afterwards, unfortunately. Yet I figured it should be solved by one of the following:
Unsupported Image Size
OpenGL relies heavily on so images, whose resolution's width and height are powers of 2 (when width/height=n*2). If the images files aren't up to that specification, LWJGL might act oddly. Also, don't worry about the image files being squished. That's dependent on the vertex input, not on the texture input.
Unsupported Image Extension
Try saving your image files as non-interlaced PNG files. Slick_Util, or whatever you use for loading the image files, might not fully support the images you're giving to it.
Correction Hints
As a last resort, you could add the following lines of code to your initialization code:
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
Hopefully one of these tips helps you.
Source: LWJGL not rendering textures correctly?
I have a really easy fix for you. Your main problem is that you are using an unsupported image size, but OpenGL is good when it just expands the actual size of the sprite to be correct. It also records the exact float value for the width and height of the image so that you can in fact use images that are any size you want. Just replace your current rendering code with this:
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); //top left
GL11.glVertex2f(playerX, playerY);
GL11.glTexCoord2f(player.getWidth(),0); //Top right
GL11.glVertex2f(playerX + 50, playerY);
GL11.glTexCoord2f(player.getWidth(), player.getHeight()); //Bottom right
GL11.glVertex2f(playerX + 50, playerY + 150);
GL11.glTexCoord2f(0, player.getHeight()); //bottom left
GL11.glVertex2f(playerX, playerY + 150);
GL11.glEnd();
Also, you can disregard the other answer as this one should be your solution.

How to translate the camera in GLES2.0?

I want to create a camera moving above a tiled plane. The camera is supposed to move in the XY-plane only and to look straight down all the time. With an orthogonal projection I expect a pseudo-2D renderer.
My problem is, that I don't know how to translate the camera. After some research it seems to me, that there is nothing like a "camera" in OpenGL and I have to translate the whole world. Changing the eye-position and view center coordinates in the Matrix.setLookAtM-function just leads to distorted results.
Translating the whole MVP-Matrix does not work either.
I'm running out of ideas now; do I have to translate every single vertex every frame directly in the vertex buffer? That does not seem plausible to me.
I derived GLSurfaceView and implemented the following functions to setup and update the scene:
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width / height;
// Setup the projection Matrix for an orthogonal view
Matrix.orthoM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}
public void onDrawFrame(GL10 unused) {
// Draw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
//Setup the camera
float[] camPos = { 0.0f, 0.0f, -3.0f }; //no matter what else I put in here the camera seems to point
float[] lookAt = { 0.0f, 0.0f, 0.0f }; // to the coordinate center and distorts the square
// Set the camera position (View matrix)
Matrix.setLookAtM( vMatrix, 0, camPos[0], camPos[1], camPos[2], lookAt[0], lookAt[1], lookAt[2], 0f, 1f, 0f);
// Calculate the projection and view transformation
Matrix.multiplyMM( mMVPMatrix, 0, projMatrix, 0, vMatrix, 0);
//rotate the viewport
Matrix.setRotateM(mRotationMatrix, 0, getRotationAngle(), 0, 0, -1.0f);
Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);
//I also tried to translate the viewport here
// (and several other places), but I could not find any solution
//draw the plane (actually a simple square right now)
mPlane.draw(mMVPMatrix);
}
Changing the eye-position and view center coordinates in the "LookAt"-function just leads to distorted results.
If you got this from the android tutorial, I think they have a bug in their code. (made a comment about it here)
Try the following fixes:
Use setLookatM to point to where you want the camera to be.
In the shader, change the gl_Position line
from: " gl_Position = vPosition * uMVPMatrix;"
to: " gl_Position = uMVPMatrix * vPosition;"
I'd think the //rotate the viewport section should be removed as well, as this is not rotating the camera properly. You can change the camera's orientation in the setlookat function.

Matrix transformations in OpenGL

I'm building a 2D physics engine in Java using OpenGL (from LWJGL) to display the objects. The problem I am having is that the transformation matrices I apply to the frame seem to be getting applied in a different order to what it says that are.
/**
* Render the current frame
*/
private void render() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getDisplayMode().getWidth(), 0, Display
.getDisplayMode().getHeight(), -1, 1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
GL11.glPushMatrix();
GL11.glTranslatef(Display.getDisplayMode().getWidth() / 2, Display
.getDisplayMode().getHeight() / 2, 0.0f);
renderFrameObjects();
GL11.glPopMatrix();
}
public void renderFrameObjects() {
Vector<ReactiveObject> objects = frame.getObjects();
for (int i = 0; i < objects.size(); i++) {
ReactiveObject currentObject = objects.get(i);
Mesh2D mesh = currentObject.getMesh();
GL11.glRotatef((float)(currentObject.getR() / Math.PI * 180), 0, 0, 1.0f);
GL11.glTranslated(currentObject.getX(), currentObject.getY(), 0);
GL11.glBegin(GL11.GL_POLYGON);
for (int j = 0; j < mesh.size(); j++) {
GL11.glVertex2d(mesh.get(j).x, mesh.get(j).y);
}
GL11.glEnd();
GL11.glTranslated(-currentObject.getX(), -currentObject.getY(), 0);
GL11.glRotatef((float)(-currentObject.getR() / Math.PI * 180), 0, 0, 1.0f);
}
}
In renderFrameObjects() I apply a rotation, a translation, draw the object (mesh coordinates are relative to the object's x, y), reverse the translation, and reverse the rotation. Yet the effect it gives when an object rotates (on collision) is similar to when one would apply a translation then a rotation (ie. rotates around a point at a radius). I can't seem to be able to figure this one out having tried various combinations of transformations.
Any help would be appreciated.
That is beacause they are applied to the local coordinate system of the object, not the object itself.
So the rotate rotates the coordinate system and the translation is applied within that rotated coordinate system.
BTW: Don't undo your matrix changes by applying negative transformations. Roundoff error's will accumulate and it probably is also less efficient then using glPushMatrix and glPopMatrix

Categories

Resources