fix typo in texels flip

add support to create a cube in TinyRenderer (quick test)
This commit is contained in:
Erwin Coumans
2016-04-28 12:28:04 -07:00
parent a3767193ce
commit 615effa4d9
7 changed files with 115 additions and 15 deletions

View File

@@ -561,9 +561,9 @@ void GLInstancingRenderer::updateTexture(int textureIndex, const unsigned cha
{
for (int j = 0; j < h.m_height; j++)
{
flippedTexels[(i + j*h.m_width) * 3] = texels[(i + (h.m_height-1-j)*h.m_width) * 3];
flippedTexels[(i + j*h.m_width) * 3] = texels[(i + (h.m_height - 1 -j )*h.m_width) * 3];
flippedTexels[(i + j*h.m_width) * 3+1] = texels[(i + (h.m_height - 1 - j)*h.m_width) * 3+1];
flippedTexels[(i + j*h.m_width) * 3+2] = texels[(i + (h.m_height - 1 - j)*h.m_width) * 3+1];
flippedTexels[(i + j*h.m_width) * 3+2] = texels[(i + (h.m_height - 1 - j)*h.m_width) * 3+2];
}
}

View File

@@ -9,7 +9,7 @@
#include "TinyRenderer/our_gl.h"
#include "../Utils/b3ResourcePath.h"
#include "Bullet3Common/b3MinMax.h"
#include "../OpenGLWindow/ShapeData.h"
Vec3f light_dir_world(1,1,1);
@@ -64,7 +64,8 @@ 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;
@@ -91,7 +92,7 @@ struct TinyRenderObjectData
};
*/
TinyRenderObjectData::TinyRenderObjectData(int width, int height, const char* fileName)
TinyRenderObjectData::TinyRenderObjectData(int width, int height)
:m_width(width),
m_height(height),
m_rgbColorBuffer(width,height,TGAImage::RGB),
@@ -101,12 +102,17 @@ m_model(0)
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))
@@ -116,6 +122,45 @@ m_model(0)
{
m_model = new Model(relativeFileName);
}
}
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++)
{
float x = halfExtentsX*cube_vertices_textured[i*9];
float y = halfExtentsY*cube_vertices_textured[i*9+1];
float z = halfExtentsZ*cube_vertices_textured[i*9+2];
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]);
}
//int shapeId = m_instancingRenderer->registerShape(&verts[0].x,numVertices,cube_indices,numIndices,B3_GL_TRIANGLES,textureIndex);
}
@@ -149,12 +194,13 @@ void TinyRenderer::renderObject(TinyRenderObjectData& renderData)
//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);

View File

@@ -24,9 +24,12 @@ struct TinyRenderObjectData
TGAImage m_rgbColorBuffer;
b3AlignedObjectArray<float> m_depthBuffer;
TinyRenderObjectData(int width, int height, const char* objFileName);
TinyRenderObjectData(int width, int height);
virtual ~TinyRenderObjectData();
void loadModel(const char* fileName);
void createCube(float HalfExtentsX,float HalfExtentsY,float HalfExtentsZ);
};

View File

@@ -1,6 +1,8 @@
#include "OpenGLWindow/SimpleOpenGL3App.h"
#include "Bullet3Common/b3Quaternion.h"
#include "Bullet3Common/b3CommandLineArgs.h"
#include "Bullet3Common/b3Transform.h"
#include "assert.h"
#include <stdio.h>
@@ -81,8 +83,12 @@ int main(int argc, char* argv[])
int textureWidth = gWidth;
int textureHeight = gHeight;
TinyRenderObjectData renderData(textureWidth, textureHeight, "floor.obj");
TinyRenderObjectData renderData(textureWidth, textureHeight);//, "african_head/african_head.obj");//floor.obj");
//renderData.loadModel("african_head/african_head.obj");
//renderData.loadModel("floor.obj");
renderData.createCube(1,1,1);
myArgs.GetCmdLineArgument("mp4_file",gVideoFileName);
@@ -127,12 +133,22 @@ int main(int argc, char* argv[])
app->m_instancingRenderer->getActiveCamera()->getCameraProjectionMatrix(projMat);
float viewMat[16];
app->m_instancingRenderer->getActiveCamera()->getCameraViewMatrix(viewMat);
B3_ATTRIBUTE_ALIGNED16(float modelMat[16]);
b3Transform tr;
tr.setIdentity();
static float posUp = 0.f;
posUp += 0.001;
b3Vector3 org = b3MakeVector3(0,posUp,0);
tr.setOrigin(org);
tr.getOpenGLMatrix(modelMat);
for (int i=0;i<4;i++)
{
for (int j=0;j<4;j++)
{
renderData.m_viewMatrix[i][j] = viewMat[i+4*j];
//renderData.m_projectionMatrix[i][j] = projMat[i+4*j];
renderData.m_modelMatrix[i][j] = modelMat[i+4*j];
}
}

View File

@@ -44,6 +44,34 @@ Model::Model(const char *filename) : verts_(), faces_(), norms_(), uv_(), diffus
load_texture(filename, "_spec.tga", specularmap_);
}
Model::Model():verts_(), faces_(), norms_(), uv_(), diffusemap_(), normalmap_(), specularmap_()
{
}
void Model::loadDiffuseTexture(const char* relativeFileName)
{
diffusemap_.read_tga_file(relativeFileName);
}
void Model::addVertex(float x,float y,float z, float normalX, float normalY, float normalZ, float u, float v)
{
verts_.push_back(Vec3f(x,y,z));
norms_.push_back(Vec3f(normalX,normalY,normalZ));
uv_.push_back(Vec2f(u,v));
}
void Model::addTriangle(int vertexposIndex0, int normalIndex0, int uvIndex0,
int vertexposIndex1, int normalIndex1, int uvIndex1,
int vertexposIndex2, int normalIndex2, int uvIndex2)
{
std::vector<Vec3i> f;
f.push_back(Vec3i(vertexposIndex0, normalIndex0, uvIndex0));
f.push_back(Vec3i(vertexposIndex1, normalIndex1, uvIndex1));
f.push_back(Vec3i(vertexposIndex2, normalIndex2, uvIndex2));
faces_.push_back(f);
}
Model::~Model() {}
int Model::nverts() {

View File

@@ -17,6 +17,13 @@ private:
void load_texture(std::string filename, const char *suffix, TGAImage &img);
public:
Model(const char *filename);
Model();
void loadDiffuseTexture(const char* relativeFileName);
void addVertex(float x,float y,float z, float normalX, float normalY, float normalZ, float u, float v);
void addTriangle(int vertexposIndex0, int normalIndex0, int uvIndex0,
int vertexposIndex1, int normalIndex1, int uvIndex1,
int vertexposIndex2, int normalIndex2, int uvIndex2);
~Model();
int nverts();
int nfaces();

View File

@@ -246,7 +246,7 @@ bool TGAImage::unload_rle_data(std::ofstream &out) {
TGAColor TGAImage::get(int x, int y) {
if (!data || x<0 || y<0 || x>=width || y>=height) {
return TGAColor();
return TGAColor(128.f,128.f,128.f,255.f);
}
return TGAColor(data+(x+y*width)*bytespp, bytespp);
}