Directx11教程(10) 画一个简易坐标轴

Stella981
• 阅读 474

原文: Directx11教程(10) 画一个简易坐标轴

      本篇教程中,我们将在三维场景中,画一个简易的坐标轴,分别用红、绿、蓝三种颜色表示x,y,z轴的正向坐标轴。

为此,我们要先建立一个AxisModelClass类,来表示坐标轴顶点。

      现在系统类之间的关系图如下:

Directx11教程(10) 画一个简易坐标轴

AxisModelClass类和前面的ModelClass类相似,只是创建顶点缓冲和索引缓冲时,指定了3条线段,表示三个坐标轴。

AxisModelClass.h的主要代码如下:

#pragma once

#include <d3d11.h>
#include <d3dx10math.h>
#include "common.h"

class AxisModelClass
    { 

        void RenderBuffers(ID3D11DeviceContext*);
       //顶点缓冲和顶点索引缓冲
        ID3D11Buffer *m_vertexBuffer, *m_indexBuffer;
        int m_vertexCount, m_indexCount;
    };

AxisModelClass.cpp的主要代码如下:

#include "AxisModelClass.h"

bool AxisModelClass::InitializeBuffers(ID3D11Device* device)
    {
    VertexType* vertices;
    unsigned long* indices;
    D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
    D3D11_SUBRESOURCE_DATA vertexData, indexData;
    HRESULT result;

    //首先,我们创建2个临时缓冲存放顶点和索引数据,以便后面使用。.

    // 设置顶点缓冲大小为6
    m_vertexCount = 6;

    // 设置索引缓冲大小.
    m_indexCount = 6;

    // 创建顶点临时缓冲.
    vertices = new VertexType[m_vertexCount];
    if(!vertices)
        {
        return false;
        }

   // 创建索引缓冲.
    indices = new unsigned long[m_indexCount];
    if(!indices)
        {
        return false;
        }

    // 设置顶点数据.
    //x轴,红色
    vertices[0].position = D3DXVECTOR3(0.0f, 0.0f, 0.0f); 
    vertices[0].color = RED;

    vertices[1].position = D3DXVECTOR3(10.0f, 0.0f, 0.0f); 
    vertices[1].color = RED;

    //y轴,绿色
    vertices[2].position = D3DXVECTOR3(0.0f, 0.0f, 0.0f); 
    vertices[2].color = GREEN;

    vertices[3].position = D3DXVECTOR3(0.0f, 10.0f, 0.0f); 
    vertices[3].color = GREEN;

   //z轴,蓝色
    vertices[4].position = D3DXVECTOR3(0.0f, 0.0f, 0.0f); 
    vertices[4].color = BLUE;

    vertices[5].position = D3DXVECTOR3(0.0f, 0.0f, 10.0f); 
    vertices[5].color = BLUE;

   // 设置索引缓冲数据.
    indices[0] = 0; 
    indices[1] = 1;
    indices[2] = 2; 
    indices[3] = 3;
    indices[4] = 4;
    indices[5] = 5; 


    return true;
    }

void AxisModelClass::RenderBuffers(ID3D11DeviceContext* deviceContext)
    {
    unsigned int stride;
    unsigned int offset;

    // 设置顶点缓冲跨度和偏移.
    stride = sizeof(VertexType);
    offset = 0;

    //在input assemberl阶段绑定顶点缓冲,以便能够被渲染
    deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);

    //在input assemberl阶段绑定索引缓冲,以便能够被渲染
    deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);

    // 设置体元语义,渲染线段,画出坐标轴 

注意:这儿指定画的体元为线段列表
    deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);

    return;
    }

为了使用颜色宏定义,我么去掉了上篇文章在ModelClass.h 中定义的颜色,而新建一个common.h文件,

ModelClass.h中将包含common.h

ModelClass.h代码改变如下:

#pragma once

#include <d3d11.h>
#include <d3dx10math.h>
#include "common.h"

class ModelClass
    {

    };

common.h的代码如下:

//定义一些常用颜色
#include <d3d11.h>
#include <d3dx10math.h>

