Handmade Hero»Forums»Code
David Butler
81 posts / 1 project
I love pretty much anything technical, whether it be programming, networking, hacking or electronics.
Trouble with software 3d renderer

Over the weekend I thought it might be fun to implement a very basic 3d wireframe renderer. I basically copied all the math from the OpenGL docs and used sgorsten's vector math library.

This is what I got so far:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*

watch -n.1 "clang++ --std=c++11 -g main.cpp -shared -o _target.so && mv _target.so target.so"


# clang++ --std=c++11 -g main.cpp  -o target && ./target
*/
#include <assert.h>
#include <stdio.h>
#include "linalg.h"
#include <math.h>
#include <time.h>

#define FOR_RANGE(index, count) for ((index) = 0; (index) < (count); (index)++)

using namespace linalg::aliases;
using namespace linalg;

struct GC {
    unsigned char * render_buffer;
    int width;
    int height;
    float near;
    float far;
};

void draw_line(float2 p1, float2 p2, GC *gc) {

    if (p1[0] == INFINITY || p1[0] == -INFINITY || isnan(p1[0]) ||
        p1[1] == INFINITY || p1[1] == -INFINITY || isnan(p1[1]) ||
        p2[0] == INFINITY || p2[0] == -INFINITY || isnan(p2[0]) ||
        p2[1] == INFINITY || p2[1] == -INFINITY || isnan(p2[1])) return;

    float2 d = normalize(p2 - p1);
    float2 last_p = p1;

    float minx = p1[0];
    if (p2[0] < p1[0]) minx = p2[0];
    float miny = p1[1];
    if (p2[1] < p1[1]) miny = p2[1];
    float maxx = p1[0];
    if (p2[0] > p1[0]) maxx = p2[0];
    float maxy = p1[1];
    if (p2[1] > p1[1]) maxy = p2[1];

    for(;;) {

        int x = last_p[0];
        int y = last_p[1];
        if (x >= 0 && y>=0 && x <gc->width && y < gc->height) {
            *(gc->render_buffer + 4 * (x + gc->width * y) + 0) = 0x00;
            *(gc->render_buffer + 4 * (x + gc->width * y) + 1) = 0x00;
            *(gc->render_buffer + 4 * (x + gc->width * y) + 2) = 0x00;
            *(gc->render_buffer + 4 * (x + gc->width * y) + 3) = 0xFF;
        }
        // else { printf("clip!\n");};

        last_p += d;
        if ( (d[0]==0 && d[1]==0) ||
            d[0] == INFINITY || d[0] == -INFINITY || isnan(d[0]) ||
            d[1] == INFINITY || d[1] == -INFINITY || isnan(d[1]) ||
            last_p[0] < minx ||
            last_p[1] < miny ||
            last_p[0] > maxx ||
            last_p[1] > maxy
        ) break;
    }

}


float4x4 look_at() {
     // gluLookAt
     float la_ey_x = 0.0f;
     float la_ey_y = 0.0f;
     float la_ey_z = 5.0f;
     float la_cn_x = 0.0f;
     float la_cn_y = 0.0f;
     float la_cn_z = 0.0f;
     float la_up_x = 0.0f;
     float la_up_y = 1.0f;
     float la_up_z = 0.0f;

     float3 la_F = {la_cn_x - la_ey_x, la_cn_y - la_ey_y, la_cn_z - la_ey_z};
     float3 la_UP = {la_up_x, la_up_y, la_up_z};

     float3 la_f = normalize(la_F);
     float3 la_UP_norm = normalize(la_UP);


//     float3 la_s = la_f * la_UP_norm;
//     float3 la_u = normalize(la_s) * la_f;
     float3 la_s = cross(la_f, la_UP_norm);
     float3 la_u = cross(normalize(la_s), la_f);


    return                {{  la_s[0],  la_s[1],  la_s[2],  0},
                           {  la_u[0],  la_u[1],  la_u[2],  0},
                           { -la_f[0], -la_f[1], -la_f[2],  0},
                           {        0,        0,        0,  1}};

}


float4x4 translate(float3 trans_p) {
    return              {{  1,  0,  0,  trans_p[0]},
                         {  0,  1,  0,  trans_p[1]},
                         {  0,  0,  1,  trans_p[2]},
                         {  0,  0,  0,  1}};
}

float4x4 rotate(float3 r_p, float r_angle) {
    r_p = normalize(r_p);
    float r_c = cos(r_angle * M_PI / 180);
    float r_s = sin(r_angle * M_PI / 180);

    return {{ powf(r_p.x, 2) * (1 - r_c) + r_c,            r_p.x * r_p.y * (1 - r_c) - r_p.z * r_s,      r_p.x * r_p.z * (1-r_c) + r_p.y * r_s,   0 },
            { r_p.y * r_p.x * (1 - r_c) +  r_p.z * r_s,   powf(r_p.y, 2) * (1 - r_c) + r_c,              r_p.y * r_p.z * (1-r_c) - r_p.x * r_s,   0 },
            { r_p.x * r_p.z * (1 - r_c) -  r_p.y * r_s,   r_p.y * r_p.z * (1 - r_c) +  r_p.x * r_s,     powf(r_p.z, 2) * (1 - r_c) + r_c,         0 },
            { 0,                                          0,                                            0,                                       1 }};

}

float4x4 ortho() {
    float o_left      = -2.0f;
    float o_right     =  2.0f;
    float o_bottom    = -2.0f;
    float o_top       =  2.0f;
    float o_near      = -2.0f;
    float o_far       =  2.0f;

    float o_tx = (o_right + o_left) / (o_right - o_left);
    float o_ty = (o_top + o_bottom) / (o_top - o_bottom);
    float o_tz = (o_far + o_near) / (o_far - o_near);

    return     {{ 2.0f / (o_right - o_left), 0, 0, o_tx},
                { 0,                         2.0f / (o_top - o_bottom), 0, o_ty},
                { 0,              0, 2.0f / (o_far - o_near), o_tz},
                { 0,              0, -1, 1}};

}

float4x4 perspective() {

    // gluPerspective
    float field_of_view = 40.0f;
    float aspect_ratio = 1.0f;
    float z_near = 8.60f;
    float z_far = 10.0f;

    // float f = cotangent(field_of_view / 2.0f);
    float f = 1.0f / tan(field_of_view / (float)(M_PI) / 180.0f / 2.0f);

    float4x4 projection = {{ f/aspect_ratio,  0, 0, 0},
                            { 0,              f, 0, 0},
                            { 0,              0, (z_far + z_near) / (z_near - z_far), (2.0f * z_far * z_near) / (z_near - z_far)},
                            { 0,              0, -1, 0}};

    return  {{ f/aspect_ratio,  1, 0, 0},
             { 0,              f, 0, 0},
             { 0,              0, (z_far + z_near) / (z_near - z_far), (2.0f * z_far * z_near) / (z_near - z_far)},
             { 0,              0, -1, 0}};

}


float3 nd_cord(GC* gc, float4 clip_cord) {
    return { clip_cord.x / clip_cord.w, clip_cord.y / clip_cord.w, clip_cord.z / clip_cord.w};
}

float3 window_cord(GC* gc, float3 nd_cord) {
    float vp_x       = 0;                   // glViewport
    float vp_y       = 0;                   // glViewport
    float vp_w       = gc->width;      // glViewport
    float vp_h       = gc->height;     // glViewport
    float dr_n       = gc->near;    //glDepthRange
    float dr_f       = gc->far;     //glDepthRange

    return {
        (vp_w / 2.0f) * nd_cord.x + (vp_x + vp_w/2.0f),
        (vp_h / 2.0f) * nd_cord.y + (vp_y + vp_h/2.0f),
        ((dr_f - dr_n)/2.0f) * nd_cord.z + (dr_f + dr_n)/2.0f
    };

}



extern "C" void target(float current_time, void * _render_buffer, int width, int height) {

    GC  gc = {(unsigned char *)_render_buffer, width, height, 1, 10};

    unsigned char * render_buffer = (unsigned char *)_render_buffer;

//    printf("\033[1A");
//    printf("\033[1A");
//    printf("\033[1A");
//    printf("\033[1A");
//    printf("\033[1A");
//    printf("\033[1A");


    // Clear white
    memset(render_buffer, 0xFF, width * height * 4);

    float4x4 identity = {{  1,  0,  0 , 0},
                         {  0,  1,  0,  0},
                         {  0,  0,  1,  0},
                         {  0,  0,  0,  1}};

    // Object cords
    float4 oc_p0 = { 1,  1,  1, 1};
    float4 oc_p1 = { 1,  1, -1, 1};
    float4 oc_p2 = { 1, -1, -1, 1};
    float4 oc_p3 = { 1, -1,  1, 1};
    float4 oc_p4 = {-1,  1,  1, 1};
    float4 oc_p5 = {-1,  1, -1, 1};
    float4 oc_p6 = {-1, -1, -1, 1};
    float4 oc_p7 = {-1, -1,  1, 1};
    // Cube with lines: p0, p1,   p1,p2    p2,p3   p3,p0      p4,p5   p5,p6   p6,p7   p7,p4    p0,p4   p1,p5   p2,p6   p3,p7


    float4x4 model_view = identity;

    // model_view = mul(model_view, look_at());

    // model_view = mul(model_view, translate({0.0, 0.0, -1.0}));

    model_view = mul(model_view, rotate({1.0, 0.0, 0.0},  20.0f));
    model_view = mul(model_view, rotate({0.0, 0.0, 1.0}, -10.0f));
    model_view = mul(model_view, rotate({0.0, 1.0, 0.0}, -10.0f));


    // eye cords
    float4 ec_p0 = mul(model_view, oc_p0);
    float4 ec_p1 = mul(model_view, oc_p1);
    float4 ec_p2 = mul(model_view, oc_p2);
    float4 ec_p3 = mul(model_view, oc_p3);
    float4 ec_p4 = mul(model_view, oc_p4);
    float4 ec_p5 = mul(model_view, oc_p5);
    float4 ec_p6 = mul(model_view, oc_p6);
    float4 ec_p7 = mul(model_view, oc_p7);




    float4x4 projection = identity;
    projection = mul(projection, ortho());
    //projection = mul(projection, perspective());



    // clip cords
    float4 cc_p0 = mul(projection, ec_p0);
    float4 cc_p1 = mul(projection, ec_p1);
    float4 cc_p2 = mul(projection, ec_p2);
    float4 cc_p3 = mul(projection, ec_p3);
    float4 cc_p4 = mul(projection, ec_p4);
    float4 cc_p5 = mul(projection, ec_p5);
    float4 cc_p6 = mul(projection, ec_p6);
    float4 cc_p7 = mul(projection, ec_p7);

    // Normalized device coordinates
    float3 ndc_p0 = nd_cord(&gc, cc_p0);
    float3 ndc_p1 = nd_cord(&gc, cc_p1);
    float3 ndc_p2 = nd_cord(&gc, cc_p2);
    float3 ndc_p3 = nd_cord(&gc, cc_p3);
    float3 ndc_p4 = nd_cord(&gc, cc_p4);
    float3 ndc_p5 = nd_cord(&gc, cc_p5);
    float3 ndc_p6 = nd_cord(&gc, cc_p6);
    float3 ndc_p7 = nd_cord(&gc, cc_p7);

    // Window Cords
    float3 wc_p0 = window_cord(&gc, ndc_p0);
    float3 wc_p1 = window_cord(&gc, ndc_p1);
    float3 wc_p2 = window_cord(&gc, ndc_p2);
    float3 wc_p3 = window_cord(&gc, ndc_p3);
    float3 wc_p4 = window_cord(&gc, ndc_p4);
    float3 wc_p5 = window_cord(&gc, ndc_p5);
    float3 wc_p6 = window_cord(&gc, ndc_p6);
    float3 wc_p7 = window_cord(&gc, ndc_p7);


#if 0

    time_t timer;
    char buffer[26];
    struct tm* tm_info;

    time(&timer);
    tm_info = localtime(&timer);

    strftime(buffer, 26, "%Y:%m:%d %H:%M:%S", tm_info);

    printf("%s  Points 0:%f,%f 1:%f,%f 2:%f,%f 3:%f,%f 4:%f,%f 5:%f,%f 6:%f,%f 7:%f,%f \n",
        buffer,
        wc_p0[0], wc_p0[1], wc_p1[0], wc_p1[1], wc_p2[0], wc_p2[1], wc_p3[0], wc_p3[1],
        wc_p4[0], wc_p4[1], wc_p5[0], wc_p5[1], wc_p6[0], wc_p6[1], wc_p7[0], wc_p7[1]
    );
    printf("%s  Pointz 0:%f    1:%f    2:%f    3:%f    4:%f    5:%f    6:%f    7:%f  \n",
        buffer,
        wc_p0[2], wc_p1[2], wc_p2[2], wc_p3[2], wc_p4[2], wc_p5[2], wc_p6[2], wc_p7[2]
    );
#endif
    // lines: p0, p1,   p1,p2    p2,p3   p3,p0      p4,p5   p5,p6   p6,p7   p7,p4    p0,p4   p1,p5   p2,p6   p3,p7

    draw_line({wc_p0[0], wc_p0[1]}, {wc_p1[0], wc_p1[1]}, &gc);
    draw_line({wc_p1[0], wc_p1[1]}, {wc_p2[0], wc_p2[1]}, &gc);
    draw_line({wc_p2[0], wc_p2[1]}, {wc_p3[0], wc_p3[1]}, &gc);
    draw_line({wc_p3[0], wc_p3[1]}, {wc_p0[0], wc_p0[1]}, &gc);
    draw_line({wc_p4[0], wc_p4[1]}, {wc_p5[0], wc_p5[1]}, &gc);
    draw_line({wc_p5[0], wc_p5[1]}, {wc_p6[0], wc_p6[1]}, &gc);
    draw_line({wc_p6[0], wc_p6[1]}, {wc_p7[0], wc_p7[1]}, &gc);
    draw_line({wc_p7[0], wc_p7[1]}, {wc_p4[0], wc_p4[1]}, &gc);
    draw_line({wc_p0[0], wc_p0[1]}, {wc_p4[0], wc_p4[1]}, &gc);
    draw_line({wc_p1[0], wc_p1[1]}, {wc_p5[0], wc_p5[1]}, &gc);
    draw_line({wc_p2[0], wc_p2[1]}, {wc_p6[0], wc_p6[1]}, &gc);
    draw_line({wc_p3[0], wc_p3[1]}, {wc_p7[0], wc_p7[1]}, &gc);


}


See also: Gist, along with hot-loader (For OS X, but should run on anywhere, along as you have SDL)

If I just use orthographic projection I get something that I expect:


However, if I get try to use a perspective matrix, or even apply a translation matrix, I get really odd results:



I'm kinda at a loss at this point, I checked the math a few times, but I probably still have a mistake somewhere...

Anyway, here is what I based the code off of:
"OpenGL Transformation" Basicall...ertices translate to window cords
glTranslate
gluPerspective

David Butler
81 posts / 1 project
I love pretty much anything technical, whether it be programming, networking, hacking or electronics.
Trouble with software 3d renderer
Ugg, I feel like such an idiot... "linalg.h" wants everything in column major order, so all my matrices were inverted...