LWJGL Forum

Programming => OpenGL => Topic started by: Trudko on March 19, 2007, 07:55:18

Title: How to do this in LWJGL?
Post by: Trudko 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 (http://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++).

float matrix[16];

This will have to init somewhere in program(i putted in ball class constructor)

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:

glPushMatrix();
glLoadMatrixf(matrix);
glRotatef(angle, 0, 1, 0); //
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
glPopMatrix();

This will be in ball render method

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
Title: Re: How to do this in LWJGL?
Post by: Matzon on March 19, 2007, 08:15:31
you just need to add it in a buffer - look at org.lwjgl.examples.Game
Title: Re: How to do this in LWJGL?
Post by: Trudko on March 19, 2007, 08:27:02
Quote from: Matzon on March 19, 2007, 08:15:31
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 ?
Title: Re: How to do this in LWJGL?
Post by: Matzon on March 19, 2007, 11:15:59
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...
Title: Re: How to do this in LWJGL?
Post by: Trudko on March 19, 2007, 11:29:10
Quote from: Matzon on March 19, 2007, 11:15:59
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.
Title: Re: How to do this in LWJGL?
Post by: Fool Running 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:

            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
Title: Re: How to do this in LWJGL?
Post by: Trudko on March 19, 2007, 21:14:58
Quote from: Fool Running 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:

            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 :)
Title: Re: How to do this in LWJGL?
Post by: Fool Running on March 20, 2007, 14:42:53
Its a lot easier to use the glRotate() and glTranslate(), but here it is:
1st part:
FloatBuffer matrix = BufferUtils.createFloatBuffer(16);
2nd part:

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

3rd part:

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:

GL11.glPushMatrix();
    GL11.glMultMatrix(matrix);
    matrix.rewind(); // not sure if you have to do this or not
    renderBall();
GL11.glPopMatrix();


Hope that helps ;D
Title: Re: How to do this in LWJGL?
Post by: Trudko 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)
Title: Re: How to do this in LWJGL?
Post by: Fool Running 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.
Title: Re: How to do this in LWJGL?
Post by: Trudko on March 20, 2007, 15:40:27
I'm sorry but nothing changed. Code:

Part1
FloatBuffer matrix = BufferUtils.createFloatBuffer(16);

Part2(Could this be In a constructor of player?)

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


Part3

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
GL11.glPushMatrix();
matrix.flip();
GL11.glMultMatrix(matrix);
player.render();
GL11.glPopMatrix()




Title: Re: How to do this in LWJGL?
Post by: Fool Running on March 20, 2007, 17:12:48
QuoteI'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.
Title: Re: How to do this in LWJGL?
Post by: Trudko on March 20, 2007, 17:21:14
Iam sure that display is created before calling these methods.