glide_test1

view 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 source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <glide.h>
4 #include <windows.h>
5 #include "mesh.h"
7 #define XSZ 640
8 #define YSZ 480
10 void display(unsigned int msec);
11 void process_vertex(Vertex *vres, const Vertex &vin);
12 void draw_mesh(const std::vector<Vertex> &varr);
13 void draw_triangle(const Vertex *v);
15 unsigned int start_time;
16 Matrix4x4 mv_matrix, proj_matrix, norm_matrix;
17 Vector4 mat_diffuse;
18 Vector3 light_dir;
19 std::vector<Vertex> torus;
21 int main(void)
22 {
23 GrHwConfiguration hwcfg;
25 if(!grSstQueryBoards(&hwcfg)) {
26 fprintf(stderr, "No 3dfx graphics boards detected!\n");
27 return 1;
28 }
29 printf("Found %d 3dfx graphics boards!\n", hwcfg.num_sst);
31 grGlideInit();
32 if(!grSstQueryHardware(&hwcfg)) {
33 fprintf(stderr, "No 3dfx graphics hardware detected!\n");
34 return 1;
35 }
36 grSstSelect(0);
38 if(!grSstWinOpen(0, GR_RESOLUTION_640x480, GR_REFRESH_60Hz, GR_COLORFORMAT_RGBA,
39 GR_ORIGIN_UPPER_LEFT, 2, 1)) {
40 fprintf(stderr, "Failed to initialize glide device\n");
41 return 1;
42 }
44 printf("Voodoo graphics initialized sucessfully!\n");
46 // setup projection matrix
47 proj_matrix.perspective(45.0f, 1.3333333f, 0.5, 250.0);
49 // generate torus mesh
50 gen_torus(torus, 1.5, 0.5, 24, 12);
52 // light
53 light_dir = normalize(Vector3(-0.6f, 1.0f, 2.0f));
55 // enable W-buffer (depth buffer)
56 grDepthBufferMode(GR_DEPTHBUFFER_WBUFFER);
57 grDepthMask(1);
58 grDepthBufferFunction(GR_CMP_LESS);
60 // main loop
61 start_time = GetTickCount();
62 unsigned int msec = 0;
63 while((msec = GetTickCount() - start_time) < 8000) {
64 display(msec);
65 }
67 grGlideShutdown();
68 return 0;
69 }
71 void display(unsigned int msec)
72 {
73 float t = (float)msec / 1000.0f;
75 grBufferClear(0x103060ff, 255, GR_WDEPTHVALUE_FARTHEST);
77 mv_matrix.set_identity();
78 mv_matrix.translate(0, 0, -8);
79 mv_matrix.rotate(t * 100.0f, 1, 0, 0);
80 mv_matrix.rotate(t * 80.0f, 0, 0, 1);
82 norm_matrix = mv_matrix;
83 norm_matrix[0][3] = norm_matrix[1][3] = norm_matrix[2][3] = 0.0f;
84 norm_matrix[3][0] = norm_matrix[3][1] = norm_matrix[3][2] = 0.0f;
85 norm_matrix[3][3] = 1.0f;
87 mat_diffuse = Vector4(1.0f, 0.6f, 0.2f, 1.0f);
88 draw_mesh(torus);
90 grBufferSwap(1);
91 }
94 void process_vertex(Vertex *vres, const Vertex &vin)
95 {
96 // transform with the modelview matrix
97 vres->pos = transform(vin.pos, mv_matrix);
98 vres->normal = transform(vin.normal, norm_matrix);
100 // calculate lighting
101 float ndotl = dot(vres->normal, light_dir);
102 if(ndotl < 0.0) ndotl = 0.0;
104 Vector3 vdir(0, 0, 1);
105 Vector3 half_dir = normalize(vdir + light_dir);
106 float ndoth = dot(vres->normal, half_dir);
107 if(ndoth < 0.0) ndoth = 0.0;
108 float spec = pow(ndoth, 32.0);
110 float red = mat_diffuse.x * ndotl + spec;
111 float green = mat_diffuse.y * ndotl + spec;
112 float blue = mat_diffuse.z * ndotl + spec;
115 vres->color.x = red > 1.0 ? 1.0 : red;
116 vres->color.y = green > 1.0 ? 1.0 : green;
117 vres->color.z = blue > 1.0 ? 1.0 : blue;
118 vres->color.w = mat_diffuse.w;
121 // transform with the projection matrix
122 vres->pos = transform(vres->pos, proj_matrix);
124 // perspective division
125 vres->pos.x = vres->pos.x / vres->pos.w;
126 vres->pos.y = vres->pos.y / vres->pos.w;
127 vres->pos.z = vres->pos.z / vres->pos.w;
129 // viewport transformation
130 vres->pos.x = (vres->pos.x * 0.5 + 0.5) * (float)XSZ;
131 vres->pos.y = (vres->pos.y * 0.5 + 0.5) * (float)YSZ;
132 }
135 void draw_mesh(const std::vector<Vertex> &varr)
136 {
137 static Vertex face_vert[3];
138 size_t num = varr.size();
140 int face_vert_idx = 0;
142 for(int i=0; i<num; i++) {
143 process_vertex(face_vert + face_vert_idx, varr[i]);
145 if(++face_vert_idx >= 3) {
146 draw_triangle(face_vert);
147 face_vert_idx = 0;
148 }
149 }
150 }
152 void draw_triangle(const Vertex *v)
153 {
154 GrVertex face[3];
156 for(int i=0; i<3; i++) {
157 face[i].x = v[i].pos.x;
158 face[i].y = v[i].pos.y;
159 face[i].r = v[i].color.x * 255.0;
160 face[i].g = v[i].color.y * 255.0;
161 face[i].b = v[i].color.z * 255.0;
162 face[i].a = v[i].color.w * 255.0;
163 face[i].oow = 1.0f / v[i].pos.w;
164 }
166 grDrawTriangle(face, face + 1, face + 2);