c++ - 程序纹理棋盘 OpenGL

标签 c++ opengl shader

我在实现棋盘的程序纹理时遇到了一些困难。这是我需要得到的:

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

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/

相关文章:

ruby - Gem 在 Mac OSX Lion 上安装 ruby​​-opengl

glsl - GLSL 中不变和精确的关键字

opengl - 除了抗锯齿之外,我可以使用多重采样缓冲区吗?

c++ - OpenGL 着色器编译错误

c++ - 编写我自己的 iostream 实用程序类 : Is this a good idea?

c++ - 停止函数循环然后恢复它们

opengl - glBindVertexBuffer 与 glBindBuffer

python - 如何生成圆环结?

c++ - 使用 C++/WinRT 制作一个简单的消息框

c++ - SFINAE 和定义顺序