From 615effa4d9cb3882c9d9561f7a803e37e8afd9ae Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Thu, 28 Apr 2016 12:28:04 -0700 Subject: [PATCH] fix typo in texels flip add support to create a cube in TinyRenderer (quick test) --- .../OpenGLWindow/GLInstancingRenderer.cpp | 6 +- examples/TinyRenderer/TinyRenderer.cpp | 58 +++++++++++++++++-- examples/TinyRenderer/TinyRenderer.h | 7 ++- examples/TinyRenderer/main.cpp | 22 ++++++- examples/TinyRenderer/model.cpp | 28 +++++++++ examples/TinyRenderer/model.h | 7 +++ examples/TinyRenderer/tgaimage.cpp | 2 +- 7 files changed, 115 insertions(+), 15 deletions(-) diff --git a/examples/OpenGLWindow/GLInstancingRenderer.cpp b/examples/OpenGLWindow/GLInstancingRenderer.cpp index 2bd7c92ad..e6304ad70 100644 --- a/examples/OpenGLWindow/GLInstancingRenderer.cpp +++ b/examples/OpenGLWindow/GLInstancingRenderer.cpp @@ -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+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] = 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+2]; } } diff --git a/examples/TinyRenderer/TinyRenderer.cpp b/examples/TinyRenderer/TinyRenderer.cpp index bbc9bbf16..9d45963b7 100644 --- a/examples/TinyRenderer/TinyRenderer.cpp +++ b/examples/TinyRenderer/TinyRenderer.cpp @@ -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)) @@ -115,8 +121,47 @@ m_model(0) } else { 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;iaddVertex(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;iaddTriangle(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; mnfaces(); i++) { for (int j=0; j<3; j++) { shader.vertex(i, j); diff --git a/examples/TinyRenderer/TinyRenderer.h b/examples/TinyRenderer/TinyRenderer.h index 1bcd49b95..0afd175fd 100644 --- a/examples/TinyRenderer/TinyRenderer.h +++ b/examples/TinyRenderer/TinyRenderer.h @@ -14,7 +14,7 @@ struct TinyRenderObjectData Matrix m_viewportMatrix; //Model (vertices, indices, textures, shader) - Matrix m_modelMatrix; + Matrix m_modelMatrix; class Model* m_model; //class IShader* m_shader; todo(erwincoumans) expose the shader, for now we use a default shader @@ -24,9 +24,12 @@ struct TinyRenderObjectData TGAImage m_rgbColorBuffer; b3AlignedObjectArray 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); + }; diff --git a/examples/TinyRenderer/main.cpp b/examples/TinyRenderer/main.cpp index 65a3352f6..7ec899320 100644 --- a/examples/TinyRenderer/main.cpp +++ b/examples/TinyRenderer/main.cpp @@ -1,6 +1,8 @@ #include "OpenGLWindow/SimpleOpenGL3App.h" #include "Bullet3Common/b3Quaternion.h" #include "Bullet3Common/b3CommandLineArgs.h" +#include "Bullet3Common/b3Transform.h" + #include "assert.h" #include @@ -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]; } } diff --git a/examples/TinyRenderer/model.cpp b/examples/TinyRenderer/model.cpp index bc7983dd5..f52b01126 100644 --- a/examples/TinyRenderer/model.cpp +++ b/examples/TinyRenderer/model.cpp @@ -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 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() { diff --git a/examples/TinyRenderer/model.h b/examples/TinyRenderer/model.h index 035cc119a..72f505f9a 100644 --- a/examples/TinyRenderer/model.h +++ b/examples/TinyRenderer/model.h @@ -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(); diff --git a/examples/TinyRenderer/tgaimage.cpp b/examples/TinyRenderer/tgaimage.cpp index 47e6ff833..6fe40f484 100644 --- a/examples/TinyRenderer/tgaimage.cpp +++ b/examples/TinyRenderer/tgaimage.cpp @@ -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); }