[GLFW] 多线程多窗口的一种实现
[C++] 根据运算符获取对应枚举
// app::DepthTest::Func::Type
enum class Type : GLenum
{
Always = GL_ALWAYS,
Never = GL_NEVER,
Less = GL_LESS,
Equal = GL_EQUAL,
LEqual = GL_LEQUAL,
Greater = GL_GREATER,
NotEqual = GL_NOTEQUAL,
GEqual = GL_GEQUAL,
Default = Less,
};
#define APP_DEPTH_FUNC_OP(op)\
(([] {\
struct { void operator##op (int) { } };/* X APP_DEPTH_FUNC_OP(== 1 && 2 ==) X */\
static_assert(std::is_same_v<bool, decltype(0 op 0)>, "Comparison operator only.");\
switch (((1 op 2) << 2) | ((3 op 3) << 1) | (5 op 4))\
{\
case 0b001: return app::DepthTest::Func::Type::Greater;\
case 0b010: return app::DepthTest::Func::Type::Equal;\
case 0b011: return app::DepthTest::Func::Type::GEqual;\
case 0b100: return app::DepthTest::Func::Type::Less;\
case 0b101: return app::DepthTest::Func::Type::NotEqual;\
case 0b110: return app::DepthTest::Func::Type::LEqual;\
}\
})())
用例:
APP_DEPTH_FUNC_OP(==) == Type::Equal;// (bool)true
[GLSL] Mipmap 相关函数
- int textureQueryLevels(sampler) : 返回该材质 Mipmap 数量
- vec2 textureQueryLod(sampler, vec2) : 返回 Mipmap 与 LOD 所在等级
[GLSL] HSV 转为 RGB
vec3 rgb(float h, float s, float v)
{
int hi = int(mod(floor(h / 60), 6));
float f = h / 60 - hi;
float p = v * (1 - s);
float q = v * (1 - f * s);
float t = v * (1 - (1 - f) * s);
switch(hi)
{
case 0:
return vec3(v, t, p);
case 1:
return vec3(q, v, p);
case 2:
return vec3(p, v, t);
case 3:
return vec3(p, q, v);
case 4:
return vec3(t, p, v);
case 5:
return vec3(v, p, q);
}
}
[OpenGL] error C7539: GLSL 1.20 does not allow nested structs
请看以下代码:
// ...
uniform struct
{
struct
{
vec3 pos;
} list[10];
} lights;
// ...
有的显卡可以正常处理以上定义,有的不行(即禁止在结构中定义结构)。
请改用以下方式声明:
// ...
struct whatever
{
vec3 pos;
};
uniform struct
{
struct whatever list[10];
} lights;
// ...
[OpenGL] C++ 封装(自动绑定/对象操作)
[OpenGL] 在片段着色器中获取 Mipmap 等级
[力扣] 算法 930 (C++)
930. 和相同的二元子数组
class Solution {
public:
int numSubarraysWithSum(vector<int>& nums, int goal) {
vector map{ 1 };
for (auto&& num : nums)
{
if (num)
{
map.emplace_back(1);
}
else
{
++map.back();
}
}
int ret = 0;
for (unsigned i = goal; i < map.size(); ++i)
{
if (goal > 0)
{
ret += map[i] * map[i - goal];
}
else
{
ret += map[i] * (map[i] - 1) / 2;
}
}
return ret;
}
};
[力扣] 算法 191 (C++)
剑指 Offer 15. 二进制中1的个数
class Solution {
public:
int hammingWeight(uint32_t n) {
n = ((n & 0xAAAAAAAA) >> 1) + (n & 0x55555555);
n = ((n & 0xCCCCCCCC) >> 2) + (n & 0x33333333);
n = ((n & 0xF0F0F0F0) >> 4) + (n & 0x0F0F0F0F);
n = ((n & 0xFF00FF00) >> 8) + (n & 0x00FF00FF);
n = ((n & 0xFFFF0000) >> 16) + (n & 0x0000FFFF);
return n;
}
};