<p>The problem was in the shader, and I had the order of multiplication wrong with the MVP matrix. </p>

<p>So this: </p>

<pre><code>out.pos = v * mvp_matrix;
</code></pre>

<p>should be: </p>

<pre><code>out.pos = mvp_matrix * v;
</code></pre>

<p>For whatever reason, I'm used to dealing with row vectors and had myself convinced the problem was due to the matrices themselves and the clip region..</p>

CC BY-SA 3.0
2016-01-21T00:41:04.463Z
2016-01-21T00:41:04.463Z
Chris
<p>I've set up a test project for learning Metal on iOS to do some rendering, but I'm a bit stumped on how to get a prism rotating correctly about its y axis. </p>

<p>Here is the prism rendered without depth testing so all sides can be seen, so it looks like this part is at least correct:</p>

<p><a href="https://i.sstatic.net/Tz8CE.png" rel="noreferrer"><img src="https://i.sstatic.net/Tz8CE.png" alt="enter image description here"></a></p>

<p>These are the vertices:</p>

<pre><code>static const float vertexData[] = {
     0.f,  1.f,  0.5f,
     1.f, -1.f,  0.f,
    -1.f, -1.f,  0.f,

     0.f,  1.f,  0.5f,
     1.f, -1.f,  1.f,
     1.f, -1.f,  0.f,

     0.f,  1.f,  0.5f,
    -1.f, -1.f,  1.f,
     1.f, -1.f,  1.f,

     0.f,  1.f,  0.5f,
    -1.f, -1.f,  0.f,
    -1.f, -1.f,  1.f
};
</code></pre>

<p>However, when I turn on rotation, this is what happens:
<a href="https://www.dropbox.com/s/esg41j3ibncofox/prism_rotate.mov?dl=0" rel="noreferrer">https://www.dropbox.com/s/esg41j3ibncofox/prism_rotate.mov?dl=0</a></p>

<p>(The video has depth testing turned on). Why is the prism clipping like that (and/or <em>what</em> is it clipping through)? And it's not rotating about it's centre.</p>

<p>Here are how the MVP matrices are being calculated:</p>

<pre><code>static simd::float3 viewEye = {0.f, 0.f, -2.f};
static simd::float3 viewCenter = {0.f, 0.f, 1.f};
static simd::float3 viewUp = {0.f, 1.f, 0.f};
static float fov = 45.f;

CGSize size = self.view.bounds.size;
Uniforms *uniforms = (Uniforms *)[uniformBuffer contents];
uniforms-&gt;view = AAPL::lookAt(viewEye, viewCenter, viewUp);
uniforms-&gt;projection = AAPL::perspective_fov(fov, size.width, size.height, 0.1f, 100.f);

uniforms-&gt;model = AAPL::translate(0.f, 0.f, 12.f) * AAPL::rotate(tAngle, 0.f, 1.f, 0.f);
tAngle += 0.5f;
</code></pre>

<p>The <code>transform</code>, <code>lookAt</code>, <code>rotate</code>, and <code>perspective_fov</code> functions are lifted straight from Apple sample code I used as reference.</p>

<p>Here is the shader:</p>

<pre><code>typedef struct {
    float4 pos [[ position ]];
    half4 color;
    float mod;
} VertexOut;

vertex VertexOut basic_vertex(const device packed_float3* vertex_array [[ buffer(0) ]],
                              const device packed_float3* colors [[ buffer(1) ]],
                              constant Uniforms&amp; uniform [[ buffer(2) ]],
                              uint vid [[ vertex_id ]],
                              uint iid [[ instance_id ]])
{
    float4 v = float4(vertex_array[vid], 1.f);
    float4x4 mvp_matrix = uniform.projection * uniform.view * uniform.model;

    VertexOut out;
    out.pos = v * mvp_matrix;
    uint colorIndex = vid / 3;
    out.color = half4(half3(colors[colorIndex]), 1.f);
    return out;
}

