Skip to content

Commit

Permalink
-Added cubemap support
Browse files Browse the repository at this point in the history
-Working on reflections in PBR shader
  • Loading branch information
maluoi committed Jul 27, 2019
1 parent 7fa2d73 commit 62c30a6
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 2 deletions.
9 changes: 9 additions & 0 deletions StereoKitC/render.cpp
@@ -1,6 +1,7 @@
#include "stereokit.h"
#include "render.h"

#include "assets.h"
#include "mesh.h"
#include "texture.h"
#include "shader.h"
Expand Down Expand Up @@ -38,6 +39,7 @@ transform_t *render_camera_transform = nullptr;
camera_t *render_camera = nullptr;
transform_t render_default_camera_tr;
camera_t render_default_camera;
tex2d_t render_cubemap;
render_global_buffer_t render_global_buffer;

void render_set_camera(camera_t &cam, transform_t &cam_transform) {
Expand All @@ -48,6 +50,9 @@ void render_set_light(const vec3 &direction, float intensity, const color128 &co
render_global_buffer.light = { direction.x, direction.y, direction.z, intensity };
render_global_buffer.light_color = color;
}
void render_set_envcube(tex2d_t cubemap) {
render_cubemap = cubemap;
}

void render_add_mesh(mesh_t mesh, material_t material, transform_t &transform) {
render_item_t item;
Expand Down Expand Up @@ -85,6 +90,8 @@ void render_draw_queue(XMMATRIX view, XMMATRIX projection) {
render_global_buffer.proj = XMMatrixTranspose(projection);
render_global_buffer.viewproj = XMMatrixTranspose(view * projection);

tex2d_set_active(render_cubemap, 6);

shaderargs_set_data (render_shader_globals, &render_global_buffer);
shaderargs_set_active(render_shader_globals);
shaderargs_set_active(render_shader_transforms);
Expand Down Expand Up @@ -135,6 +142,8 @@ void render_initialize() {
vec3 dir = { 1,2,1 };
dir = vec3_normalize(dir);
render_set_light(dir, 1, { 1,1,1,1 });

render_set_envcube((tex2d_t)assets_find("default/cubemap"));
}
void render_shutdown() {
shaderargs_destroy(render_shader_transforms);
Expand Down
11 changes: 11 additions & 0 deletions StereoKitC/stereokit.cpp
Expand Up @@ -24,6 +24,7 @@ long long sk_timev_raw = 0;
tex2d_t sk_default_tex;
shader_t sk_default_shader;
material_t sk_default_material;
tex2d_t sk_default_cubemap;
void sk_create_defaults();
void sk_destroy_defaults();

Expand Down Expand Up @@ -103,6 +104,16 @@ void sk_create_defaults() {
memset(tex_colors, 255, sizeof(uint8_t) * 4 * 4);
tex2d_set_colors(sk_default_tex, 2, 2, tex_colors);

const char *maps[] = {
"Assets/Sky/Right.jpg",
"Assets/Sky/Left.jpg",
"Assets/Sky/Top.jpg",
"Assets/Sky/Bottom.jpg",
"Assets/Sky/Back.jpg",
"Assets/Sky/Front.jpg",
};
sk_default_cubemap = tex2d_create_cubemap("default/cubemap", maps);

sk_default_shader = shader_create("default/shader", R"_(cbuffer GlobalBuffer : register(b0) {
float4x4 view;
float4x4 proj;
Expand Down
7 changes: 5 additions & 2 deletions StereoKitC/stereokit.h
Expand Up @@ -123,10 +123,12 @@ SK_API void mesh_set_inds (mesh_t mesh, uint16_t *indices, int index_count);

SK_DeclarePrivateType(tex2d_t);

SK_API tex2d_t tex2d_create (const char *name);
SK_API tex2d_t tex2d_create_file(const char *file);
SK_API tex2d_t tex2d_create (const char *name);
SK_API tex2d_t tex2d_create_cubemap(const char *id, const char **files); // x+, x-, y+, y-, z+, z-
SK_API tex2d_t tex2d_create_file (const char *file);
SK_API void tex2d_release (tex2d_t texture);
SK_API void tex2d_set_colors (tex2d_t texture, int width, int height, uint8_t *data_rgba32);
SK_API void tex2d_set_colors_cube(tex2d_t texture, int width, int height, uint8_t **data_faces_rgba32); // x+, x-, y+, y-, z+, z-

///////////////////////////////////////////

Expand Down Expand Up @@ -193,6 +195,7 @@ SK_API void camera_proj (camera_t &cam, DirectX::XMMATRIX &result);

SK_API void render_set_camera (camera_t &cam, transform_t &cam_transform);
SK_API void render_set_light (const vec3 &direction, float intensity, const color128 &color);
SK_API void render_set_envcube(tex2d_t cubemap);
SK_API void render_add_mesh (mesh_t mesh, material_t material, transform_t &transform);
SK_API void render_add_model (model_t model, transform_t &transform);

Expand Down
81 changes: 81 additions & 0 deletions StereoKitC/texture.cpp
Expand Up @@ -9,6 +9,50 @@
tex2d_t tex2d_create(const char *name) {
return (tex2d_t)assets_allocate(asset_type_texture, name);
}
tex2d_t tex2d_create_cubemap(const char *id, const char **files) {
tex2d_t result = (tex2d_t)assets_find(files[0]);
if (result != nullptr) {
assets_addref(result->header);
return result;
}

// Load all 6 faces
uint8_t *data[6] = {};
int final_width = 0;
int final_height = 0;
bool loaded = true;
for (size_t i = 0; i < 6; i++) {
int channels = 0;
int width = 0;
int height = 0;
data[i] = stbi_load(files[i], &width, &height, &channels, 4);

// Check if there were issues, or one of the images is the wrong size!
if (data == nullptr ||
(final_width != 0 && final_width != width ) ||
(final_height != 0 && final_height != height)) {
loaded = false;
break;
}
final_width = width;
final_height = height;
}

// free memory if we failed
if (!loaded) {
for (size_t i = 0; i < 6; i++) {
if (data[i] != nullptr)
free(data[i]);
}
return nullptr;
}

// Create with the data we have
result = tex2d_create(id);
tex2d_set_colors_cube(result, final_width, final_height, &data[0]);

return result;
}
tex2d_t tex2d_create_file(const char *file) {
tex2d_t result = (tex2d_t)assets_find(file);
if (result != nullptr) {
Expand Down Expand Up @@ -119,6 +163,43 @@ void tex2d_set_colors(tex2d_t texture, int width, int height, uint8_t *data_rgba
d3d_context->Unmap(texture->texture, 0);
}

void tex2d_set_colors_cube(tex2d_t texture, int width, int height, uint8_t **data_faces_rgba32) {
if (texture->resource != nullptr)
texture->resource->Release();
D3D11_TEXTURE2D_DESC desc = {};
desc.Width = width;
desc.Height = height;
desc.MipLevels = 1;
desc.ArraySize = 6;
desc.SampleDesc.Count = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.CPUAccessFlags = 0;
desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;

D3D11_SUBRESOURCE_DATA data[6];
for (size_t i = 0; i < 6; i++) {
data[i].pSysMem = data_faces_rgba32[i];
data[i].SysMemPitch = sizeof(uint8_t) * 4 * width;
data[i].SysMemSlicePitch = 0;
}

if (FAILED(d3d_device->CreateTexture2D(&desc, &data[0], &texture->texture))) {
printf("Create cubemap error!\n");
return;
}

D3D11_SHADER_RESOURCE_VIEW_DESC res_desc = {};
res_desc.Format = desc.Format;
res_desc.Texture2D.MipLevels = desc.MipLevels;
res_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
d3d_device->CreateShaderResourceView(texture->texture, &res_desc, &texture->resource);

texture->width = width;
texture->height = height;
}

void tex2d_set_active(tex2d_t texture, int slot) {
if (texture != nullptr)
d3d_context->PSSetShaderResources(slot, 1, &texture->resource);
Expand Down
Binary file added StereoKitCTest/Assets/Sky/Back.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added StereoKitCTest/Assets/Sky/Bottom.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added StereoKitCTest/Assets/Sky/Front.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added StereoKitCTest/Assets/Sky/Left.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added StereoKitCTest/Assets/Sky/Right.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added StereoKitCTest/Assets/Sky/Top.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions StereoKitCTest/Assets/pbr.hlsl
Expand Up @@ -44,6 +44,8 @@ SamplerState tex_e_sampler;
Texture2D tex_metal : register(t2);
SamplerState tex_metal_sampler;

TextureCube sk_cubemap : register(t6);

float DistributionGGX(float3 normal, float3 half_vec, float roughness);
float GeometrySchlickGGX(float NdotV, float roughness);
float GeometrySmith(float NdotL, float NdotV, float roughness);
Expand Down Expand Up @@ -75,10 +77,15 @@ float4 ps(psIn input) : SV_TARGET {
float NdotL = (dot(normal,light));
float NdotV = (dot(normal,view));

float3 reflection = reflect(-view, normal);
float3 reflection_color = sk_cubemap.Sample(tex_sampler, reflection).rgb;
return float4(reflection_color, 1);

// Lighting an object is a combination of two types of light reflections,
// a diffuse reflection, and a specular reflection. These reflections
// are approximated using functions called BRDFs (Bidirectional Reflectance
// Distribuition Function).
// https://learnopengl.com/PBR/Lighting

// For the diffuse BRDF, we'll use Lambert's, since it's simple, effective,
// and research has said you can't do much better for cheap!
Expand Down

0 comments on commit 62c30a6

Please sign in to comment.