Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
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  
  gl_quads are rendered uncorrectly  (Read 3338 times)
0 Members and 1 Guest are viewing this topic.
Offline x23b5

Junior Newbie





« Posted 2012-03-16 09:18:15 »

Hello Gentleman,

I have tried to render some figures which each consists of five quads.
Unfortunately they are shown wrong.
Does anybody have an idea, what the problem could be?



Offline theagentd
« Reply #1 - Posted 2012-03-17 02:52:46 »

Depth test problem? Lighting with weird normals? Give us code! =D

Myomyomyo.
Offline x23b5

Junior Newbie





« Reply #2 - Posted 2012-03-19 09:30:18 »

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  
       public void init(GLAutoDrawable drawable) 
       {
          w = drawable.getWidth();
           h = drawable.getHeight();
           GL2 gl = drawable.getGL().getGL2();
           glu = new GLU();
           gl.glEnable(GL2.GL_SMOOTH);
           gl.glShadeModel(GL2.GL_SMOOTH);
           gl.glClearColor(0.0f,0.0f,0.0f,1.0f);
           gl.glClearDepth(1.0f);
           gl.glClearDepth(1.0f);                                    // Depth Buffer Setup
         gl.glEnable(GL.GL_DEPTH_TEST);                              // Enables Depth Testing
         gl.glDepthFunc(GL.GL_LEQUAL);
          gl.glEnable(GL.GL_LINE_SMOOTH);
           gl.glEnable(GL.GL_BLEND);
           gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
          gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_DONT_CARE);         // Really Nice Perspective Calculations
      }

       public void display(GLAutoDrawable drawable)
       {
           GL2 gl = drawable.getGL().getGL2();
           
           w = drawable.getWidth();
           h = drawable.getHeight();
           
           gl.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT);         // Clear the colour and depth buffer
           
           gl.glViewport(0, 0, w, h);                                 // Reset The Current Viewport
         
           gl.glMatrixMode(GL2.GL_PROJECTION);                           // Select The Projection Matrix
          gl.glLoadIdentity();                                    // Reset The Projection Matrix
         
           glu.gluPerspective(45.0f,(float)w/(float)h,0.01f,1000000.0f);         // Calculate The Aspect Ratio Of The Window

           gl.glMatrixMode(GL2.GL_MODELVIEW);                             // Select The Modelview Matrix
          gl.glLoadIdentity();                                    // Reset The Modelview Matrix    
          drawScene(drawable);                                    // Draw the scene
      }

      public void reshape(GLAutoDrawable drawable, int x, int y, int w2, int h2)
       {
         GL2 gl = drawable.getGL().getGL2();
           
           w2 = drawable.getWidth();
           h2 = drawable.getHeight();
           
           gl.glMatrixMode(GL2.GL_MODELVIEW);
           gl.glLoadIdentity();
           // perspective view
          gl.glViewport(0, 0, w, h);
           gl.glMatrixMode(GL2.GL_PROJECTION);
           gl.glLoadIdentity();
           glu.gluPerspective(45.0f,(float)w/(float)h,0.01f,1000000.0f);
       }

       public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
       {      
       }
       
       public void drawScene(GLAutoDrawable drawable)
       {
          GL2 gl = drawable.getGL().getGL2();
          gl.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT);
         
          gl.glRotatef(rotate[0],1f,0f, 0f);
          gl.glRotatef(rotate[1],0f,1f, 0f);
          gl.glRotatef(rotate[2],0f,0f, 1f);
          gl.glTranslatef(trans[0],trans[1],trans[2]);
          gl.glScalef(scale[0], scale[1], scale[2]);
          float hfloat = 0.1f;
          float hcolor = 0;
          for(int i=0;i<coords.size();i++){
          if(coords.get(i).getRed()>1){
             coords.get(i).setRed(1);
          }
          if(coords.get(i).getGreen()>1){
             coords.get(i).setGreen(1);
          }
          if(coords.get(i).getBlue()>1){
             coords.get(i).setBlue(1);
          }
          if(coords.get(i).getX(1)>=1f||coords.get(i).getZ(3)>=5f){
             hfloat = 0;
          } else {
             hfloat = 5f;
          }
          if(light==i){
             hcolor = 0.5f;
          } else {
             hcolor = 0;
          }
          gl.glPushMatrix();
              //Koordinaten
            gl.glTranslatef(coords.get(i).getPositionX(),coords.get(i).getPositionY()+0.01f,coords.get(i).getPositionZ());                        // Move x,y,z
            gl.glBegin(GL2.GL_QUADS);      
             //back
            gl.glColor3f(coords.get(i).getRed()-0.2f+hcolor,coords.get(i).getGreen()-0.2f+hcolor, coords.get(i).getBlue()-0.2f+hcolor);
             gl.glVertex3f(coords.get(i).getX(3)+hfloat,coords.get(i).getY(3),coords.get(i).getZ(3)+hfloat);                        
             gl.glVertex3f(coords.get(i).getX(7)+hfloat,coords.get(i).getY(7),coords.get(i).getZ(7)+hfloat);                        
             gl.glVertex3f(coords.get(i).getX(6)-hfloat,coords.get(i).getY(6),coords.get(i).getZ(6)+hfloat);
             gl.glVertex3f(coords.get(i).getX(2)-hfloat,coords.get(i).getY(2),coords.get(i).getZ(2)+hfloat);          
             //left
            gl.glColor3f(coords.get(i).getRed()-0.3f+hcolor,coords.get(i).getGreen()-0.3f+hcolor, coords.get(i).getBlue()-0.3f+hcolor);
             gl.glVertex3f(coords.get(i).getX(4)+hfloat,coords.get(i).getY(4),coords.get(i).getZ(4)-hfloat);                    
             gl.glVertex3f(coords.get(i).getX(0)+hfloat,coords.get(i).getY(0),coords.get(i).getZ(0)-hfloat);                        
             gl.glVertex3f(coords.get(i).getX(3)+hfloat,coords.get(i).getY(3),coords.get(i).getZ(3)+hfloat);          
             gl.glVertex3f(coords.get(i).getX(7)+hfloat,coords.get(i).getY(7),coords.get(i).getZ(7)+hfloat);
             //right
            gl.glColor3f(coords.get(i).getRed()-0.4f+hcolor,coords.get(i).getGreen()-0.4f+hcolor, coords.get(i).getBlue()-0.4f+hcolor);
             gl.glVertex3f(coords.get(i).getX(2)-hfloat,coords.get(i).getY(2),coords.get(i).getZ(2)+hfloat);                    
             gl.glVertex3f(coords.get(i).getX(1)-hfloat,coords.get(i).getY(1),coords.get(i).getZ(1)-hfloat);              
             gl.glVertex3f(coords.get(i).getX(5)-hfloat,coords.get(i).getY(5),coords.get(i).getZ(5)-hfloat);          
             gl.glVertex3f(coords.get(i).getX(6)-hfloat,coords.get(i).getY(6),coords.get(i).getZ(6)+hfloat);
             //top
            gl.glColor3f(coords.get(i).getRed()-0.5f+hcolor,coords.get(i).getGreen()-0.5f+hcolor, coords.get(i).getBlue()-0.5f+hcolor);
             gl.glVertex3f(coords.get(i).getX(4)+hfloat,coords.get(i).getY(4),coords.get(i).getZ(4)-hfloat);                    
             gl.glVertex3f(coords.get(i).getX(5)-hfloat,coords.get(i).getY(5),coords.get(i).getZ(5)-hfloat);                    
             gl.glVertex3f(coords.get(i).getX(6)-hfloat,coords.get(i).getY(6),coords.get(i).getZ(6)+hfloat);
             gl.glVertex3f(coords.get(i).getX(7)+hfloat,coords.get(i).getY(7),coords.get(i).getZ(7)+hfloat);
             //front
            gl.glColor3f(coords.get(i).getRed()-hfloat+hcolor,coords.get(i).getGreen()-0.1f+hcolor, coords.get(i).getBlue()-0.1f+hcolor);
             gl.glVertex3f(coords.get(i).getX(1)-hfloat,coords.get(i).getY(1),coords.get(i).getZ(1)-hfloat);                        
             gl.glVertex3f(coords.get(i).getX(0)+hfloat,coords.get(i).getY(0),coords.get(i).getZ(0)-hfloat);                    
             gl.glVertex3f(coords.get(i).getX(4)+hfloat,coords.get(i).getY(4),coords.get(i).getZ(4)-hfloat);
             gl.glVertex3f(coords.get(i).getX(5)-hfloat,coords.get(i).getY(5),coords.get(i).getZ(5)-hfloat);
             gl.glEnd();                                          
          gl.glPopMatrix();

          }
       }



