Jump to content
  • Advertisement
Sign in to follow this  
love247

OpenGL please help me a little

This topic is 4136 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, how are you all?? i would appreciate it if you help me... i've been taking a computer graphics with openGL course for a week, but now i am asked to creat a 2D simple chess board, but i hardly know the basics.... well, i can draw the first square: . . . . . . #include<Gl/glut.h> void init(void) { glClearColor(1.0,1.0,1.0,0.0); glMatrixMode(GL_PROJECTION); gluOrtho2D(0,90,90,0); } void lineSegment(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0,0.0,0.0); glBegin( GL_QUADS ); glVertex2f( 15.0, 15.0 ); glVertex2f( -15.0, 15.0 ); glVertex2f( -15.0, -15.0 ); glVertex2f( 15.0, -15.0 ); glEnd(); glFlush(); } void main(int argc,char**argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(50,100); glutInitWindowSize(400,300); glutCreateWindow("An example openGL program"); init(); glutDisplayFunc(lineSegment); glutMainLoop(); } . . . . . . but how am i suppposed to continuo? sure there must be a loop to change the squares positions and the colors,but i just need some help in loops and parameters... anyone please?

Share this post


Link to post
Share on other sites
Advertisement
You can do something like this

void DrawSquaresOnALine(float TranslateASquareOnY, int NumberOfSquaresOfTheCheckBoardOnY )
{

for(int i = 0; i< NumberOfSquaresOfTheCheckBoardOnY; i++)
{
glTranslatef(0,Y,0);

DrawSquare();}

}

void DrawSquare(you can put dimensions here and color)
{
glColor3f(colorR, colorG, colorB);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glVertex3f(1,1,0);
glVertex3f(0,1,0);
}


Something like that - i hope this will give you a rough ideea about what you need to do.

You sould check neve.gamedev.net for lots of cool tutorials about opengl

Share this post


Link to post
Share on other sites
this is helpful, i will try to work on it, but i really don't know how to decide the (x,y) and positions of the squares... can i ask if anyone saw a whole code for drawing the board ?
i really don't love to take already done codes, but i will take it and study it.... so don't think i am a lazy person...
is there a code?

Share this post


Link to post
Share on other sites
First, there's a typo: it's nehe.gamedev.net

Like guvidu sai, you could try to implement a function that takes the number of squares in x and y direction, the colors as well as the size of each square as parameters.

The function could look as follows:


void drawCheckerBoard(int numSquaresX, int numSquaresY, //the number of squares in each of the 2 dimensions
float r1, float g1, float b1, //the first color
float r2, float g2, float b2, //the second color
float sizeX, float sizeY) //the size of each square
{
glBegin(glTriangles); //draw triangles instead of lines
for(int x = 0; x < numSquaresX; x++)
{
for(int y = 0; y < numSquaresY; y++)
{
//if x is even and y is odd (or the other way round) use color 1
if((x % 2) != (y % 2))
glColor3f(r1,g1,b1);
else //if x and y are even or odd use color 2
glColor3f(r2,g2,b2);

//draw the 2 triangles for a square (counter-clockwise winding)
//triangle 1
glVertex3f(x * sizeX, 0, y * sizeY);
glVertex3f(x * sizeX, 0, (y + 1) * sizeY);
glVertex3f((x + 1) * sizeX, 0, (y + 1) * sizeY);
//triangle 2
glVertex3f(x * sizeX, 0, y * sizeY);
glVertex3f((x + 1) * sizeX, 0, (y + 1)* sizeY);
glVertex3f((x + 1) * sizeX, 0, y * sizeY);
}
}
glEnd();
}



Note that this function draws the checker board in the xz-plane rather than the xy-plane.

Call this function in your render function:

//clear back buffer
//setup matrices

drawCheckerBoard(8, 8, 0, 0, 0, 1, 1, 1, 2, 2); //draws a 8x8 checker board with square sizes of 2x2 and black (0, 0, 0) and white (1, 1, 1) squares.

//end rendering with glFlush()

To go on:
-use a display list
-add a movable camera (e.g. gluLookAt)
-create some chessmen to play with (;))

Share this post


Link to post
Share on other sites
make function which draws quad
float s = 1.0f; // size of quad (length of side)

draw_quad(bool white)
{
if (white)
glColor3f(1, 1, 1);
else
glColor3f(0.3, 0.3, 0.3); // gray, so you can see it on black background

glBegin(GL_QUADS);
glVertex3f(0, 0, 0);
glVertex3f(s, 0, 0);
glVertex3f(s, s, 0);
glVertex3f(0, s, 0);
glEnd();
}



then you want to draw this quad 8x8 times
two 'for' loops will do that:

glLoadIdentity();
glTranslatef(0, 0, 0); // if you want to move the whole chessboard, change this

bool is_white = 0; // starts with black on 1a

for (int x=0; x<8; x++)
{
for (int y=0; y<8; y++)
{
glPushMatrix(); // push current matrix on stack
glTranslatef(x*s, y*s, 0); // translate to position of quad on board
draw_quad(is_white); // draw it
is_white = !is_white; // next color is always the other one
glPopMatrix(); // pop matrix
// (so the next loop will start at [0, 0] and can translate as needed)
}
}



i dont like writing code for people, but i think this should be good to make you understand
try to go through this code and understand every line
(if you dont, even if you try hard, you can write me)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!