Android opengl text 출력 관련 자료 찾다가 코드를 이곳 저곳에서 찾았습니다...
그것들을 가지고 class를 만들어 봤는 데...작동 하지 않습니다. 뭐가 문제 인가요?
일단 코드 입니다.
public class Render implements GLSurfaceView.Renderer {
private Context m_context = null;
OpenglText ot;
public Render(Context co){
this.m_context = co;
ot = new OpenglText();
}
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
ot.setPosition(50.0f, 40.0f, 70.0f);
ot.setObjectSize(50.0f, 50.0f);
ot.setTexturePosition();
ot.CreateTextBitMap(gl,"Hello");
ot.drawTexture(gl);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
if(height == 0){
height = 1;
}
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);
// gl.glOrthof(0, width, 0, height, 1, -1 );
//gl.glOrthof(0, 480, 0, 800, 1, -1);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO Auto-generated method stub
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_BLEND);
//gl.glDisable(GL10.GL_DEPTH_TEST);
}
}
public class OpenglText {
protected FloatBuffer m_vertexBuffer;
protected FloatBuffer m_textureBuffer;
protected float vertices[] = null;
protected float texture[] = null;
float Ov, Oh;
float TexXS, TexXL, TexYS ,TexYL;
int textureId;
protected float x,y, z;
public float r = 1.0f;
public float g = 1.0f;
public float b = 1.0f;
public float a = 1.0f;
public OpenglText(){}
public void setPosition(float x, float y, float z){
this.x = x;
this.y = y;
this.z = z;
}
public void setObjectSize(float v, float h){
this.Objectvertice = v;
this.Objectheight = h;
}
public void setTexturePosition(){
float TexXS = 0.0f;
float TexXL = 1.0f;
float TexYS = 0.0f;
float TexYL = 1.0f;
}
public void CreateTextBitMap(GL10 gl ,String str){
int mTextures[] = new int[1];
Bitmap bitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
bitmap.eraseColor(0);
// get a background image from resources
// note the image format must match the bitmap format
// Drawable background = context.getResources().getDrawable(R.drawable.ic_launcher);
// background.setBounds(0, 0, 256, 256);
// background.draw(canvas); // draw the background to our bitmap
Paint textPaint = new Paint();
textPaint.setTextSize(32);
textPaint.setAntiAlias(true);
textPaint.setARGB(0xff, 0x00, 0x00, 0x00);
canvas.drawText("Hello World", 16,112, textPaint);
gl.glGenTextures(1, mTextures, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[0]);
this.textureId = mTextures[0];
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_REPEAT);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}
public void drawTexture(GL10 gl){
vertices = new float[]{-this.Ov, -this.Oh, 0.0f,
this.Ov, -this.Oh, 0.0f,
this.Ov, this.Oh, 0.0f,
-this.Ov, this.Oh, 0.0f};
this.texture = new float[]{ TexXS, TexYL,
TexXL, TexYL,
TexXL, TexYS,
TexXS, TexYS};
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
m_vertexBuffer = byteBuf.asFloatBuffer();
m_vertexBuffer.put(vertices);
m_vertexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
m_textureBuffer = byteBuf.asFloatBuffer();
m_textureBuffer.put(texture);
m_textureBuffer.position(0);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_DST_COLOR, GL10.GL_ZERO);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL10.GL_ALPHA_TEST);
gl.glAlphaFunc(GL10.GL_NOTEQUAL, 0.0f);
gl.glColor4f(r, g, b, a);
gl.glBindTexture(GL10.GL_TEXTURE_2D, this.textureId);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glFrontFace(GL10.GL_CW);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, m_vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, m_textureBuffer);
gl.glTranslatef(this.x, this.y , -z);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, vertices.length/3);
gl.glTranslatef(-this.x, -this.y , +z);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisable(GL10.GL_BLEND);
gl.glDisable(GL10.GL_ALPHA_TEST);
}
}
public class Contoller extends GLSurfaceView {
Render render;
Context m_co;
public void setRenderer(Renderer renderer) {
// TODO Auto-generated method stub
super.setRenderer(renderer);
this.render = (Render)renderer;
}
public Contoller(Context co){
super(co);
m_co = co;
}
}
public class MainActivity extends Activity {
private Render m_gsView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Contoller Game = new Contoller(this.getApplicationContext());
m_gsView = new Render(this.getApplicationContext());
Game.setRenderer(m_gsView);
setContentView(Game);
}
}
뭐가 잘못 됐는 지 좀 가르쳐 주시면 고맙겠습니다.