Заготовка для скринсейвера

Полноэкранная красивая анимация, выполненная с помощью OpenGL, подойдет для создания неплохого скринсейвера.
Платформы: Windows , Linux
Автор(ы): Richard Eric M. Lope, создано в 2006 году.

скринсейвер

'Line cube n GL?
'Easier than doing it in software. :*)
'Heirarchal transforms roxoors.
'Relsoft 2006
'Richard Eric M. Lope
'http://rel.betterwebber.com

''Option Explicit 
''$Static 
#INCLUDE "\gl\gl.bi" 

#INCLUDE "\gl\glu.bi" 

#INCLUDE "\gl\glfw.bi" 

#INCLUDE "\gl\glext.bi" 


Const SCR_WIDTH = 640                   'Width of the screen 
Const SCR_HEIGHT = 480                  'height of the screen 
Const BITSPP = 32                       '32 bits per pixel format 

Const GL_TEXT_WID = 512

Const GL_TEXT_HEI = 512


Const PI = 3.141593

Const TWOPI = (2 * PI) 



Type DisplayType 
    W As Uinteger 
    H As Uinteger 
    R_BITS As Uinteger 
    G_BITS As Uinteger 
    B_BITS As Uinteger 
    A_BITS As Uinteger 
    D_BITS As Uinteger 
    S_BITS As Uinteger 
    MODE As Uinteger 
    GlVer As Zstring Ptr 
    As Single FOVy, Aspect, zNear, zFar 
End Type 



Declare Sub Init_GL_Window( Display As DisplayType ) 
Declare Sub Init_GL_SCREEN() 
Declare Sub init_cube()


Dim Display As DisplayType 
Init_GL_Window Display 
Init_GL_SCREEN() 


Dim Shared As glUint CubeDL

init_cube()

Dim i As Integer

Dim theta As Integer

Dim elapsed_time As Single

Dim As Single ti, t, dt
Dim As Single time_start
time_start =  glfwGetTime()
Do 
    
    elapsed_time = glfwGetTime()
    ti = glfwGetTime() * 0.001208

    t = ti * 180.5 * (Sin(elapsed_time/3150) + 360 * Cos(elapsed_time/4150) + 90 * Sin(elapsed_time/3140))
    dt = 0.80130 + 2.0124 * (Sin(elapsed_time/40) * Sin(elapsed_time/240) * Cos(elapsed_time/540))
    GlClear GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT 
        
    GlMatrixMode GL_MODELVIEW 
    GlLoadIdentity 
    glPolygonMode(GL_FRONT, GL_LINE)
    glPolygonMode(GL_BACK, GL_LINE)
    glEnable (GL_BLEND)
    glBlendFunc(GL_ONE, GL_ONE)
    glDisable (GL_DEPTH_TEST)
    
    glTranslatef (0.0,0.0,-5.0)    
    glPushMatrix        
        glTranslatef (Sin(elapsed_time)* 1,Cos(elapsed_time)* 1,Cos(elapsed_time)+ Sin(elapsed_time)* 1)        
        theta = (theta + 1) Mod 360

        Dim As Single scale = 0.01

        'glRotatef (theta, 1,0,0)
        glRotatef (theta, 0,1,0)
        glRotatef (theta, 0,0,1)
        glScalef (scale,scale,scale)
        For i = 0 To 250

            glPushMatrix
            'theta += 1
            'glRotatef (sin((elapsed_time+theta)/100) * 180, 1,0,0)
            'glRotatef (sin((elapsed_time+theta)/120) * 90, 0,1,0)
            'glRotatef (sin((elapsed_time+theta)/150) * 360, 0,0,1)
            t = t + dt
            glRotatef ((t), 1,0,0)
            glRotatef ((t), 0,1,0)
            glRotatef ((t), 0,0,1)
            'glScalef (0.9,0.9,0.9)
            glCallList(CubeDL)
            glPopMatrix
            glScalef (1.0187,1.0187,1.0187)
        Next i
    glPopMatrix    
    Do 
    Loop Until glfwGetTime() - time_start >= (1/60) 
    time_start =  glfwGetTime()
    
    GlfwSwapBuffers 
