Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (603) Games in Android Showcase (171) games submitted by our members Games in WIP (652) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Two squares/cubes (basic question OpenGL 3.x LWJGL)  (Read 1971 times) 0 Members and 1 Guest are viewing this topic.
gndn

Junior Newbie

 « Posted 2013-01-27 10:03:29 »

Hello.
I hava basic qiestion how to display two squares?
Based on this tutorial
I know how to display square even cube but I don't know how to display two square (or cube).
I was thinking that I need only to set vertices
e.g.
 1  2  3  4  5  6  7  8  9  10 float[] vertices = {             -0.2f,  0.2f, 0f, 1f,             -0.2f, -0.2f, 0f, 1f,              0.2f, -0.2f, 0f, 1f,              0.2f, 0.2f, 0f, 1f,                          0.3f,  0.2f, 0f, 1f,              0.3f, -0.2f, 0f, 1f,              0.6f, -0.2f, 0f, 1f,              0.6f, 0.2f, 0f, 1f};

and colors:
 1  2  3  4  5  6  7  8  9  10  11 float[] colors = {             1f, 0f, 0f, 1f,             0f, 1f, 0f, 1f,             0f, 0f, 1f, 1f,             1f,   1f, 1f, 1f,                        1f, 0f, 0f, 1f,             0f, 1f, 0f, 1f,             0f, 0f, 1f, 1f,             1f,   1f, 1f, 1f            };

but it doesn't work the second square does not appear.
Do I need to do something else with indices array?
How to tell OpenGL that after drawing first square move on this position and draw another square.

Here is my whole code
Magn919

Junior Devvie

Medals: 6
Exp: 4 years

 « Reply #1 - Posted 2013-01-27 12:20:36 »

You need to add the other squares indices to your indices array.
The indices tells your vboi which of the vertices it should draw and what order to draw them in.

For every new problem, a new source of solutions has come to exist.
gndn

Junior Newbie

 « Reply #2 - Posted 2013-01-27 12:32:43 »

You need to add the other squares indices to your indices array.
The indices tells your vboi which of the vertices it should draw and what order to draw them in.

what do you mean other squares indices?
If you mean something like this
 1  2  3  4  5  6  7 byte[] indices = {            0, 1, 2,            2, 3, 0,                        0, 1, 2,            2, 3, 0,      };

it doesn't work.

I tested this before that's why I asked.
Magn919

Junior Devvie

Medals: 6
Exp: 4 years

 « Reply #3 - Posted 2013-01-27 12:51:43 »

Not quite, now you tell it to draw the same square twice.

Think of the vertices as if they are on a numbered list, the first 4 vertices in your vertices array are those of the first square.
The next 4 vertices are those of your second square.

So your indices should look something like this.
 1  2  3  4  5  6  7 byte[] indices = {            0, 1, 2,            2, 3, 0,                        4, 5, 6,            6, 7, 4,      };

The indices simply tell the gpu which of the vertices you have sent it, it should draw, and what order to draw them in.
The advantage of this approach is that you don't need to send the same vertex several times to join multiple triangles together.
As an example you only sent 4 vertices for each of the squares, but you need 6 to make a square by combining two triangles, but two of them is already on the same location.

For every new problem, a new source of solutions has come to exist.
gndn

Junior Newbie

 « Reply #4 - Posted 2013-01-27 13:11:52 »

Ok, true its working but when I want to draw cube I still have problem. My indices array If I'm not wrong should be
 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 byte[] indices = {            //FRONT            0, 1, 2,            2, 3, 0,            //RIGHT            0, 4, 7,            7, 0, 3,            //BACK            4, 7, 6,            6, 5, 4,            //LEFT            5, 6, 2,            2, 1, 5,            //TOP            4, 0, 5,            5, 0, 1,            //BOTTOM            7, 3, 6,            6, 3, 2,                                    //FRONT            8, 9, 10,            10, 11, 8,            //RIGHT            8, 12, 15,            15, 8, 11,            //BACK            12, 15, 14,            14, 13, 12,            //LEFT            13, 14, 10,            10, 9, 13,            //TOP            12, 8, 13,            13, 8, 9,            //BOTTOM            15, 11, 14,            14, 11, 10                              };

where my vertices and colors array is
 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 float[] vertices = CubeUtil.merge(CubeUtil.vertices(-1.5f,0.0f,0.0f,0.5f,0.5f,0.5f), CubeUtil.vertices(1.5f,0.0f,0.0f,0.5f,0.5f,0.5f));float[] colors = CubeUtil.merge(CubeUtil.colors(), CubeUtil.colors());..../**    * @param x position on X axis    * @param y position on Y axis    * @param z position on z axis    * @param sizeX width    * @param sizeY height    * @param sizeZ length    */   public static float[] vertices(float x, float y, float z, float sizeX, float sizeY, float sizeZ){      float[] vertices = {            //FRONT            sizeX+x,   sizeY+y, sizeZ+z,            -sizeX+x,  sizeY+y, sizeZ+z,             -sizeX+x, -sizeY+y, sizeZ+z,             sizeX+x,  -sizeY+y, sizeZ+z,             //BACK            sizeX+x,   sizeY+y, -sizeZ+z,             -sizeX+x,  sizeY+y, -sizeZ+z,            -sizeX+x, -sizeY+y, -sizeZ+z,             sizeX+x,  -sizeY+y, -sizeZ+z,             //LEFT            -sizeX+x,  sizeY+y,  sizeZ+z,             -sizeX+x, -sizeY+y,  sizeZ+z,             -sizeX+x, -sizeY+y, -sizeZ+z,             -sizeX+x,  sizeY+y, -sizeZ+z,            //RIGHT            sizeX+x,  sizeY+y,  sizeZ+z,             sizeX+x, -sizeY+y,  sizeZ+z,             sizeX+x, -sizeY+y, -sizeZ+z,             sizeX+x,  sizeY+y, -sizeZ+z,             //TOP            sizeX+x,  sizeY+y,  sizeZ+z,             -sizeX+x, sizeY+y,  sizeZ+z,             -sizeX+x, sizeY+y, -sizeZ+z,             sizeX+x,  sizeY+y, -sizeZ+z,             //BOTTOM            sizeX+x, -sizeY+y,  sizeZ+z,             -sizeX+x, -sizeY+y,  sizeZ+z,             -sizeX+x, -sizeY+y,  -sizeZ+z,             sizeX+x, -sizeY+y,  -sizeZ+z       };      return vertices;   }public static float[] merge(float[] a, float[] b) {      int aLen = a.length;      int bLen = b.length;      float[] c = new float[aLen + bLen];      System.arraycopy(a, 0, c, 0, aLen);      System.arraycopy(b, 0, c, aLen, bLen);      return c;   }public static float[] colors(){      float[] colors = {            0.583f,  0.771f,  0.014f,            0.609f,  0.115f,  0.436f,            0.327f,  0.483f,  0.844f,            0.822f,  0.569f,  0.201f,                        0.435f,  0.602f,  0.223f,            0.310f,  0.747f,  0.185f,            0.597f,  0.770f,  0.761f,            0.559f,  0.436f,  0.730f,                        0.359f,  0.583f,  0.152f,            0.483f,  0.596f,  0.789f,            0.559f,  0.861f,  0.639f,            0.195f,  0.548f,  0.859f,                        0.014f,  0.184f,  0.576f,            0.771f,  0.328f,  0.970f,            0.406f,  0.615f,  0.116f,            0.676f,  0.977f,  0.133f,                        0.971f,  0.572f,  0.833f,            0.140f,  0.616f,  0.489f,            0.997f,  0.513f,  0.064f,            0.945f,  0.719f,  0.592f,                        0.543f,  0.021f,  0.978f,            0.279f,  0.317f,  0.505f,            0.167f,  0.620f,  0.077f,            0.347f,  0.857f,  0.137f      };      return colors;   }
Magn919

Junior Devvie

Medals: 6
Exp: 4 years

 « Reply #5 - Posted 2013-01-27 13:40:51 »

You only have the vertices and colors for a single cube.
Ideally you could just draw the single cube twice and adjust its location with the model matrix.

Edit: Oh didn't see you weren't using matrices, maybe you should consider using some.

For every new problem, a new source of solutions has come to exist.
gndn

Junior Newbie

 « Reply #6 - Posted 2013-01-27 14:10:39 »

You only have the vertices and colors for a single cube.
Hmmm... not quite function "CubeUtil.vertices" returns vertices for whole cube, function "CubeUtil.merge" marge
two array so in variable "vertices" i have two arrays. First param in "CubeUtil.vertices" sets cube on differents X axis positions (-1.5f and 1.5f). Same situation in colors array.
Or maybe there is some magic mistake which I don't see.

Ideally you could just draw the single cube twice and adjust its location with the model matrix.
Edit: Oh didn't see you weren't using matrices, maybe you should consider using some.
Sounds interesting but I'm to new in OpenGL and I don't know how to do this.

EDIT.
I started checking my code in indices and vertices functions and it looks like there is a bug in this functions.