Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
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  
  simple procedural texture  (Read 2236 times)
0 Members and 1 Guest are viewing this topic.
Offline 4fingers

Senior Newbie





« Posted 2007-10-11 06:16:54 »

I am just trying my hands at a procedural type of texture but so far any attempts have failed yet I can draw squares and triangles no problem.
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  
    public void display(GLAutoDrawable drawable) {

        GL gl = drawable.getGL();

        // Clear the drawing area
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        // Reset the current matrix to the "identity"
        gl.glLoadIdentity();

        // Move the "drawing cursor" around
        gl.glTranslatef(-1.5f, 1.0f, -6.0f);

        // Drawing Using Triangles
        float[] brickColour = { 0.5f, 0.15f, 0.14f};
        gl.glBegin(GL.GL_TRIANGLES);
        gl.glColor3fv(brickColour,0);    // Set the current drawing color to red
        gl.glVertex3f(0.0f, 1.0f, 0.0f);   // Top
        gl.glColor3f(0.0f, 1.0f, 0.0f);    // Set the current drawing color to green
        gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
        gl.glColor3f(0.0f, 0.0f, 1.0f);    // Set the current drawing color to blue
        gl.glVertex3f(1.0f, -1.0f, 0.0f);  // Bottom Right
        // Finished Drawing The Triangle
        gl.glEnd();

        // Move the "drawing cursor" to another position
        gl.glTranslatef(3.0f, 0.0f, 0.0f);
       
        // Draw a simple texture
        drawImage(gl);

        // Flush all drawing operations to the graphics card
        gl.glFlush();
    }

    private void drawImage(GL gl) {
       
        // draw a rectangle which should be seen if this works
        gl.glColor3f(1.0f, 0.5f, 0.0f);
        gl.glRecti(0, 1, 1, 0);
        gl.glColor3f(0.0f, 0.0f, 0.0f);


        int width = 50;
        int height = 100;
        IntBuffer buffer = IntBuffer.allocate(3 * width * height);

        for (int h = 0; h < height; h++) {
            for (int w = 0; w < width; w++) {
                buffer.put(0); // R
                buffer.put(0); // G
                buffer.put(255); // B
            }
        }
        buffer.rewind();

        gl.glDrawBuffer(GL.GL_FRONT);
        gl.glRasterPos2i(0, 0);
        gl.glDrawPixels(width, height, GL.GL_RGB, GL.GL_INT, buffer);
       
        //gl.glFinish();
 
    }

Any idea where I am going wrong.
Thanks

Edit: Woops left in two unrealted functions, now been removed
Offline 4fingers

Senior Newbie





« Reply #1 - Posted 2007-10-12 11:55:24 »

Sorry for double post but I have some developments. I was going to try get buffered image and graphics2D involved until I found this tutorial which seems to be the thing I was looking for. It doesn't use glDrawPixels instead it used glBindTexture.
http://www.java-tips.org/other-api-tips/jogl/demonstration-of-using-glbindtexture-by-creating-and-managing-two-tex.html

Until I manage to get a book on JOGL or OpenGL I am still in the learning stages, but at the moment I am wondering that instead of using bytes for the RGBA values I could use Integers instead. I am aware there might be some efficiency problems there but at the moment I find it easier working with simple ints.

I tried converting that example to use ints but so far it hasn't worked and I am currently left with two white textures.

Here is what I have tried to do.
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  
import java.awt.event.*;
import javax.swing.JFrame;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;
import java.nio.IntBuffer;

/**
 * This program demonstrates using glBindTexture() by creating and managing two
 * textures.
 *
 * @author Kiet Le (Java port)
 */

public class texbind extends JFrame implements GLEventListener, KeyListener {

    private GLCapabilities caps;
    private GLCanvas canvas;
    private GLU glu;
    private GLUT glut;
    //
    private static final int rgba = 4;
    private static final int checkImageWidth = 64;
    private static final int checkImageHeight = 64;
    // private byte checkImage[][][];
    // private byte otherImage[][][];
    private IntBuffer checkImageBuf = BufferUtil.newIntBuffer(checkImageWidth * checkImageHeight * rgba);
    private IntBuffer otherImageBuf = BufferUtil.newIntBuffer(checkImageWidth * checkImageHeight * rgba);
    private int[] texName = new int[2];

    public texbind() {
        super("texbind");

        caps = new GLCapabilities();
        canvas = new GLCanvas(caps);
        canvas.addGLEventListener(this);
        canvas.addKeyListener(this);

        getContentPane().add(canvas);
    }