Loop Until glfwGetKey( GLFW_KEY_ESC ) 


GlfwTerminate() 



Sub init_cube()
  Dim As Single col = 0.03

  CubeDL =glGenLists(1)
  glNewList(CubeDL, GL_COMPILE)
  glBegin(GL_QUADS)
    ' Front Face
    glColor3f(0.0, 0.0,col): glVertex3f(-1.0, -1.0,  1.0)
    glColor3f(col, 0.0,col): glVertex3f( 1.0, -1.0,  1.0)
    glColor3f(col, col,col): glVertex3f( 1.0,  1.0,  1.0)
    glColor3f(0.0, col,col): glVertex3f(-1.0,  1.0,  1.0)
    ' Back Face
    glColor3f(col, 0.0,col): glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(col, col,col): glVertex3f(-1.0,  1.0, -1.0)
    glColor3f(0.0, col,col): glVertex3f( 1.0,  1.0, -1.0)
    glColor3f(0.0, 0.0,col): glVertex3f( 1.0, -1.0, -1.0)
    ' Top Face
    glColor3f(0.0, col,col): glVertex3f(-1.0,  1.0, -1.0)
    glColor3f(0.0, 0.0,col): glVertex3f(-1.0,  1.0,  1.0)
    glColor3f(col, 0.0,col): glVertex3f( 1.0,  1.0,  1.0)
    glColor3f(col, col,col): glVertex3f( 1.0,  1.0, -1.0)
    ' Bottom Face
    glColor3f(col, col,col): glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(0.0, col,col): glVertex3f( 1.0, -1.0, -1.0)
    glColor3f(0.0, 0.0,col): glVertex3f( 1.0, -1.0,  1.0)
    glColor3f(col, 0.0,col): glVertex3f(-1.0, -1.0,  1.0)
    ' Right face
    glColor3f(col, 0.0,col): glVertex3f( 1.0, -1.0, -1.0)
    glColor3f(col, col,col): glVertex3f( 1.0,  1.0, -1.0)
    glColor3f(0.0, col,col): glVertex3f( 1.0,  1.0,  1.0)
    glColor3f(0.0, 0.0,col): glVertex3f( 1.0, -1.0,  1.0)
    ' Left Face
    glColor3f(0.0, 0.0,col): glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(col, 0.0,col): glVertex3f(-1.0, -1.0,  1.0)
    glColor3f(col, col,col): glVertex3f(-1.0,  1.0,  1.0)
    glColor3f(0.0, col,col): glVertex3f(-1.0,  1.0, -1.0)
  glEnd()
  glEndList()

End Sub






Sub Init_GL_Window( Display As DisplayType ) 
    Display.W     = 640 
    Display.H     = 480 
    Display.R_BITS= 8 
    Display.G_BITS= 8 
    Display.B_BITS= 8 
    Display.A_BITS= 8 
    Display.D_BITS= 32 
    Display.S_BITS= 8 
    Display.MODE  = GLFW_WINDOW 
    Display.MODE  = GLFW_FULLSCREEN
    
    If glfwInit() Then 
        'Successful! 
    Else    
        Print "Failed to initialize GLFW!" 
        Sleep 1000 
        End 
    End If 
    
    
    If  glfwOpenWindow( _ 
    Display.W     , _ 
    Display.H     , _ 
    Display.R_BITS, _ 
    Display.G_BITS, _ 
    Display.B_BITS, _ 
    Display.A_BITS, _ 
    Display.D_BITS, _ 
    Display.S_BITS, _ 
    Display.MODE  ) _ 
    Then 
    GlfwSwapInterval 1 
    Display.GlVer = glGetString(GL_VERSION) 
Else 
    GlfwTerminate() 
    End 
End If 

End Sub 


