Texture Problem very important coz of a Project Work for University
Hello dudes, I have a problem with my texture. I'm just a beginner so I don't know how to handle it.
The best I think what will help is to copy my sourcecode and compile it to see what's wrong coz I really don't know how to describe it correctly sorry. Help would be really appreciate and needed !
The problem in general is, that the texure is devided in 2 Triangles and one side of the Triangle is scrambled ... ???
So here is my source code:
#include <windows.h>
#include <d3dx9.h>
#include <d3d9.h>
#pragma comment(lib, "d3dx9.lib")
#pragma comment(lib, "d3d9.lib")
//Definition vom Vertex Koord.i.Modelspace Texturkoord. werden benutzt
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
HDC hDC = NULL; // Private GDI Schnittstelle
HWND hWnd = NULL;
HINSTANCE hInstance; // Haltet die Instanz der Applikation
LPDIRECT3D9 pD3D = NULL; // DirectX 3D Version 9
LPDIRECT3DDEVICE9 pD3DDevice = NULL; // DirectX 3D Render Schnittstelle
LPDIRECT3DTEXTURE9 pTexture = NULL;
bool keys[256]; // Array für Keyboard Routine
bool active=TRUE; // Window Active Flag Set TRUE By Default
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
IDirect3DVertexBuffer9 *pVertexBuffer = NULL; // Vertex Buffer
struct my_vertex{
FLOAT x, y, z; // untransformierte Position vom Vertex.
FLOAT tu, tv; // Texturkoordinaten
};
//* Würfelkoordinaten *
my_vertex g_vertices[] ={
// Würfelkoordinaten | Texturkoordinaten
// X Y Z X Y
{ -1.0f, -1.0f, -1.0f, 0.0f, 1.0f }, //Front
{ -1.0f, 1.0f, -1.0f, 0.0f, 0.0f },
{ 1.0f, 1.0f, -1.0f, 1.0f, 0.0f },
{ 1.0f, 1.0f, -1.0f, 1.0f, 0.0f },
{ 1.0f, -1.0f, -1.0f, 1.0f, 1.0f },
{ -1.0f, -1.0f, -1.0f, 0.0f, 1.0f },
{ 1.0f, -1.0f, 1.0f, 0.0f, 1.0f }, //Back
{ 1.0f, 1.0f, 1.0f, 0.0f, 0.0f },
{ -1.0f, 1.0f, 1.0f, 1.0f, 0.0f },
{ -1.0f, 1.0f, 1.0f, 1.0f, 0.0f },
{ -1.0f, -1.0f, 1.0f, 1.0f, 1.0f },
{ 1.0f, -1.0f, 1.0f, 0.0f, 1.0f },
{ -1.0f, 1.0f, -1.0f, 0.0f, 1.0f }, //Top
{ -1.0f, 1.0f, 1.0f, 0.0f, 0.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f, 0.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f, 0.0f },
{ 1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
{ -1.0f, 1.0f, -1.0f, 0.0f, 1.0f },
{ 1.0f, -1.0f, -1.0f, 0.0f, 1.0f }, //Bottom
{ 1.0f, -1.0f, 1.0f, 0.0f, 0.0f },
{ -1.0f, -1.0f, 1.0f, 1.0f, 0.0f },
{ -1.0f, -1.0f, 1.0f, 1.0f, 0.0f },
{ -1.0f, -1.0f, -1.0f, 1.0f, 1.0f },
{ 1.0f, -1.0f, -1.0f, 0.0f, 1.0f },
{ -1.0f, -1.0f, 1.0f, 0.0f, 1.0f }, //Left
{ -1.0f, 1.0f, 1.0f, 0.0f, 0.0f },
{ -1.0f, 1.0f, -1.0f, 1.0f, 0.0f },
{ -1.0f, 1.0f, -1.0f, 1.0f, 0.0f },
{ -1.0f, -1.0f, -1.0f, 1.0f, 1.0f },
{ -1.0f, -1.0f, 1.0f, 0.0f, 1.0f },
{ 1.0f, -1.0f, -1.0f, 0.0f, 1.0f }, //Right
{ 1.0f, 1.0f, -1.0f, 0.0f, 0.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f, 0.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f, 0.0f },
{ 1.0f, -1.0f, 1.0f, 1.0f, 1.0f },
{ 1.0f, -1.0f, -1.0f, 0.0f, 1.0f },
};
#define NUM_VERTICES (sizeof(g_vertices)/sizeof(my_vertex))
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Deklaration für WndProc
void ReSizeD3DScene(int width, int height) // Resize und Initialisierung vom D3D Fenster
{
if (height==0) // Vorbeugen von (Nulldivision) Zero durch
{
height=1; // Height 1 setzen
}
D3DXMATRIX projection_matrix;
D3DXMatrixPerspectiveFovLH(&projection_matrix, 45.0f, (float)width/(float)height, 0.1f, 100.0f );
pD3DDevice->SetTransform( D3DTS_PROJECTION, &(D3DMATRIX)projection_matrix );
D3DXMatrixIdentity(&projection_matrix);
}
int InitD3D() // Setup Für D3D
{
pD3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE ); // ZBUFFER
pD3DDevice->SetRenderState(D3DRS_CULLMODE, FALSE); // Backface Culling
pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE); // Licht
pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP, D3DTOP_DISABLE);
pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC);
pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);
pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_ANISOTROPIC);
D3DXCreateTextureFromFile( pD3DDevice, "DH.bmp",&pTexture);
pD3DDevice->CreateVertexBuffer( 24*sizeof(g_vertices),0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &pVertexBuffer, NULL );
unsigned char *pVertices = NULL;
pVertexBuffer->Lock( 0, sizeof(g_vertices), (void**)&pVertices, 0 );
memcpy( pVertices, g_vertices, sizeof(g_vertices) );
pVertexBuffer->Unlock();
return TRUE; //Initialisierung OK
}
int DrawD3DScene() // Zeichnen
{
pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, // Clear Screen und Depth Buffer
D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,0.0f), 1.0f, 0 );
static float rotqube = 0.0f; //Rotationsgeschwindigkeit
rotqube += 0.4f;
D3DXMATRIX matWorld; // World Matrix
D3DXMATRIX matTranslation; // Translation Matrix
D3DXMATRIX matRotX; // X Achse Rotations Matrix
D3DXMATRIX matRotY; // Y Achse Rotations Matrix
D3DXMATRIX matRotZ; // Z Achse Rotations Matrix
D3DXMATRIX matRotXYZ; // XYZ Rotations Matrix
D3DXMatrixTranslation( &matTranslation, 0.0f, 0.0f, 5.0f );
D3DXMatrixRotationY(&matRotY, D3DXToRadian(rotqube)); // Rotation in Y Richtung
D3DXMatrixRotationX(&matRotX, D3DXToRadian(rotqube)); // Rotation in X Richtung
D3DXMatrixRotationY(&matRotY, D3DXToRadian(rotqube)); // Rotate In Z Richtung
D3DXMatrixRotationZ(&matRotZ, D3DXToRadian(rotqube));
D3DXMatrixRotationYawPitchRoll( &matRotX, D3DXToRadian(rotqube), D3DXToRadian(rotqube), D3DXToRadian(rotqube) );
matWorld = (matRotY * matRotX * matRotZ) * matTranslation;
pD3DDevice->SetTransform( D3DTS_WORLD, &matWorld ); // Setup Transformation
pD3DDevice->BeginScene(); // Begin der Direct3D Szene
pD3DDevice->SetStreamSource( 0, pVertexBuffer, 0, sizeof(my_vertex) );
pD3DDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,NUM_VERTICES/3); //Würfel zeichnen
pD3DDevice->EndScene(); // Ende der Direct3D Szene
pD3DDevice->Present( NULL, NULL, NULL, NULL );// Ergebnis anzeigen
pD3DDevice->SetTexture( 0, pTexture );
return TRUE;
}
void KillD3DScene()
{
if( pVertexBuffer != NULL )
{
pVertexBuffer->Release(); // Vertex Buffer freimachen
pVertexBuffer = NULL;
pTexture->Release();
pTexture=NULL;
}
}
void KillD3DWindow() // Fenster schliessen
{
if (pD3DDevice != NULL) pD3DDevice->Release(); // D3D Schnittstelle freimachen
if (pD3D != NULL) pD3D->Release(); // D3D Schnittstelle freimachen
if (fullscreen) // Vollbildabfrage
{
ChangeDisplaySettings(NULL,0); // Wenn ja, zurück zum Desktop
ShowCursor(TRUE); // Mauszeiger anzeigen
}
if (hDC && !ReleaseDC(hWnd,hDC)) // Kann DC freigemacht werden
{
MessageBox(NULL,"Release Device Context Fehlgeschlagen.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC=NULL; // DC NULL setzen
}
if (hWnd && !DestroyWindow(hWnd)) // Kann man Fenster schliessen
{
MessageBox(NULL,"Fenster konnte nicht entfernt werden -> hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL; // hWnd NULL setzen
}
if (!UnregisterClass("Direct3D",hInstance)) // Kann man Klasse unregistrieren
{
MessageBox(NULL,"Programm konnte nicht korrekt abgemeldet werden.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance=NULL; // hInstance NULL setzen
}
}
//============================================================================================
// Fenstergenerierung
//===========================================================================================
BOOL CreateD3DWindow(char* title, int width, int height, bool fullscreenflag)
{
WNDCLASS wc;
DWORD dwExstyle; // Fenster Extended style
DWORD dwstyle; // Fenster aussehen
RECT WindowRect; // Werte Dreieck oben links / unten rechts
WindowRect.left=(long)0; // Linkswert 0
WindowRect.right=(long)width; // Rechtswert auf angeforderte Breite
WindowRect.top=(long)0; // Wert oben 0
WindowRect.bottom=(long)height; // Wert oben auf angeforderte Höhe
fullscreen=fullscreenflag; // Globaler Fullscreen Flag
hInstance = GetModuleHandle(NULL);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Icon
wc.hCursor = LoadCursor(NULL, IDC_CROSS); // Mauszeiger
wc.hbrBackground = NULL; // Hintergrund D3D aber 0
wc.lpszMenuName = NULL; // Menü, aber 0
wc.lpszClassName = "Direct3D"; // Klasse
if (!RegisterClass(&wc)) // Fenster registrieren
{
MessageBox(NULL,"Registrieren der Window Klasse fehlgeschlagen.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if (fullscreen) // Vollbild?
{
dwExstyle=WS_EX_APPWINDOW;
dwstyle=WS_POPUP;
ShowCursor(FALSE); // Mauszeiger verstecken
}
else
{
dwExstyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwstyle=WS_OVERLAPPEDWINDOW;
}
AdjustWindowRectEx(&WindowRect, dwstyle, FALSE, dwExstyle); // Fenster einstellen auf angeforderte Größe
// Create The Window
if (!(hWnd=CreateWindowEx( dwExstyle,
"Direct3D", // Klassenname
title, // Fenstername
dwstyle | // Definierter Fenster style
WS_CLIPSIBLINGS | // Benötigter Fenster style
WS_CLIPCHILDREN, // Benötigter Fenster style
0, 0, // Fenster Position
WindowRect.right-WindowRect.left, // Fenster Breite anpassen
WindowRect.bottom-WindowRect.top, // Fenster Höhe anpassen
NULL, // Kein parent Window
NULL, // Kein Menü
hInstance,
NULL)))
{
KillD3DWindow(); // Reset Anzeige
MessageBox(NULL,"Fenstererzeugungsfehler.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if (!(hDC=GetDC(hWnd))) // Schnittstellen Context ?
{
KillD3DWindow();
MessageBox(NULL,"Kann keinen Schnittstellen Contex erzeugen.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
pD3D = Direct3DCreate9(D3D_SDK_VERSION); // Abfrage DX Version
if ( pD3D == NULL )
{
KillD3DWindow();
MessageBox(NULL,"Kann DirectX SDK Version 9.x nicht finden","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
D3DPRESENT_PARAMETERS d3dpp= // d3dpp übergibt Paramter Windows
{
width, // Back Buffer Breite
height, // Back Buffer Höhe
D3DFMT_R5G6B5, // Back Buffer Format (Farbtiefe)
1, // Back Buffer Zählen (Double Buffer)
D3DMULTISAMPLE_NONE,
0,
D3DSWAPEFFECT_DISCARD,
hWnd,
!fullscreen, // Fenster oder Vollbild
TRUE,
D3DFMT_D16, // 16Bit Z-Buffer (Depth Buffer)
0,
D3DPRESENT_RATE_DEFAULT, // Refresh Rate auf default
D3DPRESENT_INTERVAL_DEFAULT
};
// Surface Format überprüfen
if ( FAILED( pD3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
d3dpp.BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
D3DRTYPE_SURFACE, d3dpp.AutoDepthStencilFormat ) ) )
{
KillD3DWindow(); // Reset The Display
MessageBox(NULL,"Surface Format nicht gefunden.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
// DirectX 3D Schnittstelle überprüfen
if(FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pD3DDevice ) ) )
{
KillD3DWindow();
MessageBox(NULL,"Kann DirectX 3D Schnittstelle nicht erzeugen.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
ShowWindow(hWnd,SW_SHOW); // Fenster anzeigen
SetForegroundWindow(hWnd);
SetFocus(hWnd); // Focus setzen
ReSizeD3DScene(width, height); // Perspektive setzen
if (!InitD3D()) // D3D Fenster initialisieren
{
KillD3DWindow();
MessageBox(NULL,"Initialisierung fehlgeschlagen.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK WndProc( HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam )
// MESSAGEHANDLER
{
switch( uMsg )
{
case WM_ACTIVATE: // Aktive Nachricht ?
{
if (!HIWORD(wParam)) // Minimiert ?
{
active=TRUE; // Programm aktiv
}
else
{
active=FALSE; // Programm inaktiv
}
return 0;
}
case WM_SYSCOMMAND:
{
switch (wParam)
{
case SC_SCREENSAVE: // Screensaver start ?
case SC_MONITORPOWER: // Energiemodus aktiv ?
return 0;
}
break;
}
case WM_CLOSE: // Close Nachricht erhalten?
{
PostQuitMessage(0); // Quit Message senden
return 0;
}
case WM_KEYDOWN: // Taste gedrückt?
{
keys[wParam] = TRUE; // Falls ja TRUE
return 0;
}
case WM_KEYUP: // Taste nicht gedrückt?
{
keys[wParam] = FALSE; // Falls ja FALSE
return 0;
}
case WM_SIZE: // Direct3D Fenster neue Größe
{
if(!fullscreen)
ReSizeD3DScene(LOWORD(lParam), HIWORD(lParam)); // LoWord=Breite, HiWord=Höhe
return 0;
}
}
// Alle unbehandelten Nachrichten an DefWindowProc senden
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
MSG msg;
BOOL done=FALSE; // Bool Variable um aus Schleife zu kommen
// Welcher Bildschirmmodus ?
if (MessageBox(NULL,"F1 - Vollbildmodus \nF2 - ZBUFFER aus\nESC - Programm beenden", "Funktionstasten",MB_OK|MB_ICONINFORMATION|MB_DEFBUTTON2==IDNO)==IDOK)
{
fullscreen=FALSE; // Fenstermodus
}
// Create Our DirectX 3D Window
if (!CreateD3DWindow("Projekt Graphik (C) 2005 by CP ",1024,768,fullscreen))
{
return 0; // Quit wenn Fenster nicht erzeugt
}
while(!done) // Schleife done=FALSE
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Wartet Nachricht ?
{
if (msg.message==WM_QUIT) // Quit Nachricht ?
{
done=TRUE; // If So done=TRUE
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else // Wenn keine Nachrichten vorhanden
{
// Zeichnen der Szene. Abfragen von ESC Key und DrawD3DScene()
if ((active && !DrawD3DScene()) || keys[VK_ESCAPE]) // Active? Quit erhalten?
{
done=TRUE; // ESC DrawD3DScene
}
//ZBUFFER ausschalten mit F2
if (keys[VK_F2])
{
pD3DDevice->SetRenderState(D3DRS_ZENABLE, FALSE ); // Enable Z-Buffer (Depth Buffer)
pD3DDevice->SetRenderState(D3DRS_CULLMODE, FALSE); // Disable Backface Culling
pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE); // Disable Light
pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP, D3DTOP_DISABLE);
D3DXCreateTextureFromFile( pD3DDevice, "tex.bmp",&pTexture);
pD3DDevice->CreateVertexBuffer( 24*sizeof(g_vertices),0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &pVertexBuffer, NULL );
unsigned char *pVertices = NULL;
pVertexBuffer->Lock( 0, sizeof(g_vertices), (void**)&pVertices, 0 );
memcpy( pVertices, g_vertices, sizeof(g_vertices) );
pVertexBuffer->Unlock();
}
// ZBUFFER mit F3 einschalten
if (keys[VK_F3])
{
pD3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE ); // Enable Z-Buffer (Depth Buffer)
pD3DDevice->SetRenderState(D3DRS_CULLMODE, FALSE); // Disable Backface Culling
pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE); // Disable Light
pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP, D3DTOP_DISABLE);
D3DXCreateTextureFromFile( pD3DDevice, "tex.bmp",&pTexture);
pD3DDevice->CreateVertexBuffer( 24*sizeof(g_vertices),0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &pVertexBuffer, NULL );
unsigned char *pVertices = NULL;
pVertexBuffer->Lock( 0, sizeof(g_vertices), (void**)&pVertices, 0 );
memcpy( pVertices, g_vertices, sizeof(g_vertices) );
pVertexBuffer->Unlock();
}
// Vollbild aktivieren mit F1
if (keys[VK_F1]) // F1 gedrückt ?
{
keys[VK_F1]=FALSE; // Wenn ja key = false
KillD3DWindow();
fullscreen=!fullscreen; // Vollbild / Fenstermodus
// D3D Fenster neu machen
if (!CreateD3DWindow("Projekt Graphik (C) 2005 by CP",1024,768,fullscreen))
{
return 0; // Quit wenn kein Fenster erzeugt wurde
}
}
}
}
// Shutdown
KillD3DWindow();
return (msg.wParam); // Exit
}
For our benefit, please use the [ source ] ... [ /source ] tags to make your huge listing a bit more manageable [smile]
I'm at work now, so I can't compile that code.
Regarding this line:
pD3DDevice->SetTexture( 0, pTexture );
Why is it after your Draw call? From a quick scan it'll only mess up the first frame (null texture) but it's still a little odd [smile]
-----
Any chance of a screenshot? "scrambled" can have a few interpretations - bad texture data (error during load/create) or bad texture coordinates (mapping incorrect texels) are possibles.
Have you run it against the reference device? modify the following line:
pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pD3DDevice )
to be:
pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pD3DDevice )
This will eliminate it being a hardware/driver specific issue.
After that, have you tried running it against the debug runtimes (with maximum output)?? you might be lucky and find that it's warning you against doing something (or not doing something) that is the source of your error.
hth
Jack
I'm at work now, so I can't compile that code.
Regarding this line:
pD3DDevice->SetTexture( 0, pTexture );
Why is it after your Draw call? From a quick scan it'll only mess up the first frame (null texture) but it's still a little odd [smile]
-----
Any chance of a screenshot? "scrambled" can have a few interpretations - bad texture data (error during load/create) or bad texture coordinates (mapping incorrect texels) are possibles.
Have you run it against the reference device? modify the following line:
pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pD3DDevice )
to be:
pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pD3DDevice )
This will eliminate it being a hardware/driver specific issue.
After that, have you tried running it against the debug runtimes (with maximum output)?? you might be lucky and find that it's warning you against doing something (or not doing something) that is the source of your error.
hth
Jack
"#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)"
you define your vertex to have diffuse data, but in your vertex structure:
struct my_vertex{
FLOAT x, y, z; // untransformierte Position vom Vertex.
FLOAT tu, tv; // Texturkoordinaten
};
you just define XYZ and u,v1...
your vertex structure should be:
struct my_vertex{
FLOAT x, y, z; // untransformierte Position vom Vertex.
DWORD Diffuse; // diffuse component
FLOAT tu, tv; // Texturkoordinaten
};
.....
this will problably cause your "fault" texturing. becase your FVF sais your vertex structure is 12+4+8 byte, but you define your structure as 12+8 byte, which lead to missreading in your vertexbuffer.
Sorry for the /source Tag didn't know that -> next time ^^
Your ideas didn't work so far sorry but I've made a screenshot of my problem click here
click
Thanks for your help btw
Your ideas didn't work so far sorry but I've made a screenshot of my problem click here
click
Thanks for your help btw
Check your uv coordinates....
anyway, you should match the size of your vertex structure with your FVF when using Fixed Function pipeline.
Which of the faces are you rendering(the one on the picture above)?
anyway, you should match the size of your vertex structure with your FVF when using Fixed Function pipeline.
Which of the faces are you rendering(the one on the picture above)?
@ Kuba your change to the vertex changed something. Seems nice, but not that what I wanted
Here is a screenshot of your idea or what happened after I changed it
it has something to do with the DWORD Diffuse command
click
I want to show all faces of the cube coz it's rotating
Thank you very much for your help
Here is a screenshot of your idea or what happened after I changed it
it has something to do with the DWORD Diffuse command
click
I want to show all faces of the cube coz it's rotating
Thank you very much for your help
this is problably your error:
"{ -1.0f, -1.0f, -1.0f, 0.0f, 1.0f }, //Front"
now you are setting position to (-1,-1,-1) and then the Diffuse component to 0 and the u coordinate to 0, BUT!...the v koordinate is undefined...change it to:
"{ -1.0f, -1.0f, -1.0f, 0, 0.0f, 1.0f }, //Front"
that is, put a 0 before your uv coordinates( the fourth component)
compare this to your new vertex structure:
struct my_vertex{
FLOAT x, y, z; // untransformierte Position vom Vertex.
DWROD Diffuse;
FLOAT tu, tv; // Texturkoordinaten
};
and you will se that the fourth component is the Diffuse component
and do the same to all your vertex definitions...
"pD3DDevice->CreateVertexBuffer( 24*sizeof(g_vertices),0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &pVertexBuffer, NULL );"
your are creating 24 cubes? is this your purpose?
put this:
"pD3DDevice->SetTexture( 0, pTexture );"
before this:
pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,NUM_VERTICES/3); //Würfel zeichnen
"{ -1.0f, -1.0f, -1.0f, 0.0f, 1.0f }, //Front"
now you are setting position to (-1,-1,-1) and then the Diffuse component to 0 and the u coordinate to 0, BUT!...the v koordinate is undefined...change it to:
"{ -1.0f, -1.0f, -1.0f, 0, 0.0f, 1.0f }, //Front"
that is, put a 0 before your uv coordinates( the fourth component)
compare this to your new vertex structure:
struct my_vertex{
FLOAT x, y, z; // untransformierte Position vom Vertex.
DWROD Diffuse;
FLOAT tu, tv; // Texturkoordinaten
};
and you will se that the fourth component is the Diffuse component
and do the same to all your vertex definitions...
"pD3DDevice->CreateVertexBuffer( 24*sizeof(g_vertices),0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &pVertexBuffer, NULL );"
your are creating 24 cubes? is this your purpose?
put this:
"pD3DDevice->SetTexture( 0, pTexture );"
before this:
pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,NUM_VERTICES/3); //Würfel zeichnen
@ Keba
YIPIEEEEEE that was it to put the 0' to the Vertex Coordinates
Could you please describe what the diffuse component is or what it makes
And thank you 10000 times you saved my day :)
The 24 doesn't mean 24 cubes it means 24 points to draw the cube
Uhm would it be very difficult to put a normal standard light to the scene ?
YIPIEEEEEE that was it to put the 0' to the Vertex Coordinates
Could you please describe what the diffuse component is or what it makes
And thank you 10000 times you saved my day :)
The 24 doesn't mean 24 cubes it means 24 points to draw the cube
Uhm would it be very difficult to put a normal standard light to the scene ?
1. The diffuse component is setting a color to your Triangles in the format ARGB(Alpha, Red, Green, Blue).
To use it when rendering change:
"pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP, D3DTOP_DISABLE);"
to
"pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE);
pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP, D3DTOP_DISABLE);"
the modulate command informs D3D to use the diffuse component multiplyed with the texture color as the final pixel color.
the alpha part in the diffuse component is used for transparency, but when you are adding: "pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP, D3DTOP_DISABLE);" Alpha is disabled in this texturestage. search for D3DTOP_MODULATE and D3DTA_TEXTURE in the SDK Documetns for more information about operators and operands.
2. when you write:
"pD3DDevice->CreateVertexBuffer( 24*sizeof(g_vertices),0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &pVertexBuffer, NULL );"
you are telling DX to create a vertex buffer with 24 elements that got the size of g_vertices, which in turn is 36 vertices with the size of your my_vertex structres, that is, you are allocatin memory for 24 cubes, but only using one.
"pD3DDevice->CreateVertexBuffer( 24*sizeof(my_vertex),0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &pVertexBuffer, NULL );"
would be more likley, here you are telling DX to create 24 vertices that has the size of your my_vertex struct, but! as you are rendering with TRILIST, you cwould need 36 vertices, which is the amount of vertices that you are createing. so correct in this case would be:
pD3DDevice->CreateVertexBuffer( 36*sizeof(my_vertex),0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &pVertexBuffer, NULL ) Which tells DX to create 36 vertices that has the size of my_vertex, which are the totaly amount of vertices you use.
3.
Adding a standard light to your program would be easy.
1. Add normal component to your my_vertex structure:
struct my_vertex{
FLOAT x, y, z; // untransformierte Position vom Vertex.
FLOAT nx,ny,nz; // Normal
DWORD Diffuse; // Diffuse
FLOAT tu, tv; // Texturkoordinaten
};
2. Change the FVF specification to : #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1)
3. setting upp the vertices definitions correct(as with the diffuse component). add the normals,as you can se in your new structure, after the positions
4. Create a material
5. create a Light
// in render loop.
6. Attach Light
7. Attach material
8. Render
for more clarification on part 4-8 se this
To use it when rendering change:
"pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP, D3DTOP_DISABLE);"
to
"pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE);
pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP, D3DTOP_DISABLE);"
the modulate command informs D3D to use the diffuse component multiplyed with the texture color as the final pixel color.
the alpha part in the diffuse component is used for transparency, but when you are adding: "pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP, D3DTOP_DISABLE);" Alpha is disabled in this texturestage. search for D3DTOP_MODULATE and D3DTA_TEXTURE in the SDK Documetns for more information about operators and operands.
2. when you write:
"pD3DDevice->CreateVertexBuffer( 24*sizeof(g_vertices),0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &pVertexBuffer, NULL );"
you are telling DX to create a vertex buffer with 24 elements that got the size of g_vertices, which in turn is 36 vertices with the size of your my_vertex structres, that is, you are allocatin memory for 24 cubes, but only using one.
"pD3DDevice->CreateVertexBuffer( 24*sizeof(my_vertex),0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &pVertexBuffer, NULL );"
would be more likley, here you are telling DX to create 24 vertices that has the size of your my_vertex struct, but! as you are rendering with TRILIST, you cwould need 36 vertices, which is the amount of vertices that you are createing. so correct in this case would be:
pD3DDevice->CreateVertexBuffer( 36*sizeof(my_vertex),0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &pVertexBuffer, NULL ) Which tells DX to create 36 vertices that has the size of my_vertex, which are the totaly amount of vertices you use.
3.
Adding a standard light to your program would be easy.
1. Add normal component to your my_vertex structure:
struct my_vertex{
FLOAT x, y, z; // untransformierte Position vom Vertex.
FLOAT nx,ny,nz; // Normal
DWORD Diffuse; // Diffuse
FLOAT tu, tv; // Texturkoordinaten
};
2. Change the FVF specification to : #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1)
3. setting upp the vertices definitions correct(as with the diffuse component). add the normals,as you can se in your new structure, after the positions
4. Create a material
5. create a Light
// in render loop.
6. Attach Light
7. Attach material
8. Render
for more clarification on part 4-8 se this
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement