Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2013
    Posts
    3

    C++ and DirectX10

    Hi there. I'm new to Game programming with some Delphi experience. Recently I changed to c++ and adopted fairly well. Now I’m busy learning directX 10. However, I’m running short on help and there are some grey areas in my whole understanding of DirectX. I’m using different sources for my learning material but mainly Frank Luna’s book on DirectX 10. I’ve progressed to me being able to create and texture a landscape and add lighting to it. I don’t know if this is the right place for the question but is there any one that have a understanding of C++ graphics programming that could help me?

  2. #2
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    What do you want to know?

  3. #3
    Join Date
    Apr 2013
    Posts
    3
    Thanks a lot in advance. OK, first problem I’ve encountered was that I’m trying to render a Land mass and the lower part of this land mass must be filled with water. The water is a grit 100 x 100 verts and the land the same dimensions. My procedure is to render the land mass first and then render the water mass. But the results is no as expected. Instead of rendering the water mass only where the land is low enough, it renders the water over the whole land mass. It must be Depth stencil related I think but I can’t figure out why. Like I’ve mentioned, I’m using Frank Luna’s book Beginning Game Programming with DirectX 10. I think I did everything as described. I’m adding some of my code that I think could help you form the idea of what I’m trying to achieve.

    This is my Rendering function.
    I havn't change the comments yet but I'm sure you'll understand the code.

    void CDieApp::Render()
    {
    D3DClass::Render();

    /*Set alles aan die IA en OM*/
    //If I add the depth stencil state nothing is renderd at all
    //md3dDevice->OMSetDepthStencilState(0,0);
    float blendFactors[] = {0.0f, 0.0f, 0.0f, 0.0f};
    md3dDevice->OMSetBlendState(0, blendFactors, 0xffffffff);

    md3dDevice->IASetInputLayout(RenderVAR->VertexInputLayout);
    md3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_T RIANGLELIST);
    md3dDevice->RSSetState(0);
    RenderVAR->FinalMatrix = RenderVAR->ViewMatrix * RenderVAR->ProjectionMatrix;
    RenderVAR->ShaderMatrixVar->SetMatrix((float*)RenderVAR->FinalMatrix);
    RenderVAR->ShaderCameraPosVar->SetFloatVector((float*)&RenderVAR->CameraPos);

    //Loop
    D3D10_TECHNIQUE_DESC techniqueDescription;
    RenderVAR->ShaderTech->GetDesc(&techniqueDescription);
    // Loop deur die tech passes
    for(UINT p=0; p < techniqueDescription.Passes; ++p)
    {
    //Set Textures vir die land gedeelte
    TextureVAR->DiffuseMapVar->SetResource(TextureVAR->DiffuseGrass);//Textures to shader
    TextureVAR->SpecularMapVar->SetResource(TextureVAR->SpecGrass);
    RenderVAR->ShaderTech->GetPassByIndex(p)->Apply(0);
    LandMass->DrawLand();

    //Set Textures vir die water gedeelte
    TextureVAR->DiffuseMapVar->SetResource(TextureVAR->DiffuseWater);//Textures to shader
    TextureVAR->SpecularMapVar->SetResource(TextureVAR->SpecWater);
    RenderVAR->ShaderTech->GetPassByIndex(p)->Apply(0);
    WaterMass->DrawWater();
    }

    Now this is the land creation function.

    void C_LAND::InitLand(ID3D10Device *device)
    {
    md3dDevice = device;

    //verklaar die twee arrays wat benodig gaan word vir my buffers
    Vertex vertices[NUM_VERTSX*NUM_VERTSY];
    DWORD indices[NUM_VERTSX * NUM_VERTSY * 6];
    //================================================== ========
    //Die loop nest wat die koordinate van die verts moet bepaal
    //================================================== ========
    int Add = 0;//Om te se hoeveel ek elke keer moet by tel vir die pos in die array
    float x = 0;
    float y = 0;
    float z = 0;
    D3DXVECTOR4 Diffuse = WHITE;
    D3DXVECTOR4 Spec = WHITE;

    ////Vul my vertex array
    float TexY = 5.0f / (NUM_VERTSY - 1);
    float TexX = 5.0f / (NUM_VERTSX - 1);
    float YTexPos = NUM_VERTSY - 1;

    //Create file===================================
    ofstream file("code.txt");
    file<<"File created\n";
    //Create file===================================

    for (int r = 0; r < NUM_VERTSY; r++)
    {
    for (int c = 0; c < NUM_VERTSX; c++)
    {
    //To file
    float VertexToFile = r * NUM_VERTSX + c;
    file<<"Vertex nr: "<< VertexToFile <<" (" << c*TexX << ";"<<YTexPos*TexY << ")\n";

    //X en Z koordinate
    x = (float)c * CELL_WIDTH;
    z = (float)r * CELL_HEIGHT;
    //Die hoogte
    y = 0.3f*( r*sinf(0.1f*c) + c*cosf(0.1f*r) );
    //Die kleur
    vertices[r * NUM_VERTSX + c].Pos = D3DXVECTOR3(x,y,z);
    vertices[r * NUM_VERTSX + c].Tex.x = c*TexX;
    vertices[r * NUM_VERTSX + c].Tex.y = YTexPos*TexY;

    }
    YTexPos = YTexPos - 1;
    }//Einde van for r

    file.close();/////////////////////////////////////////////////////////////////

    //================================================== =====================
    //Bou die index buffer
    //================================================== =====================

    int curIndex = 0;
    NumOfIndices = NUM_VERTSX * NUM_VERTSY * 6;
    for (int z=0; z < NUM_ROWS; z++)
    {
    for (int x=0; x < NUM_COLS; x++)
    {
    int curVertex = x + (z * NUM_VERTSX);
    // Create die indices vir die eerste driehoek. Anti-kloksgewys
    indices[curIndex] = curVertex;
    indices[curIndex+1] = curVertex + NUM_VERTSX;
    indices[curIndex+2] = curVertex + NUM_VERTSX + 1;
    // Tweede driehoek
    indices[curIndex+5] = curVertex + NUM_VERTSX + 1;
    indices[curIndex+3] = curVertex + 1;
    indices[curIndex+4] = curVertex;
    // increment curIndex met die aantal indices in 'n vierkant
    curIndex += 6;
    }
    }
    //================================================== =======================
    //Create die Normale vir elke driehoek
    //================================================== =======================
    for (DWORD i = 0; i == NumOfIndices; i+=3)
    {
    D3DXVECTOR3 v0 = vertices[indices[i]].Pos;
    D3DXVECTOR3 v1 = vertices[indices[i + 1]].Pos;
    D3DXVECTOR3 v2 = vertices[indices[i + 2]].Pos;
    D3DXVECTOR3 normal;
    D3DXVECTOR3 cross;
    D3DXVec3Cross(&cross, &D3DXVECTOR3(v1 - v0), &D3DXVECTOR3(v2 - v0));
    // assign the computed normal to each vertex in this face
    vertices[indices[i]].Normal += normal;
    vertices[indices[i + 1]].Normal += normal;
    vertices[indices[i + 2]].Normal += normal;
    }
    int NumOfVerts = NUM_VERTSX * NUM_VERTSY;
    for (DWORD p = 0; p < NumOfVerts; ++p)
    D3DXVec3Normalize(&vertices[p].Normal,&vertices[p].Normal);
    //================================================== =======================
    //Die buffers
    //================================================== =======================
    //Create die vertex buffer vir die Land en save in VertexBuffer
    D3D10_BUFFER_DESC vbd;
    vbd.Usage = D3D10_USAGE_IMMUTABLE;
    vbd.ByteWidth = sizeof(Vertex) * (NUM_VERTSX * NUM_VERTSY);
    vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
    vbd.CPUAccessFlags = 0;
    vbd.MiscFlags = 0;
    D3D10_SUBRESOURCE_DATA vinitData;
    vinitData.pSysMem = vertices;

    md3dDevice->CreateBuffer(&vbd, &vinitData, &VertexBuffer);

    //Nou create ek die index buffer vir die land
    D3D10_BUFFER_DESC bd;
    bd.Usage = D3D10_USAGE_DEFAULT;
    bd.ByteWidth = sizeof(DWORD) * (NUM_VERTSX*NUM_VERTSY*6);
    bd.BindFlags = D3D10_BIND_INDEX_BUFFER;
    bd.CPUAccessFlags = 0;
    bd.MiscFlags = 0;
    D3D10_SUBRESOURCE_DATA InitData;
    InitData.pSysMem = indices;
    md3dDevice->CreateBuffer(&bd, &InitData, &IndexBuffer);
    };

    And the draw function

    void C_LAND:rawLand()
    {
    UINT stride = sizeof(Vertex);
    UINT offset = 0;
    md3dDevice->IASetVertexBuffers(0, 1, &VertexBuffer, &stride, &offset);
    md3dDevice->IASetIndexBuffer(IndexBuffer, DXGI_FORMAT_R32_UINT, 0);
    md3dDevice->DrawIndexed((NUM_VERTSX*NUM_VERTSY*6), 0, 0);
    }
    My result:
    Click image for larger version. 

Name:	Result.jpg 
Views:	196 
Size:	95.2 KB 
ID:	73583

    The water creation part is still the same as the land except for the y height.

    Please excuse my programming skill and the level of DirectX knowledge but I’m doing it for 2 months now.

    Like I've mentioned, I don't know if you could help me but I would be realy be thank full if so.

  4. #4
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    First off all: Consider using [Code]-tags when posting code snippets. It increases readability a lot.

    It's hard to tell what's going on from just the screenshots and the code snippets you posted, but I can give some general advice on how to deal with these kinds of problems. First you want to make sure that your geometry isn't just occluded. This is where wireframe mode is most useful. I usually have a hotkey for switching to wireframe in all of my debug builds. In D3D the fastest way to get this is to set the rasterizer state of the device to the wireframe fill mode. While you are there, you might also want to check out some of the other options offered here (e.g. many people prefer to deactivate backface culling for wireframe renderings).

    If some of your geometry does not appear at all in the wireframe rendering, try using the debugger to verify that the responsible code parts are executed and if not, why that is the case.

    Once you have made sure your draw calls are all executed as expected, inspect the state changes to the renderer for errors. This is the part where beginners usually have the most trouble. Since the renderer has a global state that can be manipulated from anywhere in the program, it can be hard to isolate the exact line where an error happens (this is even worse for OpenGL, where you don't even need access to a renderer object to change the state). The best advice to avoid these kinds of errors is rigorous discipline. Make a clear design of which part of the code is supposed to do what and stick to it. Unfortunately, this is easier said than done and often impossible without a fair amount of experience.

    For example, you did not include the code were you clear the frame buffer. Clearing the frame buffer without clearing the depth buffer is a common beginner's mistake that can be hard to track down if you don't know what to look for. Similarly, clearing the depth buffer in the wrong place can lead to rather curious (i.e. wrong) results.

  5. #5
    Join Date
    Apr 2013
    Posts
    3
    Thanks for the reply. I did find the solution a couple of days before and yes, I did not clear the depth entries. Thanks again.

  6. #6
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    Glad you figured it out

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •