Author Topic: Some questions on OpenGL  (Read 14178 times)

0 Members and 1 Guest are viewing this topic.

Offline NaniBot

  • ZX 81
  • *
  • Posts: 13
  • Karma: 3
    • View Profile
Some questions on OpenGL
« on: July 18, 2013 »
Ok...so i watched Razor1911's "Insert No Coins" and i was really

surprised to see the "rzr" chips. There were like 50 of them(The

objects that im referring to are rectangles with rzr written on

them). What i want to know is that how rez managed to "make"

them...I mean he is not that stupid that he will draw each of them

and change their vertices etc. He might have used something

which randomises the vertices of the chips in a specified range

(x,y,z axis). Also all the chips move up and down together...how

did rez did this?(I wanna know how he moves all of the chips as if

they are a single object). I would love to know how to do these

things.
« Last Edit: July 18, 2013 by NaniBot »

Offline Canopy

  • Atari ST
  • ***
  • Posts: 208
  • Karma: 20
    • View Profile
Re: Some questions on OpenGL
« Reply #1 on: July 18, 2013 »
something along the lines of object instancing


this might not be exact, but based on what i've done myself so far.

 - you store the geometry for the object within a VAO/VBO (a vertex array object bound to a vertex buffer object)
   this puts the object geometry in gpu memory (no resending the object for each draw call!)
   (this depends on the drivers/memory architecture)

 - when rendering the object over and over you use the transformations (in this case translate/rotate) to position the object
 
 - and draw the object + translation data over and over and over...


in some later versions of GL there are specific extensions or new functions to help with instancing.


I've done this with a small collection of objects, its on my todo list to scale this up to see how it copes with 50, 100 or 1000 instances.

(there's also another technique which uses 'imposters' for instancing, but thats not really used for this kind of effect, those 1000s of cubes flying in some big name demo's use that, they render to a texture and repeat that drawn over a quad)
« Last Edit: July 18, 2013 by Canopy »

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Some questions on OpenGL
« Reply #2 on: July 18, 2013 »
just watched that demo pretty cool!

everything you said there is perfectly valid and spot on canopy with newer gl, some of those cube examples floating around i cannot wait too have a go at myself.

in this instance and using basic gl for clarity, personaly i think, *and i might be wrong here*...

he does a large tilemaped grid and sin waves them.. taking it too its simplest form something like this perhaps.

Code: [Select]
void draw_chip(float x, float y, float z){
       glMatrixMode(GL_MODELVIEW)
       glPushMatrix();
                 glTranslatef( x, y, z );
                 ////////////////////////draw a single chip
       glPopMatrix();
}

int zsin=0;
void draw_chip_grid(void){
       int Grid_SizeY = 10;
       int Grid_SizeX = 10;
       for (int y = 0; y<Grid_SizeY; Y+=1 ){
            for (int x = 0; x<Grid_SizeX; X+= 1 ){
                  zsin += 0.2;
                  draw_chip(x+tilesize_x,y+tilesize_y,2.0f*sin(zsin/180.0*3.14159));
            }
       }
}

this of course is again pesudo and would need some adjusting too make right but at least shows the rough idea behind tile based mapping and animation. that function would draw 100 chips  grid_sizex=10*grid_sizey=10 = 100 but you could take it too 200 chips just by changing each too 20.

sorry ive just quickly explained my thoughts.. im neck deep in a bsp tree algo and an unreal t3d map loader :).
« Last Edit: July 18, 2013 by ninogenio »
Challenge Trophies Won:

Offline Canopy

  • Atari ST
  • ***
  • Posts: 208
  • Karma: 20
    • View Profile
Re: Some questions on OpenGL
« Reply #3 on: July 18, 2013 »
:)

yeah immediate mode stylee - kind of "manual" instancing, i've discovered that my old hp nvidia laptop (opengl 2) absolutely flies with that kind of method, but my newer ati/amd opengl 4.2 desktop card crawls doing it that way.

