Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Java Game APIs & Engines / OpenGL Development / Re: Shaders with vec4, problem with texture colors on: 2013-04-08 21:19:36
you could for example do this
1  
gl_FragColor = vec4(colors.a);


and look if the .a value is what you think it should be

You're right.
I found a bug in my code (OMG!!!)
The bug was in the condition which checks if already beginning this fourth texture.

maybe you should use mix(a, b, float amount); ?
Yes this is a very cool feature but my solution allow to me obtain nice effects e.g texture splatting.


Thanks for help.
2  Java Game APIs & Engines / OpenGL Development / Shaders with vec4, problem with texture colors on: 2013-04-08 19:28:14
Hello.
I wrote a code which generating heightmap and i use four textures for texturing. Almost all working fine except one blending,
one texture on the edge is white or black. I know what it means value is bigger than 1 or equal 0 but i'm almost sure (to 99 percent) i do not pass this values.
My shaders code:
1  
2  
3  
4  
5  
6  
vec3 tx  = vec3(0.0, 0.0, 0.0);
tx += texture2D(ground_grass, pass_TextureCoord).rgb * colors.r;
tx += texture2D(ground_rock, pass_TextureCoord).rgb * colors.g;
tx += texture2D(ground_snow, pass_TextureCoord).rgb * colors.b;
tx += texture2D(ground_dirt, pass_TextureCoord).rgb * colors.a;
gl_FragColor = vec4(tx, 1.0);


The problem appearing only for texture which is multiplied by "colors.a" the r, g, b always working fine. I changed textures between each other with different borders and the problem is only with "a" value.
The value of the buffer looks something like this
1  
2  
3  
0.6f, 0.0f, 0.0f, 0.0f,
0.0f, 0.6f, 0.0f, 0.0f,
...


and my binding buffer looks like
1  
2  
3  
4  
vbocId = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbocId);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, colorsBuffer,GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, 0, 4);


So i have four values for one vertex which set textures color. I checked array with colors and i never have situation where one sequence have only 0 value or two value (or more) with 0.6f.
So what's going on with this value "a" in vec4, do any of you had similar problem.
Whether it is possible to shaders changed in some way this "a" value?


I also did a little test I created four buffers which hold values only for one texture. Values are always kept in one position and all works fine.
1  
2  
3  
4  
5  
6  
7  
vec3 tx  = vec3(0.0, 0.0, 0.0);
tx += texture2D(ground_dirt, pass_TextureCoord).rgb * colors1.r;
tx += texture2D(ground_grass, pass_TextureCoord).rgb * colors2.r;
tx += texture2D(ground_rock, pass_TextureCoord).rgb * colors3.r;
tx += texture2D(ground_snow, pass_TextureCoord).rgb * colors4.r;
gl_FragColor = vec4(tx, 1.0);
}

Of course the whole mechanism remained unchanged so surely all is set correctly, but it is a significant loss of memory.





3  Java Game APIs & Engines / OpenGL Development / Re: Basic problem - heightmap generator on: 2013-02-05 20:41:29
Ok, I figured out that I made mistake in function "getVertices()" where loop making only one step 'i++' so my vertices are overriding.
I'm not increasing correctly array index
height
height[i+1]
height[i+2]

Ehhh, silly me.
4  Java Game APIs & Engines / OpenGL Development / Basic problem - heightmap generator on: 2013-02-04 23:20:01
Hello.
I'm trying to learn how to generate heightmap but unfortunately I have problem with this.
I wanted to start from something simple.
I found this tutorial
http://www.mbsoftworks.sk/index.php?page=tutorials&series=1&tutorial=8
but as I already mentioned it doesn't work because in 100% I'm doing something wrong and what you can see on this image

