Skip to content

Commit

Permalink
vertex shader stub
Browse files Browse the repository at this point in the history
  • Loading branch information
ssloy committed Jan 25, 2015
1 parent b04fb2c commit 440d9cd
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 68 deletions.
4 changes: 0 additions & 4 deletions geometry.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include <vector>
#include <cassert>
#include <cmath>
#include <iostream>
#include "geometry.h"

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]) {}
Expand All @@ -14,7 +11,6 @@ Matrix::Matrix(Vec3f v) : m(std::vector<std::vector<float> >(4, std::vector<floa
m[2][0] = v.z;
}


Matrix::Matrix(int r, int c) : m(std::vector<std::vector<float> >(r, std::vector<float>(c, 0.f))), rows(r), cols(c) { }

int Matrix::nrows() {
Expand Down
1 change: 1 addition & 0 deletions geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <cmath>
#include <vector>
#include <iostream>

class Matrix;

Expand Down
23 changes: 10 additions & 13 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include <vector>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <limits>
#include "tgaimage.h"
#include "model.h"
#include "geometry.h"
Expand All @@ -16,24 +13,30 @@ Vec3f light_dir = Vec3f(1,1,1).normalize();
Vec3f eye(1,1,3);
Vec3f center(0,0,0);


struct Shader : public IShader {
virtual ~Shader() {}
Vec2i varying_uv[3];
float varying_inty[3];

virtual Vec3i vertex(int iface, int nthvert) {
varying_inty[nthvert] = model->normal(iface, nthvert)*light_dir;
varying_uv[nthvert] = model->uv(iface, nthvert);
Vec3f v = model->vert(iface, nthvert);
Vec3f gl_Position = Viewport*Projection*ModelView*Matrix(v);
return gl_Position;
}

virtual bool fragment(Vec3f bar, TGAColor &color) {
Vec2i uv = varying_uv[0]*bar.x + varying_uv[1]*bar.y + varying_uv[2]*bar.z;
// float inty = varying_inty[0]*bar.x + varying_inty[1]*bar.y + varying_inty[2]*bar.z;
// inty = std::max(0.f, std::min(1.f, inty));
// color = model->diffuse(uv)*inty;
float inty = model->norm(uv)*light_dir;
float inty = model->normal(uv)*light_dir;
color = model->diffuse(uv)*inty;
return false;
}
};


int main(int argc, char** argv) {
if (2==argc) {
model = new Model(argv[1]);
Expand All @@ -48,15 +51,9 @@ int main(int argc, char** argv) {
TGAImage image(width, height, TGAImage::RGB);
Shader shader;
for (int i=0; i<model->nfaces(); i++) {
std::vector<int> face = model->face(i);
Vec3i screen_coords[3];
Vec3f world_coords[3];
for (int j=0; j<3; j++) {
Vec3f v = model->vert(face[j]);
screen_coords[j] = Vec3f(Viewport*Projection*ModelView*Matrix(v));
world_coords[j] = v;
shader.varying_inty[j] = model->norm(i, j)*light_dir;
shader.varying_uv[j] = model->uv(i, j);
screen_coords[j] = shader.vertex(i, j);
}
triangle(screen_coords, shader, image, zbuffer);
}
Expand Down
20 changes: 10 additions & 10 deletions model.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include "model.h"

Model::Model(const char *filename) : verts_(), faces_(), norms_(), uv_(), diffusemap_(), normalmap_() {
Expand Down Expand Up @@ -45,8 +43,7 @@ Model::Model(const char *filename) : verts_(), faces_(), norms_(), uv_(), diffus
load_texture(filename, "_nm.tga", normalmap_);
}

Model::~Model() {
}
Model::~Model() {}

int Model::nverts() {
return (int)verts_.size();
Expand All @@ -66,6 +63,10 @@ Vec3f Model::vert(int i) {
return verts_[i];
}

Vec3f Model::vert(int iface, int nthvert) {
return verts_[faces_[iface][nthvert][0]];
}

void Model::load_texture(std::string filename, const char *suffix, TGAImage &img) {
std::string texfile(filename);
size_t dot = texfile.find_last_of(".");
Expand All @@ -80,22 +81,21 @@ TGAColor Model::diffuse(Vec2i uv) {
return diffusemap_.get(uv.x, uv.y);
}

Vec3f Model::norm(Vec2i uv) {
Vec3f Model::normal(Vec2i uv) {
TGAColor c = normalmap_.get(uv.x, uv.y);
Vec3f res;
for (int i=0; i<3; i++)
res[2-i] = (float)c[i]/255.f*2.f - 1.f;
return res;
}

Vec2i Model::uv(int iface, int nvert) {
int idx = faces_[iface][nvert][1];
Vec2i Model::uv(int iface, int nthvert) {
int idx = faces_[iface][nthvert][1];
return Vec2i(uv_[idx].x*diffusemap_.get_width(), uv_[idx].y*diffusemap_.get_height());
}

Vec3f Model::norm(int iface, int nvert) {
int idx = faces_[iface][nvert][2];
Vec3f Model::normal(int iface, int nthvert) {
int idx = faces_[iface][nthvert][2];
return norms_[idx].normalize();
}


8 changes: 5 additions & 3 deletions model.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __MODEL_H__

#include <vector>
#include <string>
#include "geometry.h"
#include "tgaimage.h"

Expand All @@ -19,10 +20,11 @@ class Model {
~Model();
int nverts();
int nfaces();
Vec3f norm(int iface, int nvert);
Vec3f norm(Vec2i uv);
Vec3f normal(int iface, int nthvert);
Vec3f normal(Vec2i uv);
Vec3f vert(int i);
Vec2i uv(int iface, int nvert);
Vec3f vert(int iface, int nthvert);
Vec2i uv(int iface, int nthvert);
TGAColor diffuse(Vec2i uv);
std::vector<int> face(int idx);
};
Expand Down
42 changes: 15 additions & 27 deletions our_gl.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,37 @@
#include <vector>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <limits>
#include "tgaimage.h"
#include "model.h"
#include "geometry.h"
#include "our_gl.h"

Matrix ModelView;
Matrix Viewport;
Matrix Projection;

Matrix viewport(int x, int y, int w, int h) {
Matrix m = Matrix::identity(4);
m[0][3] = x+w/2.f;
m[1][3] = y+h/2.f;
m[2][3] = 255.f/2.f;

m[0][0] = w/2.f;
m[1][1] = h/2.f;
m[2][2] = 255.f/2.f;
Viewport = m;
return m;
void viewport(int x, int y, int w, int h) {
Viewport = Matrix::identity(4);
Viewport[0][3] = x+w/2.f;
Viewport[1][3] = y+h/2.f;
Viewport[2][3] = 255.f/2.f;
Viewport[0][0] = w/2.f;
Viewport[1][1] = h/2.f;
Viewport[2][2] = 255.f/2.f;
}

Matrix projection(float coeff) {
void projection(float coeff) {
Projection = Matrix::identity(4);
Projection[3][2] = coeff;
return Projection;
}

Matrix lookat(Vec3f eye, Vec3f center, Vec3f up) {
void lookat(Vec3f eye, Vec3f center, Vec3f up) {
Vec3f z = (eye-center).normalize();
Vec3f x = (up^z).normalize();
Vec3f y = (z^x).normalize();
Matrix res = Matrix::identity(4);
ModelView = Matrix::identity(4);
for (int i=0; i<3; i++) {
res[0][i] = x[i];
res[1][i] = y[i];
res[2][i] = z[i];
res[i][3] = -center[i];
ModelView[0][i] = x[i];
ModelView[1][i] = y[i];
ModelView[2][i] = z[i];
ModelView[i][3] = -center[i];
}
ModelView = res;
return res;
}

Vec3f barycentric(Vec3i A, Vec3i B, Vec3i C, Vec3i P) {
Expand Down
15 changes: 4 additions & 11 deletions our_gl.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
#ifndef __OUR_GL_H__
#define __OUR_GL_H__

#include <vector>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <limits>
#include "tgaimage.h"
#include "model.h"
#include "geometry.h"
#include "our_gl.h"

extern Matrix ModelView;
extern Matrix Viewport;
extern Matrix Projection;

Matrix viewport(int x, int y, int w, int h);
Matrix projection(float coeff=0.f);
Matrix lookat(Vec3f eye, Vec3f center, Vec3f up);
void viewport(int x, int y, int w, int h);
void projection(float coeff=0.f); // coeff = -1/c
void lookat(Vec3f eye, Vec3f center, Vec3f up);

struct IShader {
virtual ~IShader() {}
virtual Vec3i vertex(int iface, int nthvert) = 0;
virtual bool fragment(Vec3f bar, TGAColor &color) = 0;
};

void triangle(Vec3i *pts, IShader &shader, TGAImage &image, TGAImage &zbuffer);

#endif //__OUR_GL_H__

0 comments on commit 440d9cd

Please sign in to comment.