glide_test1
changeset 0:f3ddb2bb7024 tip
first 3dfx glide test, initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 09 Mar 2014 06:27:58 +0200 |
parents | |
children | |
files | main.cpp matrix.h mesh.cpp mesh.h test1.dsp test1.dsw vmath.h |
diffstat | 7 files changed, 770 insertions(+), 0 deletions(-) [+] |
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
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/matrix.h Sun Mar 09 06:27:58 2014 +0200 2.3 @@ -0,0 +1,111 @@ 2.4 +#ifndef MATRIX_H_ 2.5 +#define MATRIX_H_ 2.6 + 2.7 +#define _USE_MATH_DEFINES 2.8 +#include <math.h> 2.9 + 2.10 +#ifndef M_PI 2.11 +#define M_PI 3.14159265359 2.12 +#endif 2.13 + 2.14 +class Matrix4x4 { 2.15 +public: 2.16 + float m[4][4]; 2.17 + 2.18 + inline Matrix4x4() 2.19 + { 2.20 + *this = Matrix4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); 2.21 + } 2.22 + 2.23 + inline Matrix4x4(float m00, float m01, float m02, float m03, 2.24 + float m10, float m11, float m12, float m13, 2.25 + float m20, float m21, float m22, float m23, 2.26 + float m30, float m31, float m32, float m33) 2.27 + { 2.28 + m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03; 2.29 + m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13; 2.30 + m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23; 2.31 + m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33; 2.32 + } 2.33 + 2.34 + friend inline Matrix4x4 operator *(const Matrix4x4 &a, const Matrix4x4 &b); 2.35 + 2.36 + inline void set_identity() 2.37 + { 2.38 + *this = Matrix4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); 2.39 + } 2.40 + 2.41 + inline void translate(float x, float y, float z) 2.42 + { 2.43 + *this = *this * Matrix4x4(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1); 2.44 + } 2.45 + 2.46 + inline void scale(float x, float y, float z) 2.47 + { 2.48 + *this = *this * Matrix4x4(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1); 2.49 + } 2.50 + 2.51 + inline void rotate(float angle, float x, float y, float z) 2.52 + { 2.53 + float rad = M_PI * angle / 180.0; 2.54 + float sina = (float)sin(rad); 2.55 + float cosa = (float)cos(rad); 2.56 + float invcosa = 1.0f - cosa; 2.57 + float nxsq = x * x; 2.58 + float nysq = y * y; 2.59 + float nzsq = z * z; 2.60 + 2.61 + Matrix4x4 xform; 2.62 + xform[0][0] = nxsq + (1.0f - nxsq) * cosa; 2.63 + xform[0][1] = x * y * invcosa - z * sina; 2.64 + xform[0][2] = x * z * invcosa + y * sina; 2.65 + xform[1][0] = x * y * invcosa + z * sina; 2.66 + xform[1][1] = nysq + (1.0f - nysq) * cosa; 2.67 + xform[1][2] = y * z * invcosa - x * sina; 2.68 + xform[2][0] = x * z * invcosa - y * sina; 2.69 + xform[2][1] = y * z * invcosa + x * sina; 2.70 + xform[2][2] = nzsq + (1.0f - nzsq) * cosa; 2.71 + *this = *this * xform; 2.72 + } 2.73 + 2.74 + inline void perspective(float vfov_deg, float aspect, float znear, float zfar) 2.75 + { 2.76 + float vfov_rad = M_PI * vfov_deg / 180.0; 2.77 + float f = 1.0f / tan(vfov_rad * 0.5f); 2.78 + float dz = znear - zfar; 2.79 + 2.80 + Matrix4x4 xform; 2.81 + xform[0][0] = f / aspect; 2.82 + xform[1][1] = f; 2.83 + xform[2][2] = (zfar + znear) / dz; 2.84 + xform[3][2] = -1.0f; 2.85 + xform[2][3] = 2.0f * zfar * znear / dz; 2.86 + xform[3][3] = 0.0f; 2.87 + *this = *this * xform; 2.88 + } 2.89 + 2.90 + inline float *operator [](int idx) 2.91 + { 2.92 + return m[idx]; 2.93 + } 2.94 + 2.95 + inline const float *operator[](int idx) const 2.96 + { 2.97 + return m[idx]; 2.98 + } 2.99 +}; 2.100 + 2.101 +inline Matrix4x4 operator *(const Matrix4x4 &a, const Matrix4x4 &b) 2.102 +{ 2.103 + Matrix4x4 res; 2.104 + 2.105 + for(int i=0; i<4; i++) { 2.106 + for(int j=0; j<4; j++) { 2.107 + res.m[i][j] = a.m[i][0] * b.m[0][j] + a.m[i][1] * b.m[1][j] + a.m[i][2] * b.m[2][j] + a.m[i][3] * b.m[3][j]; 2.108 + } 2.109 + } 2.110 + return res; 2.111 +} 2.112 + 2.113 + 2.114 +#endif // MATRIX_H_ 2.115 \ No newline at end of file
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/mesh.cpp Sun Mar 09 06:27:58 2014 +0200 3.3 @@ -0,0 +1,66 @@ 3.4 +#define _USE_MATH_DEFINES 3.5 +#include <math.h> 3.6 +#include "mesh.h" 3.7 + 3.8 +#ifndef M_PI 3.9 +#define M_PI 3.14159265359 3.10 +#endif 3.11 + 3.12 +void gen_sphere(std::vector<Vertex> &varr, float rad, int usub, int vsub) 3.13 +{ 3.14 +} 3.15 + 3.16 +static void torus_vertex(Vertex *vert, float u, float v, float rad, float tube_rad) 3.17 +{ 3.18 + float theta = 2.0 * M_PI * u; 3.19 + float phi = 2.0 * M_PI * v; 3.20 + 3.21 + float x = 0.0f; 3.22 + float y = sin(phi) * tube_rad; 3.23 + float z = cos(phi) * tube_rad + rad; 3.24 + 3.25 + vert->pos.x = z * sin(theta); 3.26 + vert->pos.y = y; 3.27 + vert->pos.z = -x * sin(theta) + z * cos(theta); 3.28 + vert->pos.w = 1.0; 3.29 + 3.30 + Vector3 cent; 3.31 + cent.x = sin(theta) * rad; 3.32 + cent.z = cos(theta) * rad; 3.33 + 3.34 + vert->normal = normalize(Vector3(vert->pos) - cent); 3.35 + vert->color = Vector4(1.0, 1.0, 1.0, 1.0); 3.36 +} 3.37 + 3.38 +void gen_torus(std::vector<Vertex> &varr, float rad, float tube_rad, int usub, int vsub) 3.39 +{ 3.40 + int i, j; 3.41 + 3.42 + if(usub < 3) usub = 3; 3.43 + if(vsub < 3) vsub = 3; 3.44 + 3.45 + float du = 1.0f / (float)usub; 3.46 + float dv = 1.0f / (float)vsub; 3.47 + 3.48 + 3.49 + for(i=0; i<usub; i++) { 3.50 + float u = (float)i / (float)usub; 3.51 + for(j=0; j<vsub; j++) { 3.52 + float v = (float)j / (float)vsub; 3.53 + Vertex vert[4]; 3.54 + 3.55 + torus_vertex(vert, u, v, rad, tube_rad); 3.56 + torus_vertex(vert + 1, u, v + dv, rad, tube_rad); 3.57 + torus_vertex(vert + 2, u + du, v + dv, rad, tube_rad); 3.58 + torus_vertex(vert + 3, u + du, v, rad, tube_rad); 3.59 + 3.60 + varr.push_back(vert[0]); 3.61 + varr.push_back(vert[1]); 3.62 + varr.push_back(vert[2]); 3.63 + varr.push_back(vert[0]); 3.64 + varr.push_back(vert[2]); 3.65 + varr.push_back(vert[3]); 3.66 + 3.67 + } 3.68 + } 3.69 +} 3.70 \ No newline at end of file
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/mesh.h Sun Mar 09 06:27:58 2014 +0200 4.3 @@ -0,0 +1,18 @@ 4.4 +#ifndef MESH_H_ 4.5 +#define MESH_H_ 4.6 + 4.7 +#include <vector> 4.8 +#include "vmath.h" 4.9 + 4.10 +struct Vertex { 4.11 + Vector4 pos; 4.12 + Vector4 color; 4.13 + Vector3 normal; 4.14 +}; 4.15 + 4.16 + 4.17 +void gen_sphere(std::vector<Vertex> &varr, float rad, int usub, int vsub); 4.18 +void gen_torus(std::vector<Vertex> &varr, float rad, float tube_rad, int usub, int vsub); 4.19 + 4.20 + 4.21 +#endif // MESH_H_ 4.22 \ No newline at end of file
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test1.dsp Sun Mar 09 06:27:58 2014 +0200 5.3 @@ -0,0 +1,118 @@ 5.4 +# Microsoft Developer Studio Project File - Name="test1" - Package Owner=<4> 5.5 +# Microsoft Developer Studio Generated Build File, Format Version 6.00 5.6 +# ** DO NOT EDIT ** 5.7 + 5.8 +# TARGTYPE "Win32 (x86) Console Application" 0x0103 5.9 + 5.10 +CFG=test1 - Win32 Debug 5.11 +!MESSAGE This is not a valid makefile. To build this project using NMAKE, 5.12 +!MESSAGE use the Export Makefile command and run 5.13 +!MESSAGE 5.14 +!MESSAGE NMAKE /f "test1.mak". 5.15 +!MESSAGE 5.16 +!MESSAGE You can specify a configuration when running NMAKE 5.17 +!MESSAGE by defining the macro CFG on the command line. For example: 5.18 +!MESSAGE 5.19 +!MESSAGE NMAKE /f "test1.mak" CFG="test1 - Win32 Debug" 5.20 +!MESSAGE 5.21 +!MESSAGE Possible choices for configuration are: 5.22 +!MESSAGE 5.23 +!MESSAGE "test1 - Win32 Release" (based on "Win32 (x86) Console Application") 5.24 +!MESSAGE "test1 - Win32 Debug" (based on "Win32 (x86) Console Application") 5.25 +!MESSAGE 5.26 + 5.27 +# Begin Project 5.28 +# PROP AllowPerConfigDependencies 0 5.29 +# PROP Scc_ProjName "" 5.30 +# PROP Scc_LocalPath "" 5.31 +CPP=cl.exe 5.32 +RSC=rc.exe 5.33 + 5.34 +!IF "$(CFG)" == "test1 - Win32 Release" 5.35 + 5.36 +# PROP BASE Use_MFC 0 5.37 +# PROP BASE Use_Debug_Libraries 0 5.38 +# PROP BASE Output_Dir "Release" 5.39 +# PROP BASE Intermediate_Dir "Release" 5.40 +# PROP BASE Target_Dir "" 5.41 +# PROP Use_MFC 0 5.42 +# PROP Use_Debug_Libraries 0 5.43 +# PROP Output_Dir "Release" 5.44 +# PROP Intermediate_Dir "Release" 5.45 +# PROP Ignore_Export_Lib 0 5.46 +# PROP Target_Dir "" 5.47 +# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c 5.48 +# ADD CPP /nologo /G5 /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "__MSC__" /YX /FD /c 5.49 +# ADD BASE RSC /l 0x409 /d "NDEBUG" 5.50 +# ADD RSC /l 0x409 /d "NDEBUG" 5.51 +BSC32=bscmake.exe 5.52 +# ADD BASE BSC32 /nologo 5.53 +# ADD BSC32 /nologo 5.54 +LINK32=link.exe 5.55 +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 5.56 +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib glide2x.lib /nologo /subsystem:console /machine:I386 5.57 + 5.58 +!ELSEIF "$(CFG)" == "test1 - Win32 Debug" 5.59 + 5.60 +# PROP BASE Use_MFC 0 5.61 +# PROP BASE Use_Debug_Libraries 1 5.62 +# PROP BASE Output_Dir "Debug" 5.63 +# PROP BASE Intermediate_Dir "Debug" 5.64 +# PROP BASE Target_Dir "" 5.65 +# PROP Use_MFC 0 5.66 +# PROP Use_Debug_Libraries 1 5.67 +# PROP Output_Dir "Debug" 5.68 +# PROP Intermediate_Dir "Debug" 5.69 +# PROP Ignore_Export_Lib 0 5.70 +# PROP Target_Dir "" 5.71 +# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c 5.72 +# ADD CPP /nologo /G5 /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "__MSC__" /FR /YX /FD /GZ /c 5.73 +# ADD BASE RSC /l 0x409 /d "_DEBUG" 5.74 +# ADD RSC /l 0x409 /d "_DEBUG" 5.75 +BSC32=bscmake.exe 5.76 +# ADD BASE BSC32 /nologo 5.77 +# ADD BSC32 /nologo 5.78 +LINK32=link.exe 5.79 +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept 5.80 +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib glide2x.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept 5.81 + 5.82 +!ENDIF 5.83 + 5.84 +# Begin Target 5.85 + 5.86 +# Name "test1 - Win32 Release" 5.87 +# Name "test1 - Win32 Debug" 5.88 +# Begin Group "Source Files" 5.89 + 5.90 +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" 5.91 +# Begin Source File 5.92 + 5.93 +SOURCE=.\main.cpp 5.94 +# End Source File 5.95 +# Begin Source File 5.96 + 5.97 +SOURCE=.\mesh.cpp 5.98 +# End Source File 5.99 +# End Group 5.100 +# Begin Group "Header Files" 5.101 + 5.102 +# PROP Default_Filter "h;hpp;hxx;hm;inl" 5.103 +# Begin Source File 5.104 + 5.105 +SOURCE=.\matrix.h 5.106 +# End Source File 5.107 +# Begin Source File 5.108 + 5.109 +SOURCE=.\mesh.h 5.110 +# End Source File 5.111 +# Begin Source File 5.112 + 5.113 +SOURCE=.\vmath.h 5.114 +# End Source File 5.115 +# End Group 5.116 +# Begin Group "Resource Files" 5.117 + 5.118 +# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" 5.119 +# End Group 5.120 +# End Target 5.121 +# End Project
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test1.dsw Sun Mar 09 06:27:58 2014 +0200 6.3 @@ -0,0 +1,29 @@ 6.4 +Microsoft Developer Studio Workspace File, Format Version 6.00 6.5 +# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE! 6.6 + 6.7 +############################################################################### 6.8 + 6.9 +Project: "test1"=.\test1.dsp - Package Owner=<4> 6.10 + 6.11 +Package=<5> 6.12 +{{{ 6.13 +}}} 6.14 + 6.15 +Package=<4> 6.16 +{{{ 6.17 +}}} 6.18 + 6.19 +############################################################################### 6.20 + 6.21 +Global: 6.22 + 6.23 +Package=<5> 6.24 +{{{ 6.25 +}}} 6.26 + 6.27 +Package=<3> 6.28 +{{{ 6.29 +}}} 6.30 + 6.31 +############################################################################### 6.32 +
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/vmath.h Sun Mar 09 06:27:58 2014 +0200 7.3 @@ -0,0 +1,261 @@ 7.4 +#ifndef VMATH_H_ 7.5 +#define VMATH_H_ 7.6 + 7.7 +#include <math.h> 7.8 +#include "matrix.h" 7.9 + 7.10 +class Vector4; 7.11 + 7.12 +class Vector3 { 7.13 +public: 7.14 + float x, y, z; 7.15 + 7.16 + Vector3() : x(0), y(0), z(0) {} 7.17 + Vector3(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {} 7.18 + explicit inline Vector3(const Vector4 &v); 7.19 + 7.20 + inline float &operator [](int idx) 7.21 + { 7.22 + switch(idx) { 7.23 + case 0: 7.24 + return x; 7.25 + case 1: 7.26 + return y; 7.27 + case 2: 7.28 + default: 7.29 + return z; 7.30 + } 7.31 + } 7.32 + 7.33 + inline const float &operator [](int idx) const 7.34 + { 7.35 + switch(idx) { 7.36 + case 0: 7.37 + return x; 7.38 + case 1: 7.39 + return y; 7.40 + case 2: 7.41 + default: 7.42 + return z; 7.43 + } 7.44 + } 7.45 + 7.46 + inline float length() const 7.47 + { 7.48 + return sqrt(x * x + y * y + z * z); 7.49 + } 7.50 + 7.51 + inline void normalize() 7.52 + { 7.53 + float len = length(); 7.54 + if(len != 0) { 7.55 + x /= len; 7.56 + y /= len; 7.57 + z /= len; 7.58 + } 7.59 + } 7.60 +}; 7.61 + 7.62 +inline Vector3 operator +(const Vector3 &a, const Vector3 &b) 7.63 +{ 7.64 + return Vector3(a.x + b.x, a.y + b.y, a.z + b.z); 7.65 +} 7.66 + 7.67 +inline Vector3 operator -(const Vector3 &a, const Vector3 &b) 7.68 +{ 7.69 + return Vector3(a.x - b.x, a.y - b.y, a.z - b.z); 7.70 +} 7.71 + 7.72 +inline Vector3 operator *(const Vector3 &a, const Vector3 &b) 7.73 +{ 7.74 + return Vector3(a.x * b.x, a.y * b.y, a.z * b.z); 7.75 +} 7.76 + 7.77 +inline Vector3 operator *(const Vector3 &v, float s) 7.78 +{ 7.79 + return Vector3(v.x * s, v.y * s, v.z * s); 7.80 +} 7.81 + 7.82 +inline Vector3 operator *(float s, const Vector3 &v) 7.83 +{ 7.84 + return Vector3(v.x * s, v.y * s, v.z * s); 7.85 +} 7.86 + 7.87 +inline Vector3 operator /(const Vector3 &a, const Vector3 &b) 7.88 +{ 7.89 + return Vector3(a.x / b.x, a.y / b.y, a.z / b.z); 7.90 +} 7.91 + 7.92 +inline Vector3 operator /(const Vector3 &v, float s) 7.93 +{ 7.94 + return Vector3(v.x / s, v.y / s, v.z / s); 7.95 +} 7.96 + 7.97 +inline float dot(const Vector3 &a, const Vector3 &b) 7.98 +{ 7.99 + return a.x * b.x + a.y * b.y + a.z * b.z; 7.100 +} 7.101 + 7.102 +inline Vector3 cross(const Vector3 &a, const Vector3 &b) 7.103 +{ 7.104 + return Vector3(a.y * b.z - a.z * b.y, 7.105 + a.z * b.x - a.x * b.z, 7.106 + a.x * b.y - a.y * b.z); 7.107 +} 7.108 + 7.109 + 7.110 +inline float length(const Vector3 &v) 7.111 +{ 7.112 + return sqrt(v.x * v.x + v.y * v.y + v.z * v.z); 7.113 +} 7.114 + 7.115 +inline float length_sq(const Vector3 &v) 7.116 +{ 7.117 + return v.x * v.x + v.y * v.y + v.z * v.z; 7.118 +} 7.119 + 7.120 +inline Vector3 normalize(const Vector3 &v) 7.121 +{ 7.122 + float len = length(v); 7.123 + float s = len == 0 ? 1.0 : 1.0 / len; 7.124 + return v * s; 7.125 +} 7.126 + 7.127 +inline Vector3 transform(const Vector3 &v, const Matrix4x4 &m) 7.128 +{ 7.129 + return Vector3( 7.130 + m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3], 7.131 + m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3], 7.132 + m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3]); 7.133 +} 7.134 + 7.135 + 7.136 +// ---- Vector4 ---- 7.137 +class Vector4 { 7.138 +public: 7.139 + float x, y, z, w; 7.140 + 7.141 + Vector4() : x(0), y(0), z(0), w(1) {} 7.142 + Vector4(float xx, float yy, float zz, float ww = 1.0f) : x(xx), y(yy), z(zz), w(ww) {} 7.143 + explicit Vector4(const Vector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0f) {} 7.144 + 7.145 + inline float &operator [](int idx) 7.146 + { 7.147 + switch(idx) { 7.148 + case 0: 7.149 + return x; 7.150 + case 1: 7.151 + return y; 7.152 + case 2: 7.153 + return z; 7.154 + default: 7.155 + case 3: 7.156 + return w; 7.157 + } 7.158 + } 7.159 + 7.160 + inline const float &operator [](int idx) const 7.161 + { 7.162 + switch(idx) { 7.163 + case 0: 7.164 + return x; 7.165 + case 1: 7.166 + return y; 7.167 + case 2: 7.168 + return z; 7.169 + case 3: 7.170 + default: 7.171 + return w; 7.172 + } 7.173 + } 7.174 + 7.175 + inline float length() const 7.176 + { 7.177 + return sqrt(x * x + y * y + z * z + w * w); 7.178 + } 7.179 + 7.180 + inline void normalize() 7.181 + { 7.182 + float len = length(); 7.183 + if(len != 0) { 7.184 + x /= len; 7.185 + y /= len; 7.186 + z /= len; 7.187 + w /= len; 7.188 + } 7.189 + } 7.190 +}; 7.191 + 7.192 +inline Vector4 operator +(const Vector4 &a, const Vector4 &b) 7.193 +{ 7.194 + return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); 7.195 +} 7.196 + 7.197 +inline Vector4 operator -(const Vector4 &a, const Vector4 &b) 7.198 +{ 7.199 + return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); 7.200 +} 7.201 + 7.202 +inline Vector4 operator *(const Vector4 &a, const Vector4 &b) 7.203 +{ 7.204 + return Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w); 7.205 +} 7.206 + 7.207 +inline Vector4 operator *(const Vector4 &v, float s) 7.208 +{ 7.209 + return Vector4(v.x * s, v.y * s, v.z * s, v.w * s); 7.210 +} 7.211 + 7.212 +inline Vector4 operator *(float s, const Vector4 &v) 7.213 +{ 7.214 + return Vector4(v.x * s, v.y * s, v.z * s, v.w * s); 7.215 +} 7.216 + 7.217 +inline Vector4 operator /(const Vector4 &a, const Vector4 &b) 7.218 +{ 7.219 + return Vector4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w); 7.220 +} 7.221 + 7.222 +inline Vector4 operator /(const Vector4 &v, float s) 7.223 +{ 7.224 + return Vector4(v.x / s, v.y / s, v.z / s, v.w / s); 7.225 +} 7.226 + 7.227 +inline float dot(const Vector4 &a, const Vector4 &b) 7.228 +{ 7.229 + return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; 7.230 +} 7.231 + 7.232 +inline float length(const Vector4 &v) 7.233 +{ 7.234 + return sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); 7.235 +} 7.236 + 7.237 +inline float length_sq(const Vector4 &v) 7.238 +{ 7.239 + return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w; 7.240 +} 7.241 + 7.242 +inline Vector4 normalize(const Vector4 &v) 7.243 +{ 7.244 + float len = length(v); 7.245 + float s = len == 0 ? 1.0 : 1.0 / len; 7.246 + return v * s; 7.247 +} 7.248 + 7.249 +inline Vector4 transform(const Vector4 &v, const Matrix4x4 &m) 7.250 +{ 7.251 + return Vector4( 7.252 + m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w, 7.253 + m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w, 7.254 + m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w, 7.255 + m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w); 7.256 +} 7.257 + 7.258 +inline Vector3::Vector3(const Vector4 &v) 7.259 + : x(v.x), y(v.y), z(v.z) 7.260 +{ 7.261 +} 7.262 + 7.263 + 7.264 +#endif // VMATH_H_ 7.265 \ No newline at end of file