Here
http://pastebin.com/KtweuK5M
is my whole code but basic method are "setupQuad()", "loopCycle()" and method generating vertices "getVertices()".
I'm pretty sure there is some problem with indices array.
Ignore color array and shaders.
I will be grateful for your help.
Maybe someone of you have same basic working example heghtmap in java and could share it with me (if I do not ask for too much).
5  Java Game APIs & Engines / OpenGL Development / Re: Quat4f and rotation around local axis on: 2012-03-15 20:41:26
I did it in this way
When i press key to rotate my cube I call this method which only remember central point cube.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
float localX = 0;
float localY = 0;
float localZ = 0;
private void setPosition(){
   float lX = 0;
   float lY = 0;
   float lZ = 0;
   for(int i = 0;i<array.length;i+=3) {
      lX+=array[i];
      lY+=array[i+1];
      lZ+=array[i+2];
      localX=lX/24;
      localY=lY/24;
      localZ=lZ/24;
   }
   System.out.println("x: "+localX+" y: "+localY+" z: "+localZ );
}


and then start the rotation
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  
private Vector4f vBase = new Vector4f();
private Vector4f angle = new Vector4f();
private void update(boolean rotX, boolean rotY, boolean rotZ){
   if (rotX) {
      angle = rotationX(angle, DEGTORAD);
   }
   if (rotY) {
      angle = rotationY(angle, DEGTORAD);
   }
   if (rotZ) {
      angle = rotationZ(angle, DEGTORAD);
   }
   
   for(int i = 0;i<array.length;i+=3) {
      vBase.x = array[i];
      vBase.y = array[i+1];
      vBase.z = array[i+2];
      vBase.w = (DEGTORAD);
     
      Vector4f vFin = new Vector4f();
     
      vBase.x -= localX;
      vBase.y -= localY;
      vBase.z -= localZ;
      mul(vFin, angle, vBase);
      vFin.x +=localX;
      vFin.y +=localY;
      vFin.z +=localZ;
     
      if(rotX) {
         array[i+1] = vFin.y;
         array[i+2] = vFin.z;
      }
      if(rotY) {
         array[i] = vFin.x;
         array[i+2] = vFin.z;
      }
      if(rotZ) {
         array[i] = vFin.x;
         array[i+1] = vFin.y;
      }
   }
    vertexData.put(array);
    vertexData.flip();
}

private Vector4f rotationX(Vector4f q, float a){
   System.out.println("-- rotationX --");
   a *= 0.5f;
   float sinA = (float) Math.sin(a);
   float cosA = (float) Math.cos(a);
   q.set(sinA, 0, 0, cosA);
   return q;
}

private Vector4f rotationY(Vector4f q, float a){
   System.out.println("-- rotationY --");
   a *= 0.5f;
   float sinA = (float) Math.sin(a);
   float cosA = (float) Math.cos(a);
   q.set(0.0f, sinA, 0.0f, cosA);
   return q;
}

private Vector4f rotationZ(Vector4f q, float a){
   System.out.println("-- rotationZ --");
   a *= 0.5f;
   float sinA = (float) Math.sin(a);
   float cosA = (float) Math.cos(a);
   q.set(0.0f, 0.0f, sinA, cosA);
   return q;
}

void mul(Vector4f out, Vector4f q1, Vector4f q2) {
   System.out.println("-- mul --");
   out.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y;
   out.y = q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z;
   out.z = q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x;
   out.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z;
}

All this works quite well.
But now I want to something more I want to change size but not all cube only width or height or thickness and I want to do this
on rotated cube.
I know I must to do something like this
Px' = S1 * Px
but it is not enough beacause from this

I'm geting this

Scaling works only on not rotated cube. I assume that I need to add a multiplication by the angle but I am not store information about angle.
Can you give me some advice?
6  Java Game APIs & Engines / OpenGL Development / Quat4f and rotation around local axis on: 2012-03-06 19:59:50
I assume there is a better way to do what I want to do.
I haven't problem with rotation cube around axis unfortunately global axis.
When I want to make the rotation around local axis I just remember central point of my cube then move cube to position x=0, y=0, z=0 and I start rotation.
After rotation I move cube back to saved position.
Is there any way to do it without moving cobe?
This is my code responsible for rotation.
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  
private static float angle = 4;
   private Quat4f roll = new Quat4f();
   private Quat4f quantBase = new Quat4f();
   private AxisAngle4f angle4f = new AxisAngle4f();
   private void update(boolean rotX, boolean rotY, boolean rotZ){
      if(rotX){
         angle4f.set(new Vector3f(1, 0, 0), angle * (float) (Math.PI / 180));
      } if(rotY) {
         angle4f.set(new Vector3f(0, 1, 0), angle * (float) (Math.PI / 180));
      } if(rotZ) {
         angle4f.set(new Vector3f(0, 0, 1), angle * (float) (Math.PI / 180));
      }
      roll.set(angle4f);
     
      for(int i = 0;i<array.length;i+=3) {
         quantBase.x = array[i];
         quantBase.y = array[i+1];
         quantBase.z = array[i+2];
         quantBase.w = 0f;
         quantBase.mul(roll, quantBase);
         if(rotX) {
            array[i+1] = quantBase.y;
            array[i+2] = quantBase.z;
         }
         if(rotY) {
            array[i] = quantBase.x;
            array[i+2] = quantBase.z;
         }
         if(rotZ) {
            array[i] = quantBase.x;
            array[i+1] = quantBase.y;
         }
      }
       vertexData.put(array);
        vertexData.flip();
   }

For clarity, this is how I understand the local and global axis.

7  Java Game APIs & Engines / OpenGL Development / Re: How to draw GUI on: 2012-02-24 10:16:11
Turn off depth testing, turn on ortho mode, and then just draw using screen coordinates. You can easily switch modes and depth testing in between draw calls.
It looks like it's working but until I'm not trying to load texture after this my GUI square turns black (should be blue) and loaded texture gets more blue.
I'm using SLICK TextureLoader.
Can you help me with this?
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  
186  
187  
public class TexturedCubeVA extends JApplet {
   
   private Canvas canvas;
   private Thread gameThread;
   boolean running = false;
   
   private Texture texture;
   
   public void startLWJGL(){
      gameThread = new Thread (){
         public void run(){
            running = true;
            try {
               Display.setParent(canvas);
               Display.create();
               Display.setVSyncEnabled(true);
               initGL();
            } catch (LWJGLException e) {
               e.printStackTrace();
            }
            gameLoop();
         }
      };
      gameThread.start();
   }
   
   public void stopLWJGL(){
      running = false;
      try {
         gameThread.join();
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
   }

   public void destroy(){
      remove(canvas);
      super.destroy();
   }
   
   public void init(){
      setLayout(new BorderLayout());
      canvas = new Canvas(){
         public final void addNotify(){
            super.addNotify();
            startLWJGL();
         }
         public final void removeNotify(){
            stopLWJGL();
            super.removeNotify();
         }
      };
      canvas.setSize(Conf.W, Conf.H);
      setSize(Conf.W, Conf.H);
      canvas.setBackground(Color.BLACK);
      canvas.setFocusable(true);
      canvas.requestFocus();
      canvas.setIgnoreRepaint(false);
      add(canvas);
   }
   
   private void initGL() {
      glViewport(0, 0,  Conf.W,Conf.H); // Reset The Current Viewport
     glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(45.0f, ((float)Conf.W / (float)Conf.H) , 0.1f, 100f);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
     
      GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
     glShadeModel(GL11.GL_SMOOTH); // Enables Smooth Shading
     GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Black Background
     glClearDepth(1.0f); // Depth Buffer Setup
     glEnable(GL_DEPTH_TEST); // Enables Depth Testing
     glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do
     GL11.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); // Really Nice Perspective Calculations
     
      try {
         texture = TextureLoader.getTexture("png", ResourceLoader.getResourceAsStream("res/caution.png"));
      } catch (IOException e) {
         e.printStackTrace();
      }
     
   }
   float rquad; // Angle For The Quad
  public void gameLoop(){
      final int amountOfVertices = 24;
        final int vertexSize = 3;
        final int textureVertexSize = 2;
     
        FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
        vertexData.put(CubeUtil.getCube(1f, 1f, 1f));
        vertexData.flip();
       
        FloatBuffer textureData = BufferUtils.createFloatBuffer(amountOfVertices * textureVertexSize);
        textureData.put(CubeUtil.getCubeTexture(1.0f,1.0f));
        textureData.flip();
       
      while(running) {
         update();
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
         texture.bind();
         
         glLoadIdentity();//Matrix reseting.
        GL11.glTranslatef(-1.0f, 0.0f, -8.0f); // Move Right And Into The Screen
        GL11.glRotatef(rquad, 1.0f, 1.0f, 1.0f); // Rotate The Cube On X, Y & Z
       
         glEnableClientState(GL_VERTEX_ARRAY);// Enable vertex
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
         
            glVertexPointer(vertexSize, 0, vertexData);//First cube
           glTexCoordPointer(textureVertexSize, 0, textureData);
            glDrawArrays(GL_QUADS, 0, amountOfVertices);//Draw cube
           
            glDisableClientState(GL_VERTEX_ARRAY);//Disable Vertex
           glDisableClientState(GL_TEXTURE_COORD_ARRAY);//Disable Vertex
       
            drawGUI();
         Display.sync(60);
         Display.update();
      }
     
      Display.destroy();
   }
   
