top of page

​Chap 4/100:  ​Loading a 3D or a 2D Wavefront .obj file

What I will Learn here?:

​On this Chapter we will call the artillery to have a fast start.

The Big Full version have what 1000 files data and source? I am trying to contain this explosion but we will need:

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

Note:

​We read an object with a material with 1 color only.

​

File: 004square.mtl

newmtl Material1
    Ka 0.0 0.5 0.1
    Kd 0.0 0.5 0.1

​

(MATH_LIB/src) A very simple version we will call the cavalary later.

​(ZIP_LIB/src) This lib work fine unless we use password in unzip.

packManagerClass.* Is our interface to communicate with this lib for now...

 

(main.h) - CHANGES

- Set for Chapter 4

​​

(main.cpp) - CHANGES

-Unzip our data files to tmp directory before use them, later on we will load them direct to memory.

​

​There are some basics about 3D rendering that I will assume that you have, anyway I will resume one of the most important aspects of 3D Geometry.

​

Every single object to be rendered need to pass by the 3D master formula in this order:

Rotate * Scale * Translate * ViewCamera * ProjectionWindow

 

is also know as ObjectWorld  = Rotate * Scale * Translate  and the formula become:

ObjectWorld * ViewCamera * ProjectionWindow

​

​ObjectWorld 

Rotate is the rotation of our 3D object in all Axis X, Z (Forward) and Y (Up Position)

​Scale is literally the scale of our 3D object in all Axis X, Z (Forward) and Y (Up Position), normally this values are all equal to preserve the aspect ratio.

Translate is to move the obj from its original position (in file) to a place in our 3D world

 

​

​

 

​

​

​

​

​

​

​

​

​

​

​

​ViewCamera

​This is the camera position in our 3D world, looking left, right, up, down, etc...

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​ProjectionWindow

​The projection matrix is based on near and far (how far our camera can see) view distance, angle of the view of the camera (The human eye normally see 90º so this is the value that we use) and your screen resolution proportion.

​

​

 

 

 

 

 

​

​

​

​

​

​

​

​

​

​

(Applicationclass.*) - ​CHANGES

 

 

How can I define the camera position?         

m_Camera.SetPosition(g_INIT_CAMX, g_INIT_CAMY, g_INIT_CAMZ);
m_Camera.SetRotation(g_INIT_ROTX, g_INIT_ROTY, g_INIT_ROTZ);
   
m_Camera.GetViewMatrix(g_viewMatrix);    // Get the view matrice
​​

​

How can I define the Projection Settings?         

m_DirectX11->GetProjectionMatrix(g_projectionMatrix);   

​

How to load a 3D file?

objModel.LoadObjModel (MY_SHADER_COLOR, m_DirectX11, L"engine/data/004square.obj", true)

 

How to Render the Object Loaded ?

objModel.render(pContext);

​

​

​Load OBJ file, what feactures I support or will on OBJ?

​

​Geometry

#  - comment
v  - vert position
vt - vert tex coords
vn - vert normal
g  - defines a group (Subset)
f  - defines the faces (
polygons)
mtllib - material library filename
usemtl - which material to use

Material
#  - comment
Kd - diffuse color
Ka - Ambient Color
Tr - Check for transparency (alpha value)
d  - for transparency (1 - alpha value)
map_Kd - Diffuse map
map_d - alpha map
map_bump - bump map (we're usinga normal map though)
newmtl - Declare new material
​

​Our first Shader, very, very simple:

​

////////////////////////////////////////////////////////////////////////////////
// Filename: 004Color.hlsl
////////////////////////////////////////////////////////////////////////////////

#include "000cbuffer.hlsl"

//////////////
// TYPEDEFS //
//////////////


// VERTEX:
struct VSIn
{
    float4 position : POSITION;

};

// PIXEL:
struct PSIn
{
    float4 position : SV_POSITION;

};

​////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////

PSIn MyVertexShader004Color(VSIn input)
{
    PSIn output;
   
    input.position.w = 1.0f;                    // Change the position vector to be 4 units for proper matrix calculations.
    output.position = mul(input.position, WVP);    // Calculate the position of the vertex against the world, view, and projection matrices.

    return output;
}

////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////

float4 MyPixelShader004Color(PSIn input) : SV_TARGET
{
    return
pixelColor;                  // Get a solid Color Only!
}

 

​

​

​To navigate on source code:

 

User: public

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​​

​

​

​

​│   Applicationclass.cpp
│   Applicationclass.h
│   counter.h
│   main.cpp
│   main.h

│
├───camera
│       cameraClass.cpp
│       cameraClass.h
│
├───loader
│       objModelV2Class.cpp
│       objModelV2Class.h
│
├───shader
│       shaderClass.cpp
│       shaderClass.h
│
└───system
        dx11Class.cpp
        dx11class.h
        SystemClass.cpp
        SystemClass.h
        xml_loader.cpp
        xml_loader.h

​​

​

Project Code:

​​

WoMA_Chap4.zip

​

​

What's next? All this work for a semi-plan with 1 color?

Actually this were the basic fundamentals for a 3D Aplication. Load a 3D Object (basic object) and render it on screen. Let's find out on next chap how to load an object with textures ;)

 

bottom of page