    public void run() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(250, 250);
        setLocationRelativeTo(null);
        setVisible(true);
        canvas.requestFocusInWindow();
    }

    public static void main(String[] args) {
        new texbind().run();
    }

    public void init(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        glu = new GLU();

        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL.GL_FLAT);
        gl.glEnable(GL.GL_DEPTH_TEST);

        makeCheckImages();

        gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);

        gl.glGenTextures(2, texName, 0);
        gl.glBindTexture(GL.GL_TEXTURE_2D, texName[0]);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
        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, GL.GL_RGBA, checkImageWidth, checkImageHeight, 0, GL.GL_RGBA, GL.GL_INT, checkImageBuf);

        gl.glBindTexture(GL.GL_TEXTURE_2D, texName[1]);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
        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.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_DECAL);
        gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, checkImageWidth, checkImageHeight, 0, GL.GL_RGBA, GL.GL_INT, otherImageBuf);
        gl.glEnable(GL.GL_TEXTURE_2D);
    }

    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();

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

        //First texture
        gl.glBindTexture(GL.GL_TEXTURE_2D, texName[0]);
        gl.glBegin(GL.GL_QUADS);
        gl.glTexCoord2d(0.0, 0.0);
        gl.glVertex3d(-2.0, -1.0, 0.0);
        gl.glTexCoord2d(0.0, 1.0);
        gl.glVertex3d(-2.0, 1.0, 0.0);
        gl.glTexCoord2d(1.0, 1.0);
        gl.glVertex3d(0.0, 1.0, 0.0);
        gl.glTexCoord2d(1.0, 0.0);
        gl.glVertex3d(0.0, -1.0, 0.0);
        gl.glEnd();

        //Second texture
        gl.glBindTexture(GL.GL_TEXTURE_2D, texName[1]);
        gl.glBegin(GL.GL_QUADS);
        gl.glTexCoord2d(0.0, 0.0);
        gl.glVertex3d(1.0, -1.0, 0.0);
        gl.glTexCoord2d(0.0, 1.0);
        gl.glVertex3d(1.0, 1.0, 0.0);
        gl.glTexCoord2d(1.0, 1.0);
        gl.glVertex3d(2.41421, 1.0, -1.41421);
        gl.glTexCoord2d(1.0, 0.0);
        gl.glVertex3d(2.41421, -1.0, -1.41421);
        gl.glEnd();
        gl.glFlush();
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
        GL gl = drawable.getGL();

        gl.glViewport(0, 0, w, h);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(60.0, (float) w / (float) h, 1.0, 30.0);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslated(0.0, 0.0, -3.6);
    }

    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    }

    private void makeCheckImages() {
        //byte c = 0x00;
        for (int i = 0; i < checkImageWidth; i++) {
            for (int j = 0; j < checkImageHeight; j++) {
                checkImageBuf.put((int) (Math.random()*256)); // R
                checkImageBuf.put((int) (Math.random()*256)); // G
                checkImageBuf.put((int) (Math.random()*256)); // B
                checkImageBuf.put((int) (Math.random()*256)); // A
               
                otherImageBuf.put((int) (Math.random()*256)); // R
                otherImageBuf.put((int) (Math.random()*256)); // G
                otherImageBuf.put((int) (Math.random()*256)); // B
                otherImageBuf.put((int) (Math.random()*256)); // A

            }
        }
        checkImageBuf.rewind();
        otherImageBuf.rewind();
    }

    public void keyTyped(KeyEvent key) {
    }

    public void keyPressed(KeyEvent key) {
        switch (key.getKeyCode()) {
            case KeyEvent.VK_ESCAPE:
                System.exit(0);
            default:
                break;
        }
    }

    public void keyReleased(KeyEvent key) {
    }
}
Offline Jark

Senior Newbie





« Reply #2 - Posted 2007-10-12 12:16:14 »

Hi,

I couldn't find anything wrong with your code, but you could try adding "drawable.setGL(new DebugGL(drawable.getGL()));" to your init() method. DebugGL will throw a exception if any of your GL. commands are in the wrong place. (I found out this way that there was no GL.GL_EMISSION light parameter Tongue)

And you can find the red book(a well known opengl book) online here: http://www.glprogramming.com/red/.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 4fingers

Senior Newbie





« Reply #3 - Posted 2007-10-12 13:13:27 »

Hi Jark,
Thanks for the tip and book suggestion.

But back to my problem, and after searching around I came across a MSDN page that gave more information about the glBindTextute method:
http://msdn2.microsoft.com/en-us/library/ms537140.aspx
In particular this is what it said:
Quote
Data is read from pixels as a sequence of signed or unsigned bytes, shorts or longs, or single-precision floating-point values, depending on type.
The thing that struck me was no mention of integers so I changed the type over to floats and it started to work again.

Maybe leaving a more open question if people are willing to help me gain a better understanding. At the moment I am happy to continue using this method in generating any other procedural texture work that I will do but due to the lack of knowledge I am currently unaware of any other more efficient ways in doing this. Will there be any performance issues that I might not be aware at in this moment in time?

Thanks
Offline lhkbob

JGO Knight


Medals: 32



« Reply #4 - Posted 2007-10-13 05:12:34 »

If you're still in the learning stages, I'd suggest making something much simpler than procedural textures, just to get a hang for how openGL and jogl feel.  This will make it much easier to debug once you come to the stuff you really want to do.

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.

DarkCart (16 views)
2014-10-31 21:44:48

DarkCart (21 views)
2014-10-31 21:43:57

TehJavaDev (40 views)
2014-10-27 03:28:38

TehJavaDev (31 views)
2014-10-27 03:27:51

DarkCart (44 views)
2014-10-26 19:37:11

Luminem (27 views)
2014-10-26 10:17:50

Luminem (30 views)
2014-10-26 10:14:04

theagentd (36 views)
2014-10-25 15:46:29

Longarmx (64 views)
2014-10-17 03:59:02

Norakomi (62 views)
2014-10-16 15:22:06
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!