Rendering an image texture into a cubemap in openGL android

心已入冬 提交于 2019-12-11 11:10:04

问题


I have a bitmap on my device(which is a 6x1 cubemap), which I want to render on all the faces of the cube

 InputStream is = getContext().getResources().openRawResource(R.raw.photo);
 Bitmap bitmap = BitmapFactory.decodeStream(is);
 int bytes = bitmap.getByteCount();
 ByteBuffer pixels = ByteBuffer.allocate(bytes);
 bitmap.copyPixelsToBuffer(pixels);

Here is my vertex shader:

uniform mat4 uMVPMatrix;
uniform mat4 uSTMatrix;

attribute vec4 aPosition;
attribute vec4 aTextureCoord;
attribute vec4 aColor;
varying vec2 vTextureCoord;
varying vec4 vColor;

void main() {
  gl_Position = uMVPMatrix * aPosition;
  vTextureCoord = (uSTMatrix * aTextureCoord).xy;
  vColor = aColor;
}

Here is my fragment shader:

precision mediump float;

varying vec2 vTextureCoord;
varying vec4 vColor;

uniform samplerCube sTexture;

void main() {
    gl_FragColor = textureCube(sTexture, vec3(vTextureCoord, 1.0)) * vColor;
}

And here is what I am doing in my renderer in onSurfaceCreated():

GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
int[] texIds = new int[1];
GLES20.glGenTextures(1, texIds, 0);
m360PhotoTextureId = texIds[0];

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(
    GLES20.GL_TEXTURE_CUBE_MAP,
    mTextureId);

for (int i = 0 ; i < 6 ; i++ ){
  pixels.position(0);
  GLES20.glTexImage2D(
      GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
      0,
      GLES20.GL_RGBA,
      1,
      1,
      0,
      GLES20.GL_RGBA,
      GLES20.GL_UNSIGNED_BYTE,
      pixels);
}

GLES20.glTexParameteri(
        GLES20.GL_TEXTURE_CUBE_MAP,
        GLES20.GL_TEXTURE_MIN_FILTER,
        GLES20.GL_LINEAR);
GLES20.glTexParameteri(
        GLES20.GL_TEXTURE_CUBE_MAP,
        GLES20.GL_TEXTURE_MAG_FILTER,
        GLES20.GL_LINEAR);
GLES20.glTexParameteri(
        GLES20.GL_TEXTURE_CUBE_MAP,
        GLES20.GL_TEXTURE_WRAP_S,
        GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(
        GLES20.GL_TEXTURE_CUBE_MAP,
        GLES20.GL_TEXTURE_WRAP_T,
        GLES20.GL_CLAMP_TO_EDGE);
GLES20.glBindTexture(GLES20.GL_TEXTURE_CUBE_MAP, 0);

All I see is a black screen in my texture view, when I expect to see a photo(in the pixels) being rendered on all the faces of the cube.

Any pointers or help would be appreciated.

I tried: Vertex shader:

uniform mat4 uMVPMatrix;
attribute vec4 aPosition;
varying vec3 vTextureCoord;

void main() {
  gl_Position = uMVPMatrix * aPosition;
  vTextureCoord = aPosition.xyz;
}

Fragment shader:

precision mediump float;
varying vec3 vTextureCoord;
uniform samplerCube sTexture;

void main() {
    gl_FragColor = textureCube(sTexture, vTextureCoord);
}

But I get the same black screen.


回答1:


A cubemap texture is a texture who's images represent the faces of a cube. The "texture coordinate" for a cubemap texture is the vector direction from the center of the cube which points to the color you want to use.

You are trying to use a regular old 2D texture coordinate, with a third component probably added to silence the compiler. You must provide directions, not 2D coordinates. You can generate them in the vertex shader from your position. But that requires knowing what space aPosition is, and you didn't tell me. So I can't show you how to do that.

Regardless, the vertex shader needs to be providing a 3D direction for the texture coordinate. It should either be generated or passed from a VS input.

Note that your program may have other problems. But this is the problem that can be deduced from your code.



来源:https://stackoverflow.com/questions/35391371/rendering-an-image-texture-into-a-cubemap-in-opengl-android

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