本文介绍了3D对象以看起来像2D对象的方式着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以下着色器源用于顶点和片段.

顶点着色器源:

  #define highp属性highp vec3位置;均匀的高压垫mvp;void main(无效){gl_Position = mvp * vec4(position,1.0);} 

片段着色器源:

  #define highp均匀的vec vec3颜色;void main(无效){gl_FragColor = vec4(color,1.0);} 

但是,着色器无法正常工作.如下图所示, 3D对象的颜色就像2D对象一样:


我正在使用@ Rabbid76代码进行一些小的修改:将 #version 130 添加到顶点和片段着色器源的顶部.看来我的 Intel 图形卡要求 #version 130 指令,否则会引发一些警告和错误:

#version 130 指令可解决上述警告及其后续错误.

解决方案

您的着色器正常工作.看起来是2D,因为着色器中没有任何照明.具有统一颜色的对象将看起来是2D的,因为没有诸如自阴影或镜面高光等的深度提示.

I'm using the following shader sources for vertex and fragment.

Vertex shader source:

#define highp
attribute highp vec3 position;
uniform highp mat4 mvp;
void main(void)
{
    gl_Position = mvp * vec4(position, 1.0);
}

Fragment shader source:

#define highp
uniform highp vec3 color;
void main(void)
{
    gl_FragColor = vec4(color, 1.0);
}

However, the shader is not working. As shown in the following screen-shot, a 3D object is just colored like a 2D object:


glGetShaderiv with GL_COMPILE_STATUS returns success == TRUE, so there is no shader compile error.

glGetShaderiv(shaderObj, GL_COMPILE_STATUS, &success);


I didn't try glGetError() in the code. I'm going to try it. But I suspect I'm not receiving any OpenGL errors.


I believe I need to adjust the color in vertex and fragment shader. How should I adjust the color in shader sources? Can anyone help me by giving a hint. So far, I couldn't resolve the issue by modifying the sources.


UPDATE

With the help of @Rabbid76 now the 3D object looks good:

I'm using @Rabbid76 code with a small modification: adding the #version 130 to the top of both vertex and fragment shader sources. Looks like my Intel graphics card requires the #version 130 directive, otherwise it throws some warnings and errors:

The #version 130 directive resolves the above warning and its subsequent errors.

解决方案

Your shader is working correctly. It looks like it's 2D because you don't have any lighting in your shader. An object that is a uniform color will look 2D because there are no depth cues such as self shadowing, or specular highlights, etc.

这篇关于3D对象以看起来像2D对象的方式着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 08:03