Author Topic: [Cube] my project! (have changed the topic!)  (Read 16982 times)

0 Members and 1 Guest are viewing this topic.

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: My first Opengl intro
« Reply #20 on: March 18, 2012 »
What gfx card do you have Jace? Maybe it doesn't support vsync, or it's set to "force off". Either way, probably time to look at delta timing :)
raizor

Challenge Trophies Won:

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Re: My first Opengl intro
« Reply #21 on: March 19, 2012 »
Hmm I'm discovering the obscur world of OpenGl...

Yep I think I will try the delta timing trick to force 1 swapbuffer between 2 frames.

Thank you for the help...
Challenge Trophies Won:

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Re: My first Opengl intro
« Reply #22 on: March 20, 2012 »
Could someone test the v2 version for the vsync (still in "3D" mode). I've put a delay() in the loop just before the swapbuffer(). Thank you!
Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: My first Opengl intro
« Reply #23 on: March 20, 2012 »
Could someone test the v2 version for the vsync (still in "3D" mode). I've put a delay() in the loop just before the swapbuffer(). Thank you!

Tested on Nvidia GTX580, works with Vsync set to "App controlled", "Force On" and "Force Off".  The scroller seems a bit jerky though. Seems to skip a bit every second or so.
raizor

Challenge Trophies Won:

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Re: My first Opengl intro
« Reply #24 on: March 20, 2012 »
Tested on Nvidia GTX580, works with Vsync set to "App controlled", "Force On" and "Force Off".  The scroller seems a bit jerky though. Seems to skip a bit every second or so.
ok Thank you raizor...  hmmmmm jerky even if "force on" is set?
I have set off mine too (forgot it was possible  :-[) and have change the delay method. Got now a jerk because  of frame interlacing.... but it is running good... (see first_v3.zip)
« Last Edit: March 20, 2012 by jace_stknights »
Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: My first Opengl intro
« Reply #25 on: March 20, 2012 »
v3 seems the same as v2 to me matey.
raizor

Challenge Trophies Won:

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Re: My first Opengl intro
« Reply #26 on: March 21, 2012 »
hum... I'm very disapointed...

OK you know what?? I will let down purebasic for opengl remake!!! :telloff:

EDIT: found a pearl here : http://www.purebasic.fr/english/viewtopic.php?t=28835 it seems it makes all for you and vsync is running!!!
« Last Edit: March 21, 2012 by jace_stknights »
Challenge Trophies Won:

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Re: My first Opengl intro
« Reply #27 on: April 23, 2012 »
Okay here I am again...

I've took a look at the SWOF example quoted in the last post. Hmmm I understand now the "way" the objects are taking place in the scene! (yep sorry, I was still in a global xyz wokring, like in the good old time)

In fact when you want to put something somewhere, you have to rotate, move the scene to the wanted position, put the object like it was a new 0,0,0 xyz axis and then move to the new position for the next object and so on.
So the simplest way is to translate and rotate back to the old position (with a negative action) and then make the new rotation and translation for the next object for example...

I really thought it was a bit different-> each object got its own translation and rotation, and the whole scene got it own also...

Challenge Trophies Won:

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Re: [Cube] my project! (have changed the topic!)
« Reply #28 on: April 26, 2012 »
Okay, I've changed the topic, coz I'm trying to make an opengl thing for the Cube Challenge!
But got some problems: If I'm trying to display hundred of cubes, it is really sloooowwwwww... I've tried to create a glNewList with #COMPILED option and display it, no way... still so slooow...

Someone got an idea?
Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: [Cube] my project! (have changed the topic!)
« Reply #29 on: April 26, 2012 »
The problem is that you're sending the cube data to the GPU each frame, which gets rather slow when you have a lot of cubes (or other objects). I'd suggest looking into vertex buffer objects (VBOs). These allow you to create a buffer on the GPU to store your cube data, and then you just draw the cubes each frame using the data that's in the buffer on the GPU.

So, instead of saying to the GPU:
"Draw a cube at position X" 500 times

You first upload the cube data:
"GPU, here's the data for all my cubes, store it in a VBO buffer in GPU memory for me."

And each frame:
"GPU, draw that data I sent you before."

You're just uploading the data once, and then drawing the data each frame from the buffer on the GPU rather than sending it across again and again to the GPU.

It's gets a little complicated if you want to start animating the cubes as the data in the buffer needs to be updated if you're going to move things around individually. I'm doing something similar in my intro.  My solution was to store transformation data for the cubes in a texture coordinate VBO. For each cube vertex, I store a start and end position (and rotation) for the vertex and then draw everything using a custom shader. The shader looks up the associated texture coordinates (which I'm using to store position info) for each vertex and uses a time value fed into the shader to transition the vertex location between the start and end points stored in the texture coordinates. This allows me to animate lots of cubes without resending loads of data to the GPU each frame. From memory, I think I've got about 65,000 blocks or so flying about.