i ran into performance issues doing instancing along those lines in the UDG challenge, and after the deadline learned how i should have done it, with the caveat that I dont use the deprecated gl immediate mode. first thing i did was reapproach it all to solve that issue, and although solved its why i've got testing the scalability of my instancing down as a task to look at.

i know theres a faster way than using uniforms which is to stream the transforms in a buffer, but i'm yet to push what i've done. dont want to go down a rabbit hole for a performance boundary i've not hit, and may not ever hit yet :)
« Last Edit: July 18, 2013 by Canopy »

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Some questions on OpenGL
« Reply #4 on: July 18, 2013 »
yeah newer cards like the gt650 i have here really dont like fixed function gl anymore.. i still think too myself though how are beginners meant too deal with modern gl? even yet i find myself scratching my head over tiny little things.. its a minefield  :).

ive even seen lots of experienced coders making scrambled eggs for code.. using fixed function for matrix stuff and some draw calls then using newer gl for other things( just abusing the situation really ).. it just becomes a mess.

maybe 2 versions should be made.. purely fixed pipeline gl for beginners.. and purely hard coded for advanced users..

i currently use uniforms here too, it works and not too slowly either so ill stick with it ;)
« Last Edit: July 18, 2013 by ninogenio »
Challenge Trophies Won:

Offline Canopy

  • Atari ST
  • ***
  • Posts: 208
  • Karma: 20
    • View Profile
Re: Some questions on OpenGL
« Reply #5 on: July 18, 2013 »
i still think too myself though how are beginners meant too deal with modern gl? even yet i find myself scratching my head over tiny little things.. its a minefield  :).

glad i'm not the only one that feels that way about the state of opengl these days. i've got some ideas about trying to improve that.


Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Some questions on OpenGL
« Reply #6 on: July 18, 2013 »
I don't think there's any "modern gl" going on in this intro - there's nothing that would require shaders or other extensions, so the "instancing" is probably done using a display-list, the matrix stack and a simple row/column-loop.
Challenge Trophies Won:

Offline NaniBot

  • ZX 81
  • *
  • Posts: 13
  • Karma: 3
    • View Profile
Re: Some questions on OpenGL
« Reply #7 on: July 18, 2013 »
I don't know where to place the code. Please help..
« Last Edit: July 18, 2013 by NaniBot »

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Some questions on OpenGL
« Reply #8 on: July 18, 2013 »
right,

lets take a slightly different approach too this. lets work our way through the problem slowly. take it a step at a time and get you feeling the buzz of accomplishment for your hard work.

before adding the code i put up there. lets decide where a good place too add it might be.. as a first step i would like you too look very carefully at your code and tell me a few places where you think that snippet could go. ideally its going too go some where in the rendering phase.. then we can move on too actually adding it and making it work.
Challenge Trophies Won:

Offline NaniBot

  • ZX 81
  • *
  • Posts: 13
  • Karma: 3
    • View Profile
Re: Some questions on OpenGL
« Reply #9 on: July 19, 2013 »
right,

lets take a slightly different approach too this. lets work our way through the problem slowly. take it a step at a time and get you feeling the buzz of accomplishment for your hard work.

before adding the code i put up there. lets decide where a good place too add it might be.. as a first step i would like you too look very carefully at your code and tell me a few places where you think that snippet could go. ideally its going too go some where in the rendering phase.. then we can move on too actually adding it and making it work.

Umm...I think it should be inside DrawGLScene..

Like this maybe? :-\


