Texture Mapping and Lighting Vertex Shader ErrorJava OpenGL

孤街浪徒 提交于 2019-12-07 14:07:36

问题


I am trying to map texture to a 3D cube and trying to write shaders so that it has lighting and texture.

I have tried writing texture shader only and it works. I have also tried lighting shader only with 3D values set to red color, and that light shader works too. But when i try to combine that two, I am having problem. I have provided my code below, but i am getting error that

Attached vertex shader is not compiled. Shader could not be linked.

Vertex Shader

#version 330 core

layout(location = 0) in vec4 vPosition;
layout(location = 1) in vec3 vNormal;
layout(location = 2) in vec2 VTexCoord;

out vec4 color;
out vec2 texCoord;

uniform mat4 ModelView;
uniform mat4 NormalTransform;
uniform mat4 Projection;
uniform vec4 LightPosition;
uniform vec4 AmbientProduct, DiffuseProduct, SpecularProduct;
uniform float Shininess;

void main()
{
// Transform vertex position into eye coordinates
vec3 ecPosition = (ModelView * vPosition).xyz;
// Here light position is defined in eye coordinates
vec3 L = normalize( LightPosition.xyz - ecPosition );
vec3 E = normalize( -ecPosition );
vec3 H = normalize( L + E );

// Transform vertex normal into eye coordinates
vec3 N = normalize((NormalTransform *vec4(vNormal,0)).xyz);

// Compute terms in the illumination equation
vec4 ambient = AmbientProduct;

float Kd = max( dot(L, N), 0.0 );
vec4  diffuse = Kd*DiffuseProduct;

float Ks = pow( max(dot(N, H), 0.0), Shininess );
vec4  specular = Ks * SpecularProduct;

if( dot(L, N) < 0.0 ) {
specular = vec4(0.0, 0.0, 0.0, 1.0);
} 


gl_Position = Projection * ModelView * vPosition;
texCoord    = vTexCoord;
color = ambient + diffuse + specular;
color.a = 1.0;
}

Fragment Shader

#version 330 core

in  vec4 color;
in  vec2 texCoord;

out vec4 fColor;

uniform sampler2D tex;

void main()
{
// fColor =  texture( tex, texCoord );

vec4 t = texture( tex, texCoord );
fColor = 0.1*color + 0.9* t;
//  fColor = color*(1-t) + vec4(0.8,0.5,0.3,1)* t;
  }

Compilation Linkage

    int vShader = gl.glCreateShader(GL_VERTEX_SHADER);
    int fShader = gl.glCreateShader(GL_FRAGMENT_SHADER);

    gl.glShaderSource(vShader, 1, vSource, null);
    gl.glShaderSource(fShader, 1, fSource, null);

    gl.glCompileShader(vShader);
    gl.glCompileShader(fShader);

    program = gl.glCreateProgram();
    gl.glAttachShader(program, fShader);
    gl.glAttachShader(program, vShader);

    gl.glLinkProgram(program);

I have tried to get log file.

I am not certain but the problem may be caused by that exception from log file.

