PDA

View Full Version : Trying to make a grid of Squares



avpai1992
10-18-2015, 11:52 PM
I am trying to make a grid of squares that resembles one large square. I wrote the code such that the height and width of the large square is constant and the number of smaller squares can be changed to increase " resolution". But all I'm getting is a white screen.Pls go through my code and help me figure this out.

Shapes.h


#include "glut.h"
#include <math.h>



void polygon(int v1, int v2, int v3 , int v4, float x, float y);
void Draw();

shapes.cpp


#include "Shapes.h"
#include"glut.h"
#include <stdio.h>


int x,y;
int res=25;
float hres=y/res;
float wres=x/res;

void polygon(int v1, int v2, int v3 , int v4, float x, float y) {
float vertices[][2]={{x, y+hres}, {x+wres, y+hres}, {x+wres, y}, {x, y}};
glBegin(GL_QUADS);
glVertex2fv(vertices[v1]);
glVertex2fv(vertices[v2]);
glVertex2fv(vertices[v3]);
glVertex2fv(vertices[v4]);
glEnd();
}

void Draw() {

int id = 1;

for(int y = 0; y < 5; y+=hres)
{

for(int x = 0; x < 5; x+=wres)
{

glPushName(id);
glColor3f(1.0, 0.0, 1.0); //Magenta
polygon(0,1,2,3,x,y);
/*glColor3f(1.0, 0.0, 0.0); //Red
polygon(4,5,6,7,x,y);
glColor3f(0.0, 1.0, 1.0); //Cyan
polygon(1,5,6,2,x,y);
glColor3f(1.0, 1.0, 0.0); //Yellow
polygon(0,4,7,3,x,y);
glColor3f(0.0, 0.0, 1.0); //Blue
polygon(3,2,6,7,x,y);
glColor3f(0.0, 1.0, 0.0); //Green
polygon(0,1,5,4,x,y);*/
id = id+1;
glPopName();
}
}
glFlush();
}


main.cpp


#include"glut.h"
#include<math.h>
#include "zpr.h"
#include <stdio.h>
#include "Shapes.h"

double rotate_y=0;
double rotate_x=0;
double rotate_z=0;


void RotateKeys( int key, int x, int y ) {

// Right arrow - increase rotation by 5 degree
if (key == GLUT_KEY_UP)
rotate_z += 5;

// Left arrow - decrease rotation by 5 degree
else if (key == GLUT_KEY_DOWN)
rotate_z -= 5;

else if (key == GLUT_KEY_LEFT)
rotate_y += 5;

else if (key == GLUT_KEY_RIGHT)
rotate_y -= 5;

else if (key == GLUT_KEY_PAGE_UP)
rotate_x -= 5;

else if (key == GLUT_KEY_PAGE_DOWN)
rotate_x += 5;


// Request display update
glutPostRedisplay();
}
void Initialize() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glRotatef( rotate_x, 1.0, 0.0, 0.0 );
glRotatef( rotate_y, 0.0, 1.0, 0.0 );
glRotatef( rotate_z, 0.0, 0.0, 1.0 );
glTranslatef(-.5,-.5,0);
Draw();
// glFlush();
glutSwapBuffers();
glutPostRedisplay();
}

int main(int iArgc, char** cppArgv) {
glutInit(&iArgc, cppArgv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(200, 200);
glutCreateWindow("PIXELS");
Initialize();
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutSpecialFunc(RotateKeys);
glutMainLoop();
return 0;
}