Question

I'm trying to draw a triangle which did work with the following code using C++ and DirectX:

Renderer.h

#pragma once

#include "DirectX.h"
#include "Camera.h"
#include "OBJMesh.h"

class Renderer : public DirectX
{
public:
    Renderer(std::string windowTitle, int x, int y, unsigned int windowWidth, unsigned int windowHeight, bool fullscreen);
    ~Renderer();

    void Update();
    void Render();

protected:
    OBJMesh* modelMesh;
    Camera* camera;
    VERTEX vertices[3];
};

Renderer.cpp

#include "Renderer.h"

Renderer::Renderer(std::string windowTitle, int x, int y, unsigned int windowWidth, unsigned int windowHeight, bool fullscreen) : DirectX(windowTitle, x, y, windowWidth, windowHeight, fullscreen)
{
    //camera = new Camera(Vector3(0.0f, -100.0f, 5000.0f), 0.0f, 0.0f);
    camera = new Camera(0.0f, 0.0f, D3DXVECTOR3(0.0f, 0.0f, 10.0f));
    OBJMesh* modelMesh = new OBJMesh("../Models/Tank/destroyedTank.obj");

    VERTEX v1, v2, v3;

    v1.x = 0.0f;
    v1.y = -1.0f;
    v1.z = 0.5f;
    v1.color = D3DCOLOR_XRGB(0, 0, 255);

    v2.x = 1.0f;
    v2.y = 1.0f;
    v2.z = 0.5f;
    v2.color = D3DCOLOR_XRGB(0, 255, 0);

    v3.x = -1.0f;
    v3.y = 1.0f;
    v3.z = 0.5f;
    v3.color = D3DCOLOR_XRGB(255, 0, 0);

    vertices[0] = v1;
    vertices[1] = v2;
    vertices[2] = v3;

    device->CreateVertexBuffer(3 * sizeof(VERTEX), 0, VERTEXFORMAT, D3DPOOL_MANAGED, &vertexBuffer, NULL);

    VOID* lockingAd;

    vertexBuffer->Lock(0, 0, (void**)&lockingAd, 0);
    memcpy(lockingAd, vertices, sizeof(vertices));
    vertexBuffer->Unlock();

    device->SetRenderState(D3DRS_LIGHTING, FALSE); //turn off lighting - DirectX needs to know this :(
    device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); //turns off face culling (for now)
}

Renderer::~Renderer()
{
    delete camera;
}

void Renderer::Update()
{
    window->Update();

    float msec = Window::GetGameTimer()->GetFrameTime();

    camera->Update(msec);
}

void Renderer::Render()
{
    device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(40, 40, 40), 1.0f, 0);

    device->BeginScene(); //Must be used as it tells DirectX we're starting to draw stuff.

    D3DXMATRIX worldMat;
    D3DXMatrixTranslation(&worldMat, 0.0f, 0.0f, 0.0f);
    device->SetTransform(D3DTS_WORLD, &worldMat);

    device->SetTransform(D3DTS_VIEW, &camera->BuildViewMatrix());

    D3DXMATRIX projMatrix;
    D3DXMatrixPerspectiveFovLH( &projMatrix,
                                D3DXToRadian(45),
                                (float)width/(float)height,
                                1.0f,
                                10000.0f);
    device->SetTransform(D3DTS_PROJECTION, &projMatrix);

    device->SetFVF(VERTEXFORMAT);
    device->SetStreamSource(0, vertexBuffer, 0, sizeof(VERTEX));
    device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

    device->EndScene(); //Thank you for waiting, I have finished drawing stuff on the screen, please handle the rest Mr DirectX.

    device->Present(NULL, NULL, NULL, NULL);
}

However, when I change the vertices array to VERTEX* vertices in my header file and vertices = new VERTEX[3] in my cpp file, it doesn't draw anything... why's that? How can I fix this?

Was it helpful?

Solution

I'm not sure if this is your problem, but this looks like it could be the offending line:

memcpy( lockingAd, vertices, sizeof(vertices) );

Here, this will do different things if vertices is of type VERTEX[3] and VERTEX*. In the first case the sizeof will return the size of the array, whereas in the second case it will simply return the size of a pointer on your machine, to fix this (if you have a fixed number of elements 3) you should change it to the following:

memcpy( lockingAd, vertices, sizeof(VERTEX) * 3 );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top