Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Why I draw an png image using glDrawPixels() can't work well?  (Read 2332 times)
0 Members and 1 Guest are viewing this topic.
Offline wdl7770016

Senior Newbie





« Posted 2012-08-05 15:26:28 »

now I first use the Slick-Util which lwjgl recommand to load my game data(now just some image^_^)
the code like these:
1  
2  
3  
4  
5  
6  
7  
8  
png = new PNGImageData();
      try {
      png.loadImage(ResourceLoader.getResourceAsStream("src/img/logo.png"));
      System.out.println(png.isRGB());
      }
      catch (IOException e) {
         e.getStackTrace();
      }

then I draw image like these:
1  
2  
3  
GL14.glWindowPos2d(20, 20);
      //GL11.glBitmap(142, 142, 0, 0, 0, 0, this.byteBuffer);
     GL11.glDrawPixels(142, 142, GL11.GL_RGB, GL11.GL_BYTE, png.getImageBufferData());

but the result is let me sad !! image can't display correct
!!

I don't know why? how about you to load image and draw it in openGL?
(what I sad about is 2d Image  not Texture, but I can't find any tutorial for 2D openGL)
Thx for help^_^
Offline wdl7770016

Senior Newbie





« Reply #1 - Posted 2012-08-06 02:53:57 »

is there anyone can help me? I  ???really need help!!
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2012-08-06 03:29:06 »

Try changing it to GL_RGBA and GL_UNSIGNED_BYTE

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wdl7770016

Senior Newbie





« Reply #3 - Posted 2012-08-06 04:03:32 »

Sorry I just try it
but the image is still error display...
how do you to draw an 2d img in onpenGL?
use Texture's bind? or GL11.glDrawPixels()?....
I want to make a 2d game using OpenGL but I just an Newer for it
so I need your help..
Thx
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-08-06 06:16:25 »

Images are drawn using textures. Bind the texture and draw a quad where you want the texture to show up. Don't forget to set the right texture coordinates (glTexCoords)

Offline wdl7770016

Senior Newbie





« Reply #5 - Posted 2012-08-06 08:18:10 »

uh Thx
but can we use use glDrawPixels() to direct draw image on screen?
I want to know why glDrawPixels doesn't work well
Offline wdl7770016

Senior Newbie





« Reply #6 - Posted 2012-08-06 13:30:50 »

If u use drawPixels please tell me why I can't render image rightly。。。
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2012-08-06 13:34:44 »

Merged topics. Pointing

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline gimbal

JGO Knight


Medals: 25



« Reply #8 - Posted 2012-08-06 14:12:34 »

uh Thx
but can we use use glDrawPixels() to direct draw image on screen?
I want to know why glDrawPixels doesn't work well

Might be worth mentioning what exactly is going wrong. Nothing appears? Crash? You see something but the pixels are garbage?
Offline davedes
« Reply #9 - Posted 2012-08-06 14:16:07 »

glDrawPixels requires passing around byte data from CPU to GPU, which may be potentially very huge and could potentially stall the pipeline. glTexImage2D will "store" the byte data in a texture on the GPU, allowing you to reuse it very quickly.

If you're trying to draw a sprite, load it with SlickUtil's TextureLoader (which allows for more formats than just PNG). Then set the projection matrix to orthographic and render a quad.

If that doesn't make sense, start with Google, the LWJGL wiki, and a modern OpenGL book. Smiley


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wdl7770016

Senior Newbie





« Reply #10 - Posted 2012-08-07 05:18:58 »

uh Thx
but can we use use glDrawPixels() to direct draw image on screen?
I want to know why glDrawPixels doesn't work well

Might be worth mentioning what exactly is going wrong. Nothing appears? Crash? You see something but the pixels are garbage?

I have see something but pixels are garbage....
It's my code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
import java.io.IOException;
import java.nio.ByteBuffer;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL14;
import org.newdawn.slick.opengl.PNGImageData;
import org.newdawn.slick.util.ResourceLoader;

public class GameMain2 {
   private ByteBuffer byteBuffer;
   /**
    *
    * @param args
    */

   public static void main(String[] args) {
      new GameMain2().launch();
   }

   public void launch() {
      try {
         Display.setDisplayMode(new DisplayMode(800, 600));
         Display.setTitle("YourGame");
         Display.create();
         this.initGL();
      }
      catch (LWJGLException e) {
         e.getStackTrace();
      }

      while (!Display.isCloseRequested()) {
         this.render();
         Display.update();
         Display.sync(60);
      }
      Display.destroy();
   }

   private void initGL() {
      GL11.glClearColor(0, 0, 0, 0);
     
      GL11.glOrtho(0, 800, 0, 600, -1, 1);

      //GL11.glEnable(GL11.GL_TEXTURE_2D);
     
      try {
         PNGImageData png = new PNGImageData();
         png.loadImage(ResourceLoader.getResourceAsStream("src/img/logo.png"));
         this.byteBuffer = png.getImageBufferData();
      }
      catch (IOException e) {
         e.getStackTrace();
      }
   }
   
   private void render() {
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
      //GL11.glColor3f(0, 1, 1);
     GL14.glWindowPos2d(20, 20);
      //GL11.glBitmap(142, 142, 0, 0, 0, 0, this.byteBuffer);
     GL11.glDrawPixels(142, 142, GL11.GL_RGB, GL11.GL_BYTE, this.byteBuffer);
   }
}


this is the image what I want to display(I don't know why img can't display, you can click it to display) :

and this is the garbage pixels what display:

Did you know where has wrong? I feel so anxious now....
Offline gimbal

JGO Knight


Medals: 25



« Reply #11 - Posted 2012-08-07 07:43:20 »

Weird. I would expect something like this when you have manual loading code, but you're just using standard Slick/LWJGL stuff.

I've managed to garbage up many an image in my day and it was usually because I fudged up the bit-order of the color components. The only thing I can come up with is to use GL_RGBA in stead of GL_RGB since its going to be 32 bits data with an alpha channel.
Offline wdl7770016

Senior Newbie





« Reply #12 - Posted 2012-08-07 08:27:03 »

so is this error display cause by sllick-util’s internal mechanism? I just abuse the class and the methods in slick-util?
Offline gimbal

JGO Knight


Medals: 25



« Reply #13 - Posted 2012-08-07 08:30:45 »

So my suggestion didn't change anything?

I'm not too familiar with Slick but I like to believe that you are not the first person to use that code so you're not going to be the one that discovers that it is in fact broken. Simple reasoning.
Offline wdl7770016

Senior Newbie





« Reply #14 - Posted 2012-08-07 10:18:51 »

So my suggestion didn't change anything?

I'm not too familiar with Slick but I like to believe that you are not the first person to use that code so you're not going to be the one that discovers that it is in fact broken. Simple reasoning.
sor my English is not good
so may always miss what you say...
but now just thought that there are glDrawPixels method in opengl  but I had to use the Texture bind to make a 2D game it's feel so shit。。(may because I just a newer for opengl and I always think that use Texture may more waste performance because we should draw a rec and bind texture, but drawPixels just need draw it, right?)
Offline davedes
« Reply #15 - Posted 2012-08-07 11:20:10 »

Any issues with the image is likely because of the format you are specifying -- it should be an unsigned byte in either RGB, RGBA, LUMINANCE, or LUMINANCE_ALPHA depending on the output of the PNG decoder.

Read my earlier post.

Offline theagentd
« Reply #16 - Posted 2012-08-07 11:40:30 »

The images aren't loading, but I think I know what you're missing: alignment. OpenGL works with pixel alignments of 4 by default. Your image resolution is not dividable by 4. (142 / 4 = 35.5).

Add this in initGL():

1  
2  
3  
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);



