PDA

View Full Version : Help Basic 2D Grid



SARVPRIYE SONI
06-15-2011, 01:33 AM
HI
I am new to openGL. I need to make gui of my project in which, i just need to show a 2d grid to the user of user specified dimension, then user will click the grid cells and there color and correspondingly there value will be changed in a 2d array.

Please Help.

abhishek bansal
06-15-2011, 03:03 AM
then what is the problem???

SARVPRIYE SONI
06-15-2011, 06:58 AM
I searched net and modified the code there and presently am able to enter the no. of rows and columns and that grid is created.
Now, how can I add the functionality to click a cell and its color gets changed?
Please help

#include <iostream>
#include <stdio.h>
#include <GL/glut.h>
using namespace std;
int antialiasing = 0;
int rgridSize=9;
int cgridSize=9;
GLuint selectedPoint = ~0;
int winWidth, winHeight;
GLuint selectBuffer[64];
GLdouble modelMatrix[16], projMatrix[16];
GLint viewport[4];
GLfloat grid2x2[2][2][3] =
{
{
{-2.0, -2.0, 0.0},
{2.0, -2.0, 0.0}},
{
{-2.0, 2.0, 0.0},
{2.0, 2.0, 0.0}}
};
GLfloat *grid = &amp;grid2x2[0][0][0];
int uSize = 2;
int vSize = 2;


void setupMesh(void)
{
glEnable(GL_MAP2_VERTEX_3);
glMapGrid2f(cgridSize, 0.0, 1.0, rgridSize, 0.0, 1.0);
}

void evaluateGrid(void)
{
glColor3f(1.0, 1.0, 1.0);
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, uSize, 0, 1, uSize * 3, vSize, grid);
glEvalMesh2(GL_LINE, 0, cgridSize, 0, rgridSize);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
evaluateGrid();

glutSwapBuffers();
}

void ortho(void)
{
if (winWidth <= winHeight)
glOrtho(-4.0, 4.0, -4.0 * (GLfloat) winHeight / (GLfloat) winWidth,
4.0 * (GLfloat) winHeight / (GLfloat) winWidth, -4.0, 4.0);
else
glOrtho(-4.0 * (GLfloat) winWidth / (GLfloat) winHeight,
4.0 * (GLfloat) winWidth / (GLfloat) winHeight, -4.0, 4.0, -4.0, 4.0);
}
GLuint pick(int x, int y)
{
int hits;

(void) glRenderMode(GL_SELECT);
glInitNames();
glPushName(~0);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix(x, winHeight - y, 8.0, 8.0, viewport);
ortho();
glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
hits = glRenderMode(GL_RENDER);
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
winWidth = w;
winHeight = h;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
ortho();
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
viewport[0] = 0;
viewport[1] = 0;
viewport[2] = winWidth;
viewport[3] = winHeight;
}

void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) {
selectedPoint = pick(x, y);
} else {
selectedPoint = -1;
}
}
}

static void
keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
}
}

enum {
M_TOGGLE_ANTIALIASING, M_QUIT
};

void menu(int value)
{
switch (value) {
case M_TOGGLE_ANTIALIASING:
if (antialiasing) {
antialiasing = 0;
glDisable(GL_BLEND);
glDisable(GL_LINE_SMOOTH);
glDisable(GL_POINT_SMOOTH);
} else {
antialiasing = 1;
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POINT_SMOOTH);
}
break;
case M_QUIT:
exit(0);
break;
}
glutPostRedisplay();
}

int main(int argc, char **argv)
{
cout<<"\nEnter row and column no.:\n";
cin>>rgridSize>>cgridSize;

glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("editgrid");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutCreateMenu(menu);
glutAddMenuEntry("Toggle antialiasing", M_TOGGLE_ANTIALIASING);
glutAddMenuEntry("Quit", M_QUIT);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glSelectBuffer(sizeof(selectBuffer), selectBuffer);
setupMesh();
glutMainLoop();
return 0;
}

vivek vyas
06-15-2011, 09:02 AM
if you find this
http://www.opengl.org/resources/code/samples/redbook/pickdepth.c
then the solution of your problem is also in this code