Jump to content
  • Advertisement
Sign in to follow this  
derek7

How to show a picture in screen with D3D?

This topic is 4860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

first I directly access backbuffer and present it. 2nd I load pic as texture and now how to put the texture in backbuffer? I do not want to use sprite. Just want a simple ,conveninent way.

Share this post


Link to post
Share on other sites
Advertisement
A Simple way would be to create a quad from 4 points using a struct like this



struct Vertex
{
D3DXVECTOR4 XYZRHW;
D3DXVECTOR2 TexCoords;
};


#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_TEX0)


Then assigning the 4 points at

0,ScreenHeight,0.5,1.0; TexCoords 0,1
0,0,0.5,1.0; TexCoords 0,0
ScreenWidth,ScreenHeight,0.5,1.0; TexCoords 1,1
ScreenWidth,0,0.5,1.0; TexCoords 1,0


Setting your texture
SettingThisGeometry
Rendering a tristrip.

EDIT: A more simple, yet convenient way is using a sprite :)

Share this post


Link to post
Share on other sites
As mentioned by RhoneRanger, you could use a simple quad.
What you will want to do is follow the guidelines that I am about to give you.
1) Create a simple vertex structure that will serve as a template for your vertex which is the building blocks of your primitives.
2) Create an array of these vertex structures to form your quad. (4 points)
3) Copy your array to a vertex buffer that will be sent to the GPU for rendering
4) Render the contents of the vertex buffer using IDirect3DDevice9::DrawPrimitive()

c++ Source code

//Vertex Structure
struct Vertex
{
float x, y, z,
u, v;
enum
{
FVF = D3DFVF_XYZ | D3DFVF_TEX1
};
};

//Create the vertex array
Vertex* pData;
Vertex data[] = {
{-1.0f,-1.0f, 0.0, 0.0f, 1.0f},
{-1.0f, 1.0f, 0.0, 0.0f, 0.0f},
{ 1.0f,-1.0f, 0.0, 1.0f, 1.0f},
{ 1.0f, 1.0f, 0.0, 1.0f, 0.0f}};
//Create the vertex buffer
pDevice->CreateVertexBuffer(sizeof(playerVertex) * 4, D3DUSAGE_WRITEONLY, Vertex::FVF, D3DPOOL_MANAGED, &pVB, NULL);

//Copy the contents of the vertex array to the vertex buffer
pVB->Lock(0, 0, (void**)&pData, 0);
memcpy(pData, data, sizeof(Vertex) * 4);
pVB->Unlock();

//Create the texture
D3DXCreateTextureFromFile(pDevice, "yourtexture.png", &pTex);

//Render the Quad
pDevice->SetTexture(0, pTex);
pDevice->SetStreamSource(0, pVB, 0, sizeof(Vertex));
pDevice->SetFVF(Vertex::FVF);
pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);



C# Source code

//Create the vertex array
CustomVertex.PositionTextured[] quad = new CustomVertex.PositionTextured[4];
quad[0] = new CustomVertex.PositionTextured(-1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
quad[1] = new CustomVertex.PositionTextured(-1.0f, 2.0f, 0.0f, 0.0f, 0.0f);
quad[2] = new CustomVertex.PositionTextured( 1.0f, 0.0f, 0.0f, 1.0f, 1.0f);
quad[3] = new CustomVertex.PositionTextured( 1.0f, 2.0f, 0.0f, 1.0f, 0.0f);

//Create the vertex buffer
vb = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, renderer.Device, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Managed);
//Copy the contents of the vertex array to the vertex buffer
vb.SetData(quad, 0, LockFlags.None);

//Create the texture
texture = TextureLoader.FromFile(renderer.Device, "texture.jpg");

//Render the quad
renderer.Device.SetTexture(0, texture);
renderer.Device.SetStreamSource(0, vb, 0);
renderer.Device.VertexFormat = CustomVertex.PositionTextured.Format;
renderer.Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);




I hope this helps.
Take care.

Share this post


Link to post
Share on other sites
Quote:
Original post by RhoneRanger
A Simple way would be to create a quad from 4 points using a struct like this


struct Vertex
{
D3DXVECTOR4 XYZRHW;
D3DXVECTOR2 TexCoords;
};


#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_TEX0)


Then assigning the 4 points at

0,ScreenHeight,0.5,1.0; TexCoords 0,1
0,0,0.5,1.0; TexCoords 0,0
ScreenWidth,ScreenHeight,0.5,1.0; TexCoords 1,1
ScreenWidth,0,0.5,1.0; TexCoords 1,0

Rendering a tristrip.



after set RHW , I can directly set screen coordinate?

0,ScreenHeight,0.5,1.0; TexCoords 0,1

what do 0.5 1.0 represent?? .5 = z 1.0 = RHW

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!