Mixing Monkey with OpenGl
Monkey Targets Forums/Desktop/Mixing Monkey with OpenGl
| ||
| Hi all, this time i want to show you a way to mix your monkey code with some pure OpenGl code. sometimes you want to make a certain thing that con not done with monkey only, so this demo for you. the code =========
#GLFW_USE_MINGW=False
'Ok, looks a bit crap and doesn't make much sense, but it's just a test of offset, pitch, byte order etc...
'
Import mojo
! #include <D:\MonkeyX\bananas\mak\dynamicimage\dynamicimage.buildv77a\glfw\glfw\include\GL/gl.h>
Class MyApp Extends App
Field img:Image
Method OnCreate()
SetUpdateRate 60
End
Field fullscreen:=False
Method OnUpdate()
#If TARGET="glfw"
If KeyHit( KEY_SPACE )
fullscreen=Not fullscreen
If fullscreen
GlfwGame.GetGlfwGame().SetGlfwWindow( 1024,768,8,8,8,0,0,0,True )
Else
GlfwGame.GetGlfwGame().SetGlfwWindow( 640,480,8,8,8,0,0,0,False )
Endif
End
#Endif
end
Method OnRender()
If Not img
Local w=256,h=256
img=CreateImage(w,h,1,Image.MidHandle)
Cls 128,0,255
SetColor 255,128,0 'Orange!
DrawRect 16,16,w-32,h-32
SetColor 255,255,255
DrawText "Hello World",w/2,h/2,.5,.5
Local buf:=New Int[w*2*h]
ReadPixels buf,0,0,w,h ,w,w*2
For Local i:=0 Until w*h*2
If i<w*h*2/3
buf[i]|=$ff0000
Else If i<w*h*4/3
buf[i]|=$00ff00
Else
buf[i]|=$0000ff
Endif
Next
For Local i=0 Until h
For Local j=0 Until w*2
buf[i*w*2+j]=buf[i*w*2+j] & $ffffff | (i*255/h) Shl 24
Next
Next
img.WritePixels buf,0,0,w,h ,w,w*2
End
Cls
Scale 2,2
SetColor 255,255,255
DrawImage img,MouseX/2.0,MouseY/2.0
DrawText "Testing...",0,0
inlineC
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef( 200, 100, 0);
glBegin(GL_QUADS);
glColor3f(1,0,1);
glTexCoord2f(0.0, 0.0); glVertex3f(-60.0, -30.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-60.0, 30.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f( 60.0, 30.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f( 60.0, -30.0, 0.0);
glEnd();
glPopMatrix();
endC
End
End
Function Main()
New MyApp
End
|
| ||
| You shouldn't use the fixed function pipeline, it breaks portability. |
| ||
| ok, i added a new code that uses glDrawArrays for the sake of portability. But as i said above , for win32 desktop we can use some codes that not found in monkey opengl . so that i post the topic here. the code ========
#GLFW_USE_MINGW=False
'Ok, looks a bit crap and doesn't make much sense, but it's just a test of offset, pitch, byte order etc...
'
Import mojo
Import opengl.gles11
! #include <D:\MonkeyX\modules\opengl/gl.h>
Class MyApp Extends App
Field img:Image
Method OnCreate()
SetUpdateRate 60
End
Field fullscreen:=False
Method OnUpdate()
#If TARGET="glfw"
If KeyHit( KEY_SPACE )
fullscreen=Not fullscreen
If fullscreen
GlfwGame.GetGlfwGame().SetGlfwWindow( 1024,768,8,8,8,0,0,0,True )
Else
GlfwGame.GetGlfwGame().SetGlfwWindow( 640,480,8,8,8,0,0,0,False )
Endif
End
#Endif
end
Method OnRender()
If Not img
Local w=256,h=256
img=CreateImage(w,h,1,Image.MidHandle)
Cls 128,0,255
SetColor 255,128,0 'Orange!
DrawRect 16,16,w-32,h-32
SetColor 255,255,255
DrawText "Hello World",w/2,h/2,.5,.5
Local buf:=New Int[w*2*h]
ReadPixels buf,0,0,w,h ,w,w*2
For Local i:=0 Until w*h*2
If i<w*h*2/3
buf[i]|=$ff0000
Else If i<w*h*4/3
buf[i]|=$00ff00
Else
buf[i]|=$0000ff
Endif
Next
For Local i=0 Until h
For Local j=0 Until w*2
buf[i*w*2+j]=buf[i*w*2+j] & $ffffff | (i*255/h) Shl 24
Next
Next
img.WritePixels buf,0,0,w,h ,w,w*2
End
Cls
Scale 2,2
SetColor 255,255,255
DrawImage img,MouseX/2.0,MouseY/2.0
DrawText "Testing...",0,0
' using inlin c to pass code to cpp
' using GLBegin-GLEnd
inlineC
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef( 200, 100, 0);
glBegin(GL_QUADS);
glColor3f(1,0,1);
glTexCoord2f(0.0, 0.0); glVertex3f(-60.0, -30.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-60.0, 30.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f( 60.0, 30.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f( 60.0, -30.0, 0.0);
glEnd();
glPopMatrix();
endC
' using imported opengl module
' using GLDrawArrays
Local vert:Float[] = [-60.0, -30.0, 0.0 , -60.0, 30.0, 0.0 , 60.0, 30.0, 0.0 , 60.0, -30.0, 0.0]
Local txtCord:Float[] = [0.0,0.0, 0.0,1.0, 1.0,1.0, 1.0,0.0]
glMatrixMode(GL_MODELVIEW )
glPushMatrix()
glTranslatef( 400, 100, 0)
glColor4f(1,1,1,1)
glEnableClientState( GL_VERTEX_ARRAY )
glEnableClientState( GL_TEXTURE_COORD_ARRAY )
!glVertexPointer( 3,GL_FLOAT,0,(Const Void*)&t_vert[0])
!glTexCoordPointer( 2,GL_FLOAT,0,(Const Void*)&t_txtCord[0])
!glDrawArrays(GL_TRIANGLE_FAN, 0, 4)
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_TEXTURE_COORD_ARRAY)
glPopMatrix();
End
End
Function Main()
New MyApp
End
|