OpenGL ES的质地不会画 [英] OpenGL ES texture won't draw

查看:103
本文介绍了OpenGL ES的质地不会画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照如何在OpenGL的显示图像上的教程,和我第一次不得不作出一个正方形,然后纹理它。我曾尝试 gl.glColor4f(0F,0F,0F,1F); 和工作,我可以看到广场。但后来当我尝试它纹理,屏幕只是停留白色。我做错了什么?

I have followed a tutorial on how to display an image in OpenGL, and I first had to make a square and then texture it. I have tried gl.glColor4f(0f,0f,0f,1f); and that works, I can see the square. But then when I try to texture it, the screen just stays white. What did I do wrong?

下面是屏幕上的 gl.glColor4f(0F,0F,0F,1F);

如果我申请纹理(使用自定义纹理类的教程建议),屏幕完全是白色的。

If I apply the texture (using the custom Texture class that the tutorial suggested), the screen is completely white.

下面是我的code:

Square.java:

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

Texture.java

Texture.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

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

}

我希望你能帮助我,如果你需要更多的信息,只是告诉我。
提前致谢。余did't发布白色屏幕截图,因为它似乎与白色背景计算器有一种毫无意义的。

I hope you can help me, if you need even more information just tell me. Thanks in advance. I did't post the white screen as a screenshot, because it seems kind of pointless with the white background that stackoverflow have.

推荐答案

您从未创建质感!

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()方法相反。

这篇关于OpenGL ES的质地不会画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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