Internal exceptions (10 events):
Event: 0.035 Thread 0x0000000002950800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.defineClass(Ljava/lang/String;[BII)Ljava/lang/Class; name or signature does not match> (0x000000076b78cf50) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u66\5298\hotspot\srv„;…їЎ?
Event: 0.035 Thread 0x0000000002950800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.prefetchRead(Ljava/lang/Object;J)V name or signature does not match> (0x000000076b78d238) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u66\5298\hotspot\src\share\vm\prims\j‘ћ·¬QЉё?
Event: 0.096 Thread 0x0000000002950800 Exception <a 'java/security/PrivilegedActionException'> (0x000000076b82b230) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u66\5298\hotspot\src\share\vm\prims\jvm.cpp, line 1386]
Event: 0.096 Thread 0x0000000002950800 Exception <a 'java/security/PrivilegedActionException'> (0x000000076b82b440) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u66\5298\hotspot\src\share\vm\prims\jvm.cpp, line 1386]
Event: 0.096 Thread 0x0000000002950800 Exception <a 'java/security/PrivilegedActionException'> (0x000000076b82efe8) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u66\5298\hotspot\src\share\vm\prims\jvm.cpp, line 1386]
Event: 0.096 Thread 0x0000000002950800 Exception <a 'java/security/PrivilegedActionException'> (0x000000076b82f1f8) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u66\5298\hotspot\src\share\vm\prims\jvm.cpp, line 1386]
Event: 0.103 Thread 0x0000000002950800 Exception <a 'java/io/FileNotFoundException'> (0x000000076b833d80) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u66\5298\hotspot\src\share\vm\prims\jni.cpp, line 709]
Event: 0.140 Thread 0x0000000002950800 Exception <a 'java/lang/NoSuchFieldError': method resolution failed> (0x000000076bb8bcd8) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u66\5298\hotspot\src\share\vm\prims\methodHandles.cpp, line 1146]
Event: 0.141 Thread 0x0000000002950800 Exception <a 'java/lang/NoSuchFieldError': method resolution failed> (0x000000076bb990a8) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u66\5298\hotspot\src\share\vm\prims\methodHandles.cpp, line 1146]
Event: 0.175 Thread 0x0000000002950800 Exception <a 'java/io/FileNotFoundException'> (0x000000076bc2dc50) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u66\5298\hotspot\src\share\vm\prims\jni.cpp, line 709]

Log stack frame:

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  jogamp.opengl.gl4.GL4bcImpl.dispatch_glGetShaderInfoLog1(IILjava/lang/Object;IZLjava/lang/Object;IZJ)V+0
j  jogamp.opengl.gl4.GL4bcImpl.glGetShaderInfoLog(IILjava/nio/IntBuffer;Ljava/nio/ByteBuffer;)V+123
j  Basic.ShaderProg.loadShaders(Lcom/jogamp/opengl/GL3;)V+157
j  Basic.ShaderProg.<init>(Lcom/jogamp/opengl/GL3;Ljava/lang/String;Ljava/lang/String;)V+16
j  VCCW04$Renderer.init(Lcom/jogamp/opengl/GLAutoDrawable;)V+358
j  jogamp.opengl.GLDrawableHelper.init(Lcom/jogamp/opengl/GLEventListener;Lcom/jogamp/opengl/GLAutoDrawable;Z)V+2
j  jogamp.opengl.GLDrawableHelper.init(Lcom/jogamp/opengl/GLAutoDrawable;Z)V+81
j  jogamp.opengl.GLAutoDrawableBase$1.run()V+26
j  jogamp.opengl.GLDrawableHelper.invokeGLImpl(Lcom/jogamp/opengl/GLDrawable;Lcom/jogamp/opengl/GLContext;Ljava/lang/Runnable;Ljava/lang/Runnable;)V+197
j  jogamp.opengl.GLDrawableHelper.invokeGL(Lcom/jogamp/opengl/GLDrawable;Lcom/jogamp/opengl/GLContext;Ljava/lang/Runnable;Ljava/lang/Runnable;)V+72
j  com.jogamp.newt.opengl.GLWindow.display()V+90
j  jogamp.opengl.GLAutoDrawableBase.defaultWindowResizedOp(II)V+206
j  com.jogamp.newt.opengl.GLWindow.access$200(Lcom/jogamp/newt/opengl/GLWindow;II)V+3
j  com.jogamp.newt.opengl.GLWindow$2.windowResized(Lcom/jogamp/newt/event/WindowEvent;)V+18
j  jogamp.newt.WindowImpl.consumeWindowEvent(Lcom/jogamp/newt/event/WindowEvent;)V+234
j  jogamp.newt.WindowImpl.sendWindowEvent(I)V+14
j  jogamp.newt.WindowImpl.setVisibleActionImpl(Z)V+691
j  jogamp.newt.WindowImpl$VisibleAction.run()V+8
j  com.jogamp.common.util.RunnableTask.run()V+198
j  jogamp.newt.DefaultEDTUtil$NEDT.run()V+221
v  ~StubRoutines::call_stub

