Jump to content

What is the process for binding texture to a buffer?


enablerbr
 Share

Recommended Posts

i'm trying to work out render-to-texture. yet i can't work out how to bind a texture to a buffer. also can i bind a camera to a buffer?

 

 

 

Edit: ok i worked out how to bind buffer to texture. yet the result has the buffer image as being upside down. how do i fix this? do i have to flip the image within a shader. as i see no texture commands for flipping. this is my current code (now includes render-to-texture placed in material).

 

           int width = 800;int height = 600;

           Graphic.Graphics(width, height, 0, 0, GraphicModes.GraphicsBackBuffer | GraphicModes.GraphicsDepthBuffer);
           Worlds wld = new Worlds();
           wld.CreateWorld();

           Cameras cam1 = new Cameras();
           cam1.CreateCamera(IntPtr.Zero);
           cam1.CameraZoom(Helper.FPSCameraFOV(60));
           cam1.MoveEntity(new Vector3(0f, 2.5f, -6f), 0);
           cam1.EntityHide();

           SpotLight spot1 = new SpotLight();
           spot1.CreateSpotLight(12.5f, IntPtr.Zero);
           spot1.LightConeAngles(3f, 13f);
           spot1.EntityColor(new Color4(0f, 0f, 2f,0f), Mode.Disabled);
           spot1.PositionEntity(new Vector3(0f, 10f, -2.5f), 0);
           spot1.RotateEntity(new Vector3(90f, 0f, 0f), 0);
           spot1.SetShadowSoftness(0.5f);
           spot1.SetShadowMapSize(1024);

           Cameras cam2 = new Cameras();
           cam2.CreateCamera(IntPtr.Zero);
           cam2.CameraZoom(Helper.FPSCameraFOV(60));
           cam2.MoveEntity(new Vector3(0f, 2.5f, 6f), 0);
           cam2.RotateEntity(new Vector3(180f, 0f, 180f), 0);
           cam2.EntityHide();

           SpotLight spot2 = new SpotLight();
           spot2.CreateSpotLight(12.5f, IntPtr.Zero);
           spot2.LightConeAngles(3f, 13f);
           spot2.EntityColor(new Color4(2f,0f,0f,0f),Mode.Disabled);
           spot2.PositionEntity(new Vector3(0f, 10f, 2.5f), 0);
           spot2.RotateEntity(new Vector3(90f, 0f, 0f), 0);
           spot2.SetShadowSoftness(0.5f);
           spot2.SetShadowMapSize(1024);

           Cube cube1 = new Cube();
           cube1.CreateCube(IntPtr.Zero);
           cube1.UpdateMesh();
           cube1.PositionEntity(new Vector3(0f, 2.5f, 2.5f), 0);
           cube1.EntityOcclusionMode(OcclusionMode.Hardware, Mode.Disabled);

           Cube ground = new Cube();
           ground.CreateCube(IntPtr.Zero);
           ground.ScaleEntity(new Vector3(10f, 1f, 5f));
           ground.UpdateMesh();
           ground.PositionEntity(new Vector3(0f, 0f, 2.5f), 0);
           ground.EntityOcclusionMode(OcclusionMode.Hardware, Mode.Disabled);

           Cube ground2 = new Cube();
           ground2.CreateCube(IntPtr.Zero);
           ground2.ScaleEntity(new Vector3(10f, 1f, 5f));
           ground2.UpdateMesh();
           ground2.PositionEntity(new Vector3(0f, 0f, -2.5f), 0);
           ground2.EntityOcclusionMode(OcclusionMode.Hardware, Mode.Disabled);

           Cube pictureframe = new Cube();
           pictureframe.CreateCube(IntPtr.Zero);
           pictureframe.ScaleEntity(new Vector3(1.5f, 1.5f, 0.01f));
           pictureframe.UpdateMesh();
           pictureframe.PositionEntity(new Vector3(0f, 2.5f, -2.5f), 0);
           pictureframe.EntityOcclusionMode(OcclusionMode.Hardware, Mode.Disabled);

           Cube wall = new Cube();
           wall.CreateCube(IntPtr.Zero);
           wall.ScaleEntity(new Vector3(5f, 5f, 1f));
           wall.UpdateMesh();
           wall.PositionEntity(new Vector3(0f, 2.5f, 0f), 0);
           wall.EntityOcclusionMode(OcclusionMode.Hardware, Mode.Disabled);
           Lights.SetShadowQuality(ShadowQuality.High);

           Buffers gbuffer = new Buffers();
           gbuffer.CreateBuffer(Graphic.GetGraphicsWidth(), Graphic.GetGraphicsHeight(), BufferModes.BufferColor | BufferModes.BufferDepth | BufferModes.BufferNormal);

           Buffers rttbuffer = new Buffers();
           rttbuffer.CreateBuffer(Graphic.GetGraphicsWidth(), Graphic.GetGraphicsHeight(), BufferModes.BufferColor | BufferModes.BufferDepth | BufferModes.BufferNormal);

           Textures rttexture = new Textures();
           rttexture.CreateTexture(128, 128, TextureFormats.TextureFloat);
           rttexture.Texture = rttbuffer.GetColorBuffer(0);

           Materials wallmat = new Materials();
           wallmat.LoadMaterial("Materials/RTTMat/RTTMat.mat");
           wallmat.SetMaterialTexture(rttexture.Texture, 0);

           Surfaces.PaintSurface(Surfaces.GetSurface(pictureframe.Mesh,1),wallmat.Material);

           while (!Convert.ToBoolean(Input.KeyHit(KeyboardKey.KeyEscape)) & !Convert.ToBoolean(Miscellaneous.AppTerminate()))
           {
               cube1.TurnEntity(new Vector3(0.5f, 0.5f, 0.5f), 0);
               pictureframe.TurnEntity(new Vector3(0.2f, 0f, 0f), 0);
               Timing.UpdateAppTime();
               Worlds.UpdateWorld(0f);

               // Grabs render-to-texture from camera cam2

               cam2.EntityShow();

               Buffers.SetBuffer(gbuffer.Buffer);

               Worlds.RenderWorld(Elements.EntityAll);

               Buffers.SetBuffer(Buffers.BackBuffer());

               Lights.RenderLights(gbuffer.Buffer);

               rttbuffer.CopySourceToBuffer(Buffers.BackBuffer(), Components.BufferColor | Components.BufferDepth | Components.BufferNormal);

               // Hides camera cam 2 and shows camera cam1

               cam2.EntityHide();
               cam1.EntityShow();

               Buffers.SetBuffer(gbuffer.Buffer);

               Worlds.RenderWorld(Elements.EntityAll);

               Buffers.SetBuffer(Buffers.BackBuffer());

               Lights.RenderLights(gbuffer.Buffer);

               // Draws render-to-texture to screen

               Draw.DrawImage(rttexture.Texture, 40, 40, 40 + 128, 40 + 128);

               Draw.DrawText(Convert.ToString(Timing.UPS()), Graphic.GetGraphicsWidth()-60, 40);

               Graphic.Flip(SyncMode.Disable);
           }

 

ok i just realised my mistake. cam2 shouldn't of had the z axis rotated. oops sorry.

Q6600, GTX 560Ti, 8GB DDR2, Windows 7 Home Premium 64-bit.

Link to comment
Share on other sites

When a buffer is rendered to, the image is upside down.

 

Yeah, I know it is annoying, but I am using the same code that renders to the back buffer. Maybe I could auto-flip the viewport when rendering to a buffer, but then it would break existing code.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

  • 3 weeks later...

change:

Draw.DrawImage(rttexture.Texture, 40, 40, 40 + 128, 40 + 128);

to:

Draw.DrawImage(rttexture.Texture, 40, 40+ 40 + 128, 40 + 128, -(40 + 128)); // may not be correct with the +40's :lol:

 

 

basically you can flip the image when drawing it by starting y at the bottom and supplying negative height

DrawImage( GetColorBuffer(fullscreenbuffer), 0, GraphicsHeight(), GraphicsWidth(), -GraphicsHeight() ); // like that

 

It's an obscure feature but quite common in 3d as i see...

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...