const D3DXVECTOR4 WHITE(1.0f, 1.0f, 1.0f, 1.0f);
const D3DXVECTOR4 BLACK(0.0f, 0.0f, 0.0f, 1.0f);
const D3DXVECTOR4 RED(1.0f, 0.0f, 0.0f, 1.0f);
const D3DXVECTOR4 GREEN(0.0f, 1.0f, 0.0f, 1.0f);
const D3DXVECTOR4 BLUE(0.0f, 0.0f, 1.0f, 1.0f);
const D3DXVECTOR4 YELLOW(1.0f, 1.0f, 0.0f, 1.0f);
const D3DXVECTOR4 CYAN(0.0f, 1.0f, 1.0f, 1.0f); //蓝绿色
const D3DXVECTOR4 MAGENTA(1.0f, 0.0f, 1.0f, 1.0f); //洋红色

const D3DXVECTOR4 BEACH_SAND(1.0f, 0.96f, 0.62f, 1.0f);
const D3DXVECTOR4 LIGHT_YELLOW_GREEN(0.48f, 0.77f, 0.46f, 1.0f);
const D3DXVECTOR4 DARK_YELLOW_GREEN(0.1f, 0.48f, 0.19f, 1.0f);
const D3DXVECTOR4 DARKBROWN(0.45f, 0.39f, 0.34f, 1.0f);

GraphicsClass.h修改的代码如下:

#pragma once

#include "modelclass.h"
#include "AxisModelClass.h"
#include "colorshaderclass.h"

class GraphicsClass
    { 

        ModelClass* m_Model;
        AxisModelClass* m_AxisModel;
        ColorShaderClass* m_ColorShader;

    };

GraphicsClass.cpp代码如下:

#include "GraphicsClass.h"

GraphicsClass::GraphicsClass(void)
    {
    m_D3D = 0;
    m_Camera = 0;
    m_Model = 0;
    m_AxisModel = 0;
    m_ColorShader = 0;
   
    }

bool GraphicsClass:: Initialize(int screenWidth, int screenHeight, HWND hwnd)
    { 
   …

   // 创轴建模型对象.
    m_AxisModel = new AxisModelClass;
    if(!m_AxisModel)
        {
        return false;
        }
    // 初始化坐标轴模型对象.
    result = m_AxisModel->Initialize(m_D3D->GetDevice());
    if(!result)
        {
        MessageBox(hwnd, L"Could not initialize the axis model object.", L"Error", MB_OK);
        return false;
        }

    return true;
    }

bool GraphicsClass::Frame()
    {
    bool result;

    // 调用Render函数,渲染3D场景
    // Render是GraphicsClass的私有函数.
    result = Render();
    if(!result)
        {
        return false;
        }

    return true;
    }

bool GraphicsClass::Render()
    {

    D3DXMATRIX viewMatrix, projectionMatrix, worldMatrix;
    bool result;

    // 设置framebuffer.为浅蓝色
    m_D3D->BeginScene(0.0f, 0.0f, 0.5f, 1.0f);

    // 得到3个矩阵.
    m_Camera->getViewMatrix(&viewMatrix);
    m_D3D->GetWorldMatrix(worldMatrix);
    m_D3D->GetProjectionMatrix(projectionMatrix);

    m_AxisModel->Render(m_D3D->GetDeviceContext());
    // 用shader渲染.
    result = m_ColorShader->Render(m_D3D->GetDeviceContext(), m_AxisModel->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix);
    if(!result)
        {
        return false;
        }   
    // 把模型顶点和索引缓冲放入管线,准备渲染.
    m_Model->Render(m_D3D->GetDeviceContext());

    // 用shader渲染.
    result = m_ColorShader->Render(m_D3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix);
    if(!result)
        {
        return false;
        }
   
   //把framebuffer中的图像present到屏幕上.
    m_D3D->EndScene();

    return true;
    }

程序执行后,如下图所示:

Directx11教程(10) 画一个简易坐标轴

完整的代码请参考:

工程文件myTutorialD3D11_9

代码下载:

http://files.cnblogs.com/mikewolf2002/myTutorialD3D11.zip

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
2年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
Java爬虫之JSoup使用教程
title:Java爬虫之JSoup使用教程date:201812248:00:000800update:201812248:00:000800author:mecover:https://imgblog.csdnimg.cn/20181224144920712(https://www.oschin
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
4个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这