Sign in to follow this  

Problem with texture coordinates

This topic is 4838 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

I am rendering 2 primitives with 4 vertices using DrawIndexedPrimitive(). I am assigning the texture coordinates in the following way: v0------v1 |00000000| |00000000| v3------v2 v0 = 0.0f,0.0f v1 = 1.0f,0.0f v2 = 1.0f,1.0f v3 = 0.0f,1.0f I am missing some detail that is causing the top part half of the texture to corrupt, and the bottom half to get the top half of the texture :) If it makes any difference, this is how the 2 primitives are indexed: Primitive 0 0 = (0,1,3) Primitive 1 1 = (3,1,2) Im still really new to D3D, so be nice :)

Share this post


Link to post
Share on other sites
sometimes a value of 0.5 is lost in the conversion process from floating point texture coordinates to integral screen coordinates. So adding 0.5 from the texture cooredinates may solve this problem of yours.

You may want to read the following from the sdk docs as well.

DirectX Graphics -> Programming Guide -> Direct3D Textures -> Texture Filtering -> Nearest Point Sampling


The text after the image with the artifacts has some details on the 0.5 issue.

[edit]
and the reason the top is appearing at the bottom is probably because your D3DSAMP_ADDRESSU and D3DSAMP_ADDRESSV sampler states are set to D3DTADDRESS_WRAP

see: IDirect3DDevice9::SetSamplerState function
[/edit]

Share this post


Link to post
Share on other sites
Thanks for the advice, I wish I had more luck. I have tried giving offsets to the texture coords to no avail. I also read through the suggested doc and tried a few variations, but that didnt help a bit. Also, I dont have the sampler state set to texture wrap. Ill put some code up to help.

Ill see about posting a screen shot so the error can be clearly seen.

The texture Im using is 128x128, btw. So, it matched the screen coords exactly. Any help would be appreciated.

I also set the vertices to the following:

v0-----v2
|0000000|
|0000000|
v1-----v3

Ive tried alot of different things :(




/////////////////
//Setup2DCamera//
/////////////////

int D3D_ENGINE::Setup2DCamera(int width,int height)
{
//Setup camera
D3DXMATRIX Ortho2D;
D3DXMATRIX Identity;

D3DXMatrixOrthoLH(&Ortho2D,width,height, 0.0f, 1.0f);
D3DXMatrixIdentity(&Identity);

d3d_device->SetTransform(D3DTS_PROJECTION, &Ortho2D);
d3d_device->SetTransform(D3DTS_WORLD, &Identity);
d3d_device->SetTransform(D3DTS_VIEW, &Identity);

//Set lighting to false
d3d_device->SetRenderState(D3DRS_LIGHTING,FALSE);

//Set filter
d3d_device->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
d3d_device->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);

//Set mipmap filter
d3d_device->SetSamplerState(0,D3DSAMP_MIPFILTER,D3DTEXF_POINT);

return(1);
}
/////////////////////
//End Setup2DCamera//
/////////////////////


///////////////////
//Init_vb_main_ui//
///////////////////

int RENDER_CLASS::Init_vb_main_ui()
{
//Setup D3D_VERTEX pointer
D3D_VERTEX* Vertice = NULL;
int x_offset = -512;
int y_offset = +384;

//Lock the vertex buffer
vb_main_ui->Lock(0, 12 * sizeof(D3D_VERTEX),(void**)&Vertice,NULL);

//Z-buffer
Vertice[0].z = 0;
Vertice[1].z = 0;
Vertice[2].z = 0;
Vertice[3].z = 0;

//Vertex colors
Vertice[0].color = Transparent;
Vertice[1].color = Transparent;
Vertice[2].color = Transparent;
Vertice[3].color = Transparent;

//Texture postions
//Vertice 0
Vertice[0].u = 0.00f;
Vertice[0].v = 0.00f;

//Vertice 1
Vertice[1].u = 0.00f;
Vertice[1].v = 1.00f;

//Vertice 2
Vertice[2].u = 1.00f;
Vertice[2].v = 0.00f;

//Vertice 3
Vertice[3].u = 1.00f;
Vertice[3].v = 1.00f;

//Vertex locations
//Vertice 0
Vertice[0].x = 896 + x_offset;
Vertice[0].y = 0 + y_offset;

//Vertice 1
Vertice[1].x = 896 + x_offset;
Vertice[1].y = -128 + y_offset;

//Vertice 2
Vertice[2].x = 1024 + x_offset;
Vertice[2].y = 0 + y_offset;

//Vertice 3
Vertice[3].x = 1024 + x_offset;
Vertice[3].y = -128 + y_offset;

//Unlock the buffer
vb_main_ui->Unlock();

return(1);
}
///////////////////////
//End Init_vb_main_ui//
///////////////////////

///////////////////
//Init_ib_main_ui//
///////////////////

int RENDER_CLASS::Init_ib_main_ui()
{
//Define the triangles of the primitive
WORD* Indices = 0;

//Lock the index buffer
ib_main_ui->Lock(0,0,(void**)&Indices,0);

//Triangle 0
Indices[0] = 0,Indices[1] = 2,Indices[2] = 1;

//Triangle 1
Indices[3] = 1,Indices[4] = 2,Indices[5] = 3;

//Unlock the buffer
ib_main_ui->Unlock();

return(1);
}
///////////////////////
//End Init_ib_main_ui//
///////////////////////


/////////////////////
//Render_vb_main_ui//
/////////////////////

void RENDER_CLASS::Render_vb_main_ui()
{
//Draw top pane
SetCurrentTexture(right_pane_a);
d3d_device->SetStreamSource(0,vb_main_ui,0 * sizeof(D3D_VERTEX),sizeof(D3D_VERTEX));
d3d_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,4,0,2);

//Draw middle pane
//SetCurrentTexture(NULL);

//Draw bottom pane
//SetCurrentTexture(NULL);

}
/////////////////////////
//End Render_vb_main_ui//
/////////////////////////




Share this post


Link to post
Share on other sites
are these pre-transformed vertices? whats the D3D_VERTEX structure? If they are then the y coordinate looks like inversed I think. Also you have 4 vertices and you lock the buffer for a size of 12.

Share this post


Link to post
Share on other sites
I must have a serious misunderstanding of texturing. If I load up a 64x64 size texture from my paint program, everything is fine. Is a 64x64 texture the limit? If so, I didnt read that in any books.

Share this post


Link to post
Share on other sites
I've been going through similar problems, and I managed to solve them by first shifting the texture coordinates by 0.5 and then set both the D3DSAMP_MINFILTER and D3DSAMP_MAGFILTER to D3DTEXF_POINT in the sampler state.

Hlnsi

Share this post


Link to post
Share on other sites
You can check out your max texture size by checking the device caps. Also, sometimes textures are required to be in powers of 2. Check out the DXCapsViewer software in the Utilities directory of your SDK. Look under DX Graphics/Device Types/HAL/CAPS/MaxTextureWidth and Height, and look under DX Graphics/Device Types/HAL/CAPS/Texture Caps/D3DPTEXTURECAPS_POW2.

Also note that you might have problems with the UV Texture Addressing modes.

You'd have to have a pretty OLD video card to only have a 64x64 max texture size, or it is just a really sad video card. :)

Chris

Share this post


Link to post
Share on other sites

This topic is 4838 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this