Drawing an [10][10] Field with Cubes

i’m programming battleship for Opengl.

I have already the game logic and it works fine.

Now im programming the graphics and i want to draw a field 10x10.

I do this with the drawGameField();

Problem that i don’t understand:

Line 71: why i must translate with this strange numbers? isnt there a formula ?

Line 98: again, why i must devide with 2 ?

Line 101: why *2 ?

I dont understand it.
Later i want to navigate the fields by pressing keys, and i dont know if it will really work with these numbers.

Next Problem is, that i got 10 fields but the lines are overlapping.

Field looks like this:

Thanks :slight_smile:

Rather than asking us, you should answer these yourself because it is you who has made those decisions.

Presumably you were given the code as an assignment for a lab or you are using source code. Can you post the code instead of attaching it? It would make it much easier to see what is happening.

I’m assuming your 2nd line number is actually 116, not 98.

116: glTranslatef((gamewidth*i)/2,0,0);

Since your ‘reshape’ function sets the glMatrixMode to GL_MODELVIEW, all your calls to glTranslate are in fact telling the display where to draw.

I’d take another look at your screen size constant definitions:

#define gamewidth 0.7
#define linehigh 4
#define linewidth 0.05

Assuming your game is in landscape mode, it may be better to redefine your constants similar to this:

#define gamewidth 1.3
#define gameheight 1
#define linehigh 4
#define linewidth 0.05

For some reason you are scaling a cube into a line and using that to draw the grid. It would be much easier to use GL_LINES.

Draw horizontal:

glVertex3f(0.0f, i*16, 0.0f); // origin of the line
glVertex3f(640.0f, i*16, 0.0f); // ending point of the line
glEnd( );

Draw vertical:

glVertex3f(i*16, 0.0f, 0.0f); // origin of the line
glVertex3f(i*16, 480.0f, 0.0f); // ending point of the line
glEnd( );

Assuming your screen is 640x480.

Who gave you the misleading idea to draw lines with an elongated cube? Very very bad practise :slight_smile: If in doubt, search online for ‘drawing line primitives in openGL’. Once you have got the grid drawn then worry about moving objects etc.