Hello Guest

How to do this in LWJGL?

  • 12 Replies
  • 13682 Views
How to do this in LWJGL?
« on: March 19, 2007, 07:55:18 »
So Iam just doing simple game with lwjgl. Its about the ball. Problem is that my ball si rotating very strange. You can see that on this avi short videohttp://www.kpk.unas.cz/clip.avi
I asked advice on Internet and some guy tell me that I need to do this (code is in c++).
Code: [Select]
float matrix[16];
This will have to init somewhere in program(i putted in ball class constructor)
Code: [Select]
glPushMatrix();
glLoadIdentity();
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
glPopMatrix();
This code will be in method which care about keyboard.For example this will be when player push right arrow:
Code: [Select]
glPushMatrix();
glLoadMatrixf(matrix);
glRotatef(angle, 0, 1, 0); //
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
glPopMatrix();
This will be in ball render method
Code: [Select]
glPushMatrix();
glMultMatrixf(matrix);
renderBall();
glPopMatrix();

The problem is that i dont know how  to that in LWJGL because of MultiMatrif(matrix) method, and glGetFloatv(GL_MODELVIEW_MATRIX, matrix); because they are undefinned in lwjgl for these parameters. Please help me

*

Offline Matzon

  • *****
  • 2242
Re: How to do this in LWJGL?
« Reply #1 on: March 19, 2007, 08:15:31 »
you just need to add it in a buffer - look at org.lwjgl.examples.Game

Re: How to do this in LWJGL?
« Reply #2 on: March 19, 2007, 08:27:02 »
you just need to add it in a buffer - look at org.lwjgl.examples.Game

I'am sorry but Iam  not getting a point. What is org.lwjgl.examples.Game ?


Re: How to do this in LWJGL?
« Reply #4 on: March 19, 2007, 11:29:10 »
Its in the source code:
http://svn.sourceforge.net/viewvc/java-game-lib/trunk/LWJGL/src/java/org/lwjgl/examples/Game.java?revision=2286&view=markup

it has a rotating cube...
I can't rotate it this way!!!. Look at that video. You will see what happen if i rotate with only glrotateef. As I sad I need to do that another way. I found out this way but its in C++ but in lwjgl is different. Thats what I need to know.

Re: How to do this in LWJGL?
« Reply #5 on: March 19, 2007, 19:49:29 »
The code you pasted up at the top (in C++) uses glRotatef(). It just stores it in a matrix for you to use later. It is the same as using a glRotatef(). I'm not sure what the person was trying to show you. ???

As for your problem; Make sure that you are doing your translations/rotations in the correct order (like doing translation before you do the rotation). The order of the rotations around the 3 axis is important as well (like x, y, then z or y, x, then z will produce different results). If it helps this is what I do and it produces what I would expect to happen when doing a translation and a rotation:
Code: [Select]
            glTranslatef(location.x, location.y, location.z);
            glRotatef(angle.z, 0.0f, 0.0f, 1.0f);
            glRotatef(angle.y, 0.0f, 1.0f, 0.0f);
            glRotatef(angle.x, 1.0f, 0.0f, 0.0f);

Hope that helps ;D
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Re: How to do this in LWJGL?
« Reply #6 on: March 19, 2007, 21:14:58 »
The code you pasted up at the top (in C++) uses glRotatef(). It just stores it in a matrix for you to use later. It is the same as using a glRotatef(). I'm not sure what the person was trying to show you. ???

As for your problem; Make sure that you are doing your translations/rotations in the correct order (like doing translation before you do the rotation). The order of the rotations around the 3 axis is important as well (like x, y, then z or y, x, then z will produce different results). If it helps this is what I do and it produces what I would expect to happen when doing a translation and a rotation:
Code: [Select]
            glTranslatef(location.x, location.y, location.z);
            glRotatef(angle.z, 0.0f, 0.0f, 1.0f);
            glRotatef(angle.y, 0.0f, 1.0f, 0.0f);
            glRotatef(angle.x, 1.0f, 0.0f, 0.0f);

Hope that helps ;D

No it dont :). But please take care about my first problem. How to make that code in lwjgl?Please, dont care about what is purpouse of that code just port it to lwjgl :) thanks :)

Re: How to do this in LWJGL?
« Reply #7 on: March 20, 2007, 14:42:53 »
Its a lot easier to use the glRotate() and glTranslate(), but here it is:
1st part:
Code: [Select]
FloatBuffer matrix = BufferUtils.createFloatBuffer(16);2nd part:
Code: [Select]
GL11.glPushMatrix();
    GL11.glLoadIdentity();
    GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, matrix);
    matrix.flip();
GL11.glPopMatrix();
3rd part:
Code: [Select]
GL11.glPushMatrix();
    GL11.glLoadMatrix(matrix);
    matrix.rewind(); // not sure if you have to do this or not
    GL11.glRotatef(angle, 0, 1, 0);
    GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, matrix);
    matrix.flip();
GL11.glPopMatrix();
4th part:
Code: [Select]
GL11.glPushMatrix();
    GL11.glMultMatrix(matrix);
    matrix.rewind(); // not sure if you have to do this or not
    renderBall();
GL11.glPopMatrix();

Hope that helps ;D
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Re: How to do this in LWJGL?
« Reply #8 on: March 20, 2007, 15:09:56 »
Quote
Hope that helps ;D

no It didn't ;D. I got exception:
Exception in thread "main" java.lang.IllegalArgumentException: Number of remaining buffer elements is 0, must be at least 16
   at org.lwjgl.BufferChecks.throwBufferSizeException(BufferChecks.java:184)
   at org.lwjgl.BufferChecks.checkBufferSize(BufferChecks.java:199)
   at org.lwjgl.BufferChecks.checkBuffer(BufferChecks.java:224)
   at org.lwjgl.opengl.GL11.glMultMatrix(GL11.java:2153)

Re: How to do this in LWJGL?
« Reply #9 on: March 20, 2007, 15:15:43 »
lol, whoops :-[, try putting the matrix.rewind() right before the GL11.glMultMatrix() call. That's where it was supposed to be.

Maybe that will help ;D

EDIT: Upon further reflection, I think the rewind() should be a flip() to be more correct.
« Last Edit: March 20, 2007, 15:17:40 by Fool Running »
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Re: How to do this in LWJGL?
« Reply #10 on: March 20, 2007, 15:40:27 »
I'm sorry but nothing changed. Code:

Part1
Code: [Select]
FloatBuffer matrix = BufferUtils.createFloatBuffer(16);
Part2(Could this be In a constructor of player?)
Code: [Select]

GL11.glPushMatrix();
GL11.glLoadIdentity();
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, matrix);
matrix.flip();
GL11.glPopMatrix();

Part3
Code: [Select]
GL11.glPushMatrix();
matrix.flip();
GL11.glLoadMatrix(matrix);
GL11.glRotatef(UHOL, 0, 1, 0);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, matrix);
matrix.flip();
GL11.glPopMatrix();

Part4
Code: [Select]
GL11.glPushMatrix();
matrix.flip();
GL11.glMultMatrix(matrix);
player.render();
GL11.glPopMatrix()




Re: How to do this in LWJGL?
« Reply #11 on: March 20, 2007, 17:12:48 »
Quote
I'm sorry but nothing changed.
You're still getting the same exception? In the same place?

I think part1 and part2 are designed to be called once, so putting part2 in the constructor should be fine. Just make sure you have already called Display.Create() before you call that constructor, then.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Re: How to do this in LWJGL?
« Reply #12 on: March 20, 2007, 17:21:14 »
Iam sure that display is created before calling these methods.