   private void update(){
      rquad -= 0.80f; // Decrease The Rotation Variable For The Quad
  }

   /**
    * Draw the overlay for score and lifes
    *
    * @param window The window in which the GUI is displayed
    */

   private void drawGUI() {
      enterOrtho();
      guiRenderer();
      leaveOrtho();
   }
   
   /**
    * Drawing square on screen
    */

   private void guiRenderer() {
      GL11.glColor3f(0.5f, 0.5f, 1.0f);
      GL11.glBegin(GL11.GL_QUADS);
      GL11.glVertex2f(10, 410);
      GL11.glVertex2f(70, 410);
      GL11.glVertex2f(70, 470);
      GL11.glVertex2f(10, 470);
      GL11.glEnd();
   }
   
   /**
    * Enter the orthographic mode by first recording the current state,
    * next changing us into orthographic projection.
    */

   public void enterOrtho() {
      // store the current state of the renderer
     GL11.glPushAttrib(GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_ENABLE_BIT);
      GL11.glPushMatrix();
      GL11.glLoadIdentity();
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glPushMatrix();  
     
      // now enter orthographic projection
     GL11.glLoadIdentity();      
      GL11.glOrtho(0, Conf.W, Conf.H, 0, 1, -1);
      GL11.glDisable(GL11.GL_DEPTH_TEST);
      GL11.glDisable(GL11.GL_LIGHTING);  
   }
   
   /**
    * Leave the orthographic mode by restoring the state we store
    * in enterOrtho()
    *
    * @see enterOrtho()
    */

   public void leaveOrtho() {
      // restore the state of the renderer
     GL11.glPopMatrix();
      GL11.glMatrixMode(GL11.GL_MODELVIEW);
      GL11.glPopMatrix();
      GL11.glPopAttrib();
   }
}
8  Java Game APIs & Engines / OpenGL Development / How to draw GUI on: 2012-02-23 23:19:27
I almost ashamed to ask Wink
Usually I'm doing all in 3D (and all looks like FPS shooter) but now I want to draw GUI and in fact I don't know how, how to again drawing in 2D.
Mostly my class looks like this one
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  
public class MainCanvas extends Canvas {
   private static MainCanvas canvas;
   private Thread gameThread;
   boolean running = false;
   
   private GameState gameState;
   
   public static MainCanvas get(){
      if(canvas == null) {
         canvas = new MainCanvas();
      }
      return canvas;
   }
   
   public final void addNotify(){
      super.addNotify();
      startLWJGL();
   }
   
   public final void removeNotify(){
      stopLWJGL();
      super.removeNotify();
   }
   
   public void startLWJGL(){
      gameThread = new Thread (){
         
         public void run(){
            running = true;
            try {
               Display.setParent(canvas);
               Display.create();
               Display.setVSyncEnabled(true);
               initGL();
               gameState = GameState.prepare();
            } catch (LWJGLException e) {
               e.printStackTrace();
            }
            gameLoop();
         }

      };
      gameThread.start();
   }
   