I used different deph test functions. I didn't help.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2012-03-19 12:11:43 »

You're getting z-bleeding. Change the near-z and far-z in gluPerspective() to something sane, like 0.1f - 1000f.

Myomyomyo.
Offline x23b5

Junior Newbie





« Reply #4 - Posted 2012-03-19 13:02:19 »

Hi,
thanks! It is much better now.
Can I do something additional to that, because I think it is not good enough yet.
Offline theagentd
« Reply #5 - Posted 2012-03-19 13:31:06 »

Do you still have the same problem? >_>

 - Change depth func to GL_LESS. It's better in pretty much all cases.
 - You can't use blending with z-buffering/depth testing, or at least you won't get the result you expect without sorting your triangles which in turn makes depth testing unnecessary.
 - You're also having a slightly low field of view (45 degrees).
 - Line smoothing doesn't work since it makes edges slightly transparent based on coverage -> blending -> not with depth testing.
 - glEnable(GL_SMOOTH) is not a valid command.
 - You aren't using the built-in OpenGL lighting system, so you can remove glShadeModel() too.
 - You have two glClearDepth.
 - The perspective correction hint is not needed since perspective correction became common, which was something like 10 years ago.
 - Since you setup the viewport and the projection matrix every frame (in the display() function), you don't need to do anything in reshape()
 - You're "corrupting" the modelview matrix in your renderScene() method. I'd recommend moving the glPushMatrix() to just after glClear(), before your calls to glRotatef(), glTranslate() and glScale().

Other than that, it's fine.  Grin

Myomyomyo.
Offline x23b5

Junior Newbie





« Reply #6 - Posted 2012-03-19 14:10:34 »

Thanks.
It works fine now  Grin
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 (10 views)
2014-07-30 21:08:39

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

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

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

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

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

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

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

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

Riven (30 views)
2014-07-23 20:56:16
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

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!