-
Notifications
You must be signed in to change notification settings - Fork 0
Models and Shaders
joshlengel edited this page Jun 9, 2020
·
1 revision
Models and shaders go hand-in-hand. Currently Infinity only supports Windows and uses the HLSL shading language so you will have to become acquainted with it. The following code introduces the Model and Shader classes:
void App::OnUserCreate(Infinity::UserCreateEvent *event)
{
Infinity::Context *context = event->GetContext();
context->SetClearColor(0.0f, 0.0f, 0.0f, 1.0f);
m_vertex_buffer = Infinity::VertexBuffer::CreateVertexBuffer({
{ "position", Infinity::DataType::FLOAT2 }, // Describe the data layout of your vertices
{ "color", Infinity::DataType::FLOAT4 }
});
if (!m_vertex_buffer->Init())
{
INFINITY_CLIENT_ERROR("Error creating vertex buffer");
RequestExit();
event->Consume();
return;
}
m_index_buffer = Infinity::IndexBuffer::CreateIndexBuffer();
if (!m_index_buffer->Init())
{
INFINITY_CLIENT_ERROR("Error creating index buffer");
RequestExit();
event->Consume();
return;
}
float vertices[] =
{ // position color
-0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f
};
unsigned int indices[] =
{
0, 1, 2
};
if (!m_vertex_buffer->SetData(vertices, sizeof(vertices)))
{
INFINITY_CLIENT_ERROR("Error setting vertex data");
RequestExit();
event->Consume();
return;
}
if (!m_index_buffer->SetData(indices, sizeof(indices), 3))
{
INFINITY_CLIENT_ERROR("Error setting index data");
RequestExit();
event->Consume();
return;
}
m_model = Infinity::Model::CreateModel(1); // Create model with one vertex buffer
if (!m_model->Init())
{
INFINITY_CLIENT_ERROR("Error creating model");
RequestExit();
event->Consume();
return;
}
m_model->SetVertexBuffer(0, m_vertex_buffer);
m_model->SetIndexBuffer(m_index_buffer);
// Create shader
m_shader = Infinity::Shader::CreateShader({
{ 0, "position", Infinity::DataType::FLOAT2 }, // specify which vertex buffer which data will be coming from
{ 0, "color", Infinity::DataType::FLOAT4 }
});
const char vertex_source[] = R"( // Must use HLSL
struct VertexOut
{
float4 position : SV_POSITION;
float4 color : COLOR0;
};
VertexOut main(float2 position : ATTRIBUTE0, float4 color : ATTRIBUTE1) // Attributes must have these semantic names
{
VertexOut output;
output.position = float4(position, 0.0, 1.0);
output.color = color;
return output;
}
)";
const char pixel_source[] = R"( // Must use HLSL
float4 main(float4 color : COLOR0) : SV_TARGET
{
return color;
}
)";
if (!m_shader->Init(vertex_source, sizeof(vertex_source), pixel_source, sizeof(pixel_source)))
{
INFINITY_CLIENT_ERROR("Error creating shader");
RequestExit();
event->Consume();
return;
}
}
void App::OnUserRender(Infinity::UserRenderEvent *event)
{
Infinity::Context *context = event->GetContext();
context->Clear();
m_shader->Bind();
m_model->Bind();
m_model->Render();
}
void App::OnUserDestroy(Infinity::UserDestroyEvent *event)
{
m_shader->Destroy();
m_model->Destroy();
delete m_shader;
delete m_model;
}