Würfel mit Vertex und Indexbuffer zeichnen



  • Hallo,
    ich würde gerne mit dem Vertex und Indexbuffer 2 Würfel bzw. Quader zeichnen. Es klappt auch soweit, aber die Würfel sehen seltsam aus, die Farbe ist grau und grün gemischt, obwohl ich nur Grün angegeben hab und hinten, oben und unten hat er keine Fläche, siehe screenshot: (sie rotieren sich und auf dem Screen haben sie sich um zirka 180* in diesem moment gedreht http://img23.imageshack.us/img23/5012/unbenanntmi.png
    Nun zum Code Init und Render:

    bool InitScene()
    {
    	D3DDevice->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
    	D3DDevice->SetRenderState(D3DRS_LIGHTING, false);
    	D3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    	D3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
    
    	float Aspect = ScreenX / ScreenY;
    
    	D3DXMATRIX projection;
    	D3DXMatrixPerspectiveFovLH(&projection, 90.0f * 0.0174532925199432957692369076848f, Aspect, 0.1f, 100.0f);
    	D3DDevice->SetTransform(D3DTS_PROJECTION, &projection);
    
    	Vertex *cube;
    	unsigned short *indizes; 
    
    	D3DDevice->CreateVertexBuffer(sizeof(Vertex) * 8, 0, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1, D3DPOOL_DEFAULT, &vertexBuffer, NULL);
    	vertexBuffer->Lock(0, 0, (void **)&cube, 0);
    	cube[0].pos = D3DXVECTOR3(-1.0f,  1.0f, -1.0f);
    	cube[1].pos = D3DXVECTOR3(-1.0f,  1.0f,  1.0f);
    	cube[2].pos = D3DXVECTOR3( 1.0f,  1.0f,  1.0f);
    	cube[3].pos = D3DXVECTOR3( 1.0f,  1.0f, -1.0f);
    	cube[4].pos = D3DXVECTOR3(-1.0f, -1.0f, -1.0f);
    	cube[5].pos = D3DXVECTOR3(-1.0f, -1.0f,  1.0f);
    	cube[6].pos = D3DXVECTOR3( 1.0f, -1.0f,  1.0f);
    	cube[7].pos = D3DXVECTOR3( 1.0f, -1.0f, -1.0f);
    	for(int i = 0; i != 8; ++i)
    		cube[0].color = D3DCOLOR_ARGB(255, 0, 255, 0);
    	vertexBuffer->Unlock();
    
    	D3DDevice->CreateIndexBuffer(36, 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &indexBuffer, NULL);
    	indexBuffer->Lock(0, 0, (void **)&indizes, 0);
    	unsigned short cubeIndizes[36] = {7, 3, 0,   4, 7, 0,	// Vorderseite
    			               5, 1, 2,   6, 5, 2,	// Hinterseite
    						   4, 0, 1,   5, 4, 1,	// Linke Seite
    						   6, 2, 3,   7, 6, 3,	// Rechte Seite
    						   2, 1, 0,   3, 2, 0,	// Oberseite
    						   4, 5, 6,   7, 4, 6};	// Unterseite
    	for(int i = 0; i != 36; ++i)
    		indizes[i] = cubeIndizes[i];
    	indexBuffer->Unlock();
    
    	if(D3DXCreateTextureFromFileEx(D3DDevice, TEXT("texture.jpg"), D3DX_DEFAULT, D3DX_DEFAULT,
    							 D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_NONE,
    							 D3DX_DEFAULT, 0, NULL, NULL, &chess) != D3D_OK)
    	{
    		MessageBox(0, TEXT("Failed to Create the Texture!"), AppName.c_str(), MB_ICONERROR);
    		return false;
    	}
    
    	//D3DDevice->SetTexture(0, chess); Textur kommt erst wenn es mit Farbe klappt
    	return true;
    }
    
    bool Render(float NumSecsPassed)
    {
    	D3DXMATRIX rotation;
    	D3DXMatrixRotationY(&rotation, 90.0f * 0.0174532925199432957692369076848f * (timeSum));
    	D3DXMATRIX translation;
    	D3DXMatrixTranslation(&translation, -2.0f, 0.0f, 4.0f);
    	D3DXMATRIX world = rotation * translation;
    	D3DXMATRIX translationRect;
    	D3DXMatrixTranslation(&translationRect, 2.0f, 0.0f, 4.0f);
    	D3DXMATRIX worldRect = rotation * translationRect;
    	D3DDevice->SetTransform(D3DTS_WORLD, &world);
    	if(D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0xF3, 0x30, 0xD4),
    				     1.0f, 0) != D3D_OK)
    	{
    		MessageBox(0, TEXT("D3DDevice->Clear failed!"), AppName.c_str(), MB_ICONERROR);
    		return false;
    	}
    
    	D3DDevice->BeginScene();
    
    	D3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
    	D3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
    	D3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
    	D3DDevice->SetStreamSource(0, vertexBuffer, 0, sizeof(Vertex));
    	D3DDevice->SetIndices(indexBuffer);
    
    	D3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
    
    	D3DDevice->SetTransform(D3DTS_WORLD, &worldRect);
    
    	D3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
    
    	D3DDevice->EndScene();
    
    	D3DDevice->Present(NULL, NULL, NULL, NULL);
    	return true;
    }
    


  • heda

    also ich hab jetzt keine zeit (naja, und richtig lust auch nicht 😃 ) alle werte nachzuprüfen, daher sage ich nur was zur farbe:
    in zeile 28 steht

    cube[0].color = D3DCOLOR_ARGB(255, 0, 255, 0);

    da müsste als index wohl ein i hin.



  • natürlich, danke.
    aber warum wird die Hinter, Unter und Oberseite des Würfels noch nicht gezeichnet? Eventuell wird es vor dem Rendern entfernt, da man es ohne rotierung nicht sehen kann? Wie kann ich das abschalten? Und noch ne andere Frage: wenn ich jetzt auf alle Seiten eine Textur rendern will, wie kann/muss ich da die Vertexkoordinaten der Textur angeben? Ich hab ja nur 8 Vertizen im Vertexbuffer. Gibt es auch sowas wie Indizes für TexCoords?



  • vertices gegen den Uhrzeigersinn anordnen


Anmelden zum Antworten