Object oriented programming or 3d graphics?

General FreeBASIC programming questions.
IvanisIvan
Posts: 22
Joined: Nov 07, 2019 21:57

Object oriented programming or 3d graphics?

Postby IvanisIvan » Sep 06, 2020 23:38

Does FreeBASIC have the capabilities for 3D graphics or Object-Oriented programming? I have scoured the documentation but I can't find information on either. If it is possible, could someone point me in the right direction?
- Ivan
MrSwiss
Posts: 3577
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Object oriented programming or 3d graphics?

Postby MrSwiss » Sep 06, 2020 23:47

See FB-Manual/Programmers Manual -- OOP: https://www.freebasic.net/wiki/ProPgObjectRtti

Built-in FBGFX is 2D only, you'll probably need a external lib for true 3D.
IvanisIvan
Posts: 22
Joined: Nov 07, 2019 21:57

Re: Object oriented programming or 3d graphics?

Postby IvanisIvan » Sep 06, 2020 23:50

MrSwiss wrote:See FB-Manual/Programmers Manual -- OOP: https://www.freebasic.net/wiki/ProPgObjectRtti

Built-in FBGFX is 2D only, you'll probably need a external lib for true 3D.

Thank you! Do you know if any library exists?
MrSwiss
Posts: 3577
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Object oriented programming or 3d graphics?

Postby MrSwiss » Sep 07, 2020 0:02

IvanisIvan wrote:Thank you! Do you know if any library exists?

No, not straight away.
Some guys here, manage 3D look-alikes with the 2D libraries, too.
(Search: Tips & Tricks section for examples)
IvanisIvan
Posts: 22
Joined: Nov 07, 2019 21:57

Re: Object oriented programming or 3d graphics?

Postby IvanisIvan » Sep 07, 2020 0:16

MrSwiss wrote:
IvanisIvan wrote:Thank you! Do you know if any library exists?

No, not straight away.
Some guys here, manage 3D look-alikes with the 2D libraries, too.
(Search: Tips & Tricks section for examples)

Okay, Thank you!
angros47
Posts: 1671
Joined: Jun 21, 2005 19:04

Re: Object oriented programming or 3d graphics?

Postby angros47 » Sep 07, 2020 9:44

IvanisIvan wrote:Thank you! Do you know if any library exists?



My library OpenB3D (formerly MiniB3D) should do what you need

https://www.freebasic.net/forum/viewtopic.php?t=15409
IvanisIvan
Posts: 22
Joined: Nov 07, 2019 21:57

Re: Object oriented programming or 3d graphics?

Postby IvanisIvan » Sep 14, 2020 9:19

angros47 wrote:
IvanisIvan wrote:Thank you! Do you know if any library exists?



My library OpenB3D (formerly MiniB3D) should do what you need

https://www.freebasic.net/forum/viewtopic.php?t=15409

thank you!
dodicat
Posts: 6645
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Object oriented programming or 3d graphics?

Postby dodicat » Sep 15, 2020 23:09

OpenGL is a favourite.
There are plenty of examples and nothing needs downloaded.
Nearly every compiler can use openGL. Examples are all over the internet, it is a universal graphics machine.
Example:

Code: Select all


 #Include Once "GL/glu.bi"


Dim As Integer xres,yres
Screenres 1024,768,32,,2

Screeninfo xres,yres

