2010-07-04 66 views
0

任何人都可以幫助我解決一個簡單的問題嗎?我已經把一個正方形從20 * 20三角形等多個三角形構建而成,我在這個正方形上添加了一個紋理,並且我已將法線設置爲該正方形,並且由於它的位置在零Z座標平面中,法線是很容易找到(0,0,1)。問題是,我看不到我的紋理廣場照亮的地方。我也嘗試去除環境光並減少它,但沒有機會。此外,我增加了從正方形小三角的數量爲更好的決議,但沒有運氣。我搜索基本的例子,但沒有運氣。還有人找到了解決紋理上的問題的方法,或者有人能給我一個小例子嗎? 所以:聚光燈照亮紋理的基本問題

  1. 在surfacecreated我已經把:
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_FASTEST); 
    gl.glClearColor(.5f, .5f, .5f, 1); 
    gl.glShadeModel(GL10.GL_SMOOTH); 
    gl.glEnable(GL10.GL_DEPTH_TEST); 
    gl.glEnable(GL10.GL_TEXTURE_2D); 

    float lightAmbient[] = new float[] { 0.3f, 0.3f, 0.3f, 1 }; 
    float lightDiffuse[] = new float[] { 0.7f, 0.7f, 0.7f, 1 }; 
    float lightSpecular[] = new float[] { 0.7f, 0.7f, 0.7f, 1 }; 
    lightDirection = new float[] {0.0f, 0.0f, -1.0f}; 
    lightPos = new float[] { 0, 0, 10f, 1 }; 

    gl.glEnable(GL10.GL_LIGHTING); 
    gl.glEnable(GL10.GL_LIGHT0); 


    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient, 0);  
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse, 0);  
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, lightSpecular, 0); 
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPos, 0); 
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPOT_DIRECTION, lightDirection, 0); 
    gl.glLightf(GL10.GL_LIGHT0, GL10.GL_SPOT_CUTOFF, 3f); 
    gl.glLightf(GL10.GL_LIGHT0, GL10.GL_SPOT_EXPONENT, 100f); 
    gl.glEnable(GL10.GL_DEPTH_TEST); 
    gl.glDepthFunc(GL10.GL_LESS); 
    gl.glDisable(GL10.GL_DITHER); 
    gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE); 

    int[] textures = new int[1]; 
    gl.glGenTextures(1, textures, 0); 
    mTextureID = textures[0]; 
    gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID); 

    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, 
      GL10.GL_NEAREST); 
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, 
      GL10.GL_LINEAR); 

    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, 
      GL10.GL_CLAMP_TO_EDGE); 
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, 
      GL10.GL_CLAMP_TO_EDGE); 

    gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, 
      GL10.GL_REPLACE); 

    InputStream is = mContext.getResources() 
      .openRawResource(R.raw.wood); 
    Bitmap bitmap; 
    try { 
     bitmap = BitmapFactory.decodeStream(is); 
    } finally { 
     try { 
      is.close(); 
     } catch(IOException e) { 
      // Ignore. 
     } 
    } 

    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); 
    bitmap.recycle(); 
  1. 在onDrawFrame我已經添加: gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10。 GL_DEPTH_BUFFER_BIT);

    gl.glMatrixMode(GL10.GL_MODELVIEW); 
    gl.glLoadIdentity(); 
    
    GLU.gluLookAt(gl, 0, 0, 10, 0f, 0f, 0f, 0f, 1.0f, 0.0f); 
    
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
    gl.glEnableClientState(GL10.GL_NORMAL_ARRAY); 
    
    gl.glActiveTexture(GL10.GL_TEXTURE0); 
    gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID); 
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, 
         GL10.GL_REPEAT); 
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, 
         GL10.GL_REPEAT); 
    
    mSquare.draw(gl); 
    
  2. Square類是非常簡單的:

類方形{ INT點= 1;

int x1 = -2; 
int x2 = 2; 
int y1 = -2; 
int y2 = 2; 
int W = x2 - x1; 
int H = y2 - y1; 
int dx = 30; 
int dy = 30; 


private float vertices2[] = new float[3*4*dx * dy]; 
private float normals[] = new float[3*4*dx * dy]; 

float one = 1.0f; 
float texCoords[] = new float[3*4*dx * dy]; 

private FloatBuffer vertexbuffer1 = null; 
private FloatBuffer mTextureBuffer = null; 
private FloatBuffer mNormalBuffer = null; 

private void initVertexes() { 
    float incW = W/(float)dx; 
    float incH = H/(float)dy; 
    int i = 0; 
    int j = 0; 
    int k = 0; 

    for(float y = y2; y >= (y1 + incH) ; y -= incH) 
     for(float x = x1; x<= (x2 - incW); x += incW) { 

      vertices2[i++] = x ; 
      vertices2[i++] = y - incH; 
      vertices2[i++] = -point; 

      texCoords[j++] = (x2 + x)/(4f*W); 
      texCoords[j++] = (y2 + y -incH)/(4f*H); 

      normals[k++] = 0; 
      normals[k++] = 0; 
      normals[k++] = 1; 

      vertices2[i++] = x ; 
      vertices2[i++] = y ; 
      vertices2[i++] = -point; 

      texCoords[j++] = (x2 + x)/(4f*W); 
      texCoords[j++] = (y2 + y)/(4f*H); 

      normals[k++] = 0; 
      normals[k++] = 0; 
      normals[k++] = 1; 

      vertices2[i++] = x + incW; 
      vertices2[i++] = y - incH ; 
      vertices2[i++] = -point; 

      texCoords[j++] = (x2 + x + incW)/(4f*W); 
      texCoords[j++] = (y2 + y - incH)/(4f*H); 

      normals[k++] = 0; 
      normals[k++] = 0; 
      normals[k++] = 1; 

      vertices2[i++] = x + incW; 
      vertices2[i++] = y ; 
      vertices2[i++] = -point; 

      texCoords[j++] = (x2 + x + incW)/(4f*W); 
      texCoords[j++] = (y2 + y)/(4f*H); 

      normals[k++] = 0; 
      normals[k++] = 0; 
      normals[k++] = 1; 
     } 

} 


public Square() { 

    initVertexes(); 

    ByteBuffer vbb1 = ByteBuffer.allocateDirect(vertices2.length * 4); 
    vbb1.order(ByteOrder.nativeOrder()); 
    vertexbuffer1 = vbb1.asFloatBuffer(); 
    vertexbuffer1.put(vertices2); 
    vertexbuffer1.position(0); 

    ByteBuffer txtb1 = ByteBuffer.allocateDirect(texCoords.length * 4); 
    txtb1.order(ByteOrder.nativeOrder()); 
    mTextureBuffer = txtb1.asFloatBuffer(); 
    mTextureBuffer.put(texCoords); 
    mTextureBuffer.position(0); 

    ByteBuffer nor = ByteBuffer.allocateDirect(normals.length * 4); 
    nor.order(ByteOrder.nativeOrder()); 
    mNormalBuffer = nor.asFloatBuffer(); 
    mNormalBuffer.put(normals); 
    mNormalBuffer.position(0); 

}

public void draw(GL10 gl) { 

    gl.glEnable(GL10.GL_TEXTURE_2D); 
    gl.glNormalPointer(3, 0, mNormalBuffer); 
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer); 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexbuffer1); 
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 2*2*dx*dy); 

} 

}

回答

0

您是否驗證了你的光照亮,當你不設置GL_SPOT_CUTOFFGL_SPOT_EXPONENT廣場?

從您定位燈光和相機的方式來看,我認爲您試圖從觀衆的位置照亮聚光燈。有一點需要注意的是位置/定向照明參數(即GL_POSITIONGL_SPOT_DIRECTION)被存儲在眼睛空間中,這意味着它們在您撥打glLightfv時被模型視圖矩陣的值轉換,轉換後的值將被存儲,而不是您指定的值。看起來您在繪製之前不會設置模型視圖矩陣,因此光線可能不在您認爲的位置。儘管如此,如果您試圖照亮聚光燈,您可能最好使用另一個紋理應用聚光燈形狀,而不是使用固定功能光照計算,而這種計算依賴於幾何形狀的鑲嵌細分並且不會給您足夠的靈活性來定義突出顯示的形狀。您選擇的截止點非常小,可能會導致聚光燈完全錯過頂點。

+0

Thx爲您的提示。我簡化了這個問題,這次我試圖聚焦一個沒有紋理的正方形,只有着色。我認爲在這裏我有問題。我根本沒有聚光燈。我禁用GL_SPOT_CUTOFF和指數,仍然是相同的情況。你能澄清我嗎?你想說什麼......「被存儲在眼睛空間中,這意味着它們在您調用glLightfv時被模型視圖矩陣的值轉換......」。 ?無論如何,許多thx爲您的答覆! – EddieS 2010-07-04 17:00:58

+0

OpenGL ES在使用模型視圖矩陣對其進行轉換後(但在應用投影矩陣之前)點亮頂點。光線位置/方向需要在同一個座標空間中,但是在您調用glLightfv時,而不是每次使用光線時,乘以模型視圖矩陣都會執行一次。你有沒有嘗試在調用glLightfv之前像設置繪圖一樣設置模型視圖矩陣? 您可能會發現OpenGL FAQ(http://www.opengl.org/resources/faq/technical/)的第8,9和18節有助於理解所有這些轉換如何工作。 – Pivot 2010-07-04 18:57:00

+0

謝謝Pivot! – EddieS 2010-07-04 19:32:26