Jump to content
  • Advertisement
Sign in to follow this  
Tim Lawton

Drawing text in DirectX11

This topic is 2070 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey there,

I've recently attempted to draw text to my screen in DirectX 11, I have no errors, but I think its the way Im ordering the way things are drawn in my Render function, could an experienced programmer take a look for me and tell me what Im doing wrong?

The outcome it supose to be a 2d bitmap in the top left, a 3d cube spinning in the middle with lighting, and another 2d bitmap in the top right, holding txt, but the surface is alphablended out.

thanks

Graphics::Init

bool Graphics::Init(int screenWidth, int screenHeight, HWND hwnd)
{
bool result;
D3DXMATRIX baseViewMatrix;
//-----------------
// D3D
//-----------------
//Create the Direct3D Object
d3d = new D3D;
if(!d3d)
{
return false;
}
//Initialize the Direct3D Object
result = d3d->Init(screenWidth, screenHeight, VSYNC_ENABLED, hwnd,
FULL_SCREEN, SCREEN_DEPTH, SCREEN_NEAR);
if(!result)
{
MessageBox(hwnd, "Could not initalize Direct3D", "Error", MB_OK);
return false;
}
//-----------------
// CAMERA
//-----------------
// Create the camera object.
camera = new Camera;
if(!camera)
{
return false;
}
//Initialize a base view matrix with the camera for 2D User interface rendering
camera->SetPosition(0.0f, 0.0f, -10.0f);
camera->Render();
camera->GetViewMatrix(baseViewMatrix);

//-----------------
// MODEL (3D)
//-----------------
// Create the model object.
model = new Model;
if(!model)
{
return false;
}
// Initialize the model object.
result = model->Init(d3d->GetDevice(), "Shapes/Cube.txt", "Textures/seafloor.bmp");
if(!result)
{
MessageBox(hwnd, "Could not initialize the model object.", "Error", MB_OK);
return false;
}
//-----------------
// COLORSHADER
//-----------------
// Create the color shader object.
colorshader = new Colorshader;
if(!colorshader)
{
return false;
}
// Initialize the color shader object.
result = colorshader->Init(d3d->GetDevice(), hwnd);
if(!result)
{
MessageBox(hwnd, "Could not initialize the color shader object.", "Error!", MB_OK);
return false;
}
//-----------------
// TEXTURESHADER
//-----------------
//Create the texture shader object
textureshader = new Textureshader;
if(!textureshader)
{
return false;
}
//Initialize the textureshader object
result = textureshader->Init(d3d->GetDevice(), hwnd);
if(!result)
{
MessageBox(hwnd, "Could not load the texutre shader object", "Error!", MB_OK);
return false;
}
//-----------------
// BITMAP (2D)
//-----------------
//Create the bitmap object.
bitmap = new Bitmap;
if(!bitmap)
{
return false;
}
// Initialize the bitmap object.
result = bitmap->Init(d3d->GetDevice(), screenWidth, screenHeight, "Textures/seamlessMetal.jpg", 256, 256);
if(!result)
{
MessageBox(hwnd, "Could not initialize the bitmap object.", "Error", MB_OK);
return false;
}
//-----------------
// LIGHTSHADER
//-----------------
//Create the light shader object
lightshader = new Lightshader;
if(!lightshader)
{
return false;
}
//Initialize the light shader object
result = lightshader->Init(d3d->GetDevice(), hwnd);
if(!result)
{
MessageBox(hwnd, "Could not initialize the light shader object.", "Error!", MB_OK);
return false;
}
//-----------------
// LIGHT
//-----------------
//Create the light object
light = new Light;
if(!light)
{
return false;
}
//Initialize the light object
light->SetAmbientColor(0.15f, 0.15f, 0.15f, 1.0f);
light->SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f);
light->SetDirection(0.0f, 0.0f, 1.0f);
light->SetSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
light->SetSpecularPower(32.0f);
//-----------------
// TEXT
//-----------------
//Create text object
text = new Text;
if(!text)
{
return false;
}
//Initialize the text object
result = text->Init(d3d->GetDevice(), d3d->GetDeviceContext(), hwnd, screenWidth, screenHeight, baseViewMatrix);
if(!result)
{
MessageBox(hwnd, "Could not initialize the text object", "Error!", MB_OK);
return false;
}
return true;
}





Graphics::Render

bool Graphics::Render(float rotation)
{
//-----------------
// VARIABLES
//-----------------
D3DXMATRIX worldMatrix, viewMatrix, projectionMatrix, orthoMatrix;
bool result;
//-----------------
// BEGIN SCENE
//-----------------
//Clear the buffers to a black screen
d3d->BeginScene(0.0f, 0.0f, 0.0f, 1.0f); //RGBA
//-----------------
// LIGHTING
//-----------------
// Render the model using the light shader.
result = lightshader->Render(d3d->GetDeviceContext(), model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
model->GetTexture(), light->GetDirection(), light->GetAmbientColor(), light->GetDiffuseColor(), camera->GetPosition(),
light->GetSpecularColor(), light->GetSpecularPower());
if(!result)
{
return false;
}
//-----------------
// CAMERA
//-----------------

//Generate the view matrix based on the camera's position
camera->Render();
//Get the world, view, and projection matrices from the camera and d3d objects.
camera->GetViewMatrix(viewMatrix);
d3d->GetWorldMatrix(worldMatrix);
d3d->GetProjectionMatrix(projectionMatrix); //Draw 3D
d3d->GetOrthoMatrix(orthoMatrix); //Draw 2D

//-----------------
// 2D RENDERING
//-----------------
//Turn off the ZBuffer to start 2d rendering
d3d->TurnZBufferOff();
//Turn on alpha blending to start 2d rendering
d3d->TurnAlphaBlendingOn();
//FONT
//Render the text strings
result = text->Render(d3d->GetDeviceContext(), worldMatrix, orthoMatrix);
if(!result)
{
return false;
}
//turn off alpha blending after rendering the text
d3d->TurnAlphaBlendingOff();

//RENDER 2D BITMAP
//Put the bitmap vertex and index buffers on the graphics pipeline to prepare
result = bitmap->Render(d3d->GetDeviceContext(), 1, 1);
if(!result)
{
return false;
}

//RENDER 2D TEXTURE
//Render the bitmap with the texture shader
result = textureshader->Render(d3d->GetDeviceContext(), bitmap->GetIndexCount(), worldMatrix, viewMatrix, orthoMatrix, bitmap->GetTexture());
if(!result)
{
return false;
}
//Turn zBuffer back on for 3D rendering
d3d->TurnZBufferOn();

//-----------------
// ANIMATION
//-----------------
//Rotate
D3DXMatrixRotationY(&worldMatrix, rotation);
//-----------------
// 3D RENDERING
//-----------------
//Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
model->Render(d3d->GetDeviceContext());
//-----------------
// 3D TEXTURE
//-----------------
//Render the model using the texture
result = textureshader->Render(d3d->GetDeviceContext(), model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
model->GetTexture());
if(!result)
{
return false;
}
// Render the model using the color shader.
result = colorshader->Render(d3d->GetDeviceContext(), model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix);
if(!result)
{
return false;
}

//-----------------
// END DRAW/SCENE
//-----------------
//Present the rendered scene of teh screen
d3d->EndScene();
return true;
}
Edited by Xuchilbara

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!