2017-03-10 49 views
3

我想將一些舊的OpenGL代碼翻譯成現代的OpenGL。此代碼正在讀取紋理中的數據並進行顯示。將ARB程序集翻譯成GLSL?

static const char *gl_shader_code = 
"!!ARBfp1.0\n" 
"TEX result.color, fragment.texcoord, texture[0], RECT; \n" 
"END"; 

GLuint program_id; 
glGenProgramsARB(1, &program_id); 
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program_id); 
glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei) strlen(gl_shader_code), (GLubyte *) gl_shader_code); 

我只是想轉換成GLSL這段代碼:片段着色器使用ARB_fragment_program命令當前創建。我覺得片段着色器應該是這個樣子:

#version 430 core 
    uniform sampler2DRect s; 

    void main(void) 
    { 
     gl_FragColor = texture2DRect(s, ivec2(gl_FragCoord.xy), 0); 
    } 

但我不知道幾個細節:

  1. 這是texture2DRect正確使用?
  2. 這是gl_FragCoord的正確用法嗎?

使用GL_PIXEL_UNPACK_BUFFER目標將紋理加入像素緩衝區對象。

回答

0
  1. 我想你可以使用標準sampler2D代替sampler2DRect(如果你沒有爲它的實際需求),因爲,報價the wiki,「從現代的眼光看,他們(矩形紋理)似乎基本上是無用的。 」。

然後你可以改變你的texture2DRect(...)texture(...)texelFetch(...)(模仿您的矩形取)。

  • 由於你似乎是使用OpenGL 4,則不需要(不?應)使用gl_FragColor而是聲明一個out變量和寫入。
  • 你的片段着色器應該是這個樣子到底:

    #version 430 core 
    uniform sampler2D s; 
    out vec4 out_color; 
    
    void main(void) 
    { 
        out_color = texelFecth(s, vec2i(gl_FragCoord.xy), 0); 
    } 
    
    0

    @Zouch,非常感謝你對你的迴應。我拿了它,並在這方面做了一些工作。我的最終核心與您的建議非常相似。爲了記錄最後一個頂點和片段着色器,我實現如下:

    頂點着色器:

    #version 330 core 
    
    layout(location = 0) in vec3 vertexPosition_modelspace; 
    layout(location = 1) in vec2 vertexUV; 
    
    out vec2 UV; 
    
    uniform mat4 MVP; 
    
    void main() 
    { 
        gl_Position = MVP * vec4(vertexPosition_modelspace, 1); 
        UV = vertexUV; 
    } 
    

    片段着色器:

    #version 330 core 
    
    in vec2 UV; 
    
    out vec3 color; 
    
    uniform sampler2D myTextureSampler; 
    
    void main() 
    { 
        color = texture2D(myTextureSampler, UV).rgb; 
    } 
    

    這似乎工作。

    +0

    很好用。請注意''texture2D'函數在OpenGL 3.3中已被棄用,您應該使用'texture'函數(相同的簽名) – Zouch