PDA

View Full Version : picking/selecting primitive object and performing glut function on it...!



newbiee
04-17-2014, 06:25 AM
hello i'm a newbie to openlGL, plz guide me on my simple project.
i want to create a simple game(click game) in which a object(rectange) appears in the window and is to be clicked and when it is clicked a variable(score) is updated and object is drawn at a random postion and i also need the to add timer (game exits after 30seconds). basically in this game you try to click the object (rectangle) as fast you can in a specified time i.e 30s.....

i ve written a code(not full), could you plz check whether i'm going in the right direction,

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#define N 2
#define SIZE 1024

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}

void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho2D(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void mouse(int button, int state, int x, int y)
{
unsigned int score=0;
GLuint nameBuffer[SIZE];
GLint hits;
GLint viewport[4];
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
score++;
glInitNames();
glPushName(0);
glSelectBuffer(SIZE,nameBuffer);
glGetIntegerv(GL_VIEWPORT,viewport);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(xmin,xmax,ymin,ymax);
draw(GL_SELECT);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glFlush();
hits=glRenderMode(GL_RENDER);
processhits(hits,nameBUff);
glutPostRedisplay();
printf("score=%d",score);
}
}

void draw()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadname(1);
glColor3f(1.0, 1.0, 1.0);
glRectf(-25.0, -25.0, 25.0, 25.0);

}

void display()
{
glClear(GL_BUFFER_BIT);
draw();
glFlush();
}

void processhits(GLint hits,GLuint buffer[])
{
unsigned int i,j;
GLuint names,*ptr;
printf("")
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("click on the rectangle!")
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}

thank you and unlimited gratitude

tonyo_au
04-17-2014, 08:01 PM
I don't recommend you do a draw function inside your mouse event. Just use the glutPostRedisplay function to request a redraw event

newbiee
04-18-2014, 01:20 AM
thanks for your suggestion i'll try implementing it, i need one more help, how do i add timer and random positioning of the object(rectangle) ??

tonyo_au
04-19-2014, 09:47 PM
c++ has a random number generator with the rand (http://www.cplusplus.com/reference/cstdlib/rand/)() function and various time (http://www.cplusplus.com/reference/ctime/)functions