glide_test1
diff main.cpp @ 0:f3ddb2bb7024
first 3dfx glide test, initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 09 Mar 2014 06:27:58 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/main.cpp Sun Mar 09 06:27:58 2014 +0200 1.3 @@ -0,0 +1,167 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <glide.h> 1.7 +#include <windows.h> 1.8 +#include "mesh.h" 1.9 + 1.10 +#define XSZ 640 1.11 +#define YSZ 480 1.12 + 1.13 +void display(unsigned int msec); 1.14 +void process_vertex(Vertex *vres, const Vertex &vin); 1.15 +void draw_mesh(const std::vector<Vertex> &varr); 1.16 +void draw_triangle(const Vertex *v); 1.17 + 1.18 +unsigned int start_time; 1.19 +Matrix4x4 mv_matrix, proj_matrix, norm_matrix; 1.20 +Vector4 mat_diffuse; 1.21 +Vector3 light_dir; 1.22 +std::vector<Vertex> torus; 1.23 + 1.24 +int main(void) 1.25 +{ 1.26 + GrHwConfiguration hwcfg; 1.27 + 1.28 + if(!grSstQueryBoards(&hwcfg)) { 1.29 + fprintf(stderr, "No 3dfx graphics boards detected!\n"); 1.30 + return 1; 1.31 + } 1.32 + printf("Found %d 3dfx graphics boards!\n", hwcfg.num_sst); 1.33 + 1.34 + grGlideInit(); 1.35 + if(!grSstQueryHardware(&hwcfg)) { 1.36 + fprintf(stderr, "No 3dfx graphics hardware detected!\n"); 1.37 + return 1; 1.38 + } 1.39 + grSstSelect(0); 1.40 + 1.41 + if(!grSstWinOpen(0, GR_RESOLUTION_640x480, GR_REFRESH_60Hz, GR_COLORFORMAT_RGBA, 1.42 + GR_ORIGIN_UPPER_LEFT, 2, 1)) { 1.43 + fprintf(stderr, "Failed to initialize glide device\n"); 1.44 + return 1; 1.45 + } 1.46 + 1.47 + printf("Voodoo graphics initialized sucessfully!\n"); 1.48 + 1.49 + // setup projection matrix 1.50 + proj_matrix.perspective(45.0f, 1.3333333f, 0.5, 250.0); 1.51 + 1.52 + // generate torus mesh 1.53 + gen_torus(torus, 1.5, 0.5, 24, 12); 1.54 + 1.55 + // light 1.56 + light_dir = normalize(Vector3(-0.6f, 1.0f, 2.0f)); 1.57 + 1.58 + // enable W-buffer (depth buffer) 1.59 + grDepthBufferMode(GR_DEPTHBUFFER_WBUFFER); 1.60 + grDepthMask(1); 1.61 + grDepthBufferFunction(GR_CMP_LESS); 1.62 + 1.63 + // main loop 1.64 + start_time = GetTickCount(); 1.65 + unsigned int msec = 0; 1.66 + while((msec = GetTickCount() - start_time) < 8000) { 1.67 + display(msec); 1.68 + } 1.69 + 1.70 + grGlideShutdown(); 1.71 + return 0; 1.72 +} 1.73 + 1.74 +void display(unsigned int msec) 1.75 +{ 1.76 + float t = (float)msec / 1000.0f; 1.77 + 1.78 + grBufferClear(0x103060ff, 255, GR_WDEPTHVALUE_FARTHEST); 1.79 + 1.80 + mv_matrix.set_identity(); 1.81 + mv_matrix.translate(0, 0, -8); 1.82 + mv_matrix.rotate(t * 100.0f, 1, 0, 0); 1.83 + mv_matrix.rotate(t * 80.0f, 0, 0, 1); 1.84 + 1.85 + norm_matrix = mv_matrix; 1.86 + norm_matrix[0][3] = norm_matrix[1][3] = norm_matrix[2][3] = 0.0f; 1.87 + norm_matrix[3][0] = norm_matrix[3][1] = norm_matrix[3][2] = 0.0f; 1.88 + norm_matrix[3][3] = 1.0f; 1.89 + 1.90 + mat_diffuse = Vector4(1.0f, 0.6f, 0.2f, 1.0f); 1.91 + draw_mesh(torus); 1.92 + 1.93 + grBufferSwap(1); 1.94 +} 1.95 + 1.96 + 1.97 +void process_vertex(Vertex *vres, const Vertex &vin) 1.98 +{ 1.99 + // transform with the modelview matrix 1.100 + vres->pos = transform(vin.pos, mv_matrix); 1.101 + vres->normal = transform(vin.normal, norm_matrix); 1.102 + 1.103 + // calculate lighting 1.104 + float ndotl = dot(vres->normal, light_dir); 1.105 + if(ndotl < 0.0) ndotl = 0.0; 1.106 + 1.107 + Vector3 vdir(0, 0, 1); 1.108 + Vector3 half_dir = normalize(vdir + light_dir); 1.109 + float ndoth = dot(vres->normal, half_dir); 1.110 + if(ndoth < 0.0) ndoth = 0.0; 1.111 + float spec = pow(ndoth, 32.0); 1.112 + 1.113 + float red = mat_diffuse.x * ndotl + spec; 1.114 + float green = mat_diffuse.y * ndotl + spec; 1.115 + float blue = mat_diffuse.z * ndotl + spec; 1.116 + 1.117 + 1.118 + vres->color.x = red > 1.0 ? 1.0 : red; 1.119 + vres->color.y = green > 1.0 ? 1.0 : green; 1.120 + vres->color.z = blue > 1.0 ? 1.0 : blue; 1.121 + vres->color.w = mat_diffuse.w; 1.122 + 1.123 + 1.124 + // transform with the projection matrix 1.125 + vres->pos = transform(vres->pos, proj_matrix); 1.126 + 1.127 + // perspective division 1.128 + vres->pos.x = vres->pos.x / vres->pos.w; 1.129 + vres->pos.y = vres->pos.y / vres->pos.w; 1.130 + vres->pos.z = vres->pos.z / vres->pos.w; 1.131 + 1.132 + // viewport transformation 1.133 + vres->pos.x = (vres->pos.x * 0.5 + 0.5) * (float)XSZ; 1.134 + vres->pos.y = (vres->pos.y * 0.5 + 0.5) * (float)YSZ; 1.135 +} 1.136 + 1.137 + 1.138 +void draw_mesh(const std::vector<Vertex> &varr) 1.139 +{ 1.140 + static Vertex face_vert[3]; 1.141 + size_t num = varr.size(); 1.142 + 1.143 + int face_vert_idx = 0; 1.144 + 1.145 + for(int i=0; i<num; i++) { 1.146 + process_vertex(face_vert + face_vert_idx, varr[i]); 1.147 + 1.148 + if(++face_vert_idx >= 3) { 1.149 + draw_triangle(face_vert); 1.150 + face_vert_idx = 0; 1.151 + } 1.152 + } 1.153 +} 1.154 + 1.155 +void draw_triangle(const Vertex *v) 1.156 +{ 1.157 + GrVertex face[3]; 1.158 + 1.159 + for(int i=0; i<3; i++) { 1.160 + face[i].x = v[i].pos.x; 1.161 + face[i].y = v[i].pos.y; 1.162 + face[i].r = v[i].color.x * 255.0; 1.163 + face[i].g = v[i].color.y * 255.0; 1.164 + face[i].b = v[i].color.z * 255.0; 1.165 + face[i].a = v[i].color.w * 255.0; 1.166 + face[i].oow = 1.0f / v[i].pos.w; 1.167 + } 1.168 + 1.169 + grDrawTriangle(face, face + 1, face + 2); 1.170 +} 1.171 \ No newline at end of file