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 (534)
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  
  Can anyone help, cube sides not always transparent  (Read 576 times)
0 Members and 1 Guest are viewing this topic.
Offline ajmas

Junior Member




Miles away from here!


« Posted 2005-11-04 23:45:16 »

Hi,

In the process of learning OpenGL I decided I would give myself a variation on some existing examples I have looked at (such as NeHe lesson Cool. Running the code I see a rotating cube with transparent textures, except they aren't always transparent or the faces at the back seem to be missing when looking through the transparency. I just can't work out what is happening. Follows is the 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  
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  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
public class CubeChase extends Panel implements GLEventListener, Runnable {

  GLU glu = new GLU();
  GLCanvas canvas;
  Cube cube;
  float x = 0.0f;
 
  private int[] textures = new int[3];          // Storage For 3 Textures
 
  private float[] lightAmbient = {0.5f, 0.5f, 0.5f, 1.0f};
  private float[] lightDiffuse = {1.0f, 1.0f, 1.0f, 1.0f};
  private float[] lightPosition = {0.0f, 0.0f, 0.0f, -6.0f};
 
  public CubeChase() {
    GLCapabilities capabilities = new GLCapabilities();
           
    canvas =
        GLDrawableFactory.getFactory().createGLCanvas(capabilities);
   
    canvas.addGLEventListener(this);    
   
    setLayout(new BorderLayout());
    add(canvas);
   
    Point3D centerPoint = new Point3D(0,0,0);
    //create a cube around center point
   cube = new Cube(centerPoint,2.0);
   
  }
 

  private boolean loadGLTextures(GLAutoDrawable gldrawable) {
      TextureReader.Texture texture = null;
      try {
          texture = TextureReader.readTexture("data/glass.png");
      } catch (IOException e) {
          return false;
      }

      GL gl = gldrawable.getGL();
     
      //Create Nearest Filtered Texture
     gl.glGenTextures(3, textures,0); /* * */
      gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);

      gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
      gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);

      gl.glTexImage2D(GL.GL_TEXTURE_2D,
              0,
              3,
              texture.getWidth(),
              texture.getHeight(),
              0,
              GL.GL_RGB,
              GL.GL_UNSIGNED_BYTE,
              texture.getPixels());          

      return true;
  }

 
  public void init(GLAutoDrawable drawable) {
    if (!loadGLTextures(drawable)) {
      System.out.println("Unable to load textures,Bailing!");
      System.exit(0);
    }
   
    GL gl = drawable.getGL();
   
    gl.glEnable(GL.GL_BLEND);
    gl.glDisable(GL.GL_DEPTH_TEST);
    gl.glEnable(GL.GL_LIGHTING);
    gl.glEnable(GL.GL_TEXTURE_2D);                          // Enable Texture Mapping
   gl.glShadeModel(GL.GL_SMOOTH);                            //Enables Smooth Color Shading
   gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);               //This Will Clear The Background Color To Black
   gl.glClearDepth(1.0);                                  //Enables Clearing Of The Depth Buffer
   gl.glEnable(GL.GL_DEPTH_TEST);                            //Enables Depth Testing
   gl.glDepthFunc(GL.GL_LEQUAL);                             //The Type Of Depth Test To Do
   gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);  // Really Nice Perspective Calculations
   gl.glLightfv(GL.GL_LIGHT1, GL.GL_AMBIENT, lightAmbient,0);      // Setup The Ambient Light
   gl.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE, lightDiffuse,0);      // Setup The Diffuse Light
   gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, lightPosition,0);    // Position The Light
   gl.glEnable(GL.GL_LIGHT1);                              // Enable Light One

    gl.glColor4f(1.0f, 1.0f, 1.0f, 0.5f);                   // Full Brightness.  50% Alpha (new )
   gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);                 // Set The Blending Function For Translucency (new )
   
  }

 
  public void display(GLAutoDrawable drawable) {

    GL gl = drawable.getGL();
   
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);            // Clear Screen And Depth Buffer
   gl.glLoadIdentity();          
   
    gl.glTranslatef(0.0f,0.0f,-5.0f);

    gl.glRotatef(x, 1.0f, 1.0f, 0.0f);
   
    gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);
   
    Rectangle3D[] faces = cube.getFaces();
    for ( int i=0; i<faces.length; i++) {    
      gl.glBegin(GL.GL_QUADS);    
     
      Point3D p1 = faces[i].getP1();
      Point3D p2 = faces[i].getP2();
      Point3D p3 = faces[i].getP3();
      Point3D p4 = faces[i].getP4();
     
      Point3D normal = findNormal(p3,p1);
     
      gl.glNormal3d(normal.x,normal.y,normal.z);
     
      gl.glTexCoord2f(1.0f, 1.0f);
      gl.glVertex3d(p1.x,p1.y,p1.z);
     
      gl.glTexCoord2f(1.0f, 0.0f);
      gl.glVertex3d(p2.x,p2.y,p2.z);
     
      gl.glTexCoord2f(0.0f, 0.0f);
      gl.glVertex3d(p3.x,p3.y,p3.z);
     
      gl.glTexCoord2f(0.0f, 1.0f);
      gl.glVertex3d(p4.x,p4.y,p4.z);
     
      gl.glEnd();
    }
   
  }
 
  public static double dotProduct(Point3D p1, Point3D p2) {
    return (p1.x*p2.x)+(p1.y*p2.y)+(p1.y*p2.y);
  }
 
  public static Point3D findNormal (Point3D p1, Point3D p2) {
   
    double x = (p1.y * p2.z) - (p2.y * p1.z);
    double y = (p1.z * p2.x) - (p2.z * p1.x);
    double z = (p1.x * p2.y) - (p2.x * p1.y);
    return new Point3D(x,y,z);
  }

  public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    GL gl = drawable.getGL(); ;
   
    if (height == 0) height = 1;
    gl.glViewport(0, 0, width, height);                       // Reset The Current Viewport And Perspective Transformation
   gl.glMatrixMode(GL.GL_PROJECTION);                           // Select The Projection Matrix
   gl.glLoadIdentity();                                      // Reset The Projection Matrix
   glu.gluPerspective(45.0f, width / (height*1.0f), 0.1f, 100.0f);  // Calculate The Aspect Ratio Of The Window
   gl.glMatrixMode(GL.GL_MODELVIEW);                            // Select The Modelview Matrix
   gl.glLoadIdentity();      
  }

  public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    // TODO Auto-generated method stub
   
  }
 
  public void run () {
    while (true) {      
      try {
        Thread.sleep(5);
      } catch (InterruptedException e) {
      }
      x+=0.05f;    
      canvas.repaint(0);
    }
  }
 
  public static void main ( String[] args ) {
     CubeChase cubeChase = new CubeChase();
     
     Frame f = new Frame();
     f.add(cubeChase);
     f.setBounds(50,50,400,300);
     f.setVisible(true);
     (new Thread(cubeChase)).run();
  }  
 
}
Offline kitfox

Junior Member




Java games rock!


« Reply #1 - Posted 2005-11-05 00:07:29 »

When drawing with transparency, you need to sort transparent shapes back to front.  If you draw a triangle that contains transparency which is infront of another shape, and that shape is drawn later, the second shape will not appear because it will fail the depth test.

Usually one will separate their scene objects into opaque and transparent.  One will then render their opaque objects from front to back, and then the transparent objects from back to front (also known as the painter's algorithm).  Transparent objects need to be drawn back to front because semitransparent foreground pixels will cause any further pixels which are behind them to fail their depth test.  On the other hand, opaque is drawn front to back because since the pixels are opaque there is no need to care about the appearance of pixels behind them, and when the distant sahpes are drawn, it is faster to throw away a pixel which fails the depth test than to overwrite an existing pixel with a pixel that is closer to the viewer.
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.

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

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

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

Riven (24 views)
2014-07-23 20:56:16

ctomni231 (55 views)
2014-07-18 06:55:21

Zero Volt (47 views)
2014-07-17 23:47:54

danieldean (38 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
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!