示例#1
0
void TinyRendererSetup::stepSimulation(float deltaTime)
{
    m_internalData->updateTransforms();
    
    if (!m_useSoftware)
    {
        
        for (int i=0;i<m_internalData->m_transforms.size();i++)
        {
            m_guiHelper->getRenderInterface()->writeSingleInstanceTransformToCPU(m_internalData->m_transforms[i].getOrigin(),m_internalData->m_transforms[i].getRotation(),i);
        }
        m_guiHelper->getRenderInterface()->writeTransforms();
        m_guiHelper->getRenderInterface()->renderScene();
    } else
    {
        
        TGAColor clearColor;
        clearColor.bgra[0] = 200;
        clearColor.bgra[1] = 200;
        clearColor.bgra[2] = 200;
        clearColor.bgra[3] = 255;
        for(int y=0;y<m_internalData->m_height;++y)
        {
            for(int x=0;x<m_internalData->m_width;++x)
            {
                m_internalData->m_rgbColorBuffer.set(x,y,clearColor);
                m_internalData->m_depthBuffer[x+y*m_internalData->m_width] = -1e30f;
            }
        }


        ATTRIBUTE_ALIGNED16(btScalar modelMat2[16]);
        ATTRIBUTE_ALIGNED16(float viewMat[16]);
        ATTRIBUTE_ALIGNED16(float projMat[16]);
        CommonRenderInterface* render = this->m_app->m_renderer;
        render->getActiveCamera()->getCameraViewMatrix(viewMat);
        render->getActiveCamera()->getCameraProjectionMatrix(projMat);
            

        
        for (int o=0;o<this->m_internalData->m_renderObjects.size();o++)
        {
                
            const btTransform& tr = m_internalData->m_transforms[o];
            tr.getOpenGLMatrix(modelMat2);
            
                    
            for (int i=0;i<4;i++)
            {
                for (int j=0;j<4;j++)
                {
                    m_internalData->m_renderObjects[o]->m_modelMatrix[i][j] = float(modelMat2[i+4*j]);
                    m_internalData->m_renderObjects[o]->m_viewMatrix[i][j] = viewMat[i+4*j];
                    m_internalData->m_renderObjects[o]->m_projectionMatrix[i][j] = projMat[i+4*j];
                    
                    float eye[4];
                    float center[4];
                    render->getActiveCamera()->getCameraPosition(eye);
                    render->getActiveCamera()->getCameraTargetPosition(center);

                    m_internalData->m_renderObjects[o]->m_eye.setValue(eye[0],eye[1],eye[2]);
                    m_internalData->m_renderObjects[o]->m_center.setValue(center[0],center[1],center[2]);
                }
            }
            TinyRenderer::renderObject(*m_internalData->m_renderObjects[o]);
        }
        //m_app->drawText("hello",500,500);
        render->activateTexture(m_internalData->m_textureHandle);
        render->updateTexture(m_internalData->m_textureHandle,m_internalData->m_rgbColorBuffer.buffer());
        float color[4] = {1,1,1,1};
        m_app->drawTexturedRect(0,0,m_app->m_window->getWidth(), m_app->m_window->getHeight(),color,0,0,1,1,true);
    }
}
	virtual void render(const btDiscreteDynamicsWorld* rbWorld)
	{
		OpenGLGuiHelper::render(rbWorld);

		//clear the color buffer
		TGAColor clearColor;
		clearColor.bgra[0] = 255;
		clearColor.bgra[1] = 255;
		clearColor.bgra[2] = 255;
		clearColor.bgra[3] = 255;

		clearBuffers(clearColor);

		ATTRIBUTE_ALIGNED16(btScalar modelMat[16]);
		ATTRIBUTE_ALIGNED16(float viewMat[16]);
		ATTRIBUTE_ALIGNED16(float projMat[16]);

		CommonRenderInterface* render = getRenderInterface();

		render->getActiveCamera()->getCameraProjectionMatrix(projMat);
		render->getActiveCamera()->getCameraViewMatrix(viewMat);

		btVector3 lightDirWorld(-5, 200, -40);
		switch (1)  //app->getUpAxis())
		{
			case 1:
				lightDirWorld = btVector3(-50.f, 100, 30);
				break;
			case 2:
				lightDirWorld = btVector3(-50.f, 30, 100);
				break;
			default:
			{
			}
		};

		lightDirWorld.normalize();

		for (int i = 0; i < rbWorld->getNumCollisionObjects(); i++)
		{
			btCollisionObject* colObj = rbWorld->getCollisionObjectArray()[i];
			int colObjIndex = colObj->getUserIndex();
			int shapeIndex = colObj->getCollisionShape()->getUserIndex();
			if (colObjIndex >= 0 && shapeIndex >= 0)
			{
				TinyRenderObjectData* renderObj = 0;

				int* cptr = m_swInstances[colObjIndex];
				if (cptr)
				{
					int c = *cptr;
					TinyRenderObjectData** sptr = m_swRenderObjects[c];
					if (sptr)
					{
						renderObj = *sptr;
						//sync the object transform
						const btTransform& tr = colObj->getWorldTransform();
						tr.getOpenGLMatrix(modelMat);

						for (int i = 0; i < 4; i++)
						{
							for (int j = 0; j < 4; j++)
							{
								renderObj->m_projectionMatrix[i][j] = projMat[i + 4 * j];
								renderObj->m_modelMatrix[i][j] = modelMat[i + 4 * j];
								renderObj->m_viewMatrix[i][j] = viewMat[i + 4 * j];
							}
						}
						renderObj->m_localScaling = colObj->getCollisionShape()->getLocalScaling();
						renderObj->m_lightDirWorld = lightDirWorld;
						renderObj->m_lightAmbientCoeff = 0.6;
						renderObj->m_lightDiffuseCoeff = 0.35;
						renderObj->m_lightSpecularCoeff = 0.05;
						TinyRenderer::renderObject(*renderObj);
					}
				}
			}
		}

		for (int y = 0; y < m_swHeight; ++y)
		{
			unsigned char* pi = m_image + (y)*m_swWidth * 3;
			for (int x = 0; x < m_swWidth; ++x)
			{
				const TGAColor& color = getFrameBuffer().get(x, y);
				pi[0] = color.bgra[2];
				pi[1] = color.bgra[1];
				pi[2] = color.bgra[0];
				pi += 3;
			}
		}
		render->activateTexture(m_textureHandle);
		render->updateTexture(m_textureHandle, m_image);

		static int counter = 0;
		counter++;
		if ((counter & 7) == 0)
		{
			char filename[1024];
			sprintf(filename, "framebuf%d.tga", counter);
			getFrameBuffer().write_tga_file(filename, true);
		}
		float color[4] = {1, 1, 1, 1};
		m_primRenderer->drawTexturedRect(0, 0, m_swWidth, m_swHeight, color, 0, 0, 1, 1, true);
	}