fragment half4 basic_fragment(VertexOut f [[ stage_in ]]) {
    return f.color;
}
</code></pre>

<p>Any help/tips would be appreciated!</p>

<shader><3d>
Why is rotating this prism in iOS Metal not rendering correctly?
Chris
# Why is rotating this prism in iOS Metal not rendering correctly?

(Asked by: username_0 on 2016-01-20)

I've set up a test project for learning Metal on iOS to do some rendering, but I'm a bit stumped on how to get a prism rotating correctly about its y axis.

Here is the prism rendered without depth testing so all sides can be seen, so it looks like this part is at least correct:

[![enter image description here](https://i.sstatic.net/Tz8CE.png)](https://i.sstatic.net/Tz8CE.png)

These are the vertices:

```
static const float vertexData[] = {
     0.f,  1.f,  0.5f,
     1.f, -1.f,  0.f,
    -1.f, -1.f,  0.f,

     0.f,  1.f,  0.5f,
     1.f, -1.f,  1.f,
     1.f, -1.f,  0.f,

     0.f,  1.f,  0.5f,
    -1.f, -1.f,  1.f,
     1.f, -1.f,  1.f,

     0.f,  1.f,  0.5f,
    -1.f, -1.f,  0.f,
    -1.f, -1.f,  1.f
};

```

However, when I turn on rotation, this is what happens:
<https://www.dropbox.com/s/esg41j3ibncofox/prism_rotate.mov?dl=0>

(The video has depth testing turned on). Why is the prism clipping like that (and/or *what* is it clipping through)? And it's not rotating about it's centre.

Here are how the MVP matrices are being calculated:

```
static simd::float3 viewEye = {0.f, 0.f, -2.f};
static simd::float3 viewCenter = {0.f, 0.f, 1.f};
static simd::float3 viewUp = {0.f, 1.f, 0.f};
static float fov = 45.f;

CGSize size = self.view.bounds.size;
Uniforms *uniforms = (Uniforms *)[uniformBuffer contents];
uniforms->view = AAPL::lookAt(viewEye, viewCenter, viewUp);
uniforms->projection = AAPL::perspective_fov(fov, size.width, size.height, 0.1f, 100.f);

uniforms->model = AAPL::translate(0.f, 0.f, 12.f) * AAPL::rotate(tAngle, 0.f, 1.f, 0.f);
tAngle += 0.5f;

```

The `transform`, `lookAt`, `rotate`, and `perspective_fov` functions are lifted straight from Apple sample code I used as reference.

Here is the shader:

```
typedef struct {
    float4 pos [[ position ]];
    half4 color;
    float mod;
} VertexOut;

vertex VertexOut basic_vertex(const device packed_float3* vertex_array [[ buffer(0) ]],
                              const device packed_float3* colors [[ buffer(1) ]],
                              constant Uniforms& uniform [[ buffer(2) ]],
                              uint vid [[ vertex_id ]],
                              uint iid [[ instance_id ]])
{
    float4 v = float4(vertex_array[vid], 1.f);
    float4x4 mvp_matrix = uniform.projection * uniform.view * uniform.model;

    VertexOut out;
    out.pos = v * mvp_matrix;
    uint colorIndex = vid / 3;
    out.color = half4(half3(colors[colorIndex]), 1.f);
    return out;
}

fragment half4 basic_fragment(VertexOut f [[ stage_in ]]) {
    return f.color;
}

```

Any help/tips would be appreciated!

**Tags:** shader, 3d


---

## 1 answers

### Answer by username_0 on 2016-01-21. Score: 3

The problem was in the shader, and I had the order of multiplication wrong with the MVP matrix.

So this:

```
out.pos = v * mvp_matrix;

```

should be:

```
out.pos = mvp_matrix * v;

```

For whatever reason, I'm used to dealing with row vectors and had myself convinced the problem was due to the matrices themselves and the clip region..


---

