''------------------------------------------------------------------------------
''
''  This Code Was Created By Jeff Molofee 2000
''  And Modified By Giuseppe D'Agata (waveform@tiscalinet.it)
''  If You've Found This Code Useful, Please Let Me Know.
''  Visit My Site At nehe.gamedev.net
''

''------------------------------------------------------------------------------
'' Use Space Bar to toggle bitmaps and M on keybpard to toggle masking
''
''------------------------------------------------------------------------------

$TYPECHECK ON
DECLARE SUB FormCreate
DECLARE SUB FormKeyDown (Key AS Word, KeyShift AS INTEGER)
DECLARE SUB FormSize
DECLARE SUB RenderIt
DECLARE function LoadGLTextures() as integer


$INCLUDE <RapidQ2.inc>
$include <GL/gl.inc>
$include <GL/glu.inc>
$INCLUDE <GL/QGL.inc>

	'' Global variable
dim texture(5) as dword        '' Storage For Our Five Textures
	'' Local variables
dim roll    as single                    '' Rolling Texture
dim masking as integer                   '' Masking On/Off
dim scene   as integer                   '' Which Scene To Draw
dim sm      as integer, sp as integer    '' flags to indicate key states

DIM GL AS QGL
'-- user interface here
create tmx as QTimer
  interval = 1
  enabled  = 1
  onTimer  = RenderIt
end create


create Form as QFORM
  caption       = "OpenGL Mouse Demo"
  center
  width         = 640
  height        = 480
  onshow        = FormCreate
  onkeydown     = FormKeyDown
  onresize      = FormSize
end create

Form.showmodal
end



SUB FormKeyDown (Key AS Word, KeyShift AS INTEGER) 
    SELECT CASE Key
        CASE 27
            GL.Close
            Form.close
        CASE ASC("m")
        	if sm  = False then masking = not masking : sm = true
        CASE 32 'space bar
            IF sp = false then scene = not scene : sp = true
        CASE ELSE
            sm = false
            sp = false
    END SELECT
END SUB


SUB FormSize
  GL.Resize(form.clientWidth, form.clientHeight)
END SUB



SUB FormCreate
    GL.Init(Form)
	if LoadGLTextures() = False then     '' Jump To Texture Loading Routine
	  Application.terminate              '' If Texture Didn't Load Quit
	end if
	
	glClearColor(0.0, 0.0, 0.0, 0.0)     '' Clear The Background Color To Black
	glClearDepth(1.0)                    '' Enables Clearing Of The Depth Buffer
	glEnable(GL_DEPTH_TEST)              '' Enable Depth Testing
	glShadeModel(GL_SMOOTH)              '' Enables Smooth Color Shading
	glEnable(GL_TEXTURE_2D)              '' Enable 2D Texture Mapping
	'' Set masking as default
	masking = true
	
END SUB

