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 (408)
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  
  gl_quads are rendered uncorrectly  (Read 1767 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.

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 (119 views)
2013-05-17 21:29:12

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

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

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

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

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

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

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

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

UnluckyDevil (217 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.115 seconds with 22 queries.