首页 > 解决方案 > 如何用具有纹理的对象绘制没有纹理的对象 OPENGL

问题描述

我有几个没有纹理坐标 UV 的对象传递到片段着色器,而我只有两个其他对象的纹理坐标 UV 传递到片段着色器。没有纹理的物体仍然可见,但颜色暗淡。但在插入光方程后,它会变成黑色且不可见。我如何在不改变其原始颜色并保持光照方程的情况下绘制非纹理化对象(我已经为它们创建了颜色数组并将它们传递给顶点着色器)。我已经尝试过了,但我的片段着色器无法编译。

#version 330 

  in vec3 fragmentColor;
  in vec3 fragmentNormal;
  in vec2 UV;
  in vec4 Position;
  uniform vec4 lighteye;
  uniform float intensityh;
  uniform float intensityd;
  uniform float objectd;
  uniform vec4 worldCoord;

// 纹理均匀采样器 2D texture_Colors 的数据;

if(UV.x >= 0.0)
color = intensityh * texture2D( texture_Colors, UV ).rgb * diffuse + (intensityd * texture2D( texture_Colors, UV ).rgb * something) ;
else
color = vec4(fragmentColor,1.0);

标签: openglgraphicsglsl

解决方案


据我了解,您可以执行以下操作:

color r = intensityh * texture2D( texture_Colours, worldCoord.xy).rgb * diffuse+( intensityd* texture2D (texture_Colours, worldCoord.xy).rgb * something

这应该根据对象在 3D 空间中的位置来纹理化您的对象。但是,不要忘记通过 CPU 代码为每个纹理启用纹理重复,如下所示:

glTexParameterf(GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_WRAP_T, GL_REPEAT);  

推荐阅读