Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
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  
  Green overlay when drawing textures on Quads [Solved]  (Read 1303 times)
0 Members and 1 Guest are viewing this topic.
Offline Zerali

Senior Newbie





« Posted 2012-04-24 08:26:24 »

Hello everyone, this is my first post so bear with me. I have been teaching my self Java and Game development for the past few months. I messed with Java Awt library a bit but I have now started to learn LWJGL  Grin. However I am having some trouble with the code below. It does what I want, which is drawing the texture onto the Quad but it has a weird green overlay. I noticed that this was caused when I added the following statement to the initialization code. I am using Slick2Ds Texture and TextureLoader class, here is my code below...

Line that adds the green Overlay
1  
glEnable(GL_TEXTURE_2D);


Initialization code which is called in Main.java
1  
2  
3  
4  
5  
6  
7  
public void initGL() {
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(0, 800, 0, 500, 1, -1);
      glMatrixMode(GL_MODELVIEW);
      glEnable(GL_TEXTURE_2D);
   }


Testing drawing textures on Quads, called in my Level Entity class
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  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
public void drawLevel(int zoom) {
      int w = 8*zoom;
     
//      for(int x = 0; x < 800; x+=w) {
//         for(int y = 0; y < 500; y+=w) {
//            
//            glColor3f(256, 256, 256);
//            glPushMatrix();
//               glBegin(GL11.GL_QUADS);
//                  glVertex2f(x-4,y-4);
//                  glVertex2f(x+4,y-4);
//                  glVertex2f(x+4,y+4);
//                  glVertex2f(x-4,y+4);
//               glEnd();
//            glPopMatrix();
//         }
//      }
     int x;
      int y;
     
      glClear(GL_COLOR_BUFFER_BIT);
     
      grass.bind();
      x = 40;
      y = 64;
      glPushMatrix();
         glBegin(GL11.GL_QUADS);
            glTexCoord2f(1,1);
            glVertex2f(x-32,y-32);
            glTexCoord2f(0,1);
            glVertex2f(x+32,y-32);
            glTexCoord2f(0,0);
            glVertex2f(x+32,y+32);
            glTexCoord2f(1,0);
            glVertex2f(x-32,y+32);
         glEnd();
      glPopMatrix();
     
      dirt.bind();
      x += 64;
      glBegin(GL_QUADS);
         glTexCoord2f(1,1);
         glVertex2f(x-32,y-32);
         glTexCoord2f(0,1);
         glVertex2f(x+32,y-32);
         glTexCoord2f(0,0);
         glVertex2f(x+32,y+32);
         glTexCoord2f(1,0);
         glVertex2f(x-32,y+32);
      glEnd();
     
      dirt2.bind();
      x += 64;
      glBegin(GL_QUADS);
         glTexCoord2f(1,1);
         glVertex2f(x-32,y-32);
         glTexCoord2f(0,1);
         glVertex2f(x+32,y-32);
         glTexCoord2f(0,0);
         glVertex2f(x+32,y+32);
         glTexCoord2f(1,0);
         glVertex2f(x-32,y+32);
      glEnd();
     
      brick.bind();
      x += 64;
      glBegin(GL_QUADS);
         glTexCoord2f(1,1);
         glVertex2f(x-32,y-32);
         glTexCoord2f(0,1);
         glVertex2f(x+32,y-32);
         glTexCoord2f(0,0);
         glVertex2f(x+32,y+32);
         glTexCoord2f(1,0);
         glVertex2f(x-32,y+32);
      glEnd();
     
      cement.bind();
      x += 64;
      glBegin(GL_QUADS);
         glTexCoord2f(1,1);
         glVertex2f(x-32,y-32);
         glTexCoord2f(0,1);
         glVertex2f(x+32,y-32);
         glTexCoord2f(0,0);
         glVertex2f(x+32,y+32);
         glTexCoord2f(1,0);
         glVertex2f(x-32,y+32);
      glEnd();
   }
}


Would like to add an Image but I am unsure how.

I am also interested in a way to scale the textures I am using up to different sizes, if any one knows, which I am sure someone does  Wink....
Offline theagentd
« Reply #1 - Posted 2012-04-24 12:33:57 »

Your texture coordinates are rotated 180 degrees. Also, Slick uses power-of-2 textures, so it will pad the texture if the width and/or height is not a power of 2. Also, no need for pushing and popping the matrix if you don´t do any matrix manipulations (glTranslate, glScale, glRotate, glMultMatrix, glLoadMatrix, e.t.c) This doesn´t explain the problem you´re getting, but you should fix it anyway. =P

Your commented glColor3f() call makes no sense. The colors range from 0 to 1 in float form, not 0 to 255 (that´s glColor3ub (ub = unsigned byte)). These values are clamped to 1 though, so it effectively works as you expected though...

Could you post more code? You could be leaking OpenGL state, for example leaving blending enabled or forgetting a glColor call which sets the color to green. A screenshot would also be excellent so we can actually see what you mean by "green overlay".

Myomyomyo.
Offline Zerali

Senior Newbie





« Reply #2 - Posted 2012-04-24 16:38:52 »

Thanks for comments! I know that the image is rotated 180 degrees but for some reason it was loaded in rotated, so I rotated it back...

Here is my Main.java its what calls the drawLevel from my Level entity class.
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  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.lwjgl.LWJGLException;

import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

import static org.lwjgl.opengl.GL11.*;
 
public class Main {
 
   /** time at last frame */
   private long lastFrame;
   /** frames per second */
   private int fps;
   /** last fps time */
   private long lastFPS;
   /** state of the game */
   private State state = State.INTRO;
   /** Level we are trying to load */
   private Level1 level;
   /** how far zoomed in we are*/
   private int zoom = 2;
   
   private static enum State {
      INTRO,MAIN_MENU,GAME;
   }
 
   public void start() {
      try {
         Display.setDisplayMode(new DisplayMode(800, 500));
         Display.create();
      } catch (LWJGLException e) {
         e.printStackTrace();
         System.exit(0);
      }
 
      initGL(); // init OpenGL
     getDelta(); // call once before loop to initialise lastFrame
     lastFPS = getTime(); // call before loop to initialise fps timer
     level = new Level1("temp",8);
 
      while (!Display.isCloseRequested()) {
         int delta = getDelta();
 
         update(delta);
         renderGL();
 
         Display.update();
         Display.sync(60); // cap fps to 60fps
     }
 
      Display.destroy();
   }
 
   public void update(int delta) {
             
      switch(state) {
         case INTRO:
            if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
               state = State.MAIN_MENU;
               System.out.println("Go to Main menu");
            }
            break;
         case MAIN_MENU:
            if (Keyboard.isKeyDown(Keyboard.KEY_RETURN)) {
               state = State.GAME;
               System.out.println("Go to Game");
            }
            break;
         case GAME:
            if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
               state = State.MAIN_MENU;
               System.out.println("Go to Main menu");
            }
            if(Keyboard.isKeyDown(Keyboard.KEY_ADD)) {
               zoom++;
            }
            if(Keyboard.isKeyDown(Keyboard.KEY_SUBTRACT)) {
               zoom--;
            }
            break;
      }
           
      updateFPS(); // update FPS Counter
  }
   
   
   /**
    * Calculate how many milliseconds have passed
    * since last frame.
    *
    * @return milliseconds passed since last frame
    */

   public int getDelta() {
       long time = getTime();
       int delta = (int) (time - lastFrame);
       lastFrame = time;
 
       return delta;
   }
 
   /**
    * Get the accurate system time
    *
    * @return The system time in milliseconds
    */

   public long getTime() {
       return (Sys.getTime() * 1000) / Sys.getTimerResolution();
   }
 
   /**
    * Calculate the FPS and set it in the title bar
    */

   public void updateFPS() {
      if (getTime() - lastFPS > 1000) {
         Display.setTitle("FPS: " + fps);
         fps = 0;
         lastFPS += 1000;
      }
      fps++;
   }
 
   public void initGL() {
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(0, 800, 0, 500, 1, -1);
      glMatrixMode(GL_MODELVIEW);
      glEnable(GL_TEXTURE_2D);
   }
 
   public void renderGL() {
      // Clear The Screen And The Depth Buffer
     glClear(GL_COLOR_BUFFER_BIT);
       
      switch(state) {
         case INTRO:
            glColor3f(1.0f, 0.0f, 0.0f);
            glRectf(0, 0, 800, 500);
            break;
         case MAIN_MENU:
            glColor3f(0.0f, 1.0f, 0.0f);
            glRectf(0, 0, 800, 500);
            break;
         case GAME:
            level.drawLevel(zoom);            
            break;
      }
   }
 
   public static void main(String[] argv) {
      Main Game = new Main();
      Game.start();
   }
}