Sub Init_GL_SCREEN() 

   'screen information 
   Dim w As Integer, h As Integer 
    'OpenGL params for gluerspective 
   Dim FOVy As Double            'Field of view angle in Y 
   Dim Aspect As Double          'Aspect of screen 
   Dim znear As Double           'z-near clip distance 
   Dim zfar As Double            'z-far clip distance 

    'using screen info w and h as params 
   glViewport 0, 0, SCR_WIDTH,SCR_HEIGHT

   'Set current Mode to projection(ie: 3d) 
   glMatrixMode GL_PROJECTION 

    'Load identity matrix to projection matrix 
   glLoadIdentity 

    'Set gluPerspective params 
    FOVy = 80/2                                     '45 deg fovy 
    Aspect = SCR_WIDTH / SCR_HEIGHT                                  'aspect = x/y 
    znear = 1                                       'Near clip 
    zfar = 200                                      'far clip 

    'use glu Perspective to set our 3d frustum dimension up 
   gluPerspective FOVy, aspect, znear, zfar 

    'Modelview mode 
    'ie. Matrix that does things to anything we draw 
    'as in lines, points, tris, etc. 
   glMatrixMode GL_MODELVIEW 
   'load identity(clean) matrix to modelview 
   glLoadIdentity 

   glShadeModel GL_SMOOTH                 'set shading to smooth(try GL_FLAT) 
   glClearColor 0.0, 0.0, 0.0, 1.0        'set Clear color to BLACK 
   glClearDepth 1.0                       'Set Depth buffer to 1(z-Buffer) 
   glEnable GL_DEPTH_TEST                 'Enable Depth Testing so that our z-buffer works 

   'compare each incoming pixel z value with the z value present in the depth buffer 
   'LEQUAL means than pixel is drawn if the incoming z value is less than 
    'or equal to the stored z value 
   glDepthFunc GL_LEQUAL 

    'have one or more material parameters track the current color 
    'Material is your 3d model 
   glEnable GL_COLOR_MATERIAL 


    'Enable Texturing 
    glEnable GL_TEXTURE_2D 
    

    'Set blending parameters 
    glBlendFunc GL_SRC_ALPHA, GL_ONE


   'Tell openGL that we want the best possible perspective transform 
   glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST 
   
   'Enable Backface culling
   'GlEnable GL_CULL_FACE
   glPolygonMode(GL_FRONT, GL_FILL) 
    
    
        'lighting 
  Dim As glFloat mat_ambient(0 To 3) =  { 0.5, 0.5, 1.0, 0.15 }
  Dim As glFloat mat_specular(0 To 3) = {  1.0, 1.0, 1.0, 0.15 }  
  Dim As glFloat mat_shininess = 10.0

  Dim As glFloat light_position(0 To 3)= { -7.0, 8.0, -6.0, 1.0  }
  Dim As glFloat light_ambient(0 To 3)= { 0.5, 0.5, 0.5, 1.0 }
  Dim As glFloat mat_twister(0 To 3) = { 0.75, 0.75, 0.0, 1.0 }
  Dim As GLfloat fBrightLight(0 To 3) = { 1.0, 1.0, 1.0, 1.0 }
  glMaterialfv(GL_FRONT, GL_AMBIENT, @mat_ambient(0))
  glMaterialfv(GL_FRONT, GL_SPECULAR, @mat_specular(0))
  glMaterialfv(GL_FRONT, GL_SHININESS, @mat_shininess)
  glMaterialfv(GL_FRONT, GL_DIFFUSE,@mat_twister(0))
  glMaterialfv(GL_FRONT, GL_SPECULAR, @fBrightLight(0))


  glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR)
  glLightfv(GL_LIGHT0,GL_AMBIENT, @light_ambient(0))
  glLightfv(GL_LIGHT0,GL_DIFFUSE, @light_ambient(0))
  glLightfv(GL_LIGHT0, GL_POSITION, @light_position(0))
  'glEnable(GL_LIGHTING)
  'glEnable(GL_LIGHT0)
End Sub