Skip to content

Commit

Permalink
code cleanup, c++ -pedantic checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ssloy committed Jan 23, 2015
1 parent 1cce852 commit b44188f
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 412 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SYSCONF_LINK = g++
CPPFLAGS = -O3
CPPFLAGS = -Wall -Wextra -Weffc++ -Werror -pedantic -std=c++98
LDFLAGS = -O3
LIBS = -lm

Expand Down
10 changes: 7 additions & 3 deletions geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
#include <iostream>
#include "geometry.h"

template <> template <> Vec3<int>::Vec3<>(const Vec3<float> &v) : x(int(v.x+.5)), y(int(v.y+.5)), z(int(v.z+.5)) {
}
template <> Vec3<float>::Vec3(Matrix m) : x(m[0][0]/m[3][0]), y(m[1][0]/m[3][0]), z(m[2][0]/m[3][0]) {}
template <> template <> Vec3<int>::Vec3<>(const Vec3<float> &v) : x(int(v.x+.5)), y(int(v.y+.5)), z(int(v.z+.5)) {}
template <> template <> Vec3<float>::Vec3<>(const Vec3<int> &v) : x(v.x), y(v.y), z(v.z) {}

template <> template <> Vec3<float>::Vec3<>(const Vec3<int> &v) : x(v.x), y(v.y), z(v.z) {
Matrix::Matrix(Vec3f v) : m(std::vector<std::vector<float> >(4, std::vector<float>(1, 1.f))), rows(4), cols(1) {
m[0][0] = v.x;
m[1][0] = v.y;
m[2][0] = v.z;
}


Expand Down
38 changes: 10 additions & 28 deletions geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,35 @@
#define __GEOMETRY_H__

#include <cmath>
#include <vector>

class Matrix;

template <class t> struct Vec2 {
t x, y;
Vec2<t>() : x(t()), y(t()) {}
Vec2<t>(t _x, t _y) : x(_x), y(_y) {}
Vec2<t>(const Vec2<t> &v) : x(t()), y(t()) { *this = v; }
Vec2<t> & operator =(const Vec2<t> &v) {
if (this != &v) {
x = v.x;
y = v.y;
}
return *this;
}
Vec2<t> operator +(const Vec2<t> &V) const { return Vec2<t>(x+V.x, y+V.y); }
Vec2<t> operator -(const Vec2<t> &V) const { return Vec2<t>(x-V.x, y-V.y); }
Vec2<t> operator *(float f) const { return Vec2<t>(x*f, y*f); }
t& operator[](const int i) { if (x<=0) return x; else return y; }
t& operator[](const int i) { return i<=0 ? x : y; }
template <class > friend std::ostream& operator<<(std::ostream& s, Vec2<t>& v);
};

template <class t> struct Vec3 {
t x, y, z;
Vec3<t>() : x(t()), y(t()), z(t()) { }
Vec3<t>(t _x, t _y, t _z) : x(_x), y(_y), z(_z) {}
Vec3<t>(Matrix m);
template <class u> Vec3<t>(const Vec3<u> &v);
Vec3<t>(const Vec3<t> &v) : x(t()), y(t()), z(t()) { *this = v; }
Vec3<t> & operator =(const Vec3<t> &v) {
if (this != &v) {
x = v.x;
y = v.y;
z = v.z;
}
return *this;
}
Vec3<t> operator ^(const Vec3<t> &v) const { return Vec3<t>(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); }
Vec3<t> operator +(const Vec3<t> &v) const { return Vec3<t>(x+v.x, y+v.y, z+v.z); }
Vec3<t> operator -(const Vec3<t> &v) const { return Vec3<t>(x-v.x, y-v.y, z-v.z); }
Vec3<t> operator *(float f) const { return Vec3<t>(x*f, y*f, z*f); }
t operator *(const Vec3<t> &v) const { return x*v.x + y*v.y + z*v.z; }
float norm () const { return std::sqrt(x*x+y*y+z*z); }
Vec3<t> & normalize(t l=1) { *this = (*this)*(l/norm()); return *this; }
t& operator[](const int i) { if (i<=0) return x; else if (i==1) return y; else return z; }
t& operator[](const int i) { return i<=0 ? x : (1==i ? y : z); }
template <class > friend std::ostream& operator<<(std::ostream& s, Vec3<t>& v);
};

Expand All @@ -68,26 +55,21 @@ template <class t> std::ostream& operator<<(std::ostream& s, Vec3<t>& v) {

//////////////////////////////////////////////////////////////////////////////////////////////

const int DEFAULT_ALLOC=4;

class Matrix {
std::vector<std::vector<float> > m;
int rows, cols;
public:
Matrix(int r=DEFAULT_ALLOC, int c=DEFAULT_ALLOC);
inline int nrows();
inline int ncols();

Matrix(int r=4, int c=4);
Matrix(Vec3f v);
int nrows();
int ncols();
static Matrix identity(int dimensions);
std::vector<float>& operator[](const int i);
Matrix operator*(const Matrix& a);
Matrix transpose();
Matrix inverse();

friend std::ostream& operator<<(std::ostream& s, Matrix& m);
};

/////////////////////////////////////////////////////////////////////////////////////////////


#endif //__GEOMETRY_H__
20 changes: 3 additions & 17 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@ int *zbuffer = NULL;
Vec3f light_dir(0,0,-1);
Vec3f camera(0,0,3);

Vec3f m2v(Matrix m) {
return Vec3f(m[0][0]/m[3][0], m[1][0]/m[3][0], m[2][0]/m[3][0]);
}

Matrix v2m(Vec3f v) {
Matrix m(4, 1);
m[0][0] = v.x;
m[1][0] = v.y;
m[2][0] = v.z;
m[3][0] = 1.f;
return m;
}

Matrix viewport(int x, int y, int w, int h) {
Matrix m = Matrix::identity(4);
m[0][3] = x+w/2.f;
Expand Down Expand Up @@ -57,14 +44,13 @@ void triangle(Vec3i t0, Vec3i t1, Vec3i t2, Vec2i uv0, Vec2i uv1, Vec2i uv2, TGA
Vec2i uvB = second_half ? uv1 + (uv2-uv1)*beta : uv0 + (uv1-uv0)*beta;
if (A.x>B.x) { std::swap(A, B); std::swap(uvA, uvB); }
for (int j=A.x; j<=B.x; j++) {
float phi = B.x==A.x ? 1. : (float)(j-A.x)/(float)(B.x-A.x);
float phi = B.x==A.x ? 1. : (float)(j-A.x)/(B.x-A.x);
Vec3i P = Vec3f(A) + Vec3f(B-A)*phi;
Vec2i uvP = uvA + (uvB-uvA)*phi;
int idx = P.x+P.y*width;
if (zbuffer[idx]<P.z) {
zbuffer[idx] = P.z;
TGAColor color = model->diffuse(uvP);
image.set(P.x, P.y, TGAColor(color.r*intensity, color.g*intensity, color.b*intensity));
image.set(P.x, P.y, model->diffuse(uvP)*intensity);
}
}
}
Expand Down Expand Up @@ -94,7 +80,7 @@ int main(int argc, char** argv) {
Vec3f world_coords[3];
for (int j=0; j<3; j++) {
Vec3f v = model->vert(face[j]);
screen_coords[j] = m2v(ViewPort*Projection*v2m(v));
screen_coords[j] = Vec3f(ViewPort*Projection*Matrix(v));
world_coords[j] = v;
}
Vec3f n = (world_coords[2]-world_coords[0])^(world_coords[1]-world_coords[0]);
Expand Down
2 changes: 1 addition & 1 deletion model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <vector>
#include "model.h"

Model::Model(const char *filename) : verts_(), faces_(), norms_(), uv_() {
Model::Model(const char *filename) : verts_(), faces_(), norms_(), uv_(), diffusemap_() {
std::ifstream in;
in.open (filename, std::ifstream::in);
if (in.fail()) return;
Expand Down
Loading

0 comments on commit b44188f

Please sign in to comment.