OpenGL ES 2.0/GLSL无法呈现数据(Kotlin) [英] OpenGL ES 2.0 / GLSL not rendering data (Kotlin)

查看:91
本文介绍了OpenGL ES 2.0/GLSL无法呈现数据(Kotlin)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用GLES 2/3实现基本的着色程序,并已将各种教程中的代码拼凑而成.一切对我来说似乎都是正确的,并且可以正常编译,但是我的屏幕上没有任何显示.

I am trying to implement a basic shading program with GLES 2/3, and have pieced together this code from various tutorials. Everything looks correct to me, and it compiles fine, but nothing appears on my screen.

在我添加法线和光照位置数据之前,它表现得很好,然后它破裂了,我一直无法找到一种解决方法.

It rendered fine until I added Normal and Light Position data, then it broke and I haven't been able to find a way to fix it.

任何人都可以看到这里出了什么问题吗?

Can anyone see what's wrong here?

class MyRenderer:GLSurfaceView.Renderer{

    var glProgram = -1
    var uMV       = -1
    var uMVP      = -1
    var uLightPos = -1
    var aPosition = -1
    var aNormal   = -1
    var aColor    = -1

    val verts = arrayOf(0f,1f,0f, -1f,0f,0f,  1f,0f,0f)
    val vBuf  = allocateDirect(verts.size*4).order(nativeOrder()).asFloatBuffer()
    val norms = arrayOf(0f,0f,-1f,  0f,0f,-1f,  0f,0f,-1f)
    val nBuf  = allocateDirect(norms.size*4).order(nativeOrder()).asFloatBuffer()

    override fun onSurfaceCreated(g:GL10,c:EGLConfig) {
        glClearColor(0f,0f,0f,1f)
        glClearDepthf(1f)
        glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)

        glProgram = glCreateProgram()

        val vShader = glCreateShader(GL_VERTEX_SHADER)
        glShaderSource(vShader,"#version 100\n                    " +
            "uniform   mat4 u_mvMat;                              " +
            "uniform   mat4 u_mvpMat;                             " +
            "uniform   vec3 u_LightPos;                           " +
            "attribute vec4 a_Position;                           " +
            "attribute vec3 a_Normal;                             " +
            "attribute vec4 a_Color;                              " +
            "varying   vec4 v_Color;                              " +
            "void main(){                                         " +
            "   vec3 vertex = vec3(u_mvMat*a_Position);           " +
            "   vec3 normal = vec3(u_mvMat*vec4(a_Normal,0.0));   " +
            "   vec3 lightVector = normalize(u_LightPos-vertex);  " +
            "   float distance = length(u_LightPos-vertex);       " +
            "   float diffuse = max(dot(normal,lightVector),0.1)  " +
            "                   / (1.0+distance*distance/4.0);    " +
            "   v_Color = a_Color*diffuse;                        " +
            "   gl_Position = u_mvpMat*a_Position;}               " )
        glCompileShader(vShader)
        glAttachShader(glProgram,vShader)

        val fShader = glCreateShader(GL_FRAGMENT_SHADER)
        glShaderSource(fShader,"#version 100\n                    " +
            "precision mediump float;                             " +
            "varying vec4 v_Color;                                " +
            "void main(){                                         " +
            "   gl_FragColor = v_Color;}                          " )
        glCompileShader(fShader)
        glAttachShader(glProgram,fShader)

        glLinkProgram(glProgram)
        glUseProgram(glProgram)

        uMVP      = glGetUniformLocation(glProgram,"u_mvpMat")
        uMV       = glGetUniformLocation(glProgram,"u_mvMat")
        uLightPos = glGetUniformLocation(glProgram,"u_LightPos")
        aPosition = glGetAttribLocation (glProgram,"a_Position")
        aNormal   = glGetAttribLocation (glProgram,"a_Normal")
        aColor    = glGetAttribLocation (glProgram,"a_Color")