回答1:


As I have post early we must check shader compilation linkage state before using is.

I do this in that way (OpenGL-ES 2.0):

    m_nVertexShader = glCreateShader(GL_VERTEX_SHADER);
    m_nPixelShader = glCreateShader(GL_FRAGMENT_SHADER);


    glShaderSource(m_nVertexShader, 1, &lpszVertexBuffer, NULL);
    glShaderSource(m_nPixelShader, 1, &lpszFragmentBuffer, NULL);

    glCompileShader(m_nVertexShader);

    int iIsOk = 0;

    glGetShaderiv(m_nVertexShader, GL_COMPILE_STATUS, &iIsOk);

    if(!iIsOk)
    {
        GLint infoLen = 0;

        glGetShaderiv(m_nVertexShader, GL_INFO_LOG_LENGTH, &infoLen);

        if(infoLen > 1)
        {
            char* infoLog = (char*)malloc(sizeof(char) * infoLen);

            glGetShaderInfoLog(m_nVertexShader, infoLen, NULL, infoLog);


            QMessageBox::warning(this, QString("Error"),
                                 QString(infoLog), QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Yes);

            free(infoLog);
        }

        glDeleteShader(m_nVertexShader);

        return;
    }

    glCompileShader(m_nPixelShader);

    glGetShaderiv(m_nPixelShader, GL_COMPILE_STATUS, &iIsOk);

    if(!iIsOk)
    {
        GLint infoLen = 0;

        glGetShaderiv(m_nPixelShader, GL_INFO_LOG_LENGTH, &infoLen);

        if(infoLen > 1)
        {
            char* infoLog = (char*)malloc(sizeof(char) * infoLen);

            glGetShaderInfoLog(m_nPixelShader, infoLen, NULL, infoLog);


            QMessageBox::warning(this, QString("Error"),
                                 QString(infoLog), QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Yes);

            free(infoLog);
        }

        glDeleteShader(m_nPixelShader);

        return;
    }

    m_nProgram = glCreateProgram();

    glAttachShader(m_nProgram, m_nVertexShader);
    glAttachShader(m_nProgram, m_nPixelShader);

    glBindAttribLocation(m_nProgram, 0, "rm_Vertex");

    glLinkProgram(m_nProgram);

    glGetProgramiv(m_nProgram, GL_LINK_STATUS, &iIsOk);

    // Fail to pass status validation
    if(!iIsOk)
    {
        GLint infoLen = 0;

        glGetProgramiv(m_nProgram, GL_INFO_LOG_LENGTH, &infoLen);

        if(infoLen > 1)
        {
            char* infoLog = (char*)malloc(sizeof(char) * infoLen);

            glGetProgramInfoLog(m_nProgram, infoLen, NULL, infoLog);


            QMessageBox::warning(this, QString("Error"),
                                 QString(infoLog), QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Yes);

            free(infoLog);
        }

        glDeleteProgram(m_nProgram);

        return;
    }

    glUseProgram(m_nProgram);

You can also spesify message box show calls in more informative way strings like:

QMessageBox::warning(this, QString("Vertex shader compilation error."),
                                     QString(infoLog), QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Yes);

QMessageBox::warning(this, QString("Fragment shader compilation error."),
                                     QString(infoLog), QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Yes);

QMessageBox::warning(this, QString("Shader linkage error."),
                                     QString(infoLog), QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Yes);

In that way we can significaly redure the area of bug searching.

I sudernly find some info about the bug wich looks like this JOGL2 + GLCapabilities + Windows. I think it is only the Windows issue. Try to build your application on Linux OS.

And first of all you must specify rendertarget output for before linkage.

glBindFragDataLocation(program, 0, "fColor");


来源:https://stackoverflow.com/questions/33904241/texture-mapping-and-lighting-vertex-shader-errorjava-opengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!