Function Regulate(Byval MyFps As long,Byref fps As long) As long
    Static As Double timervalue,lastsleeptime,t3,frames
    Dim As Double t=Timer
    frames+=1
    If (t-t3)>=1 Then t3=t:fps=frames:frames=0
    Dim As long sleeptime=lastsleeptime+((1/myfps)-T+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    lastsleeptime=sleeptime
    timervalue=T
    Return sleeptime
End Function

Sub glsetup(xres as long,yres as long)
    glShadeModel(GL_SMOOTH)                 ' Enables Smooth Color Shading
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
    glBlendFunc(GL_ONE,GL_ONE)
    glEnable GL_ALPHA
    glEnable GL_BLEND
    glViewport(0, 0, xres, yres)       ' Set the viewport
    glMatrixMode(GL_PROJECTION)        ' Change Matrix Mode to Projection
    glLoadIdentity                     ' Reset View
    gluPerspective(45.0, xres/yres, 1.0, 100.0)
    glMatrixMode(GL_MODELVIEW)         ' Return to the modelview matrix
    glLoadIdentity                     '  Reset View
    glclearcolor 0,0,.5,1
End Sub

'Rotate and draw the cube
Sub DrawGlCube(Byref rotangle As Single)
    glLoadIdentity()
    glTranslatef(0,0,-5)
    glRotatef(rotangle,1,.5,.25)           ' Rotate
    glBegin(GL_QUADS)
   
    glcolor3ub 255,0,0
    glVertex3f( 1.0, 1.0,-1.0)            ' Top right of the quad (top)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top left of the quad (top)
    glVertex3f(-1.0, 1.0, 1.0)            ' Bottom left of the quad (top)
    glVertex3f( 1.0, 1.0, 1.0)            ' Bottom right of the quad (top)
   
    glcolor3ub 255,100,0
    glVertex3f( 1.0,-1.0, 1.0)            ' Top right of the quad (bottom)
    glVertex3f(-1.0,-1.0, 1.0)            ' Top left of the quad (bottom)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom left of the quad (bottom)
    glVertex3f( 1.0,-1.0,-1.0)            ' Bottom right of the quad (bottom)
 
    glcolor3ub 255,0,255
    glVertex3f( 1.0, 1.0, 1.0)            ' Top right of the quad (front)
    glVertex3f(-1.0, 1.0, 1.0)            ' Top left of the quad (front)
    glVertex3f(-1.0,-1.0, 1.0)            ' Bottom left of the quad (front)
    glVertex3f( 1.0,-1.0, 1.0)            ' Bottom right of the quad (front)
   
    glcolor3ub 0,0,200
    glVertex3f( 1.0,-1.0,-1.0)            ' Bottom left of the quad (back)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom right of the quad (back)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top right of the quad (back)
    glVertex3f( 1.0, 1.0,-1.0)            ' Top left of the quad (back)
   
    glcolor3ub 0,255,0
    glVertex3f(-1.0, 1.0, 1.0)            ' Top right of the quad (left)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top left of the quad (left)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom left of the quad (left)
    glVertex3f(-1.0,-1.0, 1.0)            ' Bottom right of the quad (left)
   
    glcolor3ub 255,0,100
    glVertex3f( 1.0, 1.0,-1.0)            ' Top right of the quad (right)
    glVertex3f( 1.0, 1.0, 1.0)            ' Top left of the quad (right)
    glVertex3f( 1.0,-1.0, 1.0)            ' Bottom left of the quad (right)
    glVertex3f( 1.0,-1.0,-1.0)
    glend
End Sub

Sub drawstring(xpos As Long,ypos As Long,text As String ,col As Ulong,size As Single,xres As Long,yres As Long) export
    glMatrixMode GL_PROJECTION 'save projection
    glPushMatrix
    glMatrixMode GL_MODELVIEW
    glPushMatrix
   
    glMatrixMode GL_PROJECTION 'make ortho
    glLoadIdentity
    glOrtho 0, xres, yres, 0,-1, 1
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
    #define Red(c) ((c) Shr 16 And 255)
    #define Green(c) ((c) Shr  8 And 255)
    #define Blue(c) ((c) And 255)
    #define Alph(c) ((c) Shr 24)
    glColor4ub Red(col),Green(col),Blue(col),alph(col)
    glend
    glpointsize(1.1*size)
    glBegin (GL_POINTS)
    Type D2
        As Single x,y
    End Type
    Static As d2 cpt(),XY()
    Static As Long runflag
    If runflag=0 Then   
        Redim  XY(128,127)
        Redim cpt(1 To 64*2)
        Screen 8
        Width 640\8,200\16
        Dim As Ulong Pointer img
        Dim count As Long
        For ch As Long=1 To 127
            img=Imagecreate(640,200)
            Draw String img,(1,1),Chr(ch)
            For x As Long=1 To 8
                For y As Long=1 To 16
                    If Point(x,y,img)<>0 Then
                        count=count+1
                        XY(count,ch)=Type<D2>(x,y)
                    End If
                Next y
            Next x
            count=0
            Imagedestroy img
        Next ch
        runflag=1
    End If
    If size=0 Then Exit Sub
    Dim As D2 np,t
    #macro Scale(p1,p2,d)
    np.x=d*(p2.x-p1.x)+p1.x
    np.y=d*(p2.y-p1.y)+p1.y
    #endmacro
   
    Dim As D2 c=Type<D2>(xpos,ypos)
    Dim As Long dx=xpos,dy=ypos
    For z6 As Long=1 To Len(text)
        Var asci=text[z6-1]
        For _x1 As Long=1 To 64*2
            t=Type<D2>(XY(_x1,asci).x+dx,XY(_x1,asci).y+dy)         
            Scale(c,t,size)
            cpt(_x1)=np
           
            If XY(_x1,asci).x<>0 Then
                If Abs(size)>0 Then
                    glVertex3f (cpt(_x1).x,(cpt(_x1).y),0)
                End If
            End If
        Next _x1
        dx=dx+8
    Next z6
    glend
    glMatrixMode GL_PROJECTION 'restore
    glPopMatrix
    glMatrixMode GL_MODELVIEW
    glPopMatrix
End Sub

Sub inittext constructor
    drawstring(0,0,"",0,0,0,0)
End Sub


'NOW START OPENGL
glsetup(xres,yres)

Dim As Single angle
dim as long fps
Do
    angle=angle+1
    glClear(GL_COLOR_BUFFER_BIT)
 
     glEnable (GL_CULL_FACE)
     DrawGlcube(angle)
   
    drawstring (20,20,"OpenGL cube",rgb(255,100,0),2,xres,yres)
    drawstring (20,70,"Frames per second  = " &fps,rgb(255,255,255),1,xres,yres)
   
    Flip
    Sleep regulate(60,fps),1
Loop Until Inkey=Chr(27)

   
angros47
Posts: 1671
Joined: Jun 21, 2005 19:04

Re: Object oriented programming or 3d graphics?

Postby angros47 » Sep 16, 2020 0:02

Dodicat, glBegin...glEnd are commands deprecated from almost every version of OpenGL, so it's better not to teach OpenGL in that way
D.J.Peters
Posts: 8132
Joined: May 28, 2005 3:28
Contact:

Re: Object oriented programming or 3d graphics?

Postby D.J.Peters » Sep 16, 2020 10:11

@angros47 the fixed render pipeline are never removed and won't be removed at all.
(They can be emulated on OpenGL 4.4 devices via shader code inside the driver hidden for the end user)

OpenGL is a strong industry "standard" same as "PC" you can't remove the realmode from PC !
(without to lose the right to name your product PC compatible)

Many million dollar high end applications used the "old" OpenGL standard and won't accept it's removement.

But it's fun to learn how to code shaders and is a must for all mobile devices OpenGL ES 1,2,3 = WEB GL 1,2,3.
(Mobile devices like phones or touchpad's are not a "standard" and can be changed from year to year)

I worked for many big CAD companies in the past and I know with power they have In terms of standards and their full compliance.
(Same companies and the game industry sittin on the round table and define together with the manufacturer the OpenGL standard and it's future!)

Joshy
dodicat
Posts: 6645
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Object oriented programming or 3d graphics?

Postby dodicat » Sep 16, 2020 10:26

I had to download glut32.dll to even get started with this new opengl.
But it will be fun experimenting without the old routines.
IvanisIvan
Posts: 22
Joined: Nov 07, 2019 21:57

Re: Object oriented programming or 3d graphics?

Postby IvanisIvan » Sep 18, 2020 2:40

D.J.Peters wrote:@angros47 the fixed render pipeline are never removed and won't be removed at all.
(They can be emulated on OpenGL 4.4 devices via shader code inside the driver hidden for the end user)

OpenGL is a strong industry "standard" same as "PC" you can't remove the realmode from PC !
(without to lose the right to name your product PC compatible)

Many million dollar high end applications used the "old" OpenGL standard and won't accept it's removement.

But it's fun to learn how to code shaders and is a must for all mobile devices OpenGL ES 1,2,3 = WEB GL 1,2,3.
(Mobile devices like phones or touchpad's are not a "standard" and can be changed from year to year)

I worked for many big CAD companies in the past and I know with power they have In terms of standards and their full compliance.
(Same companies and the game industry sittin on the round table and define together with the manufacturer the OpenGL standard and it's future!)

Joshy

OpenGL sounds exciting! I am fairly new to programming and have heard that name tossed around a fair bit. I think it is time for me to delve further!
cwolf
Posts: 4
Joined: Sep 18, 2020 0:49

Re: Object oriented programming or 3d graphics?

Postby cwolf » Sep 18, 2020 21:49

dodicat wrote:OpenGL is a favourite.
There are plenty of examples and nothing needs downloaded.
Nearly every compiler can use openGL. Examples are all over the internet, it is a universal graphics machine.
Example:

Code: Select all


 #Include Once "GL/glu.bi"


Dim As Integer xres,yres
Screenres 1024,768,32,,2

Screeninfo xres,yres

Function Regulate(Byval MyFps As long,Byref fps As long) As long
    Static As Double timervalue,lastsleeptime,t3,frames
    Dim As Double t=Timer
    frames+=1
    If (t-t3)>=1 Then t3=t:fps=frames:frames=0
    Dim As long sleeptime=lastsleeptime+((1/myfps)-T+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    lastsleeptime=sleeptime
    timervalue=T
    Return sleeptime
End Function

Sub glsetup(xres as long,yres as long)
    glShadeModel(GL_SMOOTH)                 ' Enables Smooth Color Shading
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
    glBlendFunc(GL_ONE,GL_ONE)
    glEnable GL_ALPHA
    glEnable GL_BLEND
    glViewport(0, 0, xres, yres)       ' Set the viewport
    glMatrixMode(GL_PROJECTION)        ' Change Matrix Mode to Projection
    glLoadIdentity                     ' Reset View
    gluPerspective(45.0, xres/yres, 1.0, 100.0)
    glMatrixMode(GL_MODELVIEW)         ' Return to the modelview matrix
    glLoadIdentity                     '  Reset View
    glclearcolor 0,0,.5,1
End Sub

'Rotate and draw the cube
Sub DrawGlCube(Byref rotangle As Single)
    glLoadIdentity()
    glTranslatef(0,0,-5)
    glRotatef(rotangle,1,.5,.25)           ' Rotate
    glBegin(GL_QUADS)
   
    glcolor3ub 255,0,0
    glVertex3f( 1.0, 1.0,-1.0)            ' Top right of the quad (top)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top left of the quad (top)
    glVertex3f(-1.0, 1.0, 1.0)            ' Bottom left of the quad (top)
    glVertex3f( 1.0, 1.0, 1.0)            ' Bottom right of the quad (top)
   
    glcolor3ub 255,100,0
    glVertex3f( 1.0,-1.0, 1.0)            ' Top right of the quad (bottom)
    glVertex3f(-1.0,-1.0, 1.0)            ' Top left of the quad (bottom)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom left of the quad (bottom)
    glVertex3f( 1.0,-1.0,-1.0)            ' Bottom right of the quad (bottom)
 
    glcolor3ub 255,0,255
    glVertex3f( 1.0, 1.0, 1.0)            ' Top right of the quad (front)
    glVertex3f(-1.0, 1.0, 1.0)            ' Top left of the quad (front)
    glVertex3f(-1.0,-1.0, 1.0)            ' Bottom left of the quad (front)
    glVertex3f( 1.0,-1.0, 1.0)            ' Bottom right of the quad (front)
   
    glcolor3ub 0,0,200
    glVertex3f( 1.0,-1.0,-1.0)            ' Bottom left of the quad (back)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom right of the quad (back)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top right of the quad (back)
    glVertex3f( 1.0, 1.0,-1.0)            ' Top left of the quad (back)
   
    glcolor3ub 0,255,0
    glVertex3f(-1.0, 1.0, 1.0)            ' Top right of the quad (left)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top left of the quad (left)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom left of the quad (left)
    glVertex3f(-1.0,-1.0, 1.0)            ' Bottom right of the quad (left)
   
    glcolor3ub 255,0,100
    glVertex3f( 1.0, 1.0,-1.0)            ' Top right of the quad (right)
    glVertex3f( 1.0, 1.0, 1.0)            ' Top left of the quad (right)
    glVertex3f( 1.0,-1.0, 1.0)            ' Bottom left of the quad (right)
    glVertex3f( 1.0,-1.0,-1.0)
    glend
End Sub

Sub drawstring(xpos As Long,ypos As Long,text As String ,col As Ulong,size As Single,xres As Long,yres As Long) export
    glMatrixMode GL_PROJECTION 'save projection
    glPushMatrix
    glMatrixMode GL_MODELVIEW
    glPushMatrix
   
    glMatrixMode GL_PROJECTION 'make ortho
    glLoadIdentity
    glOrtho 0, xres, yres, 0,-1, 1
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
    #define Red(c) ((c) Shr 16 And 255)
    #define Green(c) ((c) Shr  8 And 255)
    #define Blue(c) ((c) And 255)
    #define Alph(c) ((c) Shr 24)
    glColor4ub Red(col),Green(col),Blue(col),alph(col)
    glend
    glpointsize(1.1*size)
    glBegin (GL_POINTS)
    Type D2
        As Single x,y
    End Type
    Static As d2 cpt(),XY()
    Static As Long runflag
    If runflag=0 Then   
        Redim  XY(128,127)
        Redim cpt(1 To 64*2)
        Screen 8
        Width 640\8,200\16
        Dim As Ulong Pointer img
        Dim count As Long
        For ch As Long=1 To 127
            img=Imagecreate(640,200)
            Draw String img,(1,1),Chr(ch)
            For x As Long=1 To 8
                For y As Long=1 To 16
                    If Point(x,y,img)<>0 Then
                        count=count+1
                        XY(count,ch)=Type<D2>(x,y)
                    End If
                Next y
            Next x
            count=0
            Imagedestroy img
        Next ch
        runflag=1
    End If
    If size=0 Then Exit Sub
    Dim As D2 np,t
    #macro Scale(p1,p2,d)
    np.x=d*(p2.x-p1.x)+p1.x
    np.y=d*(p2.y-p1.y)+p1.y
    #endmacro
   
    Dim As D2 c=Type<D2>(xpos,ypos)
    Dim As Long dx=xpos,dy=ypos
    For z6 As Long=1 To Len(text)
        Var asci=text[z6-1]
        For _x1 As Long=1 To 64*2
            t=Type<D2>(XY(_x1,asci).x+dx,XY(_x1,asci).y+dy)         
            Scale(c,t,size)
            cpt(_x1)=np
           
            If XY(_x1,asci).x<>0 Then
                If Abs(size)>0 Then
                    glVertex3f (cpt(_x1).x,(cpt(_x1).y),0)
                End If
            End If
        Next _x1
        dx=dx+8
    Next z6
    glend
    glMatrixMode GL_PROJECTION 'restore
    glPopMatrix
    glMatrixMode GL_MODELVIEW
    glPopMatrix
End Sub

Sub inittext constructor
    drawstring(0,0,"",0,0,0,0)
End Sub


'NOW START OPENGL
glsetup(xres,yres)

Dim As Single angle
dim as long fps
Do
    angle=angle+1
    glClear(GL_COLOR_BUFFER_BIT)
 
     glEnable (GL_CULL_FACE)
     DrawGlcube(angle)
   
    drawstring (20,20,"OpenGL cube",rgb(255,100,0),2,xres,yres)
    drawstring (20,70,"Frames per second  = " &fps,rgb(255,255,255),1,xres,yres)
   
    Flip
    Sleep regulate(60,fps),1
Loop Until Inkey=Chr(27)

   


dodicat, thank you SOOOOO much for that example. I'm guessing this is the smallest amount of code it would take to render an object in OpenGL

PLEASE PLEASE PLEASE dont keep quiet about what you learn with glut32.

angros47 wrote:Dodicat, glBegin...glEnd are commands deprecated from almost every version of OpenGL, so it's better not to teach OpenGL in that way


So what command should be used as an alternative?
paul doe
Posts: 1255
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Object oriented programming or 3d graphics?

Postby paul doe » Sep 19, 2020 0:29

cwolf wrote:...
So what command should be used as an alternative?

https://learnopengl.com/

A good start.
cwolf
Posts: 4
Joined: Sep 18, 2020 0:49

Re: Object oriented programming or 3d graphics?

Postby cwolf » Sep 19, 2020 1:45

paul doe wrote:
cwolf wrote:...
So what command should be used as an alternative?

https://learnopengl.com/

A good start.


Very funny.

Return to “General”

Who is online

Users browsing this forum: No registered users and 5 guests