Skip to content

Instantly share code, notes, and snippets.

@yorung
yorung / get_time.cpp
Last active August 29, 2015 14:22
GetTime using C++11
#include <chrono>
double GetTime()
{
static auto start = std::chrono::high_resolution_clock::now();
auto now = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1, 1>>>(now - start).count();
}
@yorung
yorung / build.gradle
Last active August 29, 2015 14:22
build.gradle for std::chrono
android {
defaultConfig {
ndk {
moduleName 'AdamasNative'
cFlags "-std=c++11"
ldLibs "log"
ldLibs "GLESv3"
abiFilters "armeabi", "mips", "x86"
stl "gnustl_static"
}
@yorung
yorung / layout_location.glsl
Created July 6, 2015 15:09
How to specify Input Layout Qualifiers
#version 310 es
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;
@yorung
yorung / how_to_bind.cpp
Last active August 29, 2015 14:24
How to use glBindAttribLocation
static const int POSITION_INDEX = 0;
static const int NORMAL_INDEX = 1;
glBindAttribLocation(program, POSITION_INDEX, "position");
glBindAttribLocation(program, NORMAL_INDEX, "normal");
glLinkProgram(program);
@yorung
yorung / query.cpp
Created July 6, 2015 15:22
How to query attribute location
GLint positionIndex = glGetAttribLocation(program, "position");
GLint normalIndex = glGetAttribLocation(program, "normal");
@yorung
yorung / half_float_texture.cpp
Last active August 29, 2015 14:25
half float texture creating
#ifdef OPENGL_OR_ES30
// ES 3.0
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, w, h, 0, GL_RGBA, GL_HALF_FLOAT, nullptr);
#else
// ES 2.0
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_HALF_FLOAT_OES, nullptr);
#endif
@yorung
yorung / cubemap.hlsl
Last active August 16, 2017 16:04
a pixel shader for drawing background using a cubemap or photosphere
cbuffer perObject : register(b0)
{
row_major matrix invVP;
}
TextureCube texCube : register(t0);
SamplerState samplerState : register(s0);
struct VsToPs
{
@yorung
yorung / little_planet.hlsl
Created August 21, 2015 11:47
Stereographic projection(Little Planet)
float4 mainPS(VsToPs inp) : SV_Target
{
float2 scale = float2(4.0f / 3.0f, 1) * 3; // scale & aspect ratio
float2 plane = inp.screenPos.xy * scale;
float3 dir = float3(plane.x * 2, plane.y * 2, -1 + dot(plane, plane)) / (1 + dot(plane, plane));
return texCube.Sample(samplerState, dir.xzy); // y is upper
}
@yorung
yorung / projection_equirectangular_to_stereographic.fx
Created August 21, 2015 11:56
A Stereographic Projection example using a Photo Sphere as source
float4 mainPS(VsToPs inp) : SV_Target
{
float2 scale = float2(4.0f / 3.0f, 1) * 3; // scale & aspect ratio
float2 plane = inp.screenPos.xy * scale;
float3 dir = float3(plane.x * 2, plane.y * 2, -1 + dot(plane, plane)) / (1 + dot(plane, plane));
dir = dir.xzy; // y is upper
float longitude = atan2(dir.x, dir.z) * (180 / 3.14159265f);
float latitude = asin(dir.y) * (180 / 3.14159265f);
@yorung
yorung / dds_loader.cpp
Last active August 29, 2015 14:27
DDS Loader
struct DDSHeader {
uint32_t h3[3];
int h, w;
uint32_t h2[2];
int mipCnt;
uint32_t h13[13];
uint32_t fourcc, bitsPerPixel, rMask, gMask, bMask, aMask, caps1, caps2;
bool IsCubeMap() const { return caps2 == 0xFE00; }
int GetArraySize() const { return IsCubeMap() ? 6 : 1; }
int GetMipCnt() const { return std::max(mipCnt, 1); }