About Monkey 2 › Forums › Monkey 2 Code Library › Basic opengl 3d code (complete)
This topic contains 2 replies, has 2 voices, and was last updated by 
 AdamStrange
 1 year, 9 months ago.
		Viewing 3 posts - 1 through 3 (of 3 total)
	
	- 
		AuthorPosts
 - 
		
			
				
June 24, 2017 at 6:07 am #8888
Here’s the basic framework of an opengl 3d system including shader support.
it’s very small and very simple code to play with for anyone interested.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156#import "rameses3d"#import "assets/grid.3do"'the size of our windowConst Size := New Vec2i( 950,720 )'And some nice stuffconst PI:float = 3.14159265359const PIHALF:float = PI * 0.5const SHOW_TEX:float = 1const HIDE_TEX:float = 0const USE_LIGHT:float = 1const NO_LIGHT:float = 0Class MyWindow Extends GLWindow'this is the 3d systemfield my3d:Rameses3d'we need at lest one shaderfield myShader:GLShader'and this is the 3d objectfield r3d_Grid:int'we dont need this, but it is the texture that can be fed into rameses3dfield tex:TextureGLMethod New()'create a windowSuper.New( "My Window", Size.X, Size.Y, WindowFlags.Resizable )'this is a timer. it will 'tick' 60 times a second. calling OnUpdate_timer = New Timer( 60, OnUpdate )'if you want a constant canvas size use this' Layout = "letterbox"'or If you want the canvas to always fill the window use thisLayout = "fill"'get the shaders. we are not using any user defined ones, but we wil need to get a basic oneInitShaders()'next initalize Ramesesmy3d = New Rameses3d( Width, Height )'and set a light. Think of you the observer as position 0, 0, 0.' with x being left/right' y being up/down' and z being in/out z starts at 0, and goes minus away from you, plus towards you' so... 0, 1, -1 is centered, above and into the screenmy3d.SetLight( 0, 1, -2 )'load the texture - we are not using' tex = TextureGL.Load( "asset::ship32x32_font.png", TextureFlagsGL.Dynamic )'now we load the 3d shape from the assetsr3d_Grid = my3d.Load( "asset::grid.3do", myShader )'and finally some opengl'set the clear color of the screen to blackglClearColor(0, 0, 0, 1.0)'cull the face drawing and add depthglEnable(GL_CULL_FACE)glEnable(GL_DEPTH_TEST)Endmethod InitShaders()'first get the basic internal shader from Rameses3dmyShader = New GLShader()End methodMethod OnUpdate()'this 'ticks' at 60 fps'use it For your master updating system_gameTime = Millisecs()'lets update the rotation_objectRotate += 0.005'finally call everything to render'the render is done:OnRenderGL first then standard monkey2 OnRenderRequestRender()End MethodMethod OnRender(canvas:Canvas) OverrideApp.RequestRender()'this will call the opengl renderSuper.OnRender(canvas)canvas.DrawText( "FPS="+App.FPS,Width,0,1,0 )canvas.DrawText("Monkey2 OpenGL", 10, 10)canvas.Color = Color.Bluecanvas.DrawRect( 210, 10, 10, 200 )EndMethod OnRenderGL() Override'clear the screen buffersglClearStencil( 0 )glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT )'Drawing area. You're always drawing to the entire window in GL regardless of Layout.glViewport( 0, 0 ,Frame.Width, Frame.Height )'initalize the texture - we are not using' local texint:GLuint = tex.GLTexture' tex.Bind( 0, TextureFilterGL.None )Local scale:float = 1.618'here's the format for the following drawing commands:'method DrawModel( model:int, x:float, y:float, z:float, lightMix:float, textureMix:float, baseColor:Color, scale:float = 1, yrot:float = 0, xrot:float = 0, zrot:float = 0 )my3d.DrawLines( r3d_Grid, 0, -1.5, -3.7, NO_LIGHT, HIDE_TEX, Color.Cyan, scale, 0 )my3d.DrawModel( r3d_Grid, 0, -1, -3.7, NO_LIGHT, HIDE_TEX, Color.Orange, scale*0.5, _objectRotate, _objectRotate )my3d.DrawLines( r3d_Grid, 0, -.5, -3.7, NO_LIGHT, HIDE_TEX, Color.Red, scale*0.25, 0, -_objectRotate )Endprivate'the timer ticks 60 times a second and handles the screen updateField _timer:Timer'global game time derrived from a single call to Millisecs()field _gameTime:intfield _objectRotate:float = 0EndFunction Main()Local cfg := New StringMap<String>cfg["GL_depth_buffer_enabled"] = 1cfg["GL_stencil_buffer_enabled"] = 1New AppInstance( cfg )New MyWindowApp.Run()EndI’ve included all the files needed including some .3do files from my Rameses Modeler
Attachments:
June 28, 2017 at 6:29 pm #8997Awesome Adam!
Time to breakout my Intel MAC!!!!!!!!!
June 28, 2017 at 8:36 pm #8999just been testing it on the latest release.
if you have a problem with textureGL just change any references to Texture (and remove the import from Rameses.monkey2)
Marks new code will work fine
 - 
		AuthorPosts
 
		Viewing 3 posts - 1 through 3 (of 3 total)
	
	You must be logged in to reply to this topic.