渲染图像的纹理成在OpenGL Android的一个立方体贴图 [英] Rendering an image texture into a cubemap in openGL android

查看:378
本文介绍了渲染图像的纹理成在OpenGL Android的一个立方体贴图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位图我的设备(这是一个6x1的立方体贴图),我想渲染在立方体的所有面

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;
}

这就是我在我的渲染器onSurfaceCreated()这样做的:

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.

任何指针或帮助将是AP preciated。

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;
}

片段着色器:

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

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

但我得到同样的黑屏。

But I get the same black screen.

推荐答案

一个立方体贴图是纹理谁的形象重新present立方体的面孔。在纹理坐标为立方体贴图是指向你要使用的颜色立方体的中心的矢量方向

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.

您正在尝试使用一个普通的旧的2D纹理坐标,有可能加入到沉默编译器的第三组分。您必须提供的路线的,而不是二维坐标。你可以从你的位置的顶点着色器生成它们。但需要知道什么空间。负责是的,你没有告诉我。所以我不能告诉你如何做到这一点。

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.

无论如何,顶点着色器需要被提供用于纹理坐标三维方向。它应该既可以从VS输入产生或传递。

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.

请注意,你的程序可能有其他问题。但是,这是可以从你的code推导出来的问题。

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

这篇关于渲染图像的纹理成在OpenGL Android的一个立方体贴图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