我在实现棋盘的程序纹理时遇到了一些困难。这是我需要得到的:
这是我得到的:
它很接近,但我的纹理相对于我需要得到的东西有点旋转。
这是我的着色器代码:
#version 330
in vec2 uv;
out vec3 color;
uniform sampler1D colormap;
void main() {
float sx = sin(10*3.14*uv.x)/2 + 0.5;
float sy = sin(10*3.14*uv.y)/2 + 0.5;
float s = (sx + sy)/2;
if(true){
color = texture(colormap,s).rgb;
}
colormap 是从 0 到 1 的映射,其中 0 对应红色,1 对应绿色。
我认为问题出在我使用的公式 (sx+sy)/2 上。我需要让正方形不旋转但与大正方形的边界对齐。 如果有人有想法得到好的公式。
谢谢。
最佳答案
可以给“uv”加上旋转操作:
mat2 R(float degrees){
mat2 R = mat2(1);
float alpha = radians(degrees);
R[0][0] = cos(alpha);
R[0][1] = sin(alpha);
R[1][0] = -sin(alpha);
R[1][1] = cos(alpha);
return R;
}
void main(){
ver2 new_uv = R(45) * uv;
float sx = sin(10*3.14*new_uv.x)/2 + 0.5;
float sy = sin(10*3.14*new_uv.y)/2 + 0.5;
float s = (sx + sy)/2;
color = texture(colormap,s).rgb;
}
关于c++ - 程序纹理棋盘 OpenGL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35849501/