gpt4 book ai didi

java - OpenGL ES 纹理无法绘制

转载 作者:行者123 更新时间:2023-12-02 08:05:20 25 4
gpt4 key购买 nike

我遵循了有关如何在 OpenGL 中显示图像的教程,我首先必须制作一个正方形,然后对其进行纹理处理。我已经尝试过 gl.glColor4f(0f,0f,0f,1f); 并且有效,我可以看到正方形。但当我尝试对其进行纹理处理时,屏幕只是保持白色。我做错了什么?

这是带有 gl.glColor4f(0f,0f,0f,1f); 的屏幕:

image description

如果我应用纹理(使用教程建议的自定义纹理类),屏幕将完全变白。

这是我的代码:

Square.java:

package com.chrypthic.android.reference;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;

public class Square
{
final int VERTEX_SIZE = (2+2) *4;
FloatBuffer vertices;
ShortBuffer indices;

Texture texture;

GL10 gl;
Context c;

public Square(GL10 gl, Context context)
{
this.gl = gl;
this.c = context;

ByteBuffer byteBuffer = ByteBuffer.allocateDirect(VERTEX_SIZE * 4);
byteBuffer.order(ByteOrder.nativeOrder());
vertices = byteBuffer.asFloatBuffer();
vertices.put(new float[]{
10.0f, 10.0f, 0.0f, 0.0f, //bl
160.0f, 10.0f, 1.0f, 0.0f, //br
160.0f, 160.0f, 1.0f, 1.0f, //tr
10.0f, 160.0f, 1.0f, 0.0f, //tl
});
vertices.flip();

byteBuffer = ByteBuffer.allocateDirect(VERTEX_SIZE * 4);
byteBuffer.order(ByteOrder.nativeOrder());
indices = byteBuffer.asShortBuffer();
indices.put(new short[]{
0, 1, 2, 2, 3, 0
});
indices.flip();

texture = new Texture("image/picture.png", c, gl);
}

public void draw()
{
gl.glEnable(GL10.GL_TEXTURE_2D);
texture.bind();
//gl.glColor4f(0f, 0f, 0f, 1f);

gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

vertices.position(0);
gl.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);
vertices.position(2);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);

gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, indices);
}
}

纹理.java

package com.chrypthic.android.reference;

import java.io.InputStream;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;

public class Texture {

String texturePath;
Context context;
GL10 gl;

int textureId;
int minFilter;
int magFilter;
Bitmap texture;

public Texture(String texturePath, Context context, GL10 gl)
{
this.gl = gl;
this.texturePath = texturePath;
this.context = context;
}

public void load()
{
try{
AssetManager assetManager = context.getAssets();
InputStream is = assetManager.open(texturePath);
texture = BitmapFactory.decodeStream(is);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
setFilters(GL10.GL_NEAREST, GL10.GL_NEAREST);
gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
}catch(Exception e)
{
e.printStackTrace();
}
}

public void reload()
{
load();
bind();
setFilters(minFilter, magFilter);
gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
}

public void setFilters(int minFilter, int magFilter)
{
this.minFilter = minFilter;
this.magFilter = magFilter;
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, minFilter);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, magFilter);
}

public void bind()
{
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
}

public void dispose()
{
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
int[] textureIds = {textureId};
gl.glDeleteTextures(1, textureIds, 0);
}

}

OpenGLRenderer.java

package com.chrypthic.android.reference;

import java.util.Random;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;

public class OpenGLRenderer implements Renderer
{
Random rand = new Random();
int mWidth = 0;
int mHeight = 0;

Context c;

Square square;

public OpenGLRenderer(Context c)
{
this.c = c;
}

@Override
public void onDrawFrame(GL10 gl)
{
gl.glClearColor(1, 1, 1, 1);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glViewport(0, 0, mWidth, mHeight);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, mWidth, 0, mHeight, -1, 1);

if(square != null)square.draw();
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
mWidth = width;
mHeight = height;
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
square = new Square(gl, c);
}

}

我希望您能帮助我,如果您需要更多信息,请告诉我。提前致谢。我没有将白色屏幕作为屏幕截图发布,因为对于 stackoverflow 的白色背景来说,这似乎毫无意义。

最佳答案

你永远不会创建纹理!

public void load()
{
try{
int []texture = new int[1];
gl.glGenTextures(1, texture,0);//the missing method
textureId = texture[0];
//now you can call
AssetManager assetManager = context.getAssets();
InputStream is = assetManager.open(texturePath);
texture = BitmapFactory.decodeStream(is);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);/*this fails
if textureId is a 'random' number. You must/should generate a valid id. */
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
setFilters(GL10.GL_NEAREST, GL10.GL_NEAREST);
gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);

它与您的 dispose() 方法完全相反。

关于java - OpenGL ES 纹理无法绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8273676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com