int DrawGLScene(GLvoid)
{
void draw_chip(float x, float y, float z){
          glMatrixMode(GL_MODELVIEW)
          glPushMatrix();
          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   
          glLoadIdentity();
          glTranslatef( 0.0, 0.0f, -6.0f );
          glBegin(GL_TRIANGLES);
     glColor3f(1.0f,0.0f,0.0f);
     glVertex3f(0.0f,0.1f,0.0f);
     glVertex3f(-0.1f,-0.1f,0.0f);
     glVertex3f(0.1f,-0.1f,0.0f);
   glEnd();

       glPopMatrix();
}

int zsin=0;
void draw_chip_grid(void){
       int Grid_SizeY = 10;
       int Grid_SizeX = 10;
       for (int y = 0; y<Grid_SizeY; Y+=1 ){
            for (int x = 0; x<Grid_SizeX; X+= 1 ){
                  zsin += 0.2;
                  draw_chip(x+tilesize_x,y+tilesize_y,2.0f*sin(zsin/180.0*3.14159));
            }
       }
   }
}

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Some questions on OpenGL
« Reply #10 on: July 19, 2013 »
thats good! now we have a point too begin from,

now the big thing there is your putting functions inside other functions.. you cant really do that. you can call a function from within a function but not put functions inside one another.

so.

Code: [Select]
void function1(void){
       /////////draw a cube or something
}

void function2(void){
       function1();//// here im calling function1 so this would draw a cube
       function1();//// here im calling function1 again which would draw another cube!
       function1();//// and another.... etc etc
}

void main(){
       function2();//// now this single call will draw however many cube function2 calls for
}

is perfectly fine however....
Code: [Select]
void function1(void){
       /////////draw a cube or something

       void function2(void){
              function1();//// here im calling function1 so this would draw a cube
              function1();//// here im calling function1 again which would draw another cube!
              function1();//// and another.... etc etc
       }
}

void main(){
       function2();//// now this single call will draw however many cube function2 calls for
}
is not. can you see why the second one wouldn't work and the first would


now have another little look in here and it should become clear where you might have went slightly wrong.
Code: [Select]
int DrawGLScene(GLvoid)
{
void draw_chip(float x, float y, float z){
          glMatrixMode(GL_MODELVIEW)
          glPushMatrix();
          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   
          glLoadIdentity();
          glTranslatef( 0.0, 0.0f, -6.0f );
          glBegin(GL_TRIANGLES);
     glColor3f(1.0f,0.0f,0.0f);
     glVertex3f(0.0f,0.1f,0.0f);
     glVertex3f(-0.1f,-0.1f,0.0f);
     glVertex3f(0.1f,-0.1f,0.0f);
   glEnd();

       glPopMatrix();
}

int zsin=0;
void draw_chip_grid(void){
       int Grid_SizeY = 10;
       int Grid_SizeX = 10;
       for (int y = 0; y<Grid_SizeY; Y+=1 ){
            for (int x = 0; x<Grid_SizeX; X+= 1 ){
                  zsin += 0.2;
                  draw_chip(x+tilesize_x,y+tilesize_y,2.0f*sin(zsin/180.0*3.14159));
            }
       }
   }
}
Challenge Trophies Won:

Offline NaniBot

  • ZX 81
  • *
  • Posts: 13
  • Karma: 3
    • View Profile
Re: Some questions on OpenGL
« Reply #11 on: August 10, 2013 »
I created a few triangles using for loop...

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Some questions on OpenGL
« Reply #12 on: August 10, 2013 »
excellent NaniBot!!!,

k++ for sticking with it. that looks really cool, multiple objects seem hard at first but simple for loops can draw many thousands of instances of the same object if required.

its the exact same thing for the old large 2d tilemaped games like zelda etc the world map is usually tilemaped using a relatively small selection of tiles and just drawn hundreds of times with a few lines of code.

as a good little practice exercise how about adding a y axis for loop around the x axis one you already have too draw a couple of lines of triangles?

with just another line of code you could have x and y grid mapping.

« Last Edit: August 10, 2013 by ninogenio »
Challenge Trophies Won:

Offline NaniBot

  • ZX 81
  • *
  • Posts: 13
  • Karma: 3
    • View Profile
Re: Some questions on OpenGL
« Reply #13 on: August 11, 2013 »
excellent NaniBot!!!,

k++ for sticking with it. that looks really cool, multiple objects seem hard at first but simple for loops can draw many thousands of instances of the same object if required.

its the exact same thing for the old large 2d tilemaped games like zelda etc the world map is usually tilemaped using a relatively small selection of tiles and just drawn hundreds of times with a few lines of code.

