x. files + primitives = lack textures

Started by
5 comments, last by maxest 18 years, 5 months ago
i have a problem as in the subject. first, i render two triangles with standard DrawIndexedPrimitive, the triangles have textures, everything is ok. now i want to add a sphere into the scene. i declared ID3DXMesh variable. i create the sphere with using D3DXCreateSphere. i render the sphere with ID3DXMesh's method DrawSubset. everything would be fine if it wasn't for a fact, that there are no textures on primitives! i totally can't understand what is a cause. does anyone know anything about it? regards, maxest ps: sorry for my english, it isn't my national language [Edited by - Maxest on November 6, 2005 2:04:48 PM]
Advertisement
D3DXCreateSphere doesn't create a texture-mapped sphere, it only creates the geometry. You have to clone it, lock it, and generate texture coordinates yourself. This article explains how to do all of this.

but the problem is not lack of sphere' textures. the triangles i render don't have textures. and when i don't render the sphere, the textures on primitives are.

code:
void InitDirect3D(HWND hwnd){        ...	D3DXCreateSphere(device,1.0f,16,16,&bullet,NULL);        ...}void RefreshVertices(){	VERTEX *vertices;	WORD *indices;	vertex_buffer->Lock(0,0,(void**)&vertices,0);		vertices[0].x=-20.0f;		vertices[0].y=-10.0f;		vertices[0].z=0.0f;		vertices[0].tu1=0.0f;		vertices[0].tv1=2.5f;		vertices[1].x=20.0f;		vertices[1].y=-10.0f;		vertices[1].z=0.0f;		vertices[1].tu1=5.0f;		vertices[1].tv1=2.5f;		vertices[2].x=20.0f;		vertices[2].y=10.0f;		vertices[2].z=0.0f;		vertices[2].tu1=5.0f;		vertices[2].tv1=0.0f;		vertices[3].x=-20.0f;		vertices[3].y=10.0f;		vertices[3].z=0.0f;		vertices[3].tu1=0.0f;		vertices[3].tv1=0.0f;	vertex_buffer->Unlock();	index_buffer->Lock(0,0,(void**)&indices,0);		indices[0]=0;		indices[1]=1;		indices[2]=3;		indices[3]=3;		indices[4]=1;		indices[5]=2;	index_buffer->Unlock();}void RenderBullet(){	D3DXMATRIX tra,rot,res;	D3DXMatrixTranslation(&tra,player.bullet_x,player.bullet_y,player.bullet_z);	D3DXMatrixRotationY(&rot,DegToRad(-player.bullet_hangle));	res=rot*tra;	device->SetTransform(D3DTS_WORLD,&res);	device->SetTexture(0,NULL);	bullet->DrawSubset(0); // when this method isn't called, there are             textures on triangles from vertex_buffer}void Render(){	D3DXMATRIX identity;	device->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);	SetMatrices();	RefreshVertices();	device->SetStreamSource(0,vertex_buffer,0,sizeof(VERTEX));	device->SetIndices(index_buffer);	device->BeginScene();		D3DXMatrixIdentity(&identity);		device->SetTransform(D3DTS_WORLD,&identity);		device->SetTexture(0,wall_tex);		device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,4,0,2);		RenderBullet();	device->EndScene();	device->Present(NULL,NULL,NULL,NULL);}


[Edited by - Coder on November 7, 2005 5:01:38 AM]
Here is a good way you can go about doing it.
LPD3DXMESH CreateSphere(LPDIRECT3DDEVICE9 device,DWORD radius,DWORD segment){LPD3DXMESH mesh=NULL;D3DXCreateSphere(device,radius,segment,segment,&mesh,0);//By Default there is not Texture so we will generate it.LPD3DXMESH Temp;if(!(mesh->GetFVF() & D3DFVF_NORMAL)){mesh->CloneMeshFVF(mesh->GetOptions(),mesh->GetFVF()|D3DFVF_NORMAL,device,&Temp);mesh->Release();mesh=Temp;D3DXComputeNormal(&mesh,0);}if(!(mesh->GetFVF() & D3DFVF_TEX1)){mesh->CloneMeshFVF(mesh->GetOptions(),mesh->GetFVF()|D3DFVF_TEX1,device,&Temp);mesh->Release();mesh=Temp;}//Sine Our Texture Unit Comes After the Normals in a Structure we can add values//For it's Uv like thatBYTE* p;mesh->LockVertexBuffer(0,(LPVOID*)&p);for(DWORD i=0;i<mesh->GetNumVertices();i++){p=sizeof(float)*3;      //Skip Vertex and go to Normalfloat nx=(*(float*)&*p);p+=4;float ny=(*(float*)&*p);p+=4;float nz=(*(float*)&*p);p+=4;*(*float)&*p=nx/2.0f+.5f;p+=4;*(*float)&*p=ny/2.0f+.5f;p-=16;p-=sizeof(float)*3; p+=D3DXGetFVFVertexSize(mesh->GetFVF()); }mesh->UnlockVertexBuffer();}PS: Now when you set the Texture onto the sphere you will have correct mapping


[Edited by - BornToCode on November 6, 2005 4:35:24 PM]
Maxest, the problem is that you forgot to set the flexible vertex format flag (call SetFVF). When you render a mesh, internally the mesh object will set the necessary FVF for you. But when you render things on your own, you have to set the FVF yourself.

neneboricua
Maxest, I wrapped your code in source tags. Please read this.

THX neneboricua!!! now everything's ok! geez... how could i live without GameDev so long :D

This topic is closed to new replies.

Advertisement