Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have made many Android applications and games in Java before, however I am very new to OpenGL ES. Using guides online, I have made simple things in OpenGL ES, including a simple triangle and a cube.

I would like to make a 2D game with OpenGL ES, but what I've been doing isn't working quite so well, as the images I draw aren't to scale, and no matter what guide I use, the image is always choppy and not the right size (I'm debugging on my Nexus S).

How can I draw crisp, HD images to the screen with GL ES?

Here is an example of what happens when I try to do it: enter image description here

And the actual image: enter image description here

Here is how my texture is created:

//get id
int id = -1;
gl.glGenTextures(1, texture, 0);
id = texture[0];

//get bitmap
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ball);

//parameters
gl.glBindTexture(GL10.GL_TEXTURE_2D, id);
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_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
        GL10.GL_CLAMP_TO_EDGE);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,
        GL10.GL_REPLACE);

GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

//crop image
mCropWorkspace[0] = 0;
mCropWorkspace[1] = height;
mCropWorkspace[2] = width;
mCropWorkspace[3] = -height;
((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, mCropWorkspace, 0);
share|improve this question
1  
Hi and welcome to the site! As you can see in the FAQ this is a Q&A site, not a discussion forum. As such you tend to get the best, most-focused answers from our users if you only post a single actual question per "topic." I've edited your question to remove your query about creating a game loop as it's largely unrelated to your question about crisp sprites -- you should instead hit the "Ask A Question" button to create a new topic about your game loop question. –  Josh Petrie Jan 20 '12 at 22:52
1  
That said, it would probably help if you provided at least a brief overview of the method by which you're currently rendering images to the screen and/or a screenshot of the artifacts you're seeing. –  Josh Petrie Jan 20 '12 at 22:53
    
@Josh: Thank you for your insightful words. I have added images of the problem, and how I'm currently creating textures. –  Qasim Jan 20 '12 at 22:59
    
I suggest showing the code which actually draws the texture. What you've got doesn't look unreasonable, but it's not the whole story. –  Kevin Reid Jan 21 '12 at 4:08

3 Answers 3

There is a couple of parameters you need to set on the texture, the first one is to tell OpenGL what to do if the texture need to be shrunk or magnified to match the rendered image. For a blurred image you should utilize another one. For a crisp and clean rendering image then other. For more details, followings #

  • GL_TEXTURE_MAG_FILTER : If the texture is smaller it needs to be magnified that is done with the magnification function
  • GL_TEXTURE_MIN_FILTER : If the texture needs to be scaled down using the minification function
  • GL_LINEAR : For a blurred image
  • GL_NEAREST : For a crisp and clean rendering image

Utilize GL_NEAREST

gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

share|improve this answer

You need to change the GL_TEXTURE_MAG_FILTER to GL_LINEAR.

gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

share|improve this answer

What you want to achieve is called "pixel perfect texture mapping".

You need to call

gl.glOrthof(0, displayWidth, 0 , displayHeight, 0.0f, 100.0f);

in your initialization function. This will set 1 OpenGL unit equals to 1 pixel. OpenGL coordinates will be (0,0) for the top-left angle and (width, height) for the bottom-right angle.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.