
Shaders help
Posted Tuesday, 29 June, 2010 - 15:18 by Darwin226 inI've asked on some other forums and people weren't able to help me. I'm hoping you guys can tell me what's wrong with my code. (You can ignore the commented bits)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using OpenTK; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL; namespace OpenTK_3DTest { class World { public GameWindow Window; int frameCount = 0; int lastSecond = 0; System.Drawing.Point MouseLock; int MouseX, MouseY; float YAxisAngle = 0; float up, right; Point CameraPosition = Point.Zero; public List<DisplayObject> Meshes = new List<DisplayObject>(); Matrix4 projectionMatrix, modelviewMatrix; string vertexShaderSource = @" #version 120 varying vec3 normal; varying vec3 vertex_to_light_vector; void main() { // Transforming The Vertex gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; // Transforming The Normal To ModelView-Space normal = gl_NormalMatrix * gl_Normal; // Transforming The Vertex Position To ModelView-Space vec4 vertex_in_modelview_space = gl_ModelViewMatrix * gl_Vertex; vec3 light = vec3(20.0, 20.0, 0.0); // Calculating The Vector From The Vertex Position To The Light Position vertex_to_light_vector = vec3(light - vertex_in_modelview_space.xyz); }"; string fragmentShaderSource = @" #version 120 varying vec3 normal; varying vec3 vertex_to_light_vector; void main() { // Defining The Material Colors const vec4 AmbientColor = vec4(0.1, 0.1, 0.1, 1.0); const vec4 DiffuseColor = vec4(1.0, 1.0, 1.0, 1.0); // Scaling The Input Vector To Length 1 vec3 normalized_normal = normalize(normal); vec3 normalized_vertex_to_light_vector = normalize(vertex_to_light_vector); // Calculating The Diffuse Term And Clamping It To [0;1] float DiffuseTerm = clamp(dot(normal, vertex_to_light_vector), 0.0, 1.0); // Calculating The Final Color gl_FragColor = AmbientColor + DiffuseColor * DiffuseTerm; }"; public World () { Window = new OpenTK.GameWindow( 800, 600 ); Window.UpdateFrame += new EventHandler<FrameEventArgs>( UpdateFrame ); Window.RenderFrame += new EventHandler<FrameEventArgs>( RenderFrame ); Window.Load += new EventHandler<EventArgs>( OnLoad ); Window.VSync = VSyncMode.On; Window.Run(); } public void OnLoad ( object target, EventArgs args ) { Vertex TA = new Vertex( 0, 0, 0, Color.White ), TB = new Vertex( 10, 0, 0, Color.White ), TC = new Vertex( 10, 10, 0, Color.White ), TD = new Vertex( 0, 10, 0, Color.White ); Vertex BA = new Vertex( 0, 0, 10, Color.White ), BB = new Vertex( 10, 0, 10, Color.White ), BC = new Vertex( 10, 10, 10, Color.White ), BD = new Vertex( 0, 10, 10, Color.White ); DisplayObject cube = new DisplayObject(); cube.AddQuad( TA, TB, TC, TD ); cube.AddQuad( BA, BB, BC, BD ); cube.AddQuad( TA, TD, BD, BA ); cube.AddQuad( TA, TB, BB, BA ); cube.AddQuad( TD, TC, BC, BD ); cube.AddQuad( TB, TC, BC, BB ); MouseLock = new System.Drawing.Point( 400 + Window.Location.X, 300 + Window.Location.Y ); System.Windows.Forms.Cursor.Position = MouseLock; Meshes.Add( cube ); //Light stuff GL.Light( LightName.Light0, LightParameter.Position, new float[] { 0.0f, 20.0f, -0.5f } ); //GL.Light( LightName.Light0, LightParameter.Ambient, new float[] { 0.3f, 0.3f, 0.3f, 1.0f } ); //GL.Light( LightName.Light0, LightParameter.Diffuse, new float[] { 1.0f, 1.0f, 1.0f, 1.0f } ); //GL.Light( LightName.Light0, LightParameter.Specular, new float[] { 1.0f, 1.0f, 1.0f, 1.0f } ); //GL.Light( LightName.Light0, LightParameter.SpotExponent, new float[] { 1.0f, 1.0f, 1.0f, 1.0f } ); // Create shaders int vertexShaderHandle = GL.CreateShader( ShaderType.VertexShader ); int fragmentShaderHandle = GL.CreateShader( ShaderType.FragmentShader ); GL.ShaderSource( vertexShaderHandle, vertexShaderSource ); GL.ShaderSource( fragmentShaderHandle, fragmentShaderSource ); GL.CompileShader( vertexShaderHandle ); GL.CompileShader( fragmentShaderHandle ); Console.WriteLine( GL.GetShaderInfoLog( vertexShaderHandle ) ); Console.WriteLine( GL.GetShaderInfoLog( fragmentShaderHandle ) ); // Create program int shaderProgramHandle = GL.CreateProgram(); GL.AttachShader( shaderProgramHandle, vertexShaderHandle ); GL.AttachShader( shaderProgramHandle, fragmentShaderHandle ); GL.LinkProgram( shaderProgramHandle ); Console.WriteLine( GL.GetProgramInfoLog( shaderProgramHandle ) ); GL.UseProgram( shaderProgramHandle ); //Matrix stuff GL.ClearColor( Color4.Black ); GL.Viewport( 0, 0, 800, 600 ); float aspectRatio = 800 / (float)( 600 ); projectionMatrix = Matrix4.CreatePerspectiveFieldOfView( (float)Math.PI / 4, aspectRatio, 1, 1000 ); GL.MatrixMode( MatrixMode.Projection ); GL.LoadMatrix( ref projectionMatrix ); modelviewMatrix = Matrix4.LookAt( new Vector3( 5, 5, -20 ), new Vector3( 5, 5, 0 ), new Vector3( 0, -1, 0 ) ); GL.MatrixMode( MatrixMode.Modelview ); GL.LoadMatrix( ref modelviewMatrix ); GL.Enable( EnableCap.DepthTest ); } public void UpdateFrame ( object target, FrameEventArgs args ) { MouseX = System.Windows.Forms.Cursor.Position.X; MouseY = System.Windows.Forms.Cursor.Position.Y; float MouseOffsetX = MouseX - MouseLock.X, MouseOffsetY = MouseY - MouseLock.Y; System.Windows.Forms.Cursor.Position = MouseLock; YAxisAngle += MouseOffsetX / 5; Point xaxis = Geom.Polar( Geom.DegreesToRadians( YAxisAngle ), 1 ); GL.MatrixMode( MatrixMode.Projection ); //Matrix4 translation = Matrix4.CreateTranslation( -(float)CameraPosition.X, 0, -(float)CameraPosition.Y ); //Matrix4.Mult( ref projectionMatrix, ref translation, out projectionMatrix ); GL.Translate( -(float)CameraPosition.X, 0, -(float)CameraPosition.Y ); //Matrix4 rotation = Matrix4.CreateRotationY( (float)MouseOffsetX / 180F ); //Matrix4.Mult( ref projectionMatrix, ref rotation, out projectionMatrix ); GL.Rotate( (float)MouseOffsetX / 5F, new Vector3d( 0, 1, 0 ) ); //rotation = Matrix4.CreateFromAxisAngle( new Vector3( (float)xaxis.X, 0, (float)xaxis.Y ), (float)MouseOffsetY / 180F ); //Matrix4.Mult( ref projectionMatrix, ref rotation, out projectionMatrix ); GL.Rotate( (float)MouseOffsetY / 5F, new Vector3( (float)xaxis.X, 0, (float)xaxis.Y ) ); //translation = Matrix4.CreateTranslation( (float)CameraPosition.X, 0, (float)CameraPosition.Y ); //Matrix4.Mult( ref projectionMatrix, ref translation, out projectionMatrix ); GL.Translate( (float)CameraPosition.X, 0, (float)CameraPosition.Y ); up = 0; if ( Window.Keyboard[ OpenTK.Input.Key.Up ] ) up = -0.3F; if ( Window.Keyboard[ OpenTK.Input.Key.Down ] ) up = +0.3F; right = 0; if ( Window.Keyboard[ OpenTK.Input.Key.Right ] ) right = -0.3F; if ( Window.Keyboard[ OpenTK.Input.Key.Left ] ) right = +0.3F; Point direction = Geom.Polar( Geom.DegreesToRadians( YAxisAngle - 90 ), 1 ); CameraPosition.X += direction.X * up + xaxis.X * right; CameraPosition.Y += direction.Y * up + xaxis.Y * right; //projectionMatrix *= Matrix4.CreateTranslation( (float)direction.X * up, 0, (float)direction.Y * up ); //projectionMatrix *= Matrix4.CreateTranslation( (float)xaxis.X * right, 0, (float)xaxis.Y * right ); GL.Translate( (float)direction.X * up, 0, (float)direction.Y * up ); GL.Translate( (float)xaxis.X * right, 0, (float)xaxis.Y * right ); //GL.LoadMatrix( ref projectionMatrix ); //GL.MatrixMode( MatrixMode.Modelview ); //GL.LoadMatrix( ref modelviewMatrix ); //FRAME COUNTER frameCount++; if ( lastSecond != DateTime.Now.Second ) { lastSecond = DateTime.Now.Second; Window.Title = "OpenTK_3DTest FPS: " + frameCount; frameCount = 0; } if ( Window.Keyboard[ OpenTK.Input.Key.Escape ] ) Window.Exit(); } public void RenderFrame ( object target, FrameEventArgs args ) { GL.Clear( ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit ); GL.Begin( BeginMode.Triangles ); int i = 0; while ( i < Meshes.Count ) { Meshes[ i ].Render(); ++i; } GL.End(); Window.SwapBuffers(); } } }


Comments
Re: Shaders help
What is the expected output and what are you actually getting? (error/crash/invalid rendering/other?)
May I suggest running with the debug version of OpenTK.dll, which will inform you of any OpenGL errors automatically? (You can find the debug version under "opentk-1.0/Binaries/OpenTK/Debug").
Re: Shaders help
It was supposed to be a diffuse shader from this site but then I got some strange shader error at the
vertex_to_light_vector = vec3(gl_LightSource[0].position – vertex_in_modelview_space);
So I just replaced the gl_LightSource[0].position with vec3(20.0, 20.0, 0.0)
This shader doesn't throw any errors, it just doesn't work as it should. All I see if a barely lit cube.
Re: Shaders help
Does anyone have an example like this for VB.NET.
My goal is to add a 3D preview window into a CAD wrapper using the GLControl class. I have a tessellated model loaded and now I need to add shading and eventually have some of the objects be translucent.
I am a total newb in OpenGL and you wrapper.
Thanks,