Example #1
0
int main(int argc, char* argv[])
{
	{
		b3CommandLineArgs myArgs(argc, argv);


		SimpleOpenGLApp* app = new SimpleOpenGLApp("SimpleOpenGL3App", 1024, 768);

		app->m_renderer->getActiveCamera()->setCameraDistance(13);
		app->m_renderer->getActiveCamera()->setCameraPitch(0);
		app->m_renderer->getActiveCamera()->setCameraTargetPosition(0, 0, 0);
		sOldKeyboardCB = app->m_window->getKeyboardCallback();
		app->m_window->setKeyboardCallback(MyKeyboardCallback2);
		sOldMouseMoveCB = app->m_window->getMouseMoveCallback();
		app->m_window->setMouseMoveCallback(MyMouseMoveCallback2);
		sOldMouseButtonCB = app->m_window->getMouseButtonCallback();
		app->m_window->setMouseButtonCallback(MyMouseButtonCallback2);
		sOldWheelCB = app->m_window->getWheelCallback();
		app->m_window->setWheelCallback(MyWheelCallback2);
		sOldResizeCB = app->m_window->getResizeCallback();
		app->m_window->setResizeCallback(MyResizeCallback2);


		myArgs.GetCmdLineArgument("mp4_file", gVideoFileName);
		if (gVideoFileName)
			app->dumpFramesToVideo(gVideoFileName);

		myArgs.GetCmdLineArgument("png_file", gPngFileName);
		char fileName[1024];

		int textureWidth = 128;
		int textureHeight = 128;

		unsigned char*	image = new unsigned char[textureWidth*textureHeight * 4];


		int textureHandle = app->m_renderer->registerTexture(image, textureWidth, textureHeight);



		do
		{
			static int frameCount = 0;
			frameCount++;
			if (gPngFileName)
			{
				printf("gPngFileName=%s\n", gPngFileName);

				sprintf(fileName, "%s%d.png", gPngFileName, frameCount++);
				app->dumpNextFrameToPng(fileName);
			}



			//update the texels of the texture using a simple pattern, animated using frame index
			for (int y = 0; y < textureHeight; ++y)
			{
				const int	t = (y + frameCount) >> 4;
				unsigned char*	pi = image + y*textureWidth * 3;
				for (int x = 0; x < textureWidth; ++x)
				{
					const int		s = x >> 4;
					const unsigned char	b = 180;
					unsigned char			c = b + ((s + (t & 1)) & 1)*(255 - b);
					pi[0] = pi[1] = pi[2] = pi[3] = c; pi += 3;
				}
			}

			app->m_renderer->activateTexture(textureHandle);
			app->m_renderer->updateTexture(textureHandle, image);

			float color[4] = { 1, 0, 0, 1 };
			app->m_primRenderer->drawTexturedRect(100, 200, gWidth / 2 - 50, gHeight / 2 - 50, color, 0, 0, 1, 1, true);


			app->m_renderer->init();
			int upAxis = 1;
			app->m_renderer->updateCamera(upAxis);

			app->m_renderer->renderScene();
			
			app->drawGrid();
			char bla[1024];
			sprintf(bla, "2d text:%d", frameCount);

			float yellow[4] = {1,1,0,1};
			app->drawText(bla, 10, 10, 1, yellow);
			float position[3] = {1,1,1};
			float position2[3] = {0,0,5};

			float orientation[4] = {0,0,0,1};
			
			app->drawText3D(bla,0,0,1,1);

			sprintf(bla, "3d bitmap camera facing text:%d", frameCount);			
			app->drawText3D(bla,position2,orientation,color,1,CommonGraphicsApp::eDrawText3D_OrtogonalFaceCamera);
			
			sprintf(bla, "3d bitmap text:%d", frameCount);			
			app->drawText3D(bla,position,orientation,color,0.001,0);

			float green[4] = {0,1,0,1};
			float blue[4] = {0,0,1,1};

			sprintf(bla, "3d ttf camera facing text:%d", frameCount);			
			app->drawText3D(bla,position2,orientation,green,1,CommonGraphicsApp::eDrawText3D_TrueType|CommonGraphicsApp::eDrawText3D_OrtogonalFaceCamera);

			app->drawText3D(bla,position2,orientation,green,1,CommonGraphicsApp::eDrawText3D_TrueType|CommonGraphicsApp::eDrawText3D_OrtogonalFaceCamera);
			sprintf(bla, "3d ttf text:%d", frameCount);
			b3Quaternion orn;
			orn.setEulerZYX(B3_HALF_PI/2.,0,B3_HALF_PI/2.);
			app->drawText3D(bla,position2,orn,blue,1,CommonGraphicsApp::eDrawText3D_TrueType);

			
			app->swapBuffer();
		} while (!app->m_window->requestedExit());



		delete app;

		delete[] image;
	}
	return 0;
}
Example #2
0
int main(int argc, char* argv[])
{
	{
		b3CommandLineArgs myArgs(argc, argv);


		app = new SimpleOpenGLApp("SimpleOpenGLApp", gWidth, gHeight);

		app->m_renderer->getActiveCamera()->setCameraDistance(13);
		app->m_renderer->getActiveCamera()->setCameraPitch(0);
		app->m_renderer->getActiveCamera()->setCameraTargetPosition(0, 0, 0);

		sOldKeyboardCB = app->m_window->getKeyboardCallback();
		app->m_window->setKeyboardCallback(MyKeyboardCallback);
		sOldMouseMoveCB = app->m_window->getMouseMoveCallback();
		app->m_window->setMouseMoveCallback(MyMouseMoveCallback);
		sOldMouseButtonCB = app->m_window->getMouseButtonCallback();
		app->m_window->setMouseButtonCallback(MyMouseButtonCallback);
		sOldWheelCB = app->m_window->getWheelCallback();
		app->m_window->setWheelCallback(MyWheelCallback);
		sOldResizeCB = app->m_window->getResizeCallback();
		app->m_window->setResizeCallback(MyResizeCallback);


		myArgs.GetCmdLineArgument("mp4_file", gVideoFileName);
		if (gVideoFileName)
			app->dumpFramesToVideo(gVideoFileName);

		myArgs.GetCmdLineArgument("png_file", gPngFileName);
		char fileName[1024];

		int textureWidth = 128;
		int textureHeight = 128;

		unsigned char*	image = new unsigned char[textureWidth*textureHeight * 4];


		int textureHandle = app->m_renderer->registerTexture(image, textureWidth, textureHeight);

		int cubeIndex = app->registerCubeShape(1, 1, 1);

		b3Vector3 pos = b3MakeVector3(0, 0, 0);
		b3Quaternion orn(0, 0, 0, 1);
		b3Vector3 color = b3MakeVector3(1, 0, 0);
		b3Vector3 scaling = b3MakeVector3 (1, 1, 1);
		app->m_renderer->registerGraphicsInstance(cubeIndex, pos, orn, color, scaling);
		app->m_renderer->writeTransforms();

		do
		{
			static int frameCount = 0;
			frameCount++;
			if (gPngFileName)
			{
				printf("gPngFileName=%s\n", gPngFileName);

				sprintf(fileName, "%s%d.png", gPngFileName, frameCount++);
				app->dumpNextFrameToPng(fileName);
			}

			
			


			//update the texels of the texture using a simple pattern, animated using frame index
			for (int y = 0; y < textureHeight; ++y)
			{
				const int	t = (y + frameCount) >> 4;
				unsigned char*	pi = image + y*textureWidth * 3;
				for (int x = 0; x < textureWidth; ++x)
				{
					const int		s = x >> 4;
					const unsigned char	b = 180;
					unsigned char			c = b + ((s + (t & 1)) & 1)*(255 - b);
					pi[0] = pi[1] = pi[2] = pi[3] = c; pi += 3;
				}
			}

			app->m_renderer->activateTexture(textureHandle);
			app->m_renderer->updateTexture(textureHandle, image);

			//float color[4] = { 255, 1, 1, 1 };
			//app->m_primRenderer->drawTexturedRect(100, 200, gWidth / 2 - 50, gHeight / 2 - 50, color, 0, 0, 1, 1, true);


			app->m_renderer->init();
			app->m_renderer->updateCamera(1);

			app->m_renderer->renderScene();
			app->drawGrid();
			char bla[1024];
			sprintf(bla, "Simple test frame %d", frameCount);

			//app->drawText(bla, 10, 10);

#ifdef B3_USE_IMGUI
			{
				bool show_test_window = true;
				bool show_another_window = false;
				ImVec4 clear_color = ImColor(114, 144, 154);

				 // Start the frame
				ImGuiIO& io = ImGui::GetIO();
				if (!g_FontTexture)
			        ImGui_ImplBullet_CreateDeviceObjects();

				io.DisplaySize = ImVec2((float)gWidth, (float)gHeight);
				io.DisplayFramebufferScale = ImVec2(gWidth > 0 ? ((float)1.) : 0, gHeight > 0 ? ((float)1.) : 0);
				io.DeltaTime = (float)(1.0f/60.0f);
				io.MousePos = ImVec2((float)gMouseX, (float)gMouseY);   
				io.RenderDrawListsFn = ImGui_ImplBullet_RenderDrawLists;


				for (int i=0;i<3;i++)
				{
					io.MouseDown[i] = g_MousePressed[i]|g_MousePressed2[i];
					g_MousePressed[i] = false;
				}

				io.MouseWheel = g_MouseWheel;

				ImGui::NewFrame();

				ImGui::ShowTestWindow();
				ImGui::ShowMetricsWindow();
				#if 0
				static float f = 0.0f;
				ImGui::Text("Hello, world!");
				ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
				ImGui::ColorEdit3("clear color", (float*)&clear_color);
				if (ImGui::Button("Test Window")) show_test_window ^= 1;
				if (ImGui::Button("Another Window")) show_another_window ^= 1;
				ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
				#endif
				ImGui::Render();
			}
#endif //B3_USE_IMGUI
			app->swapBuffer();
		} while (!app->m_window->requestedExit());



		delete app;

		delete[] image;
	}
	return 0;
}
Example #3
0
int main(int argc, char* argv[])
{
    b3CommandLineArgs myArgs(argc,argv);

  
    
	SimpleOpenGL3App* app = new SimpleOpenGL3App("SimpleOpenGL3App",640,480,true);
	
	app->m_instancingRenderer->getActiveCamera()->setCameraDistance(13);
	app->m_instancingRenderer->getActiveCamera()->setCameraPitch(0);
	app->m_instancingRenderer->getActiveCamera()->setCameraTargetPosition(0,0,0);
	sOldKeyboardCB = app->m_window->getKeyboardCallback();	
	app->m_window->setKeyboardCallback(MyKeyboardCallback);
	sOldMouseMoveCB = app->m_window->getMouseMoveCallback();
	app->m_window->setMouseMoveCallback(MyMouseMoveCallback);
	sOldMouseButtonCB = app->m_window->getMouseButtonCallback();
	app->m_window->setMouseButtonCallback(MyMouseButtonCallback);
	sOldWheelCB = app->m_window->getWheelCallback();
	app->m_window->setWheelCallback(MyWheelCallback);
	sOldResizeCB = app->m_window->getResizeCallback();
	app->m_window->setResizeCallback(MyResizeCallback);
  
    int textureWidth = gWidth;
    int textureHeight = gHeight;
   TGAImage rgbColorBuffer(gWidth,gHeight,TGAImage::RGB);
        b3AlignedObjectArray<float> depthBuffer;
	depthBuffer.resize(gWidth*gHeight);
 
	TinyRenderObjectData renderData(textureWidth, textureHeight,rgbColorBuffer,depthBuffer);//, "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);
    if (gVideoFileName)
        app->dumpFramesToVideo(gVideoFileName);

    myArgs.GetCmdLineArgument("png_file",gPngFileName);
    char fileName[1024];
    
 
    
    unsigned char*	image=new unsigned char[textureWidth*textureHeight*4];
        
    
    int textureHandle = app->m_renderer->registerTexture(image,textureWidth,textureHeight);

    int cubeIndex = app->registerCubeShape(1,1,1);
    
    b3Vector3 pos = b3MakeVector3(0,0,0);
    b3Quaternion orn(0,0,0,1);
    b3Vector3 color=b3MakeVector3(1,0,0);
    b3Vector3 scaling=b3MakeVector3 (1,1,1);
    app->m_renderer->registerGraphicsInstance(cubeIndex,pos,orn,color,scaling);
    app->m_renderer->writeTransforms();
    
	do
	{
	    static int frameCount = 0;
		frameCount++;
		if (gPngFileName)
        {
            printf("gPngFileName=%s\n",gPngFileName);

            sprintf(fileName,"%s%d.png",gPngFileName,frameCount++);
            app->dumpNextFrameToPng(fileName);
        }
        
       	app->m_instancingRenderer->init();
		app->m_instancingRenderer->updateCamera();

   ///clear the color and z (depth) buffer
        for(int y=0;y<textureHeight;++y)
        {
            unsigned char*	pi=image+(y)*textureWidth*3;
            for(int x=0;x<textureWidth;++x)
            {
                
                TGAColor color;
                color.bgra[0] = 255;
                color.bgra[1] = 255;
                color.bgra[2] = 255;
                color.bgra[3] = 255;
                
                renderData.m_rgbColorBuffer.set(x,y,color);
				renderData.m_depthBuffer[x+y*textureWidth] = -1e30f;
            }
        }
        
        float projMat[16];
        app->m_instancingRenderer->getActiveCamera()->getCameraProjectionMatrix(projMat);
        float viewMat[16];
        app->m_instancingRenderer->getActiveCamera()->getCameraViewMatrix(viewMat);
        B3_ATTRIBUTE_ALIGNED16(float modelMat[16]);
        
        //sync the object transform
        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_modelMatrix[i][j] = modelMat[i+4*j];
            }
        }
        
        //render the object
        TinyRenderer::renderObject(renderData);
  
        #if 1
         //update the texels of the texture using a simple pattern, animated using frame index
        for(int y=0;y<textureHeight;++y)
        {
            unsigned char*	pi=image+(y)*textureWidth*3;
            for(int x=0;x<textureWidth;++x)
            {
                
                TGAColor color = renderData.m_rgbColorBuffer.get(x,y);
				pi[0] = color.bgra[2];
				pi[1] = color.bgra[1];
				pi[2] = color.bgra[0];
				pi[3] = 255;
                pi+=3;
            }
        }
        #else
        
        //update the texels of the texture using a simple pattern, animated using frame index
        for(int y=0;y<textureHeight;++y)
        {
            const int	t=(y+frameCount)>>4;
            unsigned char*	pi=image+y*textureWidth*3;
            for(int x=0;x<textureWidth;++x)
            {
				TGAColor color = renderData.m_rgbColorBuffer.get(x,y);

                const int		s=x>>4;
                const unsigned char	b=180;					
                unsigned char			c=b+((s+(t&1))&1)*(255-b);
				pi[0]=pi[1]=pi[2]=pi[3]=c;
				pi+=3;
            }
        }
        #endif 
        
    
        app->m_renderer->activateTexture(textureHandle);
        app->m_renderer->updateTexture(textureHandle,image);
        
        float color[4] = {1,1,1,1};
        app->m_primRenderer->drawTexturedRect(0,0,gWidth/3,gHeight/3,color,0,0,1,1,true);
        
		
	
        app->m_renderer->renderScene();
		app->drawGrid();
		char bla[1024];
		sprintf(bla,"Simple test frame %d", frameCount);

		app->drawText(bla,10,10);
		app->swapBuffer();
	} while (!app->m_window->requestedExit());


	delete app;
	return 0;
}