as a good little practice exercise how about adding a y axis for loop around the x axis one you already have too draw a couple of lines of triangles?

with just another line of code you could have x and y grid mapping.

Working on it ...(THX for the help :D)

Offline Hotshot

  • DBF Aficionado
  • ******
  • Posts: 2114
  • Karma: 91
    • View Profile
Re: Some questions on OpenGL
« Reply #14 on: August 11, 2013 »
Nice one but too fast on my computer  :clap:

Well Done  :clap:

Offline Canopy

  • Atari ST
  • ***
  • Posts: 208
  • Karma: 20
    • View Profile
Re: Some questions on OpenGL
« Reply #15 on: August 12, 2013 »
good work!  k++

runs crazy fast on my laptop in a window, fullscreen then resized (1080p) slows it down a bit :)

Offline NaniBot

  • ZX 81
  • *
  • Posts: 13
  • Karma: 3
    • View Profile
Re: Some questions on OpenGL
« Reply #16 on: August 13, 2013 »
Too fast? On my pc it runs normal.... the rotation speed is 0.5f.

Offline Hotshot

  • DBF Aficionado
  • ******
  • Posts: 2114
  • Karma: 91
    • View Profile
Re: Some questions on OpenGL
« Reply #17 on: August 13, 2013 »
My Computer is

3.9 GHZ
4GB Ram
ATI 5870

 8)

Offline Canopy

  • Atari ST
  • ***
  • Posts: 208
  • Karma: 20
    • View Profile
Re: Some questions on OpenGL
« Reply #18 on: August 14, 2013 »
Too fast? On my pc it runs normal.... the rotation speed is 0.5f.

0.5f in relation to what?

on machines that don't have 'wait for vertical refresh' (VBL) enabled it'll just run flat out.

both my AMD/ATI machines had this disabled by default by the drivers AND if its off in the cpl even using the api's to turn it on don't work.

the solution is to use delta timing. http://en.wikipedia.org/wiki/Delta_timing

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Some questions on OpenGL
« Reply #19 on: August 14, 2013 »
i think this might be a step too much for NaniBot at this early stage guys,

although hugely important deltatiming/frameclamping is further down the road imho.. much more important is for him too have some fun and learn all the little fiddly techniques that come with coding first.

ill try and explain what the guys mean for you anyway.

every pc runs at different speeds depending on there internal hardware some might have supper fast gfx chips and cpu's, some may not be very fast.

opengl by default try's too keep your app running at the same speed as your monitors refresh rate. so each time your monitor refreshes the image the computer will set a flag too tell opengl thats its finished updating the screen. opengl will then allow the current frame too be drawn and your program too move onto the next frame. so it acts kind of like a pause.

no matter the speed of the machine the program would always run at the same speed as the vsync updates usually around the 60hz mark = 60fps..

now this is great in theory but if for some reason your end user has vsync disabled in his gfx control panel, then opengl will not look for this flag anymore and will just let your program run as fast as the hardware can manage. on one machine this might be 60fps and another 1000fps!
so our screen might run nice and smooth on one computer but blazing quick on another..

the best and most used way around this is delta timing. What you do is calculate how long the previous frame took too render in milliseconds then just say you rotate your triangle like rot = rot+0.5f; this would become rot = rot + (0.5f/(1000.0f-framedeltatime));

framedeltatime being a variable that holds how many millisecs the last frame took too draw. if it only takes 1 millisec on a super quick machine then you would be dividing by the value 999 but if it took 100 millisecs on a slow machine then you would be dividing your rotation by 900.

so really your scaling your movement variables based on the speed of the users machine. with delta timing every object will move the same amount each frame no matter what computer its run on.

the actual values used have too be adjusted to each individual case. but thats the rough basics.

i wouldnt worry too much with this atm though just have some fun learning then we can come back too it a little later.
« Last Edit: August 14, 2013 by ninogenio »
Challenge Trophies Won: