Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: Shockwave on January 25, 2008

Title: Window fullscreen problem.
Post by: Shockwave on January 25, 2008
I've adapted the source code that Rbraz posted to create a very small opengl initialisation, the program below (freebasic), draws a 3D starfield.

The program will compile fine, but there are problems when going fullscreen.
As long as your desktop is set at 1024 * 768 it will run with no problems, however, if your desktop is < 1024*768, the origin is wrong and the display is chopped off.

If your desktop resolution is>1024*768 then the window opens correctly but the viewport is the wrong size (too small) and at the bottom left of the screen.

Any ideas? :)

What stooopid thing have I done now?

Code: [Select]

        option explicit
        option static

'-------------------------------------
' Includes
'-------------------------------------

    #include "GL/gl.bi"
    #include "GL/glu.bi"
    #include "windows.bi"

    DIM SHARED AS BYTE W_FULLSCREEN =1 : ' 1 = FULLSCREEN
    DIM SHARED AS INTEGER W_XO =40      : ' X pos of window
    DIM SHARED AS INTEGER W_YO =40      : ' Y pos of window   
    DIM SHARED AS INTEGER W_XW =1024    : ' Width.
    DIM SHARED AS INTEGER W_YH =768    : ' Height.

    DIM SHARED pfd as PIXELFORMATDESCRIPTOR
    DIM SHARED hdc as hDC
    DECLARE SUB InitOGL()


    DIM SHARED AS INTEGER STARS=500
    DIM SHARED AS DOUBLE VVX(STARS)
    DIM SHARED AS DOUBLE VVY(STARS)
    DIM SHARED AS DOUBLE VVZ(STARS)

    DECLARE SUB STARINIT()
    DECLARE SUB STARFIELD()



    STARINIT()

DIM SHARED XR AS SINGLE : ' X ROTATION
DIM SHARED YR AS SINGLE : ' Y ROTATION
DIM SHARED ZR AS SINGLE : ' Z ROTATION
   

DIM D AS INTEGER
DIM SHARED AS DOUBLE GX,GY,GZ,TW,ZRR,XRR,YRR,RRR,delta,mm,fr

mm=timer

InitOGL()
'===============================================================================
' MAIN LOOP
'===============================================================================
WHILE((GetAsyncKeyState(VK_ESCAPE)<>-32767))


    glClear(GL_DEPTH_BUFFER_BIT + GL_COLOR_BUFFER_BIT )
   

    starfield()
    GLFLUSH()
   
        TW=TIMER
   
        fr=fr+1
        xr = xr +delta
yr = yr +delta
zr = zr +delta
       
if timer-mm>=.05 then
    delta=0.3/fr
    mm=timer
    fr=0
end if
SwapBuffers(hDC)
sleep 1


WEND

END



SUB STARFIELD
DIM A AS INTEGER

GLPUSHMATRIX
    glTranslatef(0, 0, -9)     
    glRotatef(XR, 1.0, 0.0, 0.0)
    glRotatef(YR, 0.0, 1.0, 0.0)
    glRotatef(ZR, 0.0, 0.0, 1.0)   
   
    glpointsize 1.5

    FOR A=0 TO STARS

    GLCOLOR3F 0.4,0.6,1.0
    GLBEGIN GL_POINTS
        GLVERTEX3F vvx(a),vvy(a),vvz(a)
    GLEND
   
    vvz(a)=vvz(a)+(delta*2)
    if vvz(a)>=20 then vvz(a)=vvz(a)-40
    NEXT

GLPOPMATRIX

GLPUSHMATRIX
        glTranslatef(0, 0, -12)

glbegin gl_lines
        GLCOLOR3F 0.6,0.6,1.0
        GLVERTEX3F -17.4,-9.8,0
        GLCOLOR3F 0.9,0.8,1.0
        GLVERTEX3F  17.4,-9.8,0

        GLVERTEX3F -17.4,9.8,0
        GLCOLOR3F 0.6,0.6,1.0
        GLVERTEX3F  17.4,9.8,0

glend

GLPOPMATRIX


END SUB


SUB STARINIT()

DIM A AS INTEGER

FOR A=0 TO STARS
    VVX(A)=(RND(1)*40)-20
    VVY(A)=(RND(1)*40)-20
    VVZ(A)=(RND(1)*40)-20
NEXT

END SUB


'===============================================================================
' Initialise OpenGL
'===============================================================================
sub InitOGL()

pfd.cColorBits = 32
    pfd.cDepthBits = 32
pfd.dwFlags    = PFD_SUPPORT_OPENGL + PFD_DOUBLEBUFFER
    IF W_FULLSCREEN=1 THEN hDC = GetDC(CreateWindow( "Edit", "BLAH!", WS_POPUP+WS_VISIBLE+WS_MAXIMIZE, 0 , 0 , 0 , 0, 0, 0, 0, 0))
    IF W_FULLSCREEN<>1 THEN hDC = GetDC(CreateWindow("Edit", "BLAH!", WS_POPUP+WS_VISIBLE+WS_BORDER,W_XO, W_YO, W_XW , W_YH, 0, 0, 0, 0))   
SetPixelFormat ( hDC, ChoosePixelFormat ( hDC, @pfd) , @pfd )
wglMakeCurrent ( hDC, wglCreateContext(hDC) )
    ShowCursor(FALSE)
   

    GLVIEWPORT 0, 0, W_XW, W_YH                          '' Reset The Current Viewport
GLMATRIXMODE GL_PROJECTION                          '' Select The Projection Matrix
GLLOADIDENTITY
   
    GLUPERSPECTIVE 90.0, W_XW / W_YH, 0.1, 100.0       '' Calculate The Aspect Ratio Of The Window
   
GLMATRIXMODE GL_MODELVIEW                           '' Select The Modelview Matrix
GLLOADIDENTITY                                      '' Reset The Modelview Matrix

GLSHADEMODEL GL_SMOOTH                              '' Enable Smooth Shading
GLCLEARCOLOR 0.0, 0.0, 0.0,0
GLCLEARDEPTH 1.0                                    '' Depth Buffer Setup
GLENABLE GL_DEPTH_TEST                              '' Enables Depth Testing
GLDEPTHFUNC GL_LEQUAL                               '' The Type Of Depth Testing To Do
GLHINT GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST    '' Really Nice Perspective Calculations


    GLHINT(GL_FOG_HINT,GL_NICEST)
    GLFOGF(GL_FOG_DENSITY,0.05)
    GLFOGF(GL_FOG_START,    13.0)
    GLFOGF(GL_FOG_END,     15.0)
    GLFOGI(GL_FOG_MODE,GL_EXP2)   
    GLENABLE (GL_FOG)
   
end sub

Title: Re: Window fullscreen problem.
Post by: stormbringer on January 25, 2008
I can't see and call to

glViewport(0, 0, width, height)

in your code.....
Title: Re: Window fullscreen problem.
Post by: Shockwave on January 25, 2008
It's there mate.

Code: [Select]

    GLVIEWPORT 0, 0, W_XW, W_YH                          '' Reset The Current Viewport
Title: Re: Window fullscreen problem.
Post by: stormbringer on January 25, 2008
oh sorry.. saw it now..


try to set your projection matrix at every refresh (after you clear the screen) to see if it fixes the problem. That's what I do all the time just in case the projection state is wrong at the beginning of the scene redrawal
Title: Re: Window fullscreen problem.
Post by: Shockwave on January 25, 2008
The problem seems to be that it's grabbing the resolution of the desktop for the viewport.
Title: Re: Window fullscreen problem.
Post by: Jim on January 25, 2008
Bear in mind it's not really going fullscreen, it's just fitting a window to the full size of the desktop, whatever res that is (I think you realise that).  You need to be calling ChangeDisplaySettings() to really do that.

So W_XW and W_YH would need to be set to the desktop size, not the size YOU want to set.
In fullscreen mode you need to add
Code: [Select]
W_XW=GetSystemMetrics(SM_CXSCREEN)
W_YH=GetSystemMetrics(SM_CYSCREEN)
before you call glViewport.

Jim
Title: Re: Window fullscreen problem.
Post by: stormbringer on January 25, 2008
or take the client rectangle of your window...

with this Win32 API:


   RECT   localBox;

   // get client box
   GetClientRect(hWnd,&localBox);

   glViewport(0, 0, width, height);

Title: Re: Window fullscreen problem.
Post by: Shockwave on January 25, 2008
That seems to have fixed if Jim, thanks :) I would have been a long time trying to find that! I am also going to try the solution you posted too Stormbringer.

K++ for your help guys.
Title: Re: Window fullscreen problem.
Post by: stormbringer on January 25, 2008
thanks!

The way I do it is if in full screen mode, create a window of the screen size (without any borders), otherwise any size (if in desktop mode)

I always get the client rectangle and set the viewport to the client area's with & height... then it always works, whatever the screen resolution is
Title: Re: Window fullscreen problem.
Post by: va!n on January 28, 2008
why are you guys not trying to use "ChangeDisplaySettings" API, which is the best way i think for what you are looking for.
Title: Re: Window fullscreen problem.
Post by: Jim on January 28, 2008
It uses more bytez :)

I dunno, this started off as Shockwave's OpenGL adventure, but it's ended up getting mixed in with some very Windows-specific size-coding stuff.  Very demo-coder!

Jim
Title: Re: Window fullscreen problem.
Post by: WidowMaker [retired] on January 28, 2008
The size elment is really interesting for me too.

I tried to use this framework Rbraz posted (It look a lot like Aulds) and I am now writing my first Crakctro in Opengl :P

Screenshot attached.

I will of course be releasing the source code here when I am done with it because I am using a lot of code from this great forum.
Title: Re: Window fullscreen problem.
Post by: va!n on January 28, 2008
ok, size coding related, hehe...

@WIndowMaker:
sshot looks nice... the only thing i dont really like to much is the logo, because i find its hard to read ^^ anyway nice work and still looking forward. keep on ya nice work
Title: Re: Window fullscreen problem.
Post by: WidowMaker [retired] on January 28, 2008
Va!n, my nick is Widowmaker, not WINDOWmaker :P

Trust me, Illusionmaker's logo is nice, it is hard to see from the shot where it is rotating.
Title: Re: Window fullscreen problem.
Post by: Shockwave on January 28, 2008
Real cracktro feeling Widowmaker :) Looking forward to seeing it finished!
Title: Re: Window fullscreen problem.
Post by: Shockwave on January 28, 2008
Btw logo seems very nicely done, Va!n, did you expand the thumbnail? It's quite easy to read for me.
Title: Re: Window fullscreen problem.
Post by: va!n on January 28, 2008
@Wi(n)dowmaker without "n"  :P
sorry for wrong spelling... i thought all the time your name is spelled with a "n" *sorry*.
The love the art of the logo and finally i managed to read the name *grin*
good luck with the prod!
Title: Re: Window fullscreen problem.
Post by: benny! on January 28, 2008
Screeny looks very cool indeed. Like the blocky
scrolly - and it seems like there are some flying
cubes in the stafield, too.

Logo rocks also IMHO !
Title: Re: Window fullscreen problem.
Post by: ninogenio on January 28, 2008
very nice coding shockwave!

i really like the fact that your not leaning on freebasic for the window setup or buffer controll, infact seing as your not would'nt you be beter of coding in cpp/ogl as there is very little diffrence.

you probably already know this but if your doing it in freebasic you can set your ogl window up with the screen command and you dont need hdc's to flip the buffers.

i personally like the way you are doing things though! keep it up  :cheers:
Title: Re: Window fullscreen problem.
Post by: Shockwave on January 28, 2008
Cheers mate :)

I tried with FB's screen command, it made bigger exe's though, so I'll probably stick with this type of framework.
Title: Re: Window fullscreen problem.
Post by: ninogenio on January 28, 2008
yeah thats right,

and now i come to think of it as long as you stay away from freebasic commands altogether you could use jims tinyfy processes and get your exe's really small with this.
Title: Re: Window fullscreen problem.
Post by: stormbringer on January 28, 2008
why not writing everything from scratch by yourself? it's not that hard and you can really get low sizes if you do not link with all the Windows common lib-mess. Plus you will gain extra control on everything... and for sure less problems when learning.
Title: Re: Window fullscreen problem.
Post by: ninogenio on January 29, 2008
shockwave i hope you dont mind ive had a wee play around with your code.

i spotted a gdi ogl font sample on nehe and couldnt resist trying it out in freebasic the results are quite impressive i think it lets you convert any sytem font to an ogl 3d vector font and the final exe is only 3.5k bigger uncompressed.

here is the code.

Code: [Select]
      option explicit
        option static

'-------------------------------------
' Includes
'-------------------------------------

    #include "GL/gl.bi"
    #include "GL/glu.bi"
    #include "windows.bi"
    #include "crt.bi"
   
    DIM SHARED AS BYTE W_FULLSCREEN =1 : ' 1 = FULLSCREEN
    DIM SHARED AS INTEGER W_XO =40      : ' X pos of window
    DIM SHARED AS INTEGER W_YO =40      : ' Y pos of window   
    DIM SHARED AS INTEGER W_XW =1024    : ' Width.
    DIM SHARED AS INTEGER W_YH =768    : ' Height.

    DIM SHARED pfd as PIXELFORMATDESCRIPTOR
    DIM SHARED hdc as hDC
    DECLARE SUB InitOGL()


    DIM SHARED AS INTEGER STARS=500
    DIM SHARED AS DOUBLE VVX(STARS)
    DIM SHARED AS DOUBLE VVY(STARS)
    DIM SHARED AS DOUBLE VVZ(STARS)

    DECLARE SUB STARINIT()
    DECLARE SUB STARFIELD()



    STARINIT()

DIM SHARED XR AS SINGLE : ' X ROTATION
DIM SHARED YR AS SINGLE : ' Y ROTATION
DIM SHARED ZR AS SINGLE : ' Z ROTATION
   

DIM D AS INTEGER
DIM SHARED AS DOUBLE GX,GY,GZ,TW,ZRR,XRR,YRR,RRR,delta,mm,fr

'added gl list font variable
Dim shared FBase As GLuint
'added array to hold info about font charicters
Dim shared gmf( 0 To 256 ) As GLYPHMETRICSFLOAT
Dim shared rot As Single
Declare Sub glPrint(fmt as string)

mm=timer

InitOGL()
'===============================================================================
' MAIN LOOP
'===============================================================================
WHILE((GetAsyncKeyState(VK_ESCAPE)<>-32767))


    glClear(GL_DEPTH_BUFFER_BIT + GL_COLOR_BUFFER_BIT )
    glLoadIdentity()
    GlPushMatrix
glTranslatef(0.0f,0.0f,-3.0-cos(rot/20.0f))
glRotatef(rot,1.0f,0.0f,0.0f)
glRotatef(rot*1.5f,0.0f,1.0f,0.0f)
glRotatef(rot*1.4f,0.0f,0.0f,1.0f)
glColor3f(1.0f*cast( single , cos(rot/20.0f) ),1.0f*cast( single , sin(rot/25.0f) ),1.0f-0.5f*cast( single , cos(rot/17.0f) ) )
  glPrint("Hello EveryOne")
rot += 0.5f
    GlPopMatrix
    starfield()
    GLFLUSH()
   
        TW=TIMER
   
        fr=fr+1
        xr = xr +delta
yr = yr +delta
zr = zr +delta
       
if timer-mm>=.05 then
    delta=0.3/fr
    mm=timer
    fr=0
end if
SwapBuffers(hDC)
sleep 1


WEND

glDeleteLists(FBase, 256)
END



SUB STARFIELD
DIM A AS INTEGER

GLPUSHMATRIX
    glTranslatef(0, 0, -9)     
    glRotatef(XR, 1.0, 0.0, 0.0)
    glRotatef(YR, 0.0, 1.0, 0.0)
    glRotatef(ZR, 0.0, 0.0, 1.0)   
   
    glpointsize 1.5

    FOR A=0 TO STARS

    GLCOLOR3F 0.4,0.6,1.0
    GLBEGIN GL_POINTS
        GLVERTEX3F vvx(a),vvy(a),vvz(a)
    GLEND
   
    vvz(a)=vvz(a)+(delta*2)
    if vvz(a)>=20 then vvz(a)=vvz(a)-40
    NEXT

GLPOPMATRIX

GLPUSHMATRIX
        glTranslatef(0, 0, -12)

glbegin gl_lines
        GLCOLOR3F 0.6,0.6,1.0
        GLVERTEX3F -17.4,-9.8,0
        GLCOLOR3F 0.9,0.8,1.0
        GLVERTEX3F  17.4,-9.8,0

        GLVERTEX3F -17.4,9.8,0
        GLCOLOR3F 0.6,0.6,1.0
        GLVERTEX3F  17.4,9.8,0