   public void stopLWJGL(){
      running = false;
      try {
         gameThread.join();
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
   }

   private void initGL() {
      glViewport(0, 0,  Conf.W,Conf.H); // Reset The Current Viewport
     glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(45.0f, ((float)Conf.W / (float)Conf.H) , 0.1f, 100f);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
     
      GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
     glShadeModel(GL11.GL_SMOOTH); // Enables Smooth Shading
     GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Black Background
     glClearDepth(1.0f); // Depth Buffer Setup
     glEnable(GL_DEPTH_TEST); // Enables Depth Testing
     glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do
     GL11.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); // Really Nice Perspective Calculations
  }
   
   public void gameLoop(){
     
      while(running) {
         gameState.update();
         gameState.render();
         Display.update();
      }
      Display.destroy();
   }
   
}
9  Java Game APIs & Engines / OpenGL Development / Re: VertexArray with two square, how to rotate only one of them on: 2012-02-23 22:48:56
In truth I did it on Tuesday, sitting to the midnight and trying to load textures but it was worth.
I know how to use VBO but sometimes I worked on an older computer which supporting only OpenGL version 1.3.
10K cubes move quite smoothly.
Anyway theagentd thanks for your help!

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  
public class BoxRenderer {
   // the shader program is held in the Shader class
  private Shader shaderAccess;
   
   private static final Vector3f ROT_X = new Vector3f(1, 0, 0);
    private static final Vector3f ROT_Y = new Vector3f(0, 1, 0);
    private static final Vector3f ROT_Z = new Vector3f(0, 0, 1);

   private static final int NUM_VERTICES = 24;
    final int vSize = 3;
    final int vTexSize = 2;
   
    private FloatBuffer vData;
    private FloatBuffer texData;
   
    private Matrix4f matrix;
    private FloatBuffer matrixBuffer;
   
   private ArrayList<Box> boxes = new ArrayList<Box>();
   private int vertHandle;
   private int texHandle;
   
   private Texture texture;
   
   public BoxRenderer() {
      shaderAccess = new Shader();
     
      vData = BufferUtils.createFloatBuffer(NUM_VERTICES * vSize);
        vData.put(Tool.margeCubs(Tool.getCube(0.5f, 0.5f, 0.5f)));//x,y,z
       vData.flip();
       
        texData = BufferUtils.createFloatBuffer(NUM_VERTICES * vTexSize);
        texData.put(Tool.getTexture(1.0f, 1.0f));
        texData.flip();

        matrix = new Matrix4f();
        matrixBuffer = BufferUtils.createFloatBuffer(16);
       
      boxes = ToolBox.getGenerateBoxes(10000);
     
      vertHandle = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vertHandle);
        glBufferData(GL_ARRAY_BUFFER, vData, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
       
        texHandle = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, texHandle);
        glBufferData(GL_ARRAY_BUFFER, texData, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
       
        try {
         texture = TextureLoader.getTexture("png", ResourceLoader.getResourceAsStream("res/caution.png"));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   public void draw() {
      GL11.glLoadIdentity();
      texture.bind();

      // if the shader is ok we use it
     if (shaderAccess.useShader()) {
         ARBShaderObjects.glUseProgramObjectARB(shaderAccess.getShader());
      }
     
      int matrixLocation = ARBShaderObjects.glGetUniformLocationARB(shaderAccess.getShader(), "matrix");
      int texcoord = ARBShaderObjects.glGetUniformLocationARB(shaderAccess.getShader(), "texcoord");
       
      glEnableClientState(GL_VERTEX_ARRAY);
        //glEnableClientState(GL11.GL_COLOR_ARRAY);
       glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
     
        glBindBuffer(GL_ARRAY_BUFFER, vertHandle);
        glVertexPointer(vSize, GL_FLOAT, 0, 0L);
       
        glBindBuffer(GL_ARRAY_BUFFER, texHandle);
        glTexCoordPointer(2, GL_FLOAT, 0, 0L);
       
       
        for(int i = 0; i < boxes.size(); i++){
            Box box = boxes.get(i);
           
            matrix.setIdentity();
            matrix.translate(box.position);
            matrix.scale(box.scale);
            matrix.rotate(box.rotX, ROT_X);
            matrix.rotate(box.rotY, ROT_Y);
            matrix.rotate(box.rotZ, ROT_Z);
            matrix.store(matrixBuffer);
            matrixBuffer.flip();
            if(matrixLocation > 0) {
               ARBShaderObjects.glUniformMatrix4ARB(matrixLocation, false, matrixBuffer);
            }
            if(texcoord > 0) {
               ARBShaderObjects.glUniformMatrix2ARB(matrixLocation, false, texData);
            }
            glDrawArrays(GL_QUADS, 0, NUM_VERTICES);
        }
       
        //glDisableClientState(GL_COLOR_ARRAY);
       glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);

      ARBShaderObjects.glUseProgramObjectARB(0);
   }

   public void update(){
      for(int i = 0; i < boxes.size(); i++){
            Box box = boxes.get(i);
            box.update();
      }
   }
}


1  
2  
3  
4  
5  
6  
//screen.vert
uniform mat4x4 matrix;
void main(){
   gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_Position= gl_ModelViewProjectionMatrix*matrix*gl_Vertex;
}

1  
2  
3  
4  
5  
//screen.frag
uniform sampler2D tex;
void main(){
    gl_FragColor = texture2D(tex, gl_TexCoord[0].st);
}
10  Java Game APIs & Engines / OpenGL Development / Re: VertexArray with two square, how to rotate only one of them on: 2012-02-20 20:40:47
Thanks a lot!
Example works after really small modification Wink


Believe it or not but I had the idea to do something similar but I didn't know how to get around to.
This loop little worried me what if I want to create forest with thousands tree how this will be working (therefore I didn't want to use loop)?
Do I need to combine some techniques VertexArray/VertexBuffer, shaders, instancing (maybe later Wink) ... .
11  Java Game APIs & Engines / OpenGL Development / Re: VertexArray with two square, how to rotate only one of them on: 2012-02-19 21:11:26
OK.
I spent several hours in this weekend trying to learn something about shader and unfortunately I steal don't know how to do this.
I was based on
http://lwjgl.org/wiki/index.php?title=Using_Shaders_to_Calculate_Model%27s_Position
http://lwjgl.org/wiki/index.php?title=GLSL_Shaders_with_LWJGL
and some others tutorials, something work something don't but still don't know how to rotate this square(cubs) separately using shader.
So if it isn't to much please tell me how to do this.
The problem is that the two cubes are rotated in the same direction
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  
package com.frame.shaders_calc;

import static org.lwjgl.opengl.GL11.GL_COLOR_ARRAY;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.GL_VERTEX_ARRAY;
import static org.lwjgl.opengl.GL11.glDisableClientState;
import static org.lwjgl.opengl.GL11.glDrawArrays;
import static org.lwjgl.opengl.GL11.glEnableClientState;
import static org.lwjgl.opengl.GL11.glVertexPointer;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.ARBShaderObjects;
import org.lwjgl.opengl.GL11;

import com.Tool;

public class Box {
   // the shader program is held in the Shader class
  private Shader shaderAccess;

   final int allVertices = 24;
    final int vSize = 3;
    final int colorSize = 3;
   
    private FloatBuffer vData;
   
    private FloatBuffer rotate;
    private FloatBuffer position;
   
   public Box() {
      shaderAccess = new Shader();
     
      vData = BufferUtils.createFloatBuffer((allVertices * 2) * vSize);
                                    //  x    y     z     move                //x     y     z    move
       vData.put(Tool.margeCubs(Tool.getCube(0.5f, 0.5f, 0.5f, -1.5f), Tool.getCube(0.5f, 0.5f, 0.5f, 1.0f)));
        vData.flip();

        rotate = BufferUtils.createFloatBuffer(6);
        rotate.put(new float[]{45.0f, 180.0f, -9.0f, 0.0f, 0.0f,0.0f});// <--- ONLY THE FIRST THREE ARE TAKEN
       rotate.flip();
       
        position = BufferUtils.createFloatBuffer(3);
        position.put(new float[]{0.0f, 0.4f, -5.0f});
        position.flip();
   }

   public void draw() {
      GL11.glLoadIdentity();
      GL11.glPushMatrix();

      // if the shader is ok we use it
     if (shaderAccess.useShader()) {
         ARBShaderObjects.glUseProgramObjectARB(shaderAccess.getShader());
      }
      int pos = ARBShaderObjects.glGetUniformLocationARB(shaderAccess.getShader(), "pos");
      if (pos > 0) {
         ARBShaderObjects.glUniform3ARB(pos, position);
      } else {
         ARBShaderObjects.glUseProgramObjectARB(0);
      }
     
      int rot = ARBShaderObjects.glGetUniformLocationARB(
            shaderAccess.getShader(),"rot");

      if (rot > 0) {
         ARBShaderObjects.glUniform3ARB(rot, rotate);
      } else {
         ARBShaderObjects.glUseProgramObjectARB(1);
      }
      glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
     
        glVertexPointer(vSize, 0, vData);
       
        glDrawArrays(GL_QUADS, 0, allVertices*2);
       
        glDisableClientState(GL_COLOR_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);

      ARBShaderObjects.glUseProgramObjectARB(0);
      GL11.glPopMatrix();
   }

}

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  
uniform vec3 pos;
uniform vec3 rot;
varying vec4 vertColor;

void main(){
    mat4x4 position=mat4x4(1.0);
    position[3].xyz=pos.xyz;
    mat4x4 heading=mat4x4(1.0);
    heading[0][0]=cos(rot.y);
    heading[0][2]=-(sin(rot.y));
    heading[2][0]=sin(rot.y);
    heading[2][2]=cos(rot.y);
    mat4x4 pitch=mat4x4(1.0);
    pitch[1][1]=cos(rot.x);
    pitch[1][2]=sin(rot.x);
    pitch[2][1]=-(sin(rot.x));
    pitch[2][2]=cos(rot.x);
    mat4x4 roll=mat4x4(1.0);
    roll[0][0]=cos(rot.z);
    roll[0][1]=sin(rot.z);
    roll[1][0]=-(sin(rot.z));
    roll[1][1]=cos(rot.z);

    gl_Position= gl_ModelViewProjectionMatrix*position*heading*pitch*roll*gl_Vertex;
    vertColor = vec4(0.6,0.5,0.3,1.0f);
}


theagentd
I understand that Instancing resolve my problem (it looks incredible) but for me it's to soon I'm still learning lwjgl and I just want to know how to using shader.
12  Java Game APIs & Engines / OpenGL Development / Re: VertexArray with two square, how to rotate only one of them on: 2012-02-17 15:02:26
I can explain it if you want. EDIT: Scratch that, it does require a simple vertex shader...
If it's not a problem then YES please explain.
13  Java Game APIs & Engines / OpenGL Development / Re: VertexArray with two square, how to rotate only one of them on: 2012-02-17 14:02:57
For example you could setup the transformation matrix for the first cube, call glDrawArrays on the vertices of the first cube, setup the transformation matrix again for the second cube, call glDrawArrays, repeat. This is however pretty CPU intensive, and should be avoided if you have more than, say, 10 000 cubes.
Yes I know I can do this but also I know about
This is however pretty CPU intensive, and should be avoided if you have more than, say, 10 000 cubes.
that's why I asked.
I will try to do something in vbo, vertex shader right now it's a higher school for me.
Thanks.
14  Java Game APIs & Engines / OpenGL Development / Re: VertexArray with two square, how to rotate only one of them on: 2012-02-17 10:57:44
Not really. The easiest way is to do the rotation on the CPU...
What do you mean CPU can you tell a little more?
I'm not sure do I thinking about what you thinking
15  Java Game APIs & Engines / OpenGL Development / VertexArray with two square, how to rotate only one of them on: 2012-02-17 09:44:30
Hello.
I sure it's impossible but I prefer to ask.
I'm holding position two square in one FloatBuffer. Changing position only one of them is no problem but
if is there a way to rotate only one of them (and stel holding them in same FloatBuffer)?
In code below all square rotating
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  
float rquad;
   FloatBuffer vertexData;
   FloatBuffer colorData;
   public void gameLoop(){
      final int amountOfVertices = 64;
        final int vertexSize = 3;
        final int colorSize = 3;
     
        vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
        vertexData.put(new float[]{
            -0.5f,  0.5f,  0.0f,  0.5f,  0.5f,  0.0f,  0.5f, -0.5f,  0.0f, -0.5f, -0.5f,  0.0f,
             1.0f,  0.5f,  0.0f,  2.0f,  0.5f,  0.0f,  2.0f, -0.5f,  0.0f,  1.0f, -0.5f,  0.0f
             });
        vertexData.flip();

        colorData = BufferUtils.createFloatBuffer(amountOfVertices * colorSize);
        colorData.put(new float[]{
               1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
              1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f});
        colorData.flip();
       
      while(running) {
         update();
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glLoadIdentity();//Matrix reseting.
        GL11.glTranslatef(0.0f, 0.0f, -6.0f); // Move Right And Into The Screen
        GL11.glRotatef(rquad, 0.5f, 0.5f, -0.5f); // Rotate The Cube On X, Y & Z
       
         glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_COLOR_ARRAY);
         
            glVertexPointer(vertexSize, 0, vertexData);
            glColorPointer(colorSize, 0, colorData);
           
            glDrawArrays(GL_QUADS, 0, amountOfVertices);
           
            glDisableClientState(GL_COLOR_ARRAY);
            glDisableClientState(GL_VERTEX_ARRAY);
         
         Display.sync(60);
         Display.update();
      }
     
      Display.destroy();
   }
   
   private void update(){
      rquad-=0.05f;
   }
16  Java Game APIs & Engines / OpenGL Development / Re: VertexArray and FloatBuffer modification (beginner question) on: 2012-02-13 11:19:57
Lazy me
17  Java Game APIs & Engines / OpenGL Development / VertexArray and FloatBuffer modification (beginner question) on: 2012-02-13 10:01:05
Hello.
Can any one tell me how to modify FloatBuffer for example I'm keeping square in FloatBuffer now when i press some key
new square will be add to FloatBuffer and display.
Do modification FloatBuffer is possible and if so do this operation is slow?
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  
public void gameLoop(){
      final int vertices = 8;
        final int vertexSize = 3;
        final int colorSize = 3;
     
        FloatBuffer vertexData = BufferUtils.createFloatBuffer(vertices * vertexSize);
        vertexData.put(new float[]{-0.5f,  0.5f,  0.0f,  0.5f,  0.5f,  0.0f,  0.5f, -0.5f,  0.0f, -0.5f, -0.5f,  0.0f});
        vertexData.flip();

        FloatBuffer colorData = BufferUtils.createFloatBuffer(vertices * colorSize);
        colorData.put(new float[]{1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f});
        colorData.flip();
       
      while(running) {
         update();
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glLoadIdentity();
         GL11.glTranslatef(0.0f, 0.0f, -6.0f);
         
         glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_COLOR_ARRAY);
         
            glVertexPointer(vertexSize, 0, vertexData);
            glColorPointer(colorSize, 0, colorData);
           
            glDrawArrays(GL_QUADS, 0, vertices);
           
            glDisableClientState(GL_COLOR_ARRAY);
            glDisableClientState(GL_VERTEX_ARRAY);
         
         Display.sync(60);
         Display.update();
      }
     
      Display.destroy();
   }
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (18 views)
2014-04-15 18:08:23

BurntPizza (15 views)
2014-04-15 03:46:01

UprightPath (28 views)
2014-04-14 17:39:50

UprightPath (13 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (54 views)
2014-04-10 04:04:31

BurntPizza (111 views)
2014-04-08 23:06:04

tom_mai78101 (212 views)
2014-04-05 13:34:39

trollwarrior1 (181 views)
2014-04-04 12:06:45

CJLetsGame (187 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!