And I can't figure out how to add an image to a post, please let me know how to.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Zerali

Senior Newbie





« Reply #3 - Posted 2012-04-24 16:46:22 »

Solved the issue !!!! Grin

I added this line in the drawLevel function right before I draw the Quads with the textures on them.
1  
glColor3f(1.0f, 1.0f ,1.0f);


Also do you know If I can scale the textures?
Offline theagentd
« Reply #4 - Posted 2012-04-24 18:26:24 »

Solved the issue !!!! Grin

I added this line in the drawLevel function right before I draw the Quads with the textures on them.
1  
glColor3f(1.0f, 1.0f ,1.0f);


Also do you know If I can scale the textures?
Yes, you´re leaking OpenGL state, specifically the color setting. You set the color to green when you´re on the menu and don´t set it back to white after switching to the actual game.

Scaling textures is free. Just increase the size of the quad and the texture will be stretched over it. Either change the glVertex coordinates (don´t touch the glTexCoord calls) OR use glScalef() to scale the values of all glVertex calls. A tip is to use the matrix for position too, for example:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
glPushMatrix(); //Saves the current matrix
glTranslatef(posX, posY, 0);
glScale(scaleX, scaleY, 1);

glBegin(GL_QUADS);
//glTexCoord(...); glVertex(...); ...
//Now, the point (0, 0) ends up at (posX, posY) due to translation
//and the point (10, 10) ends up at (posX + 10 x scaleX, posY + 10 x scaleY). Can´t find the asterix button on this stupid iPhone Bluetooth keyboard. -_-
glEnd();

glPopMatrix(); //restore the matrix saved by the last call to glPushMatrix();

Myomyomyo.
Offline Zerali

Senior Newbie





« Reply #5 - Posted 2012-04-25 02:00:32 »

Thanks for the response. I messed with the two ways of scaling and I understand both ways. But this causes the texture on the quad to scale up and become "blurry", I would like the image to retain the same low resolution as the image gets larger. Thanks for all your help.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #6 - Posted 2012-04-25 02:03:35 »

(whoops -- real post below)
Offline sproingie

JGO Kernel


Medals: 202



« Reply #7 - Posted 2012-04-25 02:05:58 »

Set your magnification filter to GL_NEAREST.

1  
2  
tex.bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);


Slick can set the filter from the texture loader too.  See http://stackoverflow.com/questions/6602684/opengl-applying-filters-to-textures-with-slick-util-textures for details (ignore the bit about mipmaps, just look at the first line using GL_NEAREST)
Offline Zerali

Senior Newbie





« Reply #8 - Posted 2012-04-25 02:46:18 »

That was exactly what I was looking for!!!  Smiley

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

CogWheelz (17 views)
2014-07-30 21:08:39

Riven (23 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43
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!