5x5x5 Chessboard in OpenGL

Started by
5 comments, last by jumz 11 years, 2 months ago

Hi all,

I really need some help here.

I am developing a 3d chess game, and I have chosen to use the 5x5x5 variant.However, I am at a loose end as I just started learning opengl and c++, and woud really appreciate some help here.

I have designed my chess pieces in 3ds max, but i need to export them into opengl. Also, how do I go about creating a 5x5x5 3D board in opengl? I any help will be appreciated pls.sad.png sad.png sad.png

Advertisement
Here are a few things I think you should understand when building a 3D game.

First of all knowing 3D transformations is a must. Space and Matrix Transformations - Building a 3D Engine

This website has some good tutorials for starting opengl. NeHe I have heard somebody say these are a little outdated but I still feel they help out with learning the basics.

As for loading the pieces into your game. There is no standard format for loading into OpenGL, rather opengl supplies methods of drawing geometry that is very flexible. You are responsible for finding a way to getting the 3d model data into your game. With that being said I would recommend .obj files as being a good starting point. You should be able to export as an obj and may even find an existing obj loader online.
My current game project Platform RPG

OBJ file format I find, is the easiest type of model file to parse. However you will have to make the reader yourself since there are no standard ones. This type of parsing can be difficult in C++ but there are lots of examples. Many of which you can pull straight down from the internet and use out of the box.

Remember to make sure that you include the vertex normal's when you export the file to obj format. I don't know about 3ds Max, but I know that in blender this is not done by default. You will need the vertex normal's for lighting in opengl.

If you were looking for a video tutorial on loading OBJ files in C++, here is a good one:

">

All of the tutorials in this series will be quite relevant for you as you are just getting started with opengl.

As for making the 3D chess board, I would first think about how you want each level to be viewed, for example:

- Do you want to view one level at a time ?

- Do you want a free camera, and you rotate around the stack of levels freely with the mouse, each level fading out so you can see the one below it ?

- Maybe you want to display each level in a different area of the screen

Be sure to experiment with different looks and feels to find the one that you like the most.

I'm trying to create 5x5x5 chessboard in OpenGL. I have been able to design a 5x6, and 8x8 but the code just won't work for the 5x5. Can someone look over the code and maybe point out where my mistake is as the width just wont change to 5?

Here is what I have been able to come up with so far:

#include<windows.h>
#include <glut.h>
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
float _angle = 0;
double i;
int k;
bool white_or_black;
GLuint _displayListId_blackArea; //The OpenGL id of the display list
GLuint _displayListId_whiteArea; //The OpenGL id of the display list
void handleKeypress(unsigned char key,int x,int y)
{
switch(key)
{
case 27:
exit(0);
}
}
void handleResize(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(double)w/(double)h,1.0,200);
gluLookAt(0.0f,5.5f, 15.0f,
0.0f,0.0f,0.0f,
0.0f,1.0f,0.0f);
}
void draw_BlackArea()
{
// glPushMatrix();
//glTranslatef(1.5f,0.0f,0.0f);
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glTranslatef(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.50f,0.0f,0.0f);
glVertex3f(1.5f,0.3f,0.0f);
glVertex3f(0.0f,0.3f,0.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,0.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(1.5f,0.0f,0.0f);
glVertex3f(1.5f,0.0f,-1.5f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(1.5f,0.3f,0.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,-1.5f);
glVertex3f(1.50f,0.0f,-1.5f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.50f,0.0f,0.0f);
glVertex3f(1.5f,0.0f,-1.5f);
glVertex3f(0.0f,0.0f,-1.5f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.3f,0.0f);
glVertex3f(1.50f,0.3f,0.0f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glEnd();
// glPopMatrix();
}
void draw_whiteArea()
{
// glPushMatrix();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glTranslatef(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.50f,0.0f,0.0f);
glVertex3f(1.5f,0.3f,0.0f);
glVertex3f(0.0f,0.3f,0.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,0.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(1.5f,0.0f,0.0f);
glVertex3f(1.5f,0.0f,-1.5f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(1.5f,0.3f,0.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,-1.5f);
glVertex3f(1.50f,0.0f,-1.5f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.50f,0.0f,0.0f);
glVertex3f(1.5f,0.0f,-1.5f);
glVertex3f(0.0f,0.0f,-1.5f);
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0f,1.0f,1.0f);
glVertex3f(0.0f,0.3f,0.0f);
glVertex3f(1.50f,0.3f,0.0f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glEnd();
// glPopMatrix();
}
void initRendering()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glClearColor(0.0f,0.0f,0.2f,1.0f);
//Set up a display list for drawing a cube
_displayListId_blackArea = glGenLists(1); //Make room for the display list
glNewList(_displayListId_blackArea, GL_COMPILE); //Begin the display list
draw_BlackArea(); //Add commands for drawing a black area to the display list
glEndList(); //End the display list
//Set up a display list for drawing a cube
_displayListId_whiteArea = glGenLists(2); //Make room for the display list
glNewList(_displayListId_whiteArea, GL_COMPILE); //Begin the display list
draw_whiteArea(); //Add commands for drawing a black to the display list
glEndList(); //End the display list
}
void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-2.5*1.5, 0.0, 2.5*1.5);
const unsigned BOARD_SIZE = 5;
for(unsigned i = 0; i < BOARD_SIZE; ++i){
for(unsigned j = 0; j < BOARD_SIZE; ++j){
drawBoardCell(i,j, (i*BOARD_SIZE + j) % 2 == 0);
}
}
}
void drawBoardCell(const int row, const int col, const bool isBlack){
glPushMatrix();
glTranslatef(col+1.5,0.0,row);
const int callId = isBlack ? _displayListId_blackArea : _displayListId_whiteArea;
glCallList(callId);
glPopMatrix();
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(400,400);
glutCreateWindow("chessboard");
initRendering();
glutFullScreen();
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMainLoop();
return 0;
}

Here are a few things I think you should understand when building a 3D game.

First of all knowing 3D transformations is a must. Space and Matrix Transformations - Building a 3D Engine

This website has some good tutorials for starting opengl. NeHe I have heard somebody say these are a little outdated but I still feel they help out with learning the basics.

As for loading the pieces into your game. There is no standard format for loading into OpenGL, rather opengl supplies methods of drawing geometry that is very flexible. You are responsible for finding a way to getting the 3d model data into your game. With that being said I would recommend .obj files as being a good starting point. You should be able to export as an obj and may even find an existing obj loader online.

Thank you so much. The website supplied has amazing tutorials. I'll def look into it!

Yes, I am thinking of exporting the files in .obj format. Main problem is solving the issue of the board firstsmile.png

I'm trying to create 5x5x5 chessboard in OpenGL. I have been able to design a 5x6, and 8x8 but the code just won't work for the 5x5. Can someone look over the code and maybe point out where my mistake is as the width just wont change to 5?

Here is what I have been able to come up with so far:

#include<windows.h>
#include <glut.h>
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
float _angle = 0;
double i;
int k;
bool white_or_black;
GLuint _displayListId_blackArea; //The OpenGL id of the display list
GLuint _displayListId_whiteArea; //The OpenGL id of the display list
void handleKeypress(unsigned char key,int x,int y)
{
switch(key)
{
case 27:
exit(0);
}
}
void handleResize(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(double)w/(double)h,1.0,200);
gluLookAt(0.0f,5.5f, 15.0f,
0.0f,0.0f,0.0f,
0.0f,1.0f,0.0f);
}
void draw_BlackArea()
{
// glPushMatrix();
//glTranslatef(1.5f,0.0f,0.0f);
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glTranslatef(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.50f,0.0f,0.0f);
glVertex3f(1.5f,0.3f,0.0f);
glVertex3f(0.0f,0.3f,0.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,0.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(1.5f,0.0f,0.0f);
glVertex3f(1.5f,0.0f,-1.5f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(1.5f,0.3f,0.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,-1.5f);
glVertex3f(1.50f,0.0f,-1.5f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.50f,0.0f,0.0f);
glVertex3f(1.5f,0.0f,-1.5f);
glVertex3f(0.0f,0.0f,-1.5f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.3f,0.0f);
glVertex3f(1.50f,0.3f,0.0f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glEnd();
// glPopMatrix();
}
void draw_whiteArea()
{
// glPushMatrix();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glTranslatef(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.50f,0.0f,0.0f);
glVertex3f(1.5f,0.3f,0.0f);
glVertex3f(0.0f,0.3f,0.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,0.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(1.5f,0.0f,0.0f);
glVertex3f(1.5f,0.0f,-1.5f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(1.5f,0.3f,0.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,-1.5f);
glVertex3f(1.50f,0.0f,-1.5f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.50f,0.0f,0.0f);
glVertex3f(1.5f,0.0f,-1.5f);
glVertex3f(0.0f,0.0f,-1.5f);
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0f,1.0f,1.0f);
glVertex3f(0.0f,0.3f,0.0f);
glVertex3f(1.50f,0.3f,0.0f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glEnd();
// glPopMatrix();
}
void initRendering()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glClearColor(0.0f,0.0f,0.2f,1.0f);
//Set up a display list for drawing a cube
_displayListId_blackArea = glGenLists(1); //Make room for the display list
glNewList(_displayListId_blackArea, GL_COMPILE); //Begin the display list
draw_BlackArea(); //Add commands for drawing a black area to the display list
glEndList(); //End the display list
//Set up a display list for drawing a cube
_displayListId_whiteArea = glGenLists(2); //Make room for the display list
glNewList(_displayListId_whiteArea, GL_COMPILE); //Begin the display list
draw_whiteArea(); //Add commands for drawing a black to the display list
glEndList(); //End the display list
}
void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-2.5*1.5, 0.0, 2.5*1.5);
const unsigned BOARD_SIZE = 5;
for(unsigned i = 0; i < BOARD_SIZE; ++i){
for(unsigned j = 0; j < BOARD_SIZE; ++j){
drawBoardCell(i,j, (i*BOARD_SIZE + j) % 2 == 0);
}
}
}
void drawBoardCell(const int row, const int col, const bool isBlack){
glPushMatrix();
glTranslatef(col+1.5,0.0,row);
const int callId = isBlack ? _displayListId_blackArea : _displayListId_whiteArea;
glCallList(callId);
glPopMatrix();
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(400,400);
glutCreateWindow("chessboard");
initRendering();
glutFullScreen();
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMainLoop();
return 0;
}

The main error i have here is that it keeps complaining that the function drawBlackCell does not take three argumentsunsure.png > How do I fix this pls?

Any help will be appreciated

OBJ file format I find, is the easiest type of model file to parse. However you will have to make the reader yourself since there are no standard ones. This type of parsing can be difficult in C++ but there are lots of examples. Many of which you can pull straight down from the internet and use out of the box.

Remember to make sure that you include the vertex normal's when you export the file to obj format. I don't know about 3ds Max, but I know that in blender this is not done by default. You will need the vertex normal's for lighting in opengl.

If you were looking for a video tutorial on loading OBJ files in C++, here is a good one:

">

All of the tutorials in this series will be quite relevant for you as you are just getting started with opengl.

As for making the 3D chess board, I would first think about how you want each level to be viewed, for example:

- Do you want to view one level at a time ?

- Do you want a free camera, and you rotate around the stack of levels freely with the mouse, each level fading out so you can see the one below it ?

- Maybe you want to display each level in a different area of the screen

Be sure to experiment with different looks and feels to find the one that you like the most.

Thanks for the tutorial link. Yes, I will be parsing the .obj format.

As for the 3D chess board, pls check out my code so far and maybe point out where I'm missing it?

Thank you!

This topic is closed to new replies.

Advertisement