fix typo in texels flip
add support to create a cube in TinyRenderer (quick test)
This commit is contained in:
committed by
Erwin Coumans
parent
a3767193ce
commit
aa9a276a71
@@ -3,12 +3,13 @@
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <iostream>
|
||||
#include "TinyRenderer/tgaimage.h"
|
||||
#include "TinyRenderer/model.h"
|
||||
#include "TinyRenderer/geometry.h"
|
||||
#include "TinyRenderer/our_gl.h"
|
||||
#include "tgaimage.h"
|
||||
#include "model.h"
|
||||
#include "geometry.h"
|
||||
#include "our_gl.h"
|
||||
#include "../Utils/b3ResourcePath.h"
|
||||
#include "Bullet3Common/b3MinMax.h"
|
||||
#include "../OpenGLWindow/ShapeData.h"
|
||||
|
||||
Vec3f light_dir_world(1,1,1);
|
||||
|
||||
@@ -35,7 +36,11 @@ struct Shader : public IShader {
|
||||
}
|
||||
|
||||
virtual Vec4f vertex(int iface, int nthvert) {
|
||||
varying_uv.set_col(nthvert, m_model->uv(iface, nthvert));
|
||||
|
||||
Vec2f uv = m_model->uv(iface, nthvert);
|
||||
//printf("uv = %f,%f\n", uv.x,uv.y);
|
||||
varying_uv.set_col(nthvert, uv);
|
||||
|
||||
varying_nrm.set_col(nthvert, proj<3>((m_projectionMatrix*m_modelView).invert_transpose()*embed<4>(m_model->normal(iface, nthvert), 0.f)));
|
||||
Vec4f gl_Vertex = m_projectionMatrix*m_modelView*embed<4>(m_model->vert(iface, nthvert));
|
||||
varying_tri.set_col(nthvert, gl_Vertex);
|
||||
@@ -64,49 +69,37 @@ struct Shader : public IShader {
|
||||
|
||||
Vec3f n = (B*m_model->normal(uv)).normalize();
|
||||
|
||||
float diff = b3Min(b3Max(0.f, n*m_light_dir_local+0.6f),1.f);
|
||||
//float diff = b3Min(b3Max(0.f, n*m_light_dir_local+0.3f),1.f);
|
||||
float diff = b3Max(0.f, n*m_light_dir_local);
|
||||
color = m_model->diffuse(uv)*diff;
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
struct TinyRenderObjectData
|
||||
{
|
||||
//Camera
|
||||
Matrix m_viewMatrix;
|
||||
Matrix m_projectionMatrix;
|
||||
Matrix m_viewPortMatrix;
|
||||
|
||||
//Model (vertices, indices, textures, shader)
|
||||
Matrix m_modelMatrix;
|
||||
class Model* m_model;
|
||||
class IShader* m_shader;
|
||||
|
||||
|
||||
//Output
|
||||
TGAImage m_rgbColorBuffer;
|
||||
b3AlignedObjectArray<float> m_depthBuffer;
|
||||
};
|
||||
*/
|
||||
|
||||
TinyRenderObjectData::TinyRenderObjectData(int width, int height, const char* fileName)
|
||||
TinyRenderObjectData::TinyRenderObjectData(int width, int height,TGAImage& rgbColorBuffer,b3AlignedObjectArray<float>&depthBuffer)
|
||||
:m_width(width),
|
||||
m_height(height),
|
||||
m_rgbColorBuffer(width,height,TGAImage::RGB),
|
||||
m_model(0)
|
||||
m_rgbColorBuffer(rgbColorBuffer),
|
||||
m_depthBuffer(depthBuffer),
|
||||
m_model(0),
|
||||
m_userData(0),
|
||||
m_userIndex(-1)
|
||||
{
|
||||
Vec3f eye(1,1,3);
|
||||
Vec3f center(0,0,0);
|
||||
Vec3f up(0,1,0);
|
||||
|
||||
m_modelMatrix = Matrix::identity();
|
||||
m_viewMatrix = lookat(eye, center, up);
|
||||
m_viewportMatrix = viewport(width/8, height/8, width*3/4, height*3/4);
|
||||
m_projectionMatrix = projection(-1.f/(eye-center).norm());
|
||||
|
||||
}
|
||||
|
||||
|
||||
m_depthBuffer.resize(width*height);
|
||||
void TinyRenderObjectData::loadModel(const char* fileName)
|
||||
{
|
||||
//todo(erwincoumans) move the file loading out of here
|
||||
char relativeFileName[1024];
|
||||
if (!b3ResourcePath::findResourcePath(fileName, relativeFileName, 1024))
|
||||
@@ -115,9 +108,73 @@ m_model(0)
|
||||
} else
|
||||
{
|
||||
m_model = new Model(relativeFileName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void TinyRenderObjectData::registerMeshShape(const float* vertices, int numVertices,const int* indices, int numIndices)
|
||||
{
|
||||
if (0==m_model)
|
||||
{
|
||||
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*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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
void TinyRenderObjectData::createCube(float halfExtentsX,float halfExtentsY,float halfExtentsZ)
|
||||
{
|
||||
m_model = new Model();
|
||||
|
||||
char relativeFileName[1024];
|
||||
if (b3ResourcePath::findResourcePath("floor_diffuse.tga", relativeFileName, 1024))
|
||||
{
|
||||
m_model->loadDiffuseTexture(relativeFileName);
|
||||
}
|
||||
|
||||
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TinyRenderObjectData::~TinyRenderObjectData()
|
||||
@@ -127,34 +184,20 @@ TinyRenderObjectData::~TinyRenderObjectData()
|
||||
|
||||
void TinyRenderer::renderObject(TinyRenderObjectData& renderData)
|
||||
{
|
||||
const char* fileName = "obj/floor.obj";
|
||||
|
||||
|
||||
|
||||
//new Model(relativeFileName);//argv[m]);
|
||||
Model* model = renderData.m_model;
|
||||
if (0==model)
|
||||
return;
|
||||
|
||||
const int width = renderData.m_width;
|
||||
const int height = renderData.m_height;
|
||||
b3AlignedObjectArray<float>& zbuffer = renderData.m_depthBuffer;
|
||||
|
||||
//todo(erwincoumans) make this a separate call
|
||||
for (int i=width*height; i--; zbuffer[i] = -std::numeric_limits<float>::max());
|
||||
|
||||
TGAImage& frame = renderData.m_rgbColorBuffer;
|
||||
|
||||
//lookat(eye, center, up);
|
||||
//viewport(width/8, height/8, width*3/4, height*3/4);
|
||||
//projection(-1.f/(eye-center).norm());
|
||||
|
||||
Vec3f light_dir_local = proj<3>((renderData.m_projectionMatrix*renderData.m_viewMatrix*embed<4>(light_dir_world, 0.f))).normalize();
|
||||
|
||||
Vec3f light_dir_local = proj<3>((renderData.m_projectionMatrix*renderData.m_viewMatrix*renderData.m_modelMatrix*embed<4>(light_dir_world, 0.f))).normalize();
|
||||
|
||||
{
|
||||
//for (int m=1; m<argc; m++) {
|
||||
Matrix modelViewMatrix = renderData.m_viewMatrix*renderData.m_modelMatrix;
|
||||
|
||||
Shader shader(model, light_dir_local, renderData.m_viewMatrix, renderData.m_projectionMatrix);
|
||||
Shader shader(model, light_dir_local, modelViewMatrix, renderData.m_projectionMatrix);
|
||||
for (int i=0; i<model->nfaces(); i++) {
|
||||
for (int j=0; j<3; j++) {
|
||||
shader.vertex(i, j);
|
||||
@@ -163,9 +206,6 @@ void TinyRenderer::renderObject(TinyRenderObjectData& renderData)
|
||||
}
|
||||
|
||||
}
|
||||
//frame.flip_vertically(); // to place the origin in the bottom left corner of the image
|
||||
//frame.write_tga_file("framebuffer.tga");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user