        glVertexAttribPointer(aPosition,4,GL_FLOAT,false,3*4,vBuf)
        glEnableVertexAttribArray(aPosition)
        glVertexAttribPointer(aNormal,4,GL_FLOAT,false,3*4,nBuf)
        glEnableVertexAttribArray(aNormal)

        val modelM = FloatArray(16)
        setIdentityM(modelM,0)
        val viewM = FloatArray(16)
        setLookAtM(viewM,0,  0f,0f,-5f,  0f,0f,0f,  0f,0f,1f)
        val projM = FloatArray(16)
        frustumM(projM,0, -2f,2f, 1f,-1f, 1f,50f)
        val mvM = FloatArray(16)
        multiplyMM(mvM,0,viewM,0,modelM,0)
        glUniformMatrix4fv(uMV,1,false,mvM,0)
        val mvpM = FloatArray(16)
        multiplyMM(mvpM,0,projM,0,mvM,0)
        glUniformMatrix4fv(uMVP,1,false,mvpM,0)

        glUniform3v(uLightPos,-1f,-10f,-1f)

        glVertexAttrib4f(aColor,1f,1f,1f,1f)
        glDrawArrays(GL_TRIANGLES,0,verts.size/3)
    }
    override fun onSurfaceChanged(g:GL10,w:Int,h:Int){}
    override fun onDrawFrame(g:GL10){}
}

推荐答案

如果要使用[0.0,1.0]范围内的颜色值,则必须使用

If you want to use color values in range [0.0, 1.0] then you've to use glVertexAttrib4f rather than glVertexAttribI4i:

glVertexAttribI4i(aColor,1,1,1,1)

glVertexAttrib4f(aColor,1.0f,1.0f,1.0f,1.0f)

glVertexAttribI *假定值为[-2147483647、2147483647]或[0、4294967295]范围内的有符号或无符号定点值.值1几乎是黑色的.

glVertexAttribI* assumes the values to be signed or unsigned fixed-point values in range [-2147483647, 2147483647] or [0, 4294967295]. A value of 1 is almost black.

u_LightPos的类型是浮点(vec3):

uniform vec3 u_LightPos; 

您必须使用 glUniform3f 而不是glUniform3i来设置浮点统一变量的值:

You've to use glUniform3f rather than glUniform3ito set the value of a floating point uniform variable:

glUniform3i(uLightPos,-1,-10,-1)

glUniform3f(uLightPos,-1f,-10f,-1f)


我建议通过验证是否成功编译了着色器glGetShaderiv (参数GL_COMPILE_STATUS).可以通过 glGetShaderInfoLog 来检索编译错误消息.


I recommend to verify if the shader is complied successfully by glGetShaderiv (parameter GL_COMPILE_STATUS). Compile error messages can be retrieved by glGetShaderInfoLog

我建议添加环境光组件(出于调试原因) 例如:

I recommend to add an ambient light component (for debug reasons) e.g.:

v_Color = a_Color*(diffuse + 0.5);

如果可以用环境光看到"几何,则可能存在一些问题:

If you can "see" the geometry with the ambient light, then there are some possible issues:

  1. 光源位于几何体的背面,因此背面被照亮,而不是正面.这导致从视图的角度来看几乎只有黑色,不发光的一面.

  1. the light source is on the back side of the geometry, so the back side is lit, but not the front side. That cause that only the almost black, unlit side is visible from the point of view.

光源到几何体的距离太大". distance变得非常大,因此diffuse很小,所有几何图形几乎都是黑色的.

The distance of the light source to the geometry is "too large". distance becomes a very huge value and so diffuse is very small and all the geometry is almost black.

光源在几何的封闭空间中.

The light source is in the closed volume of the geometry.

所有法线向量都指向远离相机的方向.这可能导致dot(normal, lightVector)小于0.0.

All the normal vectors point away from the camera. That may cause that dot(normal, lightVector) is less than 0.0.

这篇关于OpenGL ES 2.0/GLSL无法呈现数据(Kotlin)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