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

0 Members and 2 Guests are viewing this topic.

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Hi all,

I've began some test in opengl this afternoon. For the moment I'm using Purebasic.

There's something I really don't understand. How can I match the pixels of screen with the space axis positions? I explain:

if I'm trying to put a 32*32 pixel gfx onto screen, and that I want it to be displayed truely in 32*32pixels, how can it be done?
This is usefull for retro remakes, as the logos, fonts should be displayed like in the original way.

In my example, I've made a simple scrolltext. After playing with the values of glTranslatef, I've found "good" value to display it but it is NOT the good one that make it like the original size.

here is the part of the code, the bitmap texture is 640*300 bmp includebinary, the fonts are 64*50 pixels

Code: [Select]
   cco = 0

  For cc = 0 To 11

   
    letter = (Asc(UCase(Mid(t.s, tptr+cc, 1)))-32)
    xf = ((letter % 10)*64)
    x.f = xf/640 ; find the x postion in matrix
    yf = (5-((letter - (letter%10))/10))*50
    y.f = yf/300 ; find the x position in matrix

    larg.f = 64*1/640  ; find the size of the font in matrix depth
    haut.f = 50*1/300

    glLoadIdentity_() ;Reset The Current Matrix
   
    glTranslatef_((cco+sco)-10.0,0,-20.0) ;Center On X, Y, Z Axis
   
    glPushMatrix_()
 
    glBindTexture_(#GL_TEXTURE_2D,texture(0)) ;Select Our Texture
      glBegin_(#GL_QUADS)
   
        glTexCoord2f_(x, y) : glVertex3f_(-1.0,-1.0, 0.0) ;Bottom Left Of The Texture and Quad
        glTexCoord2f_((x+larg), y) : glVertex3f_( 1.0,-1.0, 0.0) ;Bottom Right Of The Texture and Quad
        glTexCoord2f_((x+larg), (y+haut)) : glVertex3f_( 1.0, 1.0, 0.0) ;Top Right Of The Texture and Quad
        glTexCoord2f_(x, (y+haut)) : glVertex3f_(-1.0, 1.0, 0.0) ;Top Left Of The Texture and Quad
      glEnd_()
    glPopMatrix_()
   
    cco = cco + 2 ; here is the problem : why it works with 2 ???   dunno...
  Next
 
  sco-0.2
  If sco<=-2.0
    sco = 0.0
    tptr+1
    If tptr > (Len(t.s)-12)
      tptr = 1
    EndIf
  EndIf
 

« Last Edit: April 26, 2012 by jace_stknights »
Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: My first Opengl intro
« Reply #1 on: March 16, 2012 »
When working with 3D in OpenGL, it's common to use perspective projection and have the screen bounds range from -1 to +1. You can use Orthographic projection to set the screen bounds from 0 - 640 and 0 - 480 (or any other sizes you like), which is useful for situations where you want to use exact 2D coordinates.

I found this on a Nehe tutorial. Hopefully it will help:

void ViewOrtho()                            // Set Up An Ortho View
{
    glMatrixMode(GL_PROJECTION);                    // Select Projection
    glPushMatrix();                         // Push The Matrix
    glLoadIdentity();                       // Reset The Matrix
    glOrtho( 0, 640 , 480 , 0, -1, 1 );             // Select Ortho Mode (640x480)
    glMatrixMode(GL_MODELVIEW);                 // Select Modelview Matrix
    glPushMatrix();                         // Push The Matrix
    glLoadIdentity();                       // Reset The Matrix
}
 
void ViewPerspective()                          // Set Up A Perspective View
{
    glMatrixMode( GL_PROJECTION );                  // Select Projection
    glPopMatrix();                          // Pop The Matrix
    glMatrixMode( GL_MODELVIEW );                   // Select Modelview
    glPopMatrix();                          // Pop The Matrix
}

Give that a whirl, changing the 640,480 to whatever scale you need. When drawing your vertices, you can then specify the coordinates in 2D space. Here's the official glOrtho docs too, probably worth a read.

Also, this is kinda related to what you're trying to do .

I often do something similar when doing render to texture type stuff.
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 #2 on: March 16, 2012 »
thank you Raizor!!!  :cheers:
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 #3 on: March 16, 2012 »
hmmm two things:

my first version (in perpective mode) runs with no vsync on my laptop (it has been made on my job computer and runs fine on it)... WTF?!?

and after some mods using the tricks you gave me, nothing runs on screen:

iInitGL()
Code: [Select]

 glEnable_(#GL_TEXTURE_2D) ;Enable Texture Mapping ( NEW )
 glClearColor_(0.0,0.0,0.0,0.5) ;Black Background
 glMatrixMode_(GL_PROJECTION)
 glPushMatrix_()
 glLoadIdentity_()
 glOrtho_(0, xres, xres, 0, -1, 1)
 glDisable_(GL_DEPTH_TEST)
 glMatrixMode_(GL_MODELVIEW)
 glPushMatrix_()
 glLoadIdentity_()

in DrawGLScene()
Code: [Select]
  glClear_(GL_COLOR_BUFFER_BIT)
 
  cco = 0

  ;For cc = 0 To 11

   
    ;letter = (Asc(UCase(Mid(t.s, tptr+cc, 1)))-32)
    ;x = ((letter % 10)*64)
    ;y = ((letter - (letter%10))/10)*50
 
    glLoadIdentity_() ;Reset The Current Matrix
   
   
    glBindTexture_(#GL_TEXTURE_2D,texture(0)) ;Select Our Texture
   
    glBegin_(#GL_QUADS)
       glTexCoord2f_(0.0, 0.0) : glVertex2f_(cco,0)
       glTexCoord2f_(0.5, 0.0) : glVertex2f_(cco+64,0)
       glTexCoord2f_(0.5, 0.5) : glVertex2f_(cco+64,50)
       glTexCoord2f_(0.0, 0.5) : glVertex2f_(cco,50)
    glEnd_()

this sould display a 64*50 pixel textured at screen????
Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: My first Opengl intro
« Reply #4 on: March 16, 2012 »
If you draw an untextured quad, does it work?
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 #5 on: March 16, 2012 »
if I do this:
Code: [Select]

   glColor3f_(255, 128, 64)
    glBegin_(#GL_QUADS)
    glVertex2f_(cco,0)
    glVertex2f_(cco+64,0)
    glVertex2f_(cco+64,50)
    glVertex2f_(cco,50)
    glEnd_()
I still got a black screen  :-[
Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: My first Opengl intro
« Reply #6 on: March 16, 2012 »
Can you upload the project please Jace? I'll have a look and see if I can figure out what's going on.
raizor

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: My first Opengl intro
« Reply #7 on: March 17, 2012 »
Try
 glCullMode_(GL_NONE)

Your polygon is clockwise which is back-facing.

Jim
 
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 #8 on: March 17, 2012 »
Try
 glCullMode_(GL_NONE)

Your polygon is clockwise which is back-facing.

Jim

glCullMode_  seems not be supported! :(

and I've changed the polygon  to counter-clockwise and still nothing...

I've attached the project...
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: My first Opengl intro
« Reply #9 on: March 17, 2012 »
My apologies, it's glCullFace_
Jim
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 #10 on: March 17, 2012 »
My apologies, it's glCullFace_
Jim
hehe  ;D

still not working (also tried with GL_FRONT GL_BACK) ??? ??? ??? ??? ??? ??? ??? :
Code: [Select]
    glLoadIdentity_() ;Reset The Current Matrix
   
    glCullFace_(GL_FRONT_AND_BACK)
       
    glBindTexture_(#GL_TEXTURE_2D,texture(0)) ;Select Our Texture
    glColor3f_(255, 128, 6)
    glBegin_(#GL_QUADS)
    glTexCoord2f_(0.0, 0.0)
    glVertex2f_(cco,0)
    glTexCoord2f_(0.5, 0.0)
    glVertex2f_(cco,50)
    glTexCoord2f_(0.5, 0.5)
    glVertex2f_(cco+64,50)
    glTexCoord2f_(0.0, 0.5)
    glVertex2f_(cco+64,0)
    glEnd_()
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 #11 on: March 17, 2012 »
Ok found the problem!

first I've forgotten the # before the commands!
Second : all values MUST be floats!!!

Code: [Select]
   glBindTexture_(#GL_TEXTURE_2D,texture(0)) ;Select Our Texture
    glColor3f_(1.0, 1.0, 1.0)
    glBegin_(#GL_QUADS)
    glTexCoord2f_(0.0, 0.0) : glVertex2f_(0.0,0.0)
    glTexCoord2f_(0.5, 0.0) : glVertex2f_(64.0,0.0)
    glTexCoord2f_(0.5, 0.5) : glVertex2f_(64.0,50.0)
    glTexCoord2f_(0.0, 0.5) : glVertex2f_(0.0,50.0)
    glEnd_()

that's work now... well let's code again  ;D
Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: My first Opengl intro
« Reply #12 on: March 17, 2012 »
Ok found the problem!

first I've forgotten the # before the commands!
Second : all values MUST be floats!!!

Code: [Select]
   glBindTexture_(#GL_TEXTURE_2D,texture(0)) ;Select Our Texture
    glColor3f_(1.0, 1.0, 1.0)
    glBegin_(#GL_QUADS)
    glTexCoord2f_(0.0, 0.0) : glVertex2f_(0.0,0.0)
    glTexCoord2f_(0.5, 0.0) : glVertex2f_(64.0,0.0)
    glTexCoord2f_(0.5, 0.5) : glVertex2f_(64.0,50.0)
    glTexCoord2f_(0.0, 0.5) : glVertex2f_(0.0,50.0)
    glEnd_()

that's work now... well let's code again  ;D

Cool :)

You can probably use glVertex2i_ if you want to use ints rather than floats.
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 #13 on: March 17, 2012 »
You can probably use glVertex2i_ if you want to use ints rather than floats.

 ;) I didn't now it was existing!

By the way, got it working, but there is still a problem: vsync ! The scrolltext is running 3000x to fast! I remember the tutorial from Stormbringer about this. If I have understood on some cards, the vsync is not set. Ok But how to fix it in PureBasic???

Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: My first Opengl intro
« Reply #14 on: March 17, 2012 »
You can either use delta timing to control movement (looks like this is covered in Stormbringer's tutorial), or the wglSwapIntervalEXT OpenGL extension. Have a look at this for some more info on wglSwapIntervalEXT.

So far, I've been using delta timing. I get the system time each frame and then compare to the system time from the previous frame to determine how much time has elapsed between frames. I then use this 'difference' to determine how far to move things such as scrolltext etc.  For scrolltext, you can just determine the scroll offset using the elapsed time since the intro started. Something like scrollPosX = -timeTicks*someAmount. That's always worked reasonably well for me.

wglSwapIntervalEXT may well give a smoother result though (sometimes my scroller looks a little jerky when using delta timing, but that could just be me).
raizor

Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: My first Opengl intro
« Reply #15 on: March 17, 2012 »
You can either use delta timing to control movement or the wglSwapIntervalEXT OpenGL extension.
It's always a good idea to enable vsync but it's not a reliable alternative to delta timing.
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 #16 on: March 17, 2012 »
@Raizor, thank you, will take a look at this...

But I have tested some demo, and lot of them don't runat 60fps (with the vsync). For example, your wireframe scrolltext intro runs as fast as mine, about 500000 fps!  :o So even your technique seems not to work on my computer.

I've also saw that in stormbringer remakes, the option screen is running at 60fps, and not the remake itself... So perhaps my computer got a problem?
Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: My first Opengl intro
« Reply #17 on: March 17, 2012 »
@Raizor, thank you, will take a look at this...

But I have tested some demo, and lot of them don't runat 60fps (with the vsync). For example, your wireframe scrolltext intro runs as fast as mine, about 500000 fps!  :o So even your technique seems not to work on my computer.

I've also saw that in stormbringer remakes, the option screen is running at 60fps, and not the remake itself... So perhaps my computer got a problem?

Normally your gfx card driver has a global vsync option. Something along the lines of "application controlled / force on / force off". That wireframe scroller was a bit of a mess. I did some nasty manual loop to eat up frame time until it had taken 16 ms per frame or something - a very hacky fix ;)
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 #18 on: March 17, 2012 »
Hmmm I'm installing VC++ on my laptop (it was previoulsy on the ol done wich has broken down two month ago). I will compil STormy tutos and hope they work fine here.
This will be time to start with C++ code  ;D
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 #19 on: March 18, 2012 »
after a lok at Stormy code, I found that he was also using "wglSwapIntervalEXT" to set the number of swap between two frames.
So I've tried this: (wglGetProcAddress_ is already imported by OpenGl32.lib)

Code: [Select]
   Protected wglSwapIntervalEXT.l
   wglSwapIntervalEXT = wglGetProcAddress_("wglSwapIntervalEXT")
   If wglSwapIntervalEXT
     CallFunctionFast(wglSwapIntervalEXT, 1)
     Debug "ok"
   Else
     Debug "wglSwapIntervalEXT not found"
   EndIf 

The function is present (debug ouput "ok") but still nothing  >:(
Challenge Trophies Won: