summaryrefslogtreecommitdiffstats
path: root/src/demos/misc/TiledRendering.java
blob: 08fd70e9d26a7464bba44c63690c8004d1be4196 (plain)
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
package demos.misc;

import com.jogamp.opengl.util.FileUtil;
import com.jogamp.opengl.util.TGAWriter;
import com.jogamp.opengl.util.awt.ImageUtil;
import com.jogamp.opengl.util.gl2.TileRenderer;
import demos.gears.Gears;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import javax.imageio.ImageIO;
import javax.media.opengl.GL;
import javax.media.opengl.GL2ES1;
import javax.media.opengl.GL2;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLDrawableFactory;
import javax.media.opengl.GLPbuffer;

/** Demonstrates the TileRenderer class by rendering a large version
    of the Gears demo to the specified file. */

public class TiledRendering {

  public static void main(String[] args) throws IOException {
      
    if (args.length != 1) {
      System.out.println("Usage: java TiledRendering [output file name]");
      System.out.println("Writes output (a large version of the Gears demo) to");
      System.out.println("the specified file, using either ImageIO or the fast TGA writer");
      System.out.println("depending on the file extension.");
      System.exit(1);
    }

    String filename = args[0];
    File file = new File(filename);

    GLCapabilities caps = new GLCapabilities(null);
    caps.setDoubleBuffered(false);

    if (!GLDrawableFactory.getFactory(caps.getGLProfile()).canCreateGLPbuffer(null)) {
      System.out.println("Demo requires pbuffer support");
      System.exit(1);
    }

    // Use a pbuffer for rendering
    GLPbuffer pbuffer = GLDrawableFactory.getFactory(caps.getGLProfile()).createGLPbuffer(caps, null,
                                                                       256, 256,
                                                                       null);
    
    // Fix the image size for now
    int tileWidth = 256;
    int tileHeight = 256;
    int imageWidth = tileWidth * 16;
    int imageHeight = tileHeight * 12;
    
    // Figure out the file format
    TGAWriter tga = null;
    BufferedImage img = null;
    Buffer buf = null;
    
    if (filename.endsWith(".tga")) {
      tga = new TGAWriter();
      tga.open(file,
               imageWidth,
               imageHeight,
               false);
      buf = tga.getImageData();
    } else {
      img = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_3BYTE_BGR);
      buf = ByteBuffer.wrap(((DataBufferByte) img.getRaster().getDataBuffer()).getData());
    }

    // Initialize the tile rendering library
    TileRenderer renderer = new TileRenderer();
    renderer.setTileSize(tileWidth, tileHeight, 0);
    renderer.setImageSize(imageWidth, imageHeight);
    renderer.setImageBuffer(GL2.GL_BGR, GL.GL_UNSIGNED_BYTE, buf);
    renderer.trPerspective(20.0f, (float) imageWidth / (float) imageHeight, 5.0f, 60.0f);

    GLContext context = pbuffer.getContext();
    if (context.makeCurrent() == GLContext.CONTEXT_NOT_CURRENT) {
      System.out.println("Error making pbuffer's context current");
      System.exit(1);
    }
    
    GL2 gl = pbuffer.getGL().getGL2();
    gl.glMatrixMode(GL2ES1.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glTranslatef(0.0f, 0.0f, -40.0f);
    // Tile renderer will set up projection matrix    

    do {
      renderer.beginTile(gl);
      drawGears(gl);
    } while (renderer.endTile(gl));

    context.release();

    // Close things up and/or write image using ImageIO
    if (tga != null) {
      tga.close();
    } else {
      ImageUtil.flipImageVertically(img);
      if (!ImageIO.write(img, FileUtil.getFileSuffix(file), file)) {
        System.err.println("Error writing file using ImageIO (unsupported file format?)");
      }
    }
  }

  private static void drawGears(GL2 gl) {
    float view_rotx = 20.0f, view_roty = 30.0f, view_rotz = 0.0f;
    float angle = 0.0f;
    float pos[] = { 5.0f, 5.0f, 10.0f, 0.0f };
    float red[] = { 0.8f, 0.1f, 0.0f, 1.0f };
    float green[] = { 0.0f, 0.8f, 0.2f, 1.0f };
    float blue[] = { 0.2f, 0.2f, 1.0f, 1.0f };

    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

    gl.glLightfv(GL2ES1.GL_LIGHT0, GL2ES1.GL_POSITION, pos, 0);
    gl.glEnable(GL.GL_CULL_FACE);
    gl.glEnable(GL2ES1.GL_LIGHTING);
    gl.glEnable(GL2ES1.GL_LIGHT0);
    gl.glEnable(GL.GL_DEPTH_TEST);
    gl.glEnable(GL2ES1.GL_NORMALIZE);

    gl.glPushMatrix();
    gl.glRotatef(view_rotx, 1.0f, 0.0f, 0.0f);
    gl.glRotatef(view_roty, 0.0f, 1.0f, 0.0f);
    gl.glRotatef(view_rotz, 0.0f, 0.0f, 1.0f);
            
    gl.glPushMatrix();
    gl.glTranslatef(-3.0f, -2.0f, 0.0f);
    gl.glRotatef(angle, 0.0f, 0.0f, 1.0f);
    gl.glMaterialfv(GL.GL_FRONT, GL2ES1.GL_AMBIENT_AND_DIFFUSE, red, 0);
    Gears.gear(gl, 1.0f, 4.0f, 1.0f, 20, 0.7f);
    gl.glPopMatrix();
            
    gl.glPushMatrix();
    gl.glTranslatef(3.1f, -2.0f, 0.0f);
    gl.glRotatef(-2.0f * angle - 9.0f, 0.0f, 0.0f, 1.0f);
    gl.glMaterialfv(GL.GL_FRONT, GL2ES1.GL_AMBIENT_AND_DIFFUSE, green, 0);
    Gears.gear(gl, 0.5f, 2.0f, 2.0f, 10, 0.7f);
    gl.glPopMatrix();
            
    gl.glPushMatrix();
    gl.glTranslatef(-3.1f, 4.2f, 0.0f);
    gl.glRotatef(-2.0f * angle - 25.0f, 0.0f, 0.0f, 1.0f);
    gl.glMaterialfv(GL.GL_FRONT, GL2ES1.GL_AMBIENT_AND_DIFFUSE, blue, 0);
    Gears.gear(gl, 1.3f, 2.0f, 0.5f, 10, 0.7f);
    gl.glPopMatrix();
            
    gl.glPopMatrix();
  }
}