Skip to content

Instantly share code, notes, and snippets.

@mimukit
Created June 12, 2016 17:57
Show Gist options
  • Save mimukit/ee60ea23a67f5bc298a05f38ab73303a to your computer and use it in GitHub Desktop.
Save mimukit/ee60ea23a67f5bc298a05f38ab73303a to your computer and use it in GitHub Desktop.
ChessBoard Drawing Using Loop with OpenGL
#include <stdio.h>
#include <GL/glut.h>
int x = 50, y = 50;
bool isBlack = true;
void whiteBox(int x, int y)
{
glBegin(GL_LINE_LOOP);
glVertex2i(x, y);
glVertex2i(x, y + 50);
glVertex2i(x + 50, y + 50);
glVertex2i(x + 50, y);
glEnd();
}
void blackBox(int x, int y)
{
glBegin(GL_POLYGON);
glVertex2i(x, y);
glVertex2i(x, y + 50);
glVertex2i(x + 50, y + 50);
glVertex2i(x + 50, y);
glEnd();
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
glPointSize(1.0);
for (int i = 0; i < 8; i++)
{
if (i % 2 == 0)
{
isBlack = true;
}
else
{
isBlack = false;
}
for (int j = 0; j < 8; j++)
{
if (isBlack)
{
blackBox(x, y);
isBlack = false;
}
else
{
whiteBox(x, y);
isBlack = true;
}
x += 50;
}
y += 50;
x = 50;
}
blackBox(100, 100);
whiteBox(150, 100);
glFlush();
}
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 150);
glutCreateWindow("Draw an 8X8 chess board using loop");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment