public override void Render() { Vector3 eyePos = new Vector3(); eyePos.X = cameraAngle.Z * -(float)Math.Sin(cameraAngle.X * rads * (float)Math.Cos(cameraAngle.Y * rads)); eyePos.Y = cameraAngle.Z * -(float)Math.Sin(cameraAngle.Y * rads); eyePos.Z = -cameraAngle.Z * (float)Math.Cos(cameraAngle.X * rads * (float)Math.Cos(cameraAngle.Y * rads)); Matrix4 lookAt = Matrix4.LookAt(eyePos, new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f)); GL.LoadMatrix(Matrix4.Transpose(lookAt).Matrix); float[] pos = new float[] { 0.0f, 3.0f, 3.0f, 1.0f }; GL.Light(LightName.Light0, LightParameter.Position, pos); grid.Render(6); GL.Disable(EnableCap.Lighting); GL.PushMatrix(); { GL.Translate(pos[0], pos[1], pos[2]); GL.Scale(0.25f, 0.25f, 0.25f); GL.Color3(1.0f, 1.0f, 0.0f); Primitives.DrawSphere(); GL.Begin(PrimitiveType.Lines); GL.Vertex3(0.0f, 0.0f, 0.0f); Vector3 lightDirection = new Vector3(-2.0f, -1.0f, -3.0f); lightDirection.Normalize(); GL.Vertex3(lightDirection[0] * 5.0f, lightDirection[1] * 5.0f, lightDirection[2] * 5.0f); GL.End(); } GL.PopMatrix(); GL.Enable(EnableCap.Lighting); GL.Color3(0.0f, 1.0f, 0.0f); GL.PushMatrix(); { GL.Translate(0.0f, 2.5f, -2.0f); Primitives.Torus(); } GL.PopMatrix(); GL.Color3(1.0f, 0.0f, 0.0f); GL.PushMatrix(); { GL.Translate(2.5f, 1.0f, -0.5f); Primitives.DrawSphere(); } GL.PopMatrix(); GL.Color3(0.0f, 0.0f, 1.0f); GL.PushMatrix(); { GL.Translate(-1.0f, 0.5f, 0.5f); Primitives.Cube(); } GL.PopMatrix(); }
public override void Render() { Vector3 eyePos = new Vector3(); eyePos.X = cameraAngle.Z * -(float)Math.Sin(cameraAngle.X * rads * (float)Math.Cos(cameraAngle.Y * rads)); eyePos.Y = cameraAngle.Z * -(float)Math.Sin(cameraAngle.Y * rads); eyePos.Z = -cameraAngle.Z * (float)Math.Cos(cameraAngle.X * rads * (float)Math.Cos(cameraAngle.Y * rads)); Matrix4 lookAt = Matrix4.LookAt(eyePos, new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f)); GL.LoadMatrix(Matrix4.Transpose(lookAt).Matrix); grid.Render(); GL.Color3(0.0f, 1.0f, 0.0f); GL.PushMatrix(); { GL.Translate(0.0f, 2.5f, -2.0f); Primitives.Torus(); } GL.PopMatrix(); GL.Color3(1.0f, 0.0f, 0.0f); GL.PushMatrix(); { GL.Translate(2.5f, 1.0f, -0.5f); Primitives.DrawSphere(); } GL.PopMatrix(); GL.Color3(0.0f, 0.0f, 1.0f); GL.PushMatrix(); { GL.Translate(-1.0f, 0.5f, 0.5f); Primitives.Cube(); } GL.PopMatrix(); }
public override void Render() { Vector3 eyePos = new Vector3(); eyePos.X = cameraAngle.Z * -(float)Math.Sin(cameraAngle.X * rads * (float)Math.Cos(cameraAngle.Y * rads)); eyePos.Y = cameraAngle.Z * -(float)Math.Sin(cameraAngle.Y * rads); eyePos.Z = -cameraAngle.Z * (float)Math.Cos(cameraAngle.X * rads * (float)Math.Cos(cameraAngle.Y * rads)); Matrix4 lookAt = Matrix4.LookAt(eyePos, new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f)); GL.LoadMatrix(Matrix4.Transpose(lookAt).Matrix); //set light position //w of 1 makes the light a point light float[] position = new float[] { 0.5f, 1.0f, 0.5f, 1.0f }; GL.Light(LightName.Light0, LightParameter.Position, position); grid.Render(3); //set ray debug position Vector3 redPosition = new Vector3(); redPosition.X = 1.0f * -(float)Math.Sin(redAngle.X * rads) * (float)Math.Cos(redAngle.Y * rads); redPosition.Y = 1.0f * -(float)Math.Sin(redAngle.Y * rads); redPosition.Z = -1.0f * (float)Math.Cos(redAngle.X * rads) * (float)Math.Cos(redAngle.Y * rads); Vector3 greenPosition = new Vector3(); greenPosition.X = 1.0f * -(float)Math.Sin(greenAngle.X * rads) * (float)Math.Cos(greenAngle.Y * rads); greenPosition.Y = 1.0f * -(float)Math.Sin(greenAngle.Y * rads); greenPosition.Z = -1.0f * (float)Math.Cos(greenAngle.X * rads) * (float)Math.Cos(greenAngle.Y * rads); //add debug visualization GL.Disable(EnableCap.Lighting); GL.PushMatrix(); GL.Translate(4f, 4f, 0f); GL.Begin(PrimitiveType.Lines); //draw red ray GL.Color3(1.0f, 0.0f, 0.0f); GL.Vertex3(0f, 0f, 0f); redPosition.Normalize(); redPosition *= -1.0f; GL.Vertex3(redPosition.X, redPosition.Y, redPosition.Z); //draw green ray GL.Color3(0.0f, 1.0f, 0.0f); GL.Vertex3(0.0f, 0.0f, 0.0f); greenPosition.Normalize(); greenPosition *= -2.0f; GL.Vertex3(greenPosition.X, greenPosition.Y, greenPosition.Z); GL.End(); GL.PopMatrix(); //re-enable lights GL.Enable(EnableCap.Lighting); GL.Color3(0.0f, 1.0f, 0.0f); GL.PushMatrix(); { GL.Translate(0.0f, 2.5f, -2.0f); Primitives.Torus(); } GL.PopMatrix(); GL.Enable(EnableCap.Light1); GL.Color3(1.0f, 0.0f, 0.0f); GL.PushMatrix(); { GL.Translate(2.5f, 1.0f, -0.5f); //move light into place GL.PushMatrix(); { GL.Rotate(lightAngle, 0.0f, 1.0f, 0.0f); GL.Translate(-2.0f, 0.0f, -2.0f); //render light where model-view matrix is position = new float[] { 0.0f, 0.0f, 0.0f, 1.0f }; GL.Light(LightName.Light1, LightParameter.Position, position); //disable lighting GL.Disable(EnableCap.Light1); //make light smaller GL.Scale(0.25f, 0.25f, 0.25f); //and blue GL.Color3(0.0f, 0.0f, 1.0f); //draw light visualization Primitives.DrawSphere(); //re-enable lighting GL.Enable(EnableCap.Light1); } //get rid of light matrix GL.PopMatrix(); Primitives.DrawSphere(5); } GL.PopMatrix(); GL.Disable(EnableCap.Light1); GL.Color3(0.0f, 0.0f, 1.0f); GL.PushMatrix(); { GL.Translate(-1.0f, 0.5f, 0.5f); Primitives.Cube(); } GL.PopMatrix(); }
public override void Render() { Vector3 eyePos = new Vector3(); eyePos.X = cameraAngle.Z * -(float)Math.Sin(cameraAngle.X * rads) * (float)Math.Cos(cameraAngle.Y * rads); eyePos.Y = cameraAngle.Z * -(float)Math.Sin(cameraAngle.Y * rads); eyePos.Z = -cameraAngle.Z * (float)Math.Cos(cameraAngle.X * rads) * (float)Math.Cos(cameraAngle.Y * rads); Matrix4 lookAt = Matrix4.LookAt(eyePos, new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f)); GL.LoadMatrix(Matrix4.Transpose(lookAt).Matrix); grid.Render(); //set light1 to red float[] red = new float[] { 1.0f, 0.0f, 0.0f, 1.0f }; GL.Light(LightName.Light1, LightParameter.Diffuse, red); GL.Light(LightName.Light1, LightParameter.Ambient, red); Vector3 redPosition = new Vector3(); redPosition.X = 1.0f * -(float)Math.Sin(redAngle.X * rads) * (float)Math.Cos(redAngle.Y * rads); redPosition.Y = 1.0f * -(float)Math.Sin(redAngle.Y * rads); redPosition.Z = -1.0f * (float)Math.Cos(redAngle.X * rads) * (float)Math.Cos(redAngle.Y * rads); Vector3 greenPosition = new Vector3(); greenPosition.X = 1.0f * -(float)Math.Sin(greenAngle.X * rads) * (float)Math.Cos(greenAngle.Y * rads); greenPosition.Y = 1.0f * -(float)Math.Sin(greenAngle.Y * rads); greenPosition.Z = -1.0f * (float)Math.Cos(greenAngle.X * rads) * (float)Math.Cos(greenAngle.Y * rads); GL.Light(LightName.Light1, LightParameter.Position, new float[] { redPosition.X, redPosition.Y, redPosition.Z, 0.0f }); GL.Light(LightName.Light2, LightParameter.Position, new float[] { greenPosition.X, greenPosition.Y, greenPosition.Z, 0.0f }); //add debug visualization GL.Disable(EnableCap.Lighting); GL.PushMatrix(); GL.Translate(4f, 4f, 0f); GL.Begin(PrimitiveType.Lines); //draw red ray GL.Color3(1.0f, 0.0f, 0.0f); GL.Vertex3(0f, 0f, 0f); redPosition.Normalize(); redPosition *= -1.0f; GL.Vertex3(redPosition.X, redPosition.Y, redPosition.Z); //draw green ray GL.Color3(0.0f, 1.0f, 0.0f); GL.Vertex3(0.0f, 0.0f, 0.0f); greenPosition.Normalize(); greenPosition *= -2.0f; GL.Vertex3(greenPosition.X, greenPosition.Y, greenPosition.Z); GL.End(); GL.PopMatrix(); //re-enable lights GL.Enable(EnableCap.Lighting); GL.Disable(EnableCap.Light0); GL.Disable(EnableCap.Light2); //enable red light GL.Enable(EnableCap.Light1); //draw torus GL.Color3(0.0f, 1.0f, 0.0f); GL.PushMatrix(); { GL.Translate(0.0f, 2.5f, -2.0f); Primitives.Torus(); } GL.PopMatrix(); //only red light is enabled, disable it GL.Disable(EnableCap.Light1); //enable green light GL.Enable(EnableCap.Light2); //draw sphere GL.Color3(1.0f, 0.0f, 0.0f); GL.PushMatrix(); { GL.Translate(2.5f, 1.0f, -0.5f); Primitives.DrawSphere(); } GL.PopMatrix(); //disable green and red lights GL.Disable(EnableCap.Light2); //enable blue light GL.Enable(EnableCap.Light1); GL.Enable(EnableCap.Light0); //change color of light 1 to purple from red float[] purple = new float[] { 1.0f, 0.0f, 1.0f, 1.0f }; GL.Light(LightName.Light1, LightParameter.Diffuse, purple); GL.Light(LightName.Light1, LightParameter.Ambient, purple); //specular can stay white //update position of light1 to static GL.Light(LightName.Light1, LightParameter.Position, new float[] { 0.0f, -0.5f, -0.5f, 0.0f }); //draw cube GL.Color3(0.0f, 0.0f, 1.0f); GL.PushMatrix(); { GL.Translate(-1.0f, 0.5f, 0.5f); Primitives.Cube(); } GL.PopMatrix(); }