2014-04-04 55 views
1

我是一名關於OpenGL和GLSL的新學員。我正在編寫一個程序,我想將一組數據存儲在紋理中,並通過對頂點着色器中的紋理進行採樣來獲取數據。然後我想將數據設置爲地形位置的y座標。但我得到一個麻煩,它的失敗,我不能得到正確的value___all在Y座標爲0如何將數組存儲到紋理中並正確採樣頂點着色器中的紋理?

主要的代碼如下:

GLuint terrainShader;  // The perspective demonstration shader 
GLint locMVP,locTex;     // The location of the uniform in vertex shader 

GLuint vao;     // The VAO 
GLuint vertexBuffer;   
GLuint elementBuffer; 
GLuint vertexTexture;  

const int n=127; 
float verteces[n*n*2];// vertex array 
GLuint vIndexFine[(n-1)*2*n];//index array 
GLsizei countFine[n-1]; 
GLvoid* offsetFine[n-1]; 

float *data=new float[n*n]; 
    terrainShader = gltLoadShaderPairWithAttributes("terrain.vp", "terrain.fp", 1, 
            GLT_ATTRIBUTE_VERTEX, "vVertex"); 

    locMVP = glGetUniformLocation(terrainShader, "mvpMatrix"); 
    locTex = glGetUniformLocation(terrainShader, "vertexTexture"); 

    //creat a terrain with size of n*n 
    for(int i=0;i<n;i++) 
    { 
     int sum=i*n; 
     for(int j=0;j<n;j++) 
     { 
      verteces[(sum+j)*2]=float(j); 
      verteces[(sum+j)*2+1]=float(i); 
     } 
    } 
    //initialize the index array 
    for(int i=0;i<n-1;i++) 
    { 
     if(i==0)    //the first line 
     { 
      for(int j=0;j<n;j++) 
      { 
       vIndexFine[2*j]=j; 
       vIndexFine[2*j+1]=j+n; 
      } 
     } 
     else 
     {     //if it's not the first line,then just add n to get the indexes of new line 
      for(int k=0;k<2*n;k++) 
      { 
       vIndexFine[i*2*n+k]=vIndexFine[(i-1)*2*n+k]+n; 
      } 
     } 
    } 

    glGenVertexArrays(1, &vao); 
    glBindVertexArray(vao); 

    glGenBuffers(1, &vertexBuffer); 
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(verteces), verteces, GL_STATIC_DRAW); 
    glVertexAttribPointer(GLT_ATTRIBUTE_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, NULL); 
    glEnableVertexAttribArray(GLT_ATTRIBUTE_VERTEX); 

    glGenBuffers(1, &elementBuffer); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vIndexFine), vIndexFine, GL_STATIC_DRAW); 
     //initialize data array with random integers between 1 and 100 
    for(int i=0;i<n*n;i++) 
     data[i]=float(rand()%100)/100.0; 

     //creat a texture and store data 
    glGenTextures(1, &vertexTexture); 
    glActiveTexture(GL_TEXTURE0); 
    glBindTexture(GL_TEXTURE_2D, vertexTexture); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, n,n, 0,GL_RED, GL_FLOAT, data); 

    //compute every trianglestrip's indexcount and offset 
    int temp=n*2*sizeof(GLuint); 
    for(int i=0;i<n-1;i++) 
    { 
     offsetFine[i]=(void*)(temp*i); 
     countFine[i]=GLsizei(n*2); 
    } 

    glUseProgram(terrainShader); 
    glUniform1i(locTex, 0); 

    modelViewMatrix.PushMatrix(viewFrame); 
    modelViewMatrix.Translate(-n/2.0, -10.0f, -n/2.0); 
    glUniformMatrix4fv(locMVP, 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix()); 
    glMultiDrawElements(GL_TRIANGLE_STRIP,countFine,GL_UNSIGNED_INT, (const GLvoid**)offsetFine,n-1); 
    modelViewMatrix.PopMatrix(); 

頂點着色器:

#version 410 

uniform mat4 mvpMatrix; 
uniform sampler2D vertexTexture; 
in vec2 vVertex; 

void main(void) 
    { 
    vec2 vTexCoords=vVertex/127.0; 
    float height = texture2DLod(vertexTexture, vTexCoords,0.0).x*100.0; 
    vec4 position=vec4(vVertex.x,height,vVertex.y,1.0); 
    gl_Position = mvpMatrix * position; 
    } 

我認爲這個錯誤必須是應用程序中存儲數據的一部分,或者是從頂點着色器中的紋理獲取數據的部分。真的可以爲我指出嗎?

回答

0

它解決now.the答案是here 嘗試設置紋理的縮小過濾器GL_NEAREST或GL_LINEAR glTexImage2D()後:

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

OpenGL的默認設置是使用mipmap,並且您沒有發送任何使紋理不完整的紋理,並且會禁用紋理圖像單元。

然後,您可以在着色器中使用紋理(vertexTexture,vTexCoords),而不使用棄用的texture2DLOD()版本,並使用顯式的LOD訪問。

+1

儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的重要部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 –

+0

@Dave Cemmer好的,我明白了。 – Qingsong

相關問題