About Monkey 2 › Forums › Monkey 2 Programming Help › shaders question?
This topic contains 3 replies, has 3 voices, and was last updated by 
 nerobot 1 year, 4 months ago.
- 
		AuthorPosts
 - 
		
			
				
December 5, 2017 at 2:28 am #12125
I was just looking at some of the threads about shaders and run into the thread started by @ethernaut were he post a link to a shader book and a link to some of examples converted to Monkey2. I tried to match them but they are completely different for example the the second example, the original shader illustrated:
[/crayon]Monkey123456789101112[crayon-5cba154577d25787369997 inline="true" ]#ifdef GL_ESprecision mediump float;#endifuniform float u_time;void main() {gl_FragColor = vec4(abs(sin(u_time)),0.0,0.0,1.0);}and the Monkey2 code looks like this:
[/crayon]Monkey12345678910111213141516171819202122232425262728293031323334353637383940[crayon-5cba154577d30425239849 inline="true" ]#Import "<std>"#Import "<mojo>"#Import "shaders/test02_color.glsl"Using std..Using mojo..'In this example, the shader takes the canvas.Color through the attribute "a_Color".'A color cycling is performed in the canvas.Color, not in the shader.Class MyWindow Extends WindowField img :ImageField testShader :ShaderMethod New()Super.New( "Shader test",1024,600,WindowFlags.Resizable )Local testShader := New Shader( "test02", LoadString("asset::test02_color.glsl"), "" )img = New Image( 512, 512, TextureFlags.FilterMipmap, testShader )img.Handle = New Vec2f( 0.5 )EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()Local flash := Abs( Sin(Millisecs()/500.0) ) 'Negative values won't look right, so we use Abs() to remove themcanvas.Color = New Color( flash, flash , flash )canvas.DrawImage( img, Width/2, Height/2 )EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()Endand this:
[/crayon]Monkey12345678910111213141516171819202122[crayon-5cba154577d3b245555353 inline="true" ]//@renderpasses 0varying vec4 v_Color;//@vertexattribute vec4 a_Position;attribute vec4 a_Color;uniform mat4 r_ModelViewProjectionMatrix;uniform vec4 m_ImageColor;void main(){v_Color=m_ImageColor * a_Color;gl_Position=r_ModelViewProjectionMatrix * a_Position;}//@fragmentvoid main() {gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 ) * v_Color ;}it makes the two completely different. for starters the monkey 2 version does not use the time variable or the trig functions inside the shader. I tried to use it the same way as the original and it doesn’t rotate the colors. I am guessing because the variable u_time doesn’t work with monkey2. This leads me to the conclusion that Monkey2 doesn’t pass variables to shaders?
If that is the case, does that make the shader a bit limited and/or slower?
this also makes the tutorial book posted by @ethernaut(https://thebookofshaders.com )in most cases almost impossible to duplicate with Monkey2. it’s obviously doable but in my opinion it doesn’t illustrate the full power of the shader.
?????????????????????????????????I really don’t know what I am doing. This is just a reflection of my ignorance.
December 5, 2017 at 3:27 am #12126Hi, Sorry if that was confusing! Maybe I should remove the reference to the book to make it less confusing.
I wasn’t trying to translate the book’s examples one by one, I was just experimenting with how to achieve certain things using Monkey2 (transparency, passing variables to the shader, using textures, etc) and was using the book as a rough guide.
For instance, example 2 uses Canvas.Color, which is something specific to Mojo and isn’t mentioned in the book, which is about WebGL using a javascript library. But as far as I can tell, all examples they have are completely doable in Monkey2.
I recommend simply doing the book’s examples in their website without worrying about Monkey2, then taking a peek at the M2 code just as examples on how to solve some specific problems using Mojo.
December 5, 2017 at 12:49 pm #12142Thanks, I have been playing around with the code, I can get the examples as they are in the book, and I can modify the code in the GLSL editor and get different results but I am having problems converting the code to Monkey2. I will keep trying but any help would really be appreciated.
December 5, 2017 at 2:55 pm #12144Mark recently had moved to opengl from gles2.0.
Now we have more possibilities.
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.