glend

GLPOPMATRIX


END SUB


SUB STARINIT()

DIM A AS INTEGER

FOR A=0 TO STARS
    VVX(A)=(RND(1)*40)-20
    VVY(A)=(RND(1)*40)-20
    VVZ(A)=(RND(1)*40)-20
NEXT

END SUB


'===============================================================================
' Initialise OpenGL
'===============================================================================
sub InitOGL()

pfd.cColorBits = 32
    pfd.cDepthBits = 32
pfd.dwFlags    = PFD_SUPPORT_OPENGL + PFD_DOUBLEBUFFER
    IF W_FULLSCREEN=1 THEN hDC = GetDC(CreateWindow( "Edit", "BLAH!", WS_POPUP+WS_VISIBLE+WS_MAXIMIZE, 0 , 0 , 0 , 0, 0, 0, 0, 0))
    IF W_FULLSCREEN<>1 THEN hDC = GetDC(CreateWindow("Edit", "BLAH!", WS_POPUP+WS_VISIBLE+WS_BORDER,W_XO, W_YO, W_XW , W_YH, 0, 0, 0, 0))   
SetPixelFormat ( hDC, ChoosePixelFormat ( hDC, @pfd) , @pfd )
wglMakeCurrent ( hDC, wglCreateContext(hDC) )
    ShowCursor(FALSE)
   

    GLVIEWPORT 0, 0, W_XW, W_YH                          '' Reset The Current Viewport
GLMATRIXMODE GL_PROJECTION                          '' Select The Projection Matrix
GLLOADIDENTITY
   
    GLUPERSPECTIVE 90.0, W_XW / W_YH, 0.1, 100.0       '' Calculate The Aspect Ratio Of The Window
   
GLMATRIXMODE GL_MODELVIEW                           '' Select The Modelview Matrix
GLLOADIDENTITY                                      '' Reset The Modelview Matrix

GLSHADEMODEL GL_SMOOTH                              '' Enable Smooth Shading
GLCLEARCOLOR 0.0, 0.0, 0.0,0
GLCLEARDEPTH 1.0                                    '' Depth Buffer Setup
GLENABLE GL_DEPTH_TEST                              '' Enables Depth Testing
GLDEPTHFUNC GL_LEQUAL                               '' The Type Of Depth Testing To Do
GLHINT GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST    '' Really Nice Perspective Calculations


    GLHINT(GL_FOG_HINT,GL_NICEST)
    GLFOGF(GL_FOG_DENSITY,0.05)
    GLFOGF(GL_FOG_START,    13.0)
    GLFOGF(GL_FOG_END,     15.0)
    GLFOGI(GL_FOG_MODE,GL_EXP2)   
    GLENABLE (GL_FOG)
   
    'tiny ogl font init
    dim Font as HFONT
FBase = glGenLists(256)
Font = CreateFont( -12, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, FF_DONTCARE or DEFAULT_PITCH, "Comic Sans MS")
SelectObject(hDC, Font)
wglUseFontOutlines( hDC, 0, 255, FBase, 0.0f, 0.2f, WGL_FONT_POLYGONS, @gmf(0))

end sub

Sub glPrint(fmt As String)

        Dim As Single length = 0
        Dim text As ZString Ptr
        text = Allocate( 256 + 1 )

        *text = fmt
        Dim as integer MLoop
        for MLoop = 0 to strlen(text)
length += gmf(text[MLoop]).gmfCellIncX
        Next

glTranslatef(-Length/2,0.0f,0.0f)

glPushAttrib(GL_LIST_BIT)
glListBase(FBase)
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text)
glPopAttrib()

End Sub

have a play around and see what you think try changing WGL_FONT_POLYGONS to WGL_FONT_LINES
Title: Re: Window fullscreen problem.
Post by: Shockwave on January 29, 2008
Hi Nino, that's a nice useful example :)

Thank you for posting it and have some good Karma. Loading truetype fonts was going to be one of my next projects, you must have read my mind!
Title: Re: Window fullscreen problem.
Post by: ninogenio on January 29, 2008
no problem mate.

if you need any info on controlling the fonts you create this is a good read.

http://functionx.com/visualc/gdi/fonts.htm

im glad the example is of some use and thanks for the karma!