Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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  
  [SOLVED] Problem with OpenGL via LWJGL  (Read 724 times)
0 Members and 1 Guest are viewing this topic.
Offline paulo.carabuena

Senior Newbie





« Posted 2011-12-06 20:34:19 »

hi guys.. first and foremost.. this is my first post, idk if this is the right section.. but here it goes..

i'm working on a project.. it's a game programming library, focusing on portability in the java environment.. i am currently working on the display function.. as a system dependency, J2SE can use OpenGL for its graphics functionalities such as display or for rendering, etc..

i'm not doing things from scratch, my adviser (now on temporary leave) told me to look for some open source libraries in the Internet to lighten my load.. I found LWJGL which provides classes and interface for OpenGL.. so i use it for the J2SE OpenGL module of my library..

the program i've written so far works perfectly for basic polygon rendering in both OpenGL and native hardware accelerated graphics.. however, when I proceed to image rendering there seems to be some problem..



the image above shows the output after i run the OpenGL rendering program..

here is the code for initializing LWJGL

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  
private void initGL() {
      GL11.glEnable(GL11.GL_TEXTURE_2D);
      GL11.glDisable(GL11.GL_DEPTH_TEST);
 
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glLoadIdentity();
 
      GL11.glOrtho(0, this.size.width, this.size.height, 0, -1, 1);
 
      GL11.glEnable(GL11.GL_BLEND);
      GL11.glClearColor(0, 0, 0, 0);
 
      GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
   }
 
   private void initGraphics() {
      try {
         DisplayMode best = (this.isFullscreen) ?
               this.getBestDisplayMode(size) : new DisplayMode(
                     this.size.width, this.size.height);
 
         if(best == null) {
            throw new Exception();
         }
 
         org.lwjgl.opengl.Display.setDisplayMode(best);
      } catch(Exception e) {
 
      }
 
      try {
         org.lwjgl.opengl.Display.setTitle(this.title);
         org.lwjgl.opengl.Display.setFullscreen(this.isFullscreen);
         org.lwjgl.opengl.Display.setVSyncEnabled(this.usingVsync);
         org.lwjgl.opengl.Display.create();
      } catch(Exception e) {
 
      }
   }


and here is the code for rendering the image:

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  
public boolean drawImage(Image img,
                      int x, int y, int width, int height,
                      ImageObserver observer) {
      startPainting();
 
 
      GL11.glPushMatrix();
 
       Texture texture = textureLoader.getTexture((BufferedImage) img);
      texture.bind();
 
      GL11.glTranslatef(x, y, 0);
 
      GL11.glBegin(GL11.GL_QUADS);
         GL11.glTexCoord2f(0, 0);
         GL11.glVertex2f(0, 0);
 
         GL11.glTexCoord2f(0, texture.getHeight());
         GL11.glVertex2f(0, height);
 
         GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
         GL11.glVertex2f(width, height);
 
         GL11.glTexCoord2f(texture.getWidth(), 0);
         GL11.glVertex2f(width, 0);
      GL11.glEnd();
 
      GL11.glPopMatrix();
 
 
      endPainting();
 
       return true;
    }


oh btw here is how i called the rendering routine:

1  
2  
3  
4  
5  
public void render(Graphics g) {
      g.setColor(Color.RED);
      g.fillRect(0, 0, 640, 480);
      image.render(g, 0, 0);
   }


any help to make the OpenGL rendered image look exactly like the image below? thanks in advance Smiley

Offline theagentd
« Reply #1 - Posted 2011-12-06 20:39:42 »

You're drawing a red quad for the background before calling drawImage, right? Your call to glColor is still in effect. Add this in your drawImage() function (just after GL11.glTranslatef(x, y, 0); is fine):
1  
GL11.glColor3f(1, 1, 1);

When with drawing textures, the texture sample is multiplied by the OpenGL color you've specified.

Myomyomyo.
Offline paulo.carabuena

Senior Newbie





« Reply #2 - Posted 2011-12-06 20:48:12 »

it's just as you said, i was thinking of the same about the color but idk what to do, i thought i need GL.glFlush(), silly me  Roll Eyes .. but anyway, it works now  Grin thanks alot  Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (84 views)
2013-05-17 21:29:12

alaslipknot (92 views)
2013-05-16 21:24:48

gouessej (123 views)
2013-05-16 00:53:38

gouessej (117 views)
2013-05-16 00:17:58

theagentd (127 views)
2013-05-15 15:01:13

theagentd (114 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (187 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.194 seconds with 21 queries.