I'm learning OpenGL for a class and for the lulz and I'm trying to write a program that does the following:
-Open a 500x500 window with a 10x10 grid of black squares with a 1 pixel white border around them. (done, works)
-Make the window resize without it going wonky. (done, works)
-When you click on a square, it changes colour. (broken)
When you click on a square, it will sometimes make a square change colour somewhere else on the grid. Only the bottom-leftmost square will change colour when you click on it.
I know it's a problem with the coordinate system, but I have no idea how, why, or where in my code this is happening. Can someone help me figure out where the problem is?
My code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h> // Header File For Windows
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLU32 Library
#include <gl\glut.h> // Header File For The GLUT32 Library
#include <gl\glaux.h> // Header File For The GLaux Library
// Initial display-window size.
GLsizei winWidth = 500, winHeight = 500;
GLfloat red[10][10];
GLfloat green[10][10];
GLfloat blue[10][10];
GLuint gridSquare;
void drawQuads(void)
{
GLint i, j;
int m = 0;
int n = 0;
glBegin(GL_QUADS); // Draw some quads!
for(i = 0; i < winWidth; i=i+(winWidth/10))
{
for(j = 0; j < winHeight; j=j+(winHeight/10))
{
glColor3f (red[n][m], green[n][m], blue[n][m]); // Set fill color for quads to black.
glVertex2i(i+1, j+1); // Top Left
glVertex2i((i+(winWidth/10)), j+1); // Top Right
glVertex2i((i+(winWidth/10)), (j+(winHeight/10))); // Bottom Right
glVertex2i(i+1, (j+(winHeight/10))); // Bottom Left
n++;
}
m++;
}
glEnd(); // Done Drawing The Quads
glFlush ( ); //Process, OpenGL. DO IT NOW.
printf("Done with drawQuads\n");
}
void init(void)
{
glClearColor (1.0, 1.0, 1.0, 0.0); // Display-window color = white.
for(int x = 0; x < 10; x++)
{
for(int y = 0; y < 10; y++)
{
red[x][y] = 0.0;
green[x][y] = 0.0;
blue[x][y] = 0.0;
}
}
printf("Done with init\n");
}
void winReshapeFcn (int newWidth, int newHeight)
{
winWidth = newWidth;
winHeight = newHeight;
glMatrixMode (GL_PROJECTION);
glLoadIdentity ( );
gluOrtho2D (0.0, (GLdouble) newWidth, 0.0, (GLdouble) newHeight);
glViewport(0, 0, newWidth, newHeight);
glClear (GL_COLOR_BUFFER_BIT);
printf("Done with winReshapeFnc\n");
}
void mouseFunc(GLint button, GLint action, GLint xMouse, GLint yMouse)
{
if(action == GLUT_UP)
{ double w, h, width, height = 0.0;
int squareX, squareY = 0;
width = winWidth;
height = winHeight;
w = width/10;
h = height/10;
yMouse = winHeight - yMouse;
printf("Mouse click at %d, %d\n", xMouse, yMouse);
squareX = (int)(xMouse/w);
squareY = (int)(yMouse/h);
red[squareX][squareY] = .5;
green[squareX][squareY] = .5;
blue[squareX][squareY] = .5;
printf("R: %d G: %d B: %d\n", red[squareX][squareY],green[squareX][squareY],blue[squareX][squareY]);
printf("squareX: %d, squareY: %d\n",squareX,squareY);
glutPostRedisplay();
printf("Done with mouseFunc\n");
}
}
void main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (100, 100);
glutInitWindowSize (winWidth, winHeight);
glutCreateWindow ("The GRIDS that is killing ED");
init ( );
glutDisplayFunc (drawQuads);
glutReshapeFunc (winReshapeFcn);
glutMouseFunc(mouseFunc);
glutMainLoop ( );
}