2016-04-27 03:52:52 +00:00
|
|
|
#include "TinyRenderer.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <limits>
|
|
|
|
#include <iostream>
|
2016-04-28 19:28:04 +00:00
|
|
|
#include "tgaimage.h"
|
|
|
|
#include "model.h"
|
|
|
|
#include "geometry.h"
|
|
|
|
#include "our_gl.h"
|
2016-04-27 04:08:02 +00:00
|
|
|
#include "../Utils/b3ResourcePath.h"
|
2016-04-27 03:52:52 +00:00
|
|
|
#include "Bullet3Common/b3MinMax.h"
|
2016-04-28 19:28:04 +00:00
|
|
|
#include "../OpenGLWindow/ShapeData.h"
|
2016-05-13 06:03:12 +00:00
|
|
|
#include "LinearMath/btAlignedObjectArray.h"
|
|
|
|
#include "LinearMath/btVector3.h"
|
2016-06-01 18:04:10 +00:00
|
|
|
|
2016-04-27 03:52:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct Shader : public IShader {
|
|
|
|
|
|
|
|
Model* m_model;
|
|
|
|
Vec3f m_light_dir_local;
|
2016-05-25 22:37:28 +00:00
|
|
|
Matrix& m_modelMat;
|
2016-06-01 18:04:10 +00:00
|
|
|
Matrix m_invModelMat;
|
|
|
|
|
2016-05-25 22:37:28 +00:00
|
|
|
Matrix& m_modelView1;
|
2016-04-27 03:52:52 +00:00
|
|
|
Matrix& m_projectionMatrix;
|
2016-05-27 00:36:01 +00:00
|
|
|
Vec3f m_localScaling;
|
2016-06-02 00:47:41 +00:00
|
|
|
Vec4f m_colorRGBA;
|
2016-05-27 00:36:01 +00:00
|
|
|
|
2016-04-27 03:52:52 +00:00
|
|
|
mat<2,3,float> varying_uv; // triangle uv coordinates, written by the vertex shader, read by the fragment shader
|
|
|
|
mat<4,3,float> varying_tri; // triangle coordinates (clip coordinates), written by VS, read by FS
|
|
|
|
mat<3,3,float> varying_nrm; // normal per vertex to be interpolated by FS
|
2016-05-27 00:36:01 +00:00
|
|
|
//mat<3,3,float> ndc_tri; // triangle in normalized device coordinates
|
2016-04-27 03:52:52 +00:00
|
|
|
|
2016-06-02 00:47:41 +00:00
|
|
|
Shader(Model* model, Vec3f light_dir_local, Matrix& modelView, Matrix& projectionMatrix, Matrix& modelMat, Vec3f localScaling, const Vec4f& colorRGBA)
|
2016-04-27 03:52:52 +00:00
|
|
|
:m_model(model),
|
|
|
|
m_light_dir_local(light_dir_local),
|
2016-05-25 22:37:28 +00:00
|
|
|
m_modelView1(modelView),
|
|
|
|
m_projectionMatrix(projectionMatrix),
|
2016-05-27 00:36:01 +00:00
|
|
|
m_modelMat(modelMat),
|
2016-06-02 00:47:41 +00:00
|
|
|
m_localScaling(localScaling),
|
|
|
|
m_colorRGBA(colorRGBA)
|
2016-04-27 03:52:52 +00:00
|
|
|
{
|
2016-06-01 18:04:10 +00:00
|
|
|
m_invModelMat = m_modelMat.invert_transpose();
|
2016-04-27 03:52:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Vec4f vertex(int iface, int nthvert) {
|
2016-04-28 19:28:04 +00:00
|
|
|
|
|
|
|
Vec2f uv = m_model->uv(iface, nthvert);
|
|
|
|
//printf("uv = %f,%f\n", uv.x,uv.y);
|
|
|
|
varying_uv.set_col(nthvert, uv);
|
|
|
|
|
2016-05-25 22:37:28 +00:00
|
|
|
//varying_nrm.set_col(nthvert, proj<3>((m_projectionMatrix*m_modelView).invert_transpose()*embed<4>(m_model->normal(iface, nthvert), 0.f)));
|
2016-06-01 18:04:10 +00:00
|
|
|
varying_nrm.set_col(nthvert, proj<3>(m_invModelMat*embed<4>(m_model->normal(iface, nthvert), 0.f)));
|
|
|
|
//m_localNormal = m_model->normal(iface, nthvert);
|
|
|
|
//varying_nrm.set_col(nthvert, m_model->normal(iface, nthvert));
|
|
|
|
|
2016-05-27 00:36:01 +00:00
|
|
|
Vec3f unScaledVert = m_model->vert(iface, nthvert);
|
2016-06-01 18:04:10 +00:00
|
|
|
|
2016-05-27 00:36:01 +00:00
|
|
|
Vec3f scaledVert=Vec3f(unScaledVert[0]*m_localScaling[0],unScaledVert[1]*m_localScaling[1],unScaledVert[2]*m_localScaling[2]);
|
2016-06-01 18:04:10 +00:00
|
|
|
|
|
|
|
Vec4f gl_Vertex = m_projectionMatrix*m_modelView1*embed<4>(scaledVert);
|
2016-05-27 00:36:01 +00:00
|
|
|
|
2016-04-27 03:52:52 +00:00
|
|
|
varying_tri.set_col(nthvert, gl_Vertex);
|
2016-05-27 00:36:01 +00:00
|
|
|
//ndc_tri.set_col(nthvert, proj<3>(gl_Vertex/gl_Vertex[3]));
|
2016-04-27 03:52:52 +00:00
|
|
|
return gl_Vertex;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool fragment(Vec3f bar, TGAColor &color) {
|
|
|
|
Vec3f bn = (varying_nrm*bar).normalize();
|
|
|
|
Vec2f uv = varying_uv*bar;
|
|
|
|
|
2016-05-27 00:36:01 +00:00
|
|
|
//float diff = 1;//full-bright
|
|
|
|
float ambient = 0.7;
|
2016-06-01 18:04:10 +00:00
|
|
|
//float diff = ambient+b3Min(b3Max(0.f, bn*light_dir_world),(1-ambient));
|
|
|
|
float diff = ambient+b3Min(b3Max(0.f, bn*m_light_dir_local),(1-ambient));
|
2016-05-06 20:57:48 +00:00
|
|
|
//float diff = b3Max(0.f, n*m_light_dir_local);
|
2016-04-27 03:52:52 +00:00
|
|
|
color = m_model->diffuse(uv)*diff;
|
2016-06-02 20:54:52 +00:00
|
|
|
|
|
|
|
//warning: bgra color is swapped to rgba to upload texture
|
|
|
|
color.bgra[0] *= m_colorRGBA[0];
|
|
|
|
color.bgra[1] *= m_colorRGBA[1];
|
|
|
|
color.bgra[2] *= m_colorRGBA[2];
|
|
|
|
color.bgra[3] *= m_colorRGBA[3];
|
|
|
|
|
2016-04-27 03:52:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-06-07 23:11:58 +00:00
|
|
|
TinyRenderObjectData::TinyRenderObjectData(TGAImage& rgbColorBuffer,b3AlignedObjectArray<float>&depthBuffer)
|
|
|
|
:m_rgbColorBuffer(rgbColorBuffer),
|
2016-04-28 19:28:04 +00:00
|
|
|
m_depthBuffer(depthBuffer),
|
|
|
|
m_model(0),
|
|
|
|
m_userData(0),
|
|
|
|
m_userIndex(-1)
|
2016-04-27 03:52:52 +00:00
|
|
|
{
|
|
|
|
Vec3f eye(1,1,3);
|
|
|
|
Vec3f center(0,0,0);
|
2016-05-18 06:57:19 +00:00
|
|
|
Vec3f up(0,0,1);
|
2016-05-27 00:36:01 +00:00
|
|
|
m_lightDirWorld.setValue(0,0,0);
|
|
|
|
m_localScaling.setValue(1,1,1);
|
2016-04-28 19:28:04 +00:00
|
|
|
m_modelMatrix = Matrix::identity();
|
|
|
|
|
|
|
|
}
|
2016-04-27 03:52:52 +00:00
|
|
|
|
2016-04-28 19:28:04 +00:00
|
|
|
void TinyRenderObjectData::loadModel(const char* fileName)
|
|
|
|
{
|
2016-04-27 03:52:52 +00:00
|
|
|
//todo(erwincoumans) move the file loading out of here
|
|
|
|
char relativeFileName[1024];
|
|
|
|
if (!b3ResourcePath::findResourcePath(fileName, relativeFileName, 1024))
|
|
|
|
{
|
|
|
|
printf("Cannot find file %s\n", fileName);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
m_model = new Model(relativeFileName);
|
2016-04-28 19:28:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-02 00:47:41 +00:00
|
|
|
void TinyRenderObjectData::registerMeshShape(const float* vertices, int numVertices,const int* indices, int numIndices, const float rgbaColor[4],
|
2016-05-18 06:57:19 +00:00
|
|
|
unsigned char* textureImage, int textureWidth, int textureHeight)
|
2016-04-28 19:28:04 +00:00
|
|
|
{
|
|
|
|
if (0==m_model)
|
|
|
|
{
|
|
|
|
m_model = new Model();
|
2016-06-02 00:47:41 +00:00
|
|
|
m_model->setColorRGBA(rgbaColor);
|
2016-05-18 06:57:19 +00:00
|
|
|
if (textureImage)
|
2016-04-28 19:28:04 +00:00
|
|
|
{
|
2016-05-18 06:57:19 +00:00
|
|
|
m_model->setDiffuseTextureFromData(textureImage,textureWidth,textureHeight);
|
|
|
|
} else
|
|
|
|
{
|
2016-06-02 00:47:41 +00:00
|
|
|
/*char relativeFileName[1024];
|
2016-05-18 06:57:19 +00:00
|
|
|
if (b3ResourcePath::findResourcePath("floor_diffuse.tga", relativeFileName, 1024))
|
|
|
|
{
|
|
|
|
m_model->loadDiffuseTexture(relativeFileName);
|
|
|
|
}
|
2016-06-02 00:47:41 +00:00
|
|
|
*/
|
2016-04-28 19:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0;i<numVertices;i++)
|
|
|
|
{
|
|
|
|
m_model->addVertex(vertices[i*9],
|
|
|
|
vertices[i*9+1],
|
|
|
|
vertices[i*9+2],
|
|
|
|
vertices[i*9+4],
|
|
|
|
vertices[i*9+5],
|
|
|
|
vertices[i*9+6],
|
|
|
|
vertices[i*9+7],
|
|
|
|
vertices[i*9+8]);
|
|
|
|
}
|
|
|
|
for (int i=0;i<numIndices;i+=3)
|
|
|
|
{
|
|
|
|
m_model->addTriangle(indices[i],indices[i],indices[i],
|
|
|
|
indices[i+1],indices[i+1],indices[i+1],
|
|
|
|
indices[i+2],indices[i+2],indices[i+2]);
|
|
|
|
}
|
2016-04-27 03:52:52 +00:00
|
|
|
}
|
2016-04-28 19:28:04 +00:00
|
|
|
}
|
2016-05-13 06:03:12 +00:00
|
|
|
|
|
|
|
void TinyRenderObjectData::registerMesh2(btAlignedObjectArray<btVector3>& vertices, btAlignedObjectArray<btVector3>& normals,btAlignedObjectArray<int>& indices)
|
|
|
|
{
|
|
|
|
if (0==m_model)
|
|
|
|
{
|
|
|
|
int numVertices = vertices.size();
|
|
|
|
int numIndices = indices.size();
|
|
|
|
|
|
|
|
m_model = new Model();
|
|
|
|
char relativeFileName[1024];
|
|
|
|
if (b3ResourcePath::findResourcePath("floor_diffuse.tga", relativeFileName, 1024))
|
|
|
|
{
|
|
|
|
m_model->loadDiffuseTexture(relativeFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0;i<numVertices;i++)
|
|
|
|
{
|
|
|
|
m_model->addVertex(vertices[i].x(),
|
|
|
|
vertices[i].y(),
|
|
|
|
vertices[i].z(),
|
|
|
|
normals[i].x(),
|
|
|
|
normals[i].y(),
|
|
|
|
normals[i].z(),
|
|
|
|
0.5,0.5);
|
|
|
|
}
|
|
|
|
for (int i=0;i<numIndices;i+=3)
|
|
|
|
{
|
|
|
|
m_model->addTriangle(indices[i],indices[i],indices[i],
|
|
|
|
indices[i+1],indices[i+1],indices[i+1],
|
|
|
|
indices[i+2],indices[i+2],indices[i+2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 19:28:04 +00:00
|
|
|
void TinyRenderObjectData::createCube(float halfExtentsX,float halfExtentsY,float halfExtentsZ)
|
|
|
|
{
|
|
|
|
m_model = new Model();
|
2016-04-27 03:52:52 +00:00
|
|
|
|
2016-04-28 19:28:04 +00:00
|
|
|
char relativeFileName[1024];
|
|
|
|
if (b3ResourcePath::findResourcePath("floor_diffuse.tga", relativeFileName, 1024))
|
|
|
|
{
|
|
|
|
m_model->loadDiffuseTexture(relativeFileName);
|
|
|
|
}
|
2016-04-27 03:52:52 +00:00
|
|
|
|
2016-04-28 19:28:04 +00:00
|
|
|
|
|
|
|
int strideInBytes = 9*sizeof(float);
|
|
|
|
int numVertices = sizeof(cube_vertices_textured)/strideInBytes;
|
|
|
|
int numIndices = sizeof(cube_indices)/sizeof(int);
|
|
|
|
|
|
|
|
for (int i=0;i<numVertices;i++)
|
|
|
|
{
|
|
|
|
m_model->addVertex(halfExtentsX*cube_vertices_textured[i*9],
|
|
|
|
halfExtentsY*cube_vertices_textured[i*9+1],
|
|
|
|
halfExtentsY*cube_vertices_textured[i*9+2],
|
|
|
|
cube_vertices_textured[i*9+4],
|
|
|
|
cube_vertices_textured[i*9+5],
|
|
|
|
cube_vertices_textured[i*9+6],
|
|
|
|
cube_vertices_textured[i*9+7],
|
|
|
|
cube_vertices_textured[i*9+8]);
|
|
|
|
}
|
|
|
|
for (int i=0;i<numIndices;i+=3)
|
|
|
|
{
|
|
|
|
m_model->addTriangle(cube_indices[i],cube_indices[i],cube_indices[i],
|
|
|
|
cube_indices[i+1],cube_indices[i+1],cube_indices[i+1],
|
|
|
|
cube_indices[i+2],cube_indices[i+2],cube_indices[i+2]);
|
|
|
|
}
|
|
|
|
|
2016-04-27 03:52:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TinyRenderObjectData::~TinyRenderObjectData()
|
|
|
|
{
|
|
|
|
delete m_model;
|
|
|
|
}
|
|
|
|
|
2016-06-08 00:02:47 +00:00
|
|
|
void TinyRenderer::renderObject(TinyRenderObjectData& renderData)
|
2016-04-27 03:52:52 +00:00
|
|
|
{
|
2016-06-08 00:02:47 +00:00
|
|
|
int width = renderData.m_rgbColorBuffer.get_width();
|
|
|
|
int height = renderData.m_rgbColorBuffer.get_height();
|
|
|
|
|
2016-06-01 18:04:10 +00:00
|
|
|
Vec3f light_dir_local = Vec3f(renderData.m_lightDirWorld[0],renderData.m_lightDirWorld[1],renderData.m_lightDirWorld[2]);
|
2016-04-27 03:52:52 +00:00
|
|
|
Model* model = renderData.m_model;
|
|
|
|
if (0==model)
|
|
|
|
return;
|
|
|
|
|
2016-05-27 00:36:01 +00:00
|
|
|
|
2016-05-18 06:57:19 +00:00
|
|
|
|
2016-06-07 23:11:58 +00:00
|
|
|
renderData.m_viewportMatrix = viewport(0,0,width, height);
|
|
|
|
|
2016-04-27 03:52:52 +00:00
|
|
|
b3AlignedObjectArray<float>& zbuffer = renderData.m_depthBuffer;
|
|
|
|
|
|
|
|
TGAImage& frame = renderData.m_rgbColorBuffer;
|
2016-04-28 19:28:04 +00:00
|
|
|
|
2016-06-01 18:04:10 +00:00
|
|
|
|
2016-04-27 03:52:52 +00:00
|
|
|
|
|
|
|
{
|
2016-04-28 19:28:04 +00:00
|
|
|
Matrix modelViewMatrix = renderData.m_viewMatrix*renderData.m_modelMatrix;
|
2016-05-27 00:36:01 +00:00
|
|
|
Vec3f localScaling(renderData.m_localScaling[0],renderData.m_localScaling[1],renderData.m_localScaling[2]);
|
2016-06-02 00:47:41 +00:00
|
|
|
Shader shader(model, light_dir_local, modelViewMatrix, renderData.m_projectionMatrix,renderData.m_modelMatrix, localScaling, model->getColorRGBA());
|
2016-06-01 18:04:10 +00:00
|
|
|
|
2016-06-07 01:54:05 +00:00
|
|
|
//printf("Render %d triangles.\n",model->nfaces());
|
2016-06-01 18:04:10 +00:00
|
|
|
for (int i=0; i<model->nfaces(); i++)
|
|
|
|
{
|
2016-05-18 06:57:19 +00:00
|
|
|
|
2016-04-27 03:52:52 +00:00
|
|
|
for (int j=0; j<3; j++) {
|
|
|
|
shader.vertex(i, j);
|
|
|
|
}
|
|
|
|
triangle(shader.varying_tri, shader, frame, &zbuffer[0], renderData.m_viewportMatrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|