Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (512)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  View Frustum Culling for LWJGL and GL4Java  (Read 4361 times)
0 Members and 1 Guest are viewing this topic.
Offline Captain-Goatse

Junior Duke




I suck at teh 2D. XBOX IS BIG LOL!111


« Posted 2003-02-04 14:39:23 »

On my hosting, now!

http://suicidesolutions.com/

at codes section. Damn I frame, so I can't link straight, otherwise you'll get my nasty 404 error page. Anyway have fun. They are not noob friendly so if you don't know what you are doing, you'll most likely fail.

I will also update the site with more user friendly LWGL tutorials. No offence CHMAN Grin


Edit: Wrong call, something is still wrong with the LWGL one, but the GL4Java one works for sure.
Offline Chman

Junior Duke




Nothing more that... Java games are cool !


« Reply #1 - Posted 2003-02-15 16:28:24 »

Quote

I will also update the site with more user friendly LWGL tutorials. No offence CHMAN  Grin


LoL Grin
I'm hapy to see that there will be others site on lwjgl. Tell me when your site will contain some tuts, i will add a link to my site ! Smiley

++
Chman
Offline ap_kelly

Junior Duke




Java rocks!


« Reply #2 - Posted 2003-02-23 23:18:00 »

I've been porting some code that does frustum culling, from the Game Tutorials site. I was wondering why this bit of code doesn't work as I expect.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
float[] proj = new float[16];
float[] modl = new float[16];
float[] clip = new float[16];

FloatBuffer projBuf;
FloatBuffer modlBuf;

public CFrustum() {
  projBuf = ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder()).asFloatBuffer();
  projBuf.put(proj);
  modlBuf = ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder()).asFloatBuffer();
  modlBuf.put(modl);
}


void CalculateFrustum(GL gl) {
  gl.getFloatv(GL.PROJECTION_MATRIX,Sys.getDirectBufferAddress(projBuf));
  gl.getFloatv(GL.MODELVIEW_MATRIX,Sys.getDirectBufferAddress(modlBuf));


  clip[ 0] = modl[ 0] * proj[ 0] + modl[ 1] * proj[ 4] + modl[ 2] * proj[ 8] + modl[ 3] * proj[12];
  clip[ 1] = modl[ 0] * proj[ 1] + modl[ 1] * proj[ 5] + modl[ 2] * proj[ 9] + modl[ 3] * proj[13];
  clip[ 2] = modl[ 0] * proj[ 2] + modl[ 1] * proj[ 6] + modl[ 2] * proj[10] + modl[ 3] * proj[14];
  clip[ 3] = modl[ 0] * proj[ 3] + modl[ 1] * proj[ 7] + modl[ 2] * proj[11] + modl[ 3] * proj[15];

...

}


My question is this, currently as the code stands my modl and proj arrays are empty so when I do my calulations and normalise my planes etc I get NAN (not a number) as the results, which is useless later when I do my checking in boxInFrustum(...) .

If I insert the following code into my calculateFrustum(...) method after the gl.getFloatV calls, everything works.

1  
2  
3  
4  
for (int i=0; i<16; i++) {
  proj[i] = projBuf.get(i);
  modl[i] = modlBuf.get(i);
}


I was wondering why my float[] arrays proj and modl aren't populated directly when the FloatBuffer is used in the getFloatv opengl method.

Am I using these buffers incorrectly or is there a bug?

Thanks,

Andy.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2003-02-24 07:13:41 »

Yes, you're using them incorrectly; you have to copy the data out of the buffer into the float arrays. What you want is my new struct keyword which would go some way to wrapping native buffers with Java objects. It needs lots of votes at JDC, but it still hasn't appeared yet...

The RFE is 4820062 - when it finally does turn up.


Cas Smiley

Offline ap_kelly

Junior Duke




Java rocks!


« Reply #4 - Posted 2003-02-24 09:08:47 »

So in my situation what would be the correct way, if I insert the following code everything works

1  
2  
3  
4  
for (int i=0; i<16 i++) {
  proj[i] = projBuf.get(i);
  modl[i] = modlBuf.get(i);
}


How would you do things differently?

Thanks,

Andy.

Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2003-02-24 09:22:57 »

That is the correct way.
The best way is of course to use the bulk array get and put commands:

1  
2  
modlBuf.get(modl);
projBuf.get(proj);

Offline Micke

Senior Newbie




Yada-yada


« Reply #6 - Posted 2003-02-24 16:37:16 »

Quote
Edit: Wrong call, something is still wrong with the LWGL one, but the GL4Java one works for sure.


Could it be the normalization; I get the feeling something should get divided by 'magnitude' in there...?

/M
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #7 - Posted 2003-02-24 17:09:05 »

Yes, after extracting all the planes they need to be normalised, eg. a, b, c, and d need to be divided by the magnitude of the vector a, b, c. I think.

Cas Smiley

Offline ap_kelly

Junior Duke




Java rocks!


« Reply #8 - Posted 2003-02-24 19:58:01 »

I have all the normalisation stuff in there, it was just the nio buffers I was having problems with.

Cas, when I replace my for loop code with the projBuf.get(proj); example you gave, I get BufferUnderflowExceptions, I'll have to investigate the reason why otherwise I'll have to go back to my for loop solution.

Thanks,

Andy.

Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #9 - Posted 2003-02-24 21:07:01 »

Gah! You programmers never read the bloody docs! Before you can read from the start of a buffer you've got to rewind() it first.

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ap_kelly

Junior Duke




Java rocks!


« Reply #10 - Posted 2003-02-24 21:39:25 »

Cas,

You're right! I just came here to let you know I've found the solution, and here you are already. If I add the following code, it all works again.

1  
2  
3  
4  
5  
    projBuf.rewind();
    modlBuf.rewind();

    projBuf.get(proj);
    modlBuf.get(modl);


Thanks,

Andy.

Offline ap_kelly

Junior Duke




Java rocks!


« Reply #11 - Posted 2004-02-17 02:34:06 »

I'm back, and I have a rather strange problem that I'm hoping someone can help me with.

I've recently started a new project and based it on code I've had working under LWJGL 0.6, I'm now using 0.8 (I've also switched from Java 1.4.2 to 1.5b).

The problem is this when I rotate my camera, the edges of my frustum come into view and I can see what was previous clipped.

My camera update code looks like this

1  
2  
3  
GL.glMatrixMode(GL.GL_MODELVIEW);
GL.glLoadIdentity();
GLU.gluLookAt(m_Position.x, m_Position.y, m_Position.z, m_CurrentLookAt.x, m_CurrentLookAt.y, m_CurrentLookAt.z, m_LookUp.x, m_LookUp.y, m_LookUp.z);


and the first part of my calculate frustum code is as follows

1  
2  
3  
4  
5  
6  
7  
8  
GL.glGetFloat( GL.GL_PROJECTION_MATRIX, projBuf);
GL.glGetFloat( GL.GL_MODELVIEW_MATRIX, modlBuf);

projBuf.rewind();
modlBuf.rewind();

projBuf.get(proj);
modlBuf.get(modl);


I call them in my render loop

1  
2  
3  
4  
camera.update(); // does a glLookAt() current view
calculateFrustum(); // as described in this thread.

// render code with fustrum testing goes here.



So what I see is the same view (terrain in this case) frustum culled but then rotated into view.

I'm not explaining this very well, so here is a screenshot, my terrain in this shot has been rendered and I've rotated the view, and instead of getting the rest of my terrain, I get empty space. my current x, y coordinates put me i the center of my heightmap, not in a corner.




Anyone got any ideas what I might be doing wrong, it's probibly something really stupid, I have code that I can show if that'll help.

Thanks,

Andy.

Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #12 - Posted 2004-02-17 08:46:47 »

Try actually rendering your frustum. It should be more or less invisible - just a square border at the edge of the screen. If you can see any more of it your frustum is b0rked.

To render your frustum, simply draw lines between the corners of the frustum to draw a "cube". The edges of the frustum are at 0,0,0 -> screen width, screen height,furthest depth.

Cas Smiley

Offline ap_kelly

Junior Duke




Java rocks!


« Reply #13 - Posted 2004-02-17 10:51:15 »

I've finally worked out the problem, in LWJGL v0.8 I have to change my calculateFrustum() code in the following way

1  
2  
3  
4  
5  
6  
7  
8  
GL.glGetFloat( GL.GL_PROJECTION_MATRIX, projBuf);
GL.glGetFloat( GL.GL_MODELVIEW_MATRIX, modlBuf);

projBuf.rewind();
modlBuf.rewind();

projBuf.get(proj);
modlBuf.get(modl);


becomes

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
projBuf.rewind();
modlBuf.rewind();

GL.glGetFloat( GL.GL_PROJECTION_MATRIX, projBuf);
GL.glGetFloat( GL.GL_MODELVIEW_MATRIX, modlBuf);

projBuf.rewind();
modlBuf.rewind();

projBuf.get(proj);
modlBuf.get(modl);


What I was seeing is the same project and matrix values being returned, and I took a guess that the buffers may be being appended to, so I was always reading out the same first 16 values from each.

It would have been nice to get some sort of ArrayIndexOutOfBounds type exception since I only declared the buffers of being size 64, or to have the glGetFloat do an automatic rewind for me.

Regards,

Andy.

Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #14 - Posted 2004-02-17 11:45:25 »

'sfunny you should say that... buffer checks are going into 0.9.

Cas Smiley

Offline tom
« Reply #15 - Posted 2004-02-17 13:37:50 »

Quote
buffer checks are going into 0.9

Thats great new. I had a bug where I forgot to rewind a buffer befor sending it to a opengl get function. It trashed the memory wich caused random vm crashes. Had no idee what was going on at the time, and was very difficult to debug  Cry

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (35 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!