2016-03-07 100 views
0

我遇到了一些困難來實現棋盤格的程序紋理。以下是我需要得到:程序紋理棋盤格OpenGL

enter image description here

這裏是我得到:

enter image description here

這是接近,但我的紋理就什麼,我需要得到一種旋轉。

這是我的着色器的代碼:

#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; 
    } 

顏色表是從0到1的映射,其中0對應於紅色,1爲綠色。

我認爲問題來自我使用的公式,(sx + sy)/ 2。我需要讓廣場不旋轉,但與大廣場的邊界對齊。 如果有人有想法得到好的公式。

謝謝。

回答

1

您可以添加回轉操作 「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; 
}