Skip to content
Alexander Scheurer edited this page Feb 14, 2014 · 8 revisions

Getting a picture

This and all following lessons will have the needed assets attached in their project, so if you are coding along, don't forget to add them to your project.

We now need at least 4 more things to get a picture.

  • a mesh to be rendered
  • a shader the mesh will be rendered with
  • a parameter for the shader to (in this case) change the color
  • a camera

The first 3 are straight forward, we create a class private variable for each one:

private Mesh _cubeMesh;
private ShaderProgram _spColor;
private IShaderParam _colorParam;

Note: For now we'll work with ShaderProgram, later on we'll switch to ShaderEffects, which are much more powerfull and allow for multipass rendering.

Next, inizialize these variables in Init() {}:

_cubeMesh = MeshReader.LoadMesh(@"Assets/cube.obj.model");
_spColor = MoreShaders.GetDiffuseColorShader(RC);
_colorParam = _spColor.GetShaderParam("color");

We load the file "Assets/cube.obj.model" (This is a standard Wavefront OBJ file, we just like to add the .model extension, so Visual Studio doesn't confuse them), by using MeshReader.LoadMesh(). Then we take the default diffuse color shader, shipping with FUSEE and get its parameter "color".

At last we use this within RenderAFrame() {}:

RC.ModelView = float4x4.LookAt(0, 200, 500, 0, 0, 0, 0, 1, 0);
RC.SetShader(_spColor);
RC.SetShaderParam(_colorParam, new float4(0.5f, 0.8f, 0, 1));
RC.Render(_cubeMesh);

As mentioned in the last lesson, the RC (RenderContext) does all the rendering. RC.ModelView in this case just takes the "camera" represented by the float4x4.LookAt(). The LookAt() methode takes 9 parameters, being 3 vectors: 1. the xyz-coordinates of the camera position, 2. the xyz-coordinates of the point it looks at and 3. the up-vector (in this case telling the camera that the positiv y-axis is its up). Next we set the shader for the following render operation and also set the shaders paramater (in this case a RGBA value resulting in a light green). Now we have everything prepared for rendering, so we just hand RC our mesh and tell it to render it with the previously set parameters.

Exercise:

  • Try playing around with the float4x4.LookAt() to get a feel for how it affects the camera.
  • Look at float4x4.CreateTranslation() and float4x4.CreateRotation() and see how it influences the model view when you multiply it to it. Example: RC.ModelView = float4x4.CreateTranslation(100, 0, -300) * float4x4.LookAt(0, 200, 500, 0, 0, 0, 0, 1, 0);.
  • Duplicate the 4 lines we put into RenderAFrame() {} and try to render a second object with the same mesh but different color and in another position.

[> Lesson 02 - Control](Lesson 02)