As well as drawing all the vertices in the vbo, you can just draw a section of the data (first 24 vertices, vertices 24 to 48 etc). This is what I was doing previously. I uploaded all my cube data into a VBO without transformations, and then drew the data in chunks of 24 vertices (1 cubes worth). In each iteration of the loop, I used glTranslate and glRotate to handle the position and rotation for the cube. This worked reasonably well, but is much, much slower than the method mentioned above. I think I was maxing out at around 5,000 cubes. It worked like this (excuse bad pseudo code):

for i = 0 to numCubes do

  // translate/rotate according to cube position/angle
  glTranslate (cube(i).translation)
  glRotate(cube(i).rotation)

  // draw 24 cube vertices at origin 0,0,0
  DrawCube()

  // undo translate/rotate ready for next cube draw
  glRotate(-cube(i).rotation)
  glTranslate (-cube(i).translation)

next i

That's probably not helped massively, but hopefully you get the gist a little. I had quite a job getting my VBO stuff working and understanding the concepts, so bear with it.

If you like, I'll package up my work-in-progress entry and send it to you. The code is quite messy, but may be of some use...

EDIT: Goddamn BBCode :P
« Last Edit: April 26, 2012 by Raizor »
raizor

Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
raizor

Challenge Trophies Won:

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Re: [Cube] my project! (have changed the topic!)
« Reply #31 on: April 27, 2012 »
Raizoooooor MY HERO!!!!!!!  :cheers: :cheers: :cheers: :cheers: :cheers: :cheers: :cheers: :cheers:

Ok I'm taking a STRONG look at all this!!!

EDIT: Hmmm gonna have a headache...
« Last Edit: April 27, 2012 by jace_stknights »
Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: [Cube] my project! (have changed the topic!)
« Reply #32 on: April 27, 2012 »
Try getting a VBO to just draw a quad or a single triangle first. Once that's working you can beef it up.

My code looks something like this:

// vboVerts is a GLuint, this asks OpenGL to create me a buffer (VBO) and save the ID number in vboVerts
glGenBuffersARB(1, &vboVerts);

// ** upload vertex data to vbo
// bind to our VBO
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboVerts);
// upload the vertex data from our verts4 pointer location. A vertex 4 is a vertex with X,Y,Z,W coords, we have 24 vertices per block as this is a cube.
// you could use just a 3 point vertex (X,Y,Z), I use 4 so I can store the vertex index in W for doing lookups in the shader later.
// vertex4 is just a custom struct type with x,y,z,w float values.
glBufferDataARB(GL_ARRAY_BUFFER_ARB, numblocks*24*sizeof(vertex4), &verts4[0], GL_STATIC_DRAW_ARB);
// unbind the VBO
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);

// and to draw the stuff
// tell OpenGL we want to enable vertex arrays to draw vertices
glEnableClientState(GL_VERTEX_ARRAY);   
// bind our VBO containing our vertices
glBindBufferARB( GL_ARRAY_BUFFER, vboVerts );
// Set pointer to vertex data fro drawing. The 4 is there because we have 4 points per vertex. The first 0 is stride (how many entries to skip after each vertex,
// sometimes we store vertex, color and normal data together in a chunk. If we do that we need to use stride to get GL to skip over the color and normal data for
// each entry in order to get to the next vertex. Since vboVerts contains only vertices, our stride is 0). The second 0 is used to point to a pointer containing the
// vertex data. Since we're using a VBO and we just bound it, we can provide 0 as the pointer and GL will use the VBO we just bound.
glVertexPointer(4, GL_FLOAT, 0, 0);

That's the basics. If you want to use VBOs for color and normal data, you create the VBOs in the same was as above and use the glEnableClientState with GL_NORMAL_ARRAY and GL_COLOR_ARRAY arguments. Bind them in the same way and use:

// set pointer to normal data. Normal data is always 3 points (x,y,z), so no size specified.
glNormalPointer(GL_FLOAT, 0, 0);
// same with color. here we're using 4 parts for color (r,g,b,a).
glColorPointer(4, GL_FLOAT, 0, 0);

When you've done your drawing, remember to disable any client states you enabled above:

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);


I'll leave the texcoord stuff until later as it will get confusing otherwise...




raizor

Challenge Trophies Won:

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Re: [Cube] my project! (have changed the topic!)
« Reply #33 on: April 27, 2012 »
thanx :D  will try this week-end if got some spare time -> the monsters have come back home!!!
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [Cube] my project! (have changed the topic!)
« Reply #34 on: May 01, 2012 »
Quote
In fact when you want to put something somewhere, you have to rotate, move the scene to the wanted position, put the object like it was a new 0,0,0 xyz axis and then move to the new position for the next object and so on.
So the simplest way is to translate and rotate back to the old position (with a negative action) and then make the new rotation and translation for the next object for example...
You don't need to do that.

Use the projection matrix to set up the view/camera matrix, then for each object
glPushMatrix()
glTranslate()
glRotate()
glPopMatrix()
no need to back-out with 'negative actions', glPush/PopMatrix are a stack of remembered matrices.

Jim
Challenge Trophies Won:

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Re: [Cube] my project! (have changed the topic!)
« Reply #35 on: May 02, 2012 »
thank you, i've discovered this when making my code... :P
Challenge Trophies Won: