I'm trying to implement a basic physics engine in Java and I'm using the JOGL bindings so I can visualize the results. I can create and rotate shapes easily enough, but have run into problems whilst manipulating the viewport and whilst moving the shapes.
I don't think a clipping issue - I've tried using the gluPerspective method with a massive range (0.0001f - 10000f) with no success. When I move the camera further away from my objects or move the objects themselves, they disappear.
Tutorials about JOGL are few and far between and many also use different versions of OpenGL, so I turn to the only friend I have left: the wonderful users of stack overflow. :)
Flattery aside, the code follows:
public class JoglEventListener implements GLEventListener, KeyListener, MouseListener, MouseMotionListener {
// keep pointer to associated canvas so we can refresh the screen (equivalent to glutPostRedisplay())
public GLCanvas canvas;
public Particle triforce;
public float x;
// constructor
public JoglEventListener(GLCanvas canvas) {
this.canvas = canvas;
}
#Override
public void display(GLAutoDrawable drawable) {
update();
render(drawable);
}
#Override
public void init(GLAutoDrawable drawable) {
triforce = new Particle();
x = 0;
}
private void update() {
triforce.integrate(0.0001);
x = x + 0.25f;
}
private void render(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
GLU glu = new GLU();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
//gl.glFrustum (.5f, -.5f, -.5f * 1080, .5f * 960, 1.f, 500.f);
glu.gluPerspective(0, 1, 0.1f, 100f);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glHint(GL2.GL_CLIP_VOLUME_CLIPPING_HINT_EXT,GL2.GL_FASTEST);
glu.gluLookAt(0, 0, 1.5, 0, 0, -10, 0, 1, 0);
//gl.glRotatef(90, 0f , 1f , 0f );
//Draw some scale lines
gl.glBegin(GL.GL_LINES);
gl.glColor3f(0.75f, 0.75f, 0.75f);
for (int i = 0; i < 20; i += 1)
{
gl.glVertex3f(-5.0f, 0.0f, i + 0.5f);
gl.glVertex3f(5.0f, 0.0f, i + 0.5f);
}
gl.glEnd();
//gl.glRotatef(x, 1f , 1f , 1f );
gl.glPushMatrix();
gl.glTranslated(triforce.position.x, triforce.position.y, triforce.position.z);
gl.glBegin(GL.GL_TRIANGLE_STRIP);
gl.glColor3f(1f, 0f, 0f);
gl.glVertex3d(0, 0, -2);
gl.glColor3f(0f, 1f, 0f);
gl.glVertex3d(0, 0.25d, -2);
gl.glColor3f(0f, 0f, 1f);
gl.glVertex3d(0.25d, 0, -2);
gl.glColor3f(1f, 1f, 0f);
gl.glVertex3d(0.25d, 0.25d, -2.25d);
gl.glEnd();
gl.glPopMatrix();
gl.glFlush();
}
// (empty overridden methods omitted)
public Particle () {
setMass(200d);
velocity = new Vector3(0d, 30d, 40d);
acceleration = new Vector3(0d, -20d, 0d);
position = new Vector3(0d, 0d, 0d);
damping = 0.99d;
}
public void integrate (double duration) {
if (inverseMass <= 0.0d) {
return;
}
assert (duration > 0.0);
position.addScaledVector(velocity, duration);
Vector3 resultingAcc = new Vector3(acceleration.x, acceleration.y, acceleration.z);
velocity.addScaledVector(resultingAcc, duration);
velocity.multEquals(Math.pow(damping, duration));
//clearAccumulator();
}
public void setMass(double mass)
{
assert(mass != 0);
inverseMass = (1.0d)/mass;
}
Before movement / starting position:
The shape drifts upward and is obscured from the right and top, becoming invisible:
Any help would be greatly appreciated! Thanks!
The massive view range can be a problem. The coordinates of the objects are only so precise, and with a huge view range, things that should be near each other are determined to be at the same point. This can cause an object that should be in front of another to disappear behind it. Try using a smaller view range.
I had the same problem. Objects disappearing, while some stay in the scene. After removing:
gl.glEnable(GL2.GL_CULL_FACE);
everything was working just fine ! Of course this is JOGL code, in C, the command would be without all those objects. Just to make this answer clear for everyone.
In the render function, change the value of last parameter of gluPerspective from 100f to 1000f. It will solve your problem.
gl.gluPerspective(0, 1, 0.1f, 100f);
to
gl.gluPerspective(0, 1, 0.1f, 500f);
And I think in your code you have done a mistake in the above line writing glu.gluperspective
I think it is gl.gluPerspective.
In the end, I was never able to track down the issue, and started again from scratch. I didn't run into any further clipping issues on my new build.
My best guess as to my initial failure is an improperly used glHint or glClear call, or perhaps some problem with the version of JOGL I was referencing.
Related
The following demo works well on its own program
https://github.com/LWJGL/lwjgl3/blob/master/modules/samples/src/test/java/org/lwjgl/demo/stb/EasyFont.java
but I just can't get it draw in my game.
I copied from line 53 to line 63 (make it a draw() method) to my game loop , and put other things( like those in front of the while loop in original code) in a init() method,
public void init(){
ByteBuffer charBuffer = BufferUtils.createByteBuffer(text.length() * 270);
int quads = stb_easy_font_print(0, 0, getText(), null, charBuffer);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 16, charBuffer);
glClearColor(43f / 255f, 43f / 255f, 43f / 255f, 0f); // BG color
glColor3f(169f / 255f, 183f / 255f, 198f / 255f); // Text color
}
public void draw(){
float scaleFactor = 1.0f + getScale() * 0.25f;
glPushMatrix();
// Zoom
glScalef(scaleFactor, scaleFactor, 1f);
// Scroll
glTranslatef(4.0f, 4.0f - getLineOffset() * getFontHeight(), 0f);
glDrawArrays(GL_QUADS, 0, quads * 4);
glPopMatrix();
}
but I just can't see any text in my game, is it because I am using a shader (I've tried unbind it but didn't work) or a camera (which relates to a "projection" Matrix4f uniform in shader) or a VBO?
or I need to to do some glEnable or glDisable stuff?
my game loop looks like this
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
shader.bind();
mainCamera.getPosition().lerp(new Vector3f(0.64f*main.getPos().x,0.86f*main.getPos().y,0).mul(-1,new Vector3f()), 0.07f);
onEntitiesDraw();
input.update();
EasyFont.draw(); //much the same as in the while loop of original code
glfwSwapBuffers(window);
glfwPollEvents();
Solved. The texts are rendered but in dark color, so I can barely see them.To make it be the color we assigned in that glColor3f(),
we just need to do
the glDisable(GL_TEXTURE_2D).
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.
There is an awful flickering that happens occasionally on my screen from top to bottom. It only happens when the camera moves around (which follows my player character). It does not matter where I am on the map for this to happen. If I stop moving at the right moment, the lines will remain visible until I start moving again. I've added a screenshot so you can see what it looks like:
If you look closely, you might notice that the lines represent a part of my "mountain wall" tile (That tile is used a little bit lower in the map):
I've tried putting the cam.update() in the beginning of the update() method, as some other people on StackOverflow have suggested, but that doesn't seem to work. I have also made sure that there is absolutely no space in between tiles in the tileset image. Most questions I see about this issue experience a different kind of flickering than I am.
Here is the code of my screen and all the rendering that happens:
public PlayScreen(MyGame game){
cam = new OrthographicCamera();
batch = new SpriteBatch();
gameMap = new TestMap(startMap);
player = new Player(this);
Gdx.input.setInputProcessor(this);
}
public void update(float delta){
player.update(delta);
Rectangle playerRect = player.getNextRectangle(delta);
Rectangle teleportRect = gameMap.teleportTo(playerRect);
if(!gameMap.isColliding(playerRect)){
player.setPosition(playerRect);
}
if(teleportRect != null){
player.setPosition(teleportRect);
}
cam.viewportHeight = Gdx.graphics.getHeight() / zoomLevel;
cam.viewportWidth = Gdx.graphics.getWidth() / zoomLevel;
cam.position.x = player.getX() + (player.getWidth() / 2);
cam.position.y = player.getY() + (player.getHeight() / 2);
cam.update();
}
#Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameMap.render(cam);
Gdx.graphics.setTitle("FPS: "+Gdx.graphics.getFramesPerSecond());
gameMap.getMapRenderer().getBatch().begin();
player.draw(gameMap.getMapRenderer().getBatch());
gameMap.getMapRenderer().getBatch().end();
}
I want to use Alpha/Blend mode for future stuff (transactions mainly and possible image blending).
Well, I can't get it to work using LWJGL (GL1.1), I already tried other blend modes but didn't worked, nor changing the background or anything like that...
Screenshots:
http://i.imgur.com/cHU4YGS.png - GL_BLEND always enabled, everything is transparent
http://i.imgur.com/sPmPqne.png - GL_BLEND enabled on QUAD and text, I can see the line that is on disabled GL_BLEND
i imgur com/nkda41v png - GL_BLEND disabled on everything but the text -> I need some reputation to post more than 2 links, sorry about that but I belive this image is important so i'll post it anyway. Just fill with dots
The results are the same with or without alpha argument on all these tests
Code:
` private void init() {
try {
Display.setDisplayMode(new DisplayMode(DEFAULT_WIDTH, DEFAULT_HEIGHT));
Display.setResizable(true);
Display.setVSyncEnabled(true);
Display.setTitle(DEFAULT_TITLE + " v" + VERSION);
Display.create();
updateMatrix();
} catch(LWJGLException e) {
e.printStackTrace();
}
Keyboard.enableRepeatEvents(true);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Font consolas = new Font("consolas", Font.PLAIN, 13);
font = new TrueTypeFont(consolas, antiAliasedFont);
}
private void updateMatrix() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, DEFAULT_WIDTH, DEFAULT_HEIGHT, 0, 1, -1);
//glScaled((double) DEFAULT_WIDTH / (double) Display.getWidth(), (double) DEFAULT_HEIGHT / (double) Display.getHeight(), 0);
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glMatrixMode(GL_MODELVIEW);
}
#Override
public void run() {
init();
Main main = Main.getMain();
while(!Display.isCloseRequested()) {
currentGraphicsTick++;
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0f, 0f, 0f, 1f);
if(Display.wasResized()) updateMatrix();
if(vsyncMode == 1) Display.setVSyncEnabled(true);
else if(vsyncMode == 2) Display.setVSyncEnabled(false);
if(Display.isActive()) {
glPushMatrix();
try { // Draw
float alpha = (float) Math.cos(Math.toRadians(currentGraphicsTick % 90));
System.out.println("Alpha: " + alpha);
glBegin(GL_LINE_STRIP);
{
float sin = (float) Math.abs(Math.sin(Math.toRadians(currentGraphicsTick % 360)));
new Color(0.7f, 0.7f, 0.7f, alpha).bind();
glVertex2f(DEFAULT_WIDTH * 0.03f, DEFAULT_HEIGHT * 0.05f);
glVertex2f(DEFAULT_WIDTH * 0.93f * sin, DEFAULT_HEIGHT * 0.95f * sin);
}
glEnd();
glBegin(GL_QUAD_STRIP);
{
new Color(0.5f, 0.5f, 0.5f, alpha).bind();
glVertex2i(0, 0);
glVertex2i(0, DEFAULT_HEIGHT);
glVertex2i(DEFAULT_WIDTH, 0);
glVertex2i(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
glEnd();
String[] split = main.getGameLoopThread().getDebugString().split("\n");
for(int i = 0; i < split.length; i++) {
font.drawString(1, 1 + (i * font.getLineHeight()), split[i], Color.white);
}
} catch(Throwable throwable) {
throwable.printStackTrace();
}
glPopMatrix();
}
Display.update();
Display.sync(TARGET_FPS);
}
}
Display.destroy();
closeRequested = true;
}
I already tried:
Removing the 'alpha' argument from the Slick's Color constructor
Using OpenGL's glColor with and without alpha argument
Disabling/Enabling GL_BLEND in part of the code (I know some things wouldn't work, but you never know, right?)
Used constants to the alpha variable (such as 0.3f, 0.5f, 0.7f, 1f) instead of making it variable through Math.sin/cos using the tick as the degree
Using glRect(...)
Changing the background
Removing the glClearColor
Removing glClear (nice effect, never did this lol)
What I expect to see was a fading moving LINE_STRIP:
On one side it moves from the (0, 0) to (width - 7%, height - 5%)
On the other it stand still on (width + 3%, height + 5%)
the rectangle would make it fade (the original idea would use the same color as the background, but it didn't on my tests because I want to see the rectangle)
I've had a similar(in terms of what I had to tackle) when doing a 2D game using my own engine. LWJGL's blend functions always have a few issues and there aren't really that many concrete answers for them as it really boils down to your code and how you placed things.
I presume you're using SlickUtil, and to that I would say write your own (or of course search around) your own methods for this. Util's methods were always somewhat wonky with blending.
Removing alpha shouldn't have changed much in terms of getting
what you want
The only time I explicitly enable GL_BLEND is
when rendering lights(always presuming you're doing 3D) and when I would render textures
I suggest not removing glClearColor, that doesn't directly affect your situation as
long as you put it in the correct place
Like I said this bug/problem could be due to quite a few things that I can't quite pin-point from what you've posted.
My suggestions are:
Organise the whole project well to be able to point bugs out quickly
Make alot of stuff generic ASAP(Doesn't matter if you just want to quickly write something)
Avoid SlickUtil, it's great and I LOVE it but it does get it's issues
Sorry I can't help too much but tracking down your issue from the jumbled up code you posted is a bit difficult. I'll keep my eye on this question in case I may be of help in the future.
Besides the tips from #Juxhin, what fixed my problem with alpha blend was TextureImpl.bindNone(); from SlickUtils (check openGL's similar below)
This method is similar to glDisable(GL_TEXTURE_2D) before rendering the thing that needs to be blent (I know that searching and checking Slick's source)
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.