zoukankan      html  css  js  c++  java
  • C++小项目:directx11图形程序(三):graphicsclass

    这是框架的第三层graphicsclass,这个类才真正可以说是整个程序的框架,因为它组织了后面所有的成员。

    代码:

    graphicsclass.h

     1 #pragma once
     2 #include<Windows.h>
     3 #include <d3d11.h>
     4 #include <d3dcompiler.h>
     5 #include <D3DX11.h>
     6 #include <xnamath.h>
     7 
     8 #include"cameraclass.h"
     9 #include"d3dclass.h"
    10 #include"modelclass.h"
    11 #include"shaderclass.h"
    12 #include"inputclass.h"
    13 #include"particleSysclass.h"
    14 
    15 #pragma comment(lib,"d3dx11.lib")
    16 #pragma comment(lib,"d3d11.lib")
    17 #pragma comment(lib,"d3dcompiler.lib")
    18 class graphicsclass
    19 {
    20 public:
    21     graphicsclass();
    22     ~graphicsclass();
    23     bool Initialize(HWND hWnd );
    24     void Shutdown();
    25     bool Frame();
    26 
    27 private:        
    28     d3dclass* m_d3d;
    29     shaderclass* m_shader;
    30     cameraclass* m_camera;
    31     modelclass* m_model;
    32     modelclass* m_model1;
    33     inputclass* m_input;
    34     particleSysclass *m_particles;
    35 
    36     ID3D11Device* m_device;
    37     ID3D11DeviceContext* m_context;
    38 };

    从这个头文件里可以看到,首先包含了directx11开发需要用到的头文件和lib,其次包含了我自己写的也是框架出现过的类的头文件。从私有数据成员里也可窥见一斑。

    但是多了两个

    ID3D11Device* m_device;
    ID3D11DeviceContext* m_context;

    这样的指针。简单介绍一下,ID3D11Device是dx11的设备对象,ID3D11DeviceContext是dx11的设备上下文对象。在当前类里面并不涉及他们的创建。我们在这里申明它只是用来起一个桥梁作用。

    graphicsclass.cpp

      1 #include "graphicsclass.h"
      2 
      3 graphicsclass::graphicsclass()
      4 {
      5 }
      6 graphicsclass::~graphicsclass()
      7 {
      8 }
      9 bool graphicsclass::Initialize(HWND hWnd)
     10 {
     11     
     12     m_d3d = new d3dclass;
     13     m_d3d->Initialize(hWnd);
     14     m_d3d->Getdevice(m_device);
     15     m_d3d->Getcontext(m_context);
     16 
     17     m_shader = new shaderclass;    
     18     m_shader->Initialize(m_device, m_context);
     19 
     20     m_camera = new cameraclass;
     21     
     22     m_model = new modelclass;
     23     m_model->Initialize(m_device, L"cube.txt", L"dx11.dds");
     24 
     25     m_model1 = new modelclass;
     26     m_model1->Initialize(m_device, L"cube.txt", L"stone.dds");
     27 
     28     m_input = new inputclass;
     29     m_input->Initialize(hWnd);
     30 
     31     m_particles = new particleSysclass;
     32     m_particles->Initialize(m_device, L"star.dds", 20);
     33     
     34     return true;
     35 }
     36 void graphicsclass::Shutdown()
     37 {
     38     m_input->Shutdown();
     39     delete m_input;
     40 
     41     m_model1->Shutdown();
     42     delete m_model1;
     43 
     44     m_model->Shutdown();
     45     delete m_model;
     46 
     47     delete m_camera;
     48 
     49     m_shader->Shutdown();
     50     delete m_shader;
     51 
     52     m_d3d->Shutdown();
     53     delete m_d3d;
     54 }
     55 bool graphicsclass::Frame()
     56 {
     57     if (m_input->keydown(DIK_ESCAPE))
     58     {
     59         return false;
     60     }
     61 
     62     static float t = 0;
     63     t += 0.002f;
     64 
     65     XMMATRIX view,pro;
     66     m_d3d->Getpromtrx(pro);
     67 
     68     static float r = 0, n = 0, m = 1;
     69     int a = 0, b = 0, z = 0;
     70     if (m_input->mouseinput(0))
     71     {
     72         m_input->GetmouseI(a, b);
     73         
     74     }
     75     r += a,n += b;    
     76     
     77     m += z;
     78     m_camera->SetPositon(0, 0, -6);
     79     m_camera->SetRotation(n/50, r/50, 0);
     80     m_camera->Getviewmatrix(view);
     81 
     82     XMVECTOR axis = { 0, 1, 0 };
     83     m_model->Setposition(0, 1, 0);
     84     m_model1->Setposition(0, 0, 5);
     85     m_model1->RotationAxis(axis, 90);
     86     m_model->RotationAxis(axis, t);    
     87 
     88     ID3D11VertexShader* vertexshader;
     89     ID3D11PixelShader* pixelshader;
     90     m_shader->GetShaders(vertexshader, pixelshader);
     91 
     92     m_d3d->Beginrender(0, 0, 0, 0);
     93 
     94     m_model->Render(m_context, view, pro, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, vertexshader, pixelshader);
     95     m_model1->Render(m_context, view, pro, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, vertexshader, pixelshader);    
     96     m_particles->Render(m_context, view, pro, vertexshader, pixelshader);
     97 
     98     m_d3d->Endrender();
     99     return true;
    100 }

    这段代码,我个人的感觉是非常的整齐。

    Initialize()函数:就是创建对象,初始化,创建对象,初始化.....。不过与众不同的是,使用了m_d3d的Getdevice()和Getcontext()。为什么要使用呢?因为后面对象的初始化要用到ID3D11Device和ID3D11DeviceContext对象啊。这就是我所说的起到的桥梁的作用

    Shutdown()函数:依然是调用组件的Shutdown()函数并释放他们,起到一个析构函数的作用

    Frame()函数:调用组件的各个函数,可能是输入检测,可能是设置位置,可能是获取组件的成员,也可能是渲染。

    依然好像没有涉及dx11的图形渲染。但是我们可以看到,涉及图形渲染的部分一定就在这个类所管理的组件里面了。还是继续看接下来的对各个组件的介绍吧。

  • 相关阅读:
    【转】win8.1下安装ubuntu
    Codeforces 1025G Company Acquisitions (概率期望)
    Codeforces 997D Cycles in Product (点分治、DP计数)
    Codeforces 997E Good Subsegments (线段树)
    Codeforces 1188E Problem from Red Panda (计数)
    Codeforces 1284E New Year and Castle Building (计算几何)
    Codeforces 1322D Reality Show (DP)
    AtCoder AGC043C Giant Graph (图论、SG函数、FWT)
    Codeforces 1305F Kuroni and the Punishment (随机化)
    AtCoder AGC022E Median Replace (字符串、自动机、贪心、计数)
  • 原文地址:https://www.cnblogs.com/woody-245/p/5577351.html
Copyright © 2011-2022 走看看