Textures are faster than glDrawPixels()! They are more difficult than glDrawPixels(), but they are faster because GPUs are made for drawing triangles with textures (in 3D games) and the texture is stored on the GPU's VRAM, so it does not have to be uploaded every frame.

Myomyomyo.
Offline wdl7770016

Senior Newbie





« Reply #17 - Posted 2012-08-07 13:14:33 »

The images aren't loading, but I think I know what you're missing: alignment. OpenGL works with pixel alignments of 4 by default. Your image resolution is not dividable by 4. (142 / 4 = 35.5).

Add this in initGL():

1  
2  
3  
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);



Textures are faster than glDrawPixels()! They are more difficult than glDrawPixels(), but they are faster because GPUs are made for drawing triangles with textures (in 3D games) and the texture is stored on the GPU's VRAM, so it does not have to be uploaded every frame.

thanks for your help
but it's still no work even if I add these code 
So now I don't want to think it until one day I know the cause  Roll Eyes
Offline davedes
« Reply #18 - Posted 2012-08-07 13:20:28 »

... If you had taken my advice ...

LWJGL Wiki - Loading Images with Slick-Util

Maybe you should start smaller -- learn the core concepts of programming, objects, etc. and try to make a simple Pong clone in LibGDX, Slick2D or another simple library. Smiley

Offline wdl7770016

Senior Newbie





« Reply #19 - Posted 2012-08-07 13:27:34 »

... If you had taken my advice ...

LWJGL Wiki - Loading Images with Slick-Util

Maybe you should start smaller -- learn the core concepts of programming, objects, etc. and try to make a simple Pong clone in LibGDX, Slick2D or another simple library. Smiley

Thx for your advice
 I will try Cheesy
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (38 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (31 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (53 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!