SUB RenderIt
	glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)      '' Clear The Screen And The Depth Buffer
	glLoadIdentity                                         '' Reset The Modelview Matrix
	glTranslatef(0.0,0.0,-2.0)                               '' Move Into The Screen 5 Units

	glBindTexture(GL_TEXTURE_2D, texture(0))                 '' Select Our Logo Texture
	glBegin(GL_QUADS)                    '' Start Drawing A Textured Quad
		glTexCoord2f(0.0, -roll+0.0): glVertex3f(-1.1, -1.1,  0.0)    '' Bottom Left
		glTexCoord2f(3.0, -roll+0.0): glVertex3f( 1.1, -1.1,  0.0)    '' Bottom Right
		glTexCoord2f(3.0, -roll+3.0): glVertex3f( 1.1,  1.1,  0.0)    '' Top Right
		glTexCoord2f(0.0, -roll+3.0): glVertex3f(-1.1,  1.1,  0.0)    '' Top Left
	glEnd                              '' Done Drawing The Quad

	glEnable(GL_BLEND)                   '' Enable Blending
	glDisable(GL_DEPTH_TEST)             '' Disable Depth Testing

	if masking then                      '' Is Masking Enabled?
		glBlendFunc(GL_DST_COLOR,GL_ZERO)  '' Blend Screen Color With Zero (Black)
	end if

	if (scene) then                      '' Are We Drawing The Second Scene?
		glTranslatef(0.0,0.0,-1.0)         '' Translate Into The Screen One Unit
		glRotatef(roll*360,0.0,0.0,1.0)    '' Rotate On The Z Axis 360 Degrees.
		if masking then                    '' Is Masking On?
			glBindTexture(GL_TEXTURE_2D, texture(3))          '' Select The Second Mask Texture
			glBegin(GL_QUADS)           '' Start Drawing A Textured Quad
				glTexCoord2f(0.0, 0.0): glVertex3f(-1.1, -1.1,  0.0)    '' Bottom Left
				glTexCoord2f(1.0, 0.0): glVertex3f( 1.1, -1.1,  0.0)    '' Bottom Right
				glTexCoord2f(1.0, 1.0): glVertex3f( 1.1,  1.1,  0.0)    '' Top Right
				glTexCoord2f(0.0, 1.0): glVertex3f(-1.1,  1.1,  0.0)    '' Top Left
			glEnd                     '' Done Drawing The Quad
		end if

		glBlendFunc(GL_ONE, GL_ONE)                          '' Copy Image 2 Color To The Screen
		glBindTexture(GL_TEXTURE_2D, texture(4))             '' Select The Second Image Texture
		glBegin(GL_QUADS)             '' Start Drawing A Textured Quad
			glTexCoord2f(0.0, 0.0): glVertex3f(-1.1, -1.1,  0.0)    '' Bottom Left
			glTexCoord2f(1.0, 0.0): glVertex3f( 1.1, -1.1,  0.0)    '' Bottom Right
			glTexCoord2f(1.0, 1.0): glVertex3f( 1.1,  1.1,  0.0)    '' Top Right
			glTexCoord2f(0.0, 1.0): glVertex3f(-1.1,  1.1,  0.0)    '' Top Left
		glEnd                       '' Done Drawing The Quad

	else                            '' Otherwise

 		if masking then             '' Is Masking On?
 			glBindTexture(GL_TEXTURE_2D, texture(1))          '' Select The First Mask Texture
 			glBegin(GL_QUADS)           '' Start Drawing A Textured Quad
 				glTexCoord2f(roll+0.0, 0.0): glVertex3f(-1.1, -1.1,  0.0)    '' Bottom Left
 				glTexCoord2f(roll+4.0, 0.0): glVertex3f( 1.1, -1.1,  0.0)    '' Bottom Right
 				glTexCoord2f(roll+4.0, 4.0): glVertex3f( 1.1,  1.1,  0.0)    '' Top Right
 				glTexCoord2f(roll+0.0, 4.0): glVertex3f(-1.1,  1.1,  0.0)    '' Top Left
 			glEnd                     '' Done Drawing The Quad
 		end if
 
 		glBlendFunc(GL_ONE, GL_ONE)                          '' Copy Image 1 Color To The Screen
 		glBindTexture(GL_TEXTURE_2D, texture(2))             '' Select The First Image Texture
 		glBegin(GL_QUADS)             '' Start Drawing A Textured Quad
 			glTexCoord2f(roll+0.0, 0.0): glVertex3f(-1.1, -1.1,  0.0)    '' Bottom Left
 			glTexCoord2f(roll+4.0, 0.0): glVertex3f( 1.1, -1.1,  0.0)    '' Bottom Right
 			glTexCoord2f(roll+4.0, 4.0): glVertex3f( 1.1,  1.1,  0.0)    '' Top Right
 			glTexCoord2f(roll+0.0, 4.0): glVertex3f(-1.1,  1.1,  0.0)    '' Top Left
 		glEnd                        '' Done Drawing The Quad
	end if

	glEnable(GL_DEPTH_TEST)          '' Enable Depth Testing
	glDisable(GL_BLEND)              '' Disable Blending

	roll = roll + 0.002              '' Increase Our Texture Roll Variable
	if (roll > 1.0) then             '' Is Roll Greater Than One
		roll = roll - 1.0              '' Subtract 1 From Roll
	end if
	GL.flip
Form.Draw(0,0, QGLinternalBMP.BMP)
END SUB



'' Load Bitmaps And Convert To Textures
function LoadGLTextures() as integer

  texture(0)=GL.CreateTextureFromFile("Logo.bmp")                '' Logo Texture
  texture(1)=GL.CreateTextureFromFile("Mask1.bmp")               '' First Mask
  texture(2)=GL.CreateTextureFromFile("Image1.bmp")              '' First Image
  texture(3)=GL.CreateTextureFromFile("Mask2.bmp")               '' Second Mask
  texture(4)=GL.CreateTextureFromFile("Image2.bmp")              '' Second Image

   if (texture(0) = 0 or _
   	  texture(1) = 0 or _
   	  texture(2) = 0 or _
   	  texture(3) = 0 or _
   	  texture(4) = 0) then Showmessage "texture loading error"
     glGenTextures(5, texture(0))     '' Create Five Textures
end function


















