/* --------------------------------------------------------------------------
A program to draw tetris blocks using Bresenhams Algorithm
------------------------------------------------------------------------- */
#include<GL/freeglut.h>
#include <stdlib.h>
#include <stdio.h>
#include<math.h>
void init(void)
{
//set display-window background color to white
glClearColor(1.0,1.0,1.0,0.0);
//set projection paramaters
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,300.0,0.0,300.0);
}
void setPixel(GLint xCoordinate, GLint yCoordinate)
{
glBegin(GL_POINTS);
glVertex2i(xCoordinate,yCoordinate);
glEnd();
glFlush(); //executes all OpenGL functions as quickly as possible
}
//Bresenham line-drawing procedure for |m| < 1.0
void lineBres(GLint x0, GLint y0, GLint xEnd, GLint yEnd)
{
GLint dx = fabs(xEnd - x0);
GLint dy = fabs(yEnd - y0);
GLint p = 2 * dy - dx;
GLint twoDy = 2 * dy;
GLint twoDyMinusDx = 2 * (dy-dx);
GLint x,y;
// determine which endpoint to use as start position
if (x0 > xEnd)
{
x = xEnd;
y = yEnd;
xEnd = x;
}
else
{
x = x0;
y = y0;
}
setPixel(x,y);
while(x<xEnd)
{
x++;
if(p<0)
p += twoDy;
else
{
y++;
p += twoDyMinusDx;
}
setPixel(x,y);
}
}
void drawbox(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);//Red color.
int i=0,j=150,k =0;
GLint x0,y0,xEnd,yEnd;
for(j =150;j<200;j++)
for(i =100;i<150;i++)
{
glPointSize(10.0); // the width of the point.
GLint x0 =i; // initial point - x
GLint y0 = j; // '' '' - y
GLint xEnd = i;
GLint yEnd = j;
lineBres(x0,y0,xEnd,yEnd);
}
}
void drawZ(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);//Red color.
glPointSize(40.0); // the width of the point.
int i=0,j=150,k =0;
GLint x0,y0,xEnd,yEnd;
x0 = 100;
y0 = 150;
xEnd = 110;
yEnd = 150;
lineBres(x0,y0,xEnd,yEnd);
x0 = 121;
y0 = 171;
xEnd = 130;
yEnd = 171;
lineBres(x0,y0,xEnd,yEnd);
}
void drawiZ(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);//Red color.
glPointSize(40.0); // the width of the point.
int i=0,j=150,k =0;
GLint x0,y0,xEnd,yEnd;
x0 = 100;
y0 = 150;
xEnd = 110;
yEnd = 150;
lineBres(x0,y0,xEnd,yEnd);
x0 = 81;
y0 = 171;
xEnd = 90;
yEnd = 171;
lineBres(x0,y0,xEnd,yEnd);
}
void drawMyLine(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPointSize(10.0);
GLint x0 = 100;
GLint y0 = 150;
GLint xEnd = 150;
GLint yEnd = 150;
lineBres(x0,y0,xEnd,yEnd);
}
void drawT(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPointSize(40.0);
GLint x0 = 100;
GLint y0 = 150;
GLint xEnd = 150;
GLint yEnd = 150;
lineBres(x0,y0,xEnd,yEnd);
x0 = 125;
y0 = 120;
xEnd = 125;
yEnd = 120;
lineBres(x0,y0,xEnd,yEnd);
}
void drawJ(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPointSize(40.0);
GLint x0 = 100;
GLint y0 = 150;
GLint xEnd = 150;
GLint yEnd = 150;
lineBres(x0,y0,xEnd,yEnd);
x0 = 100;
y0 = 120;
xEnd = 100;
yEnd = 120;
lineBres(x0,y0,xEnd,yEnd);
}
void drawL(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPointSize(40.0);
GLint x0 = 100;
GLint y0 = 150;
GLint xEnd = 150;
GLint yEnd = 150;
lineBres(x0,y0,xEnd,yEnd);
x0 = 150;
y0 = 120;
xEnd = 150;
yEnd = 120;
lineBres(x0,y0,xEnd,yEnd);
}
void reshape (int width, int height) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window
glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed
glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we don't get any artifacts (cleaning up)
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes
glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we can start drawing shapes correctly
}
void keyPressed(unsigned char key, int x, int y)
{
glutDisplayFunc(drawbox);
int i =0;
while(key != 27)
{
if(key == 116)
{
if(i++ == 0&& key == 116)
glutDisplayFunc(drawbox);
else if(i++ == 1&& key == 116)
glutDisplayFunc(drawZ);
else if(i++ == 2)
glutDisplayFunc(drawiZ);
else if(i++ == 3)
glutDisplayFunc(drawJ);
else if(i++ == 4)
glutDisplayFunc(drawL);
else if(i++ == 5)
glutDisplayFunc(drawT);
else if(i++ == 6)
glutDisplayFunc(drawMyLine);
else{
glutDisplayFunc(drawMyLine);
break;
}
i++;
}
else
printf("%c",key);
}
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
void main(int argc, char**argv)
{
//initialize GLUT
glutInit(&argc,argv);
//initialize display mode
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//set display-window width & height
glutInitWindowSize(400,400);
//set display-window upper-left position
glutInitWindowPosition(0,0);
//create display-window with a title
glutCreateWindow("DDA by V");
//initialze OpenGL
init();
//keyboard func.
glutKeyboardFunc(keyPressed);
// glutDisplayFunc(drawiZ);
//glutReshapeFunc(reshape);
glutDisplayFunc(drawJ);
// glutKeyboardFunc(keyPressed);
//call graphics to be displayed on the window
//display everything and wait
glutMainLoop();
}
Hi,
I am new to OpenGL and GLUT programming.
So i started making sometetris blocks. and wanted to change the shape between the 7 blocks. But whenever i try to run the program. It goes into an infinite loop. I would be deeply obliged if someone could guide me through this.
P.S.: the remaining code just has the displayfunctions for the different shapes.(and they work perfectly fine without the keybooardFunc.)
Thanking you again and if the code is a bit unindented please excuse me. (It is amazing late at my place.)
a Newbie.