Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: picking/selecting primitive object and performing glut function on it...!

  1. #1
    Newbie Newbie
    Join Date
    Apr 2014
    Posts
    3

    picking/selecting primitive object and performing glut function on it...!

    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

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    I don't recommend you do a draw function inside your mouse event. Just use the glutPostRedisplay function to request a redraw event

  3. #3
    Newbie Newbie
    Join Date
    Apr 2014
    Posts
    3
    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) ??

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    c++ has a random number generator with the rand() function and various time functions

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •