0

i want to create a game similare to this one http://www.sciencekids.co.nz/gamesactivities/movinggrowing.html

1-how to move the image according to coordinates from the mouse? i know how to translate an object that i drow like polygon but not images?

2- how to check if its in right plce and make it stuck there i used gltranslate but the image didn't move , and tried copypixl but it kept copying the image every where the mouse hits .

 #include <windows.h>
#include <GL/glut.h>
#include <gl/glaux.h>

#pragma comment(lib,"glaux.lib")

AUX_RGBImageRec *image ;
GLsizei wh = 600, ww = 800;
//-----------------------------------------------------------------------------------------------
void display(void){

    image = new AUX_RGBImageRec();
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    image = auxDIBImageLoad(L"boy.bmp");
    glDrawPixels(image->sizeX, image->sizeY, GL_RGB,
    GL_UNSIGNED_BYTE,image->data);

    glFlush();
}
//-----------------------------------------------------------------------------------------------
void mouse (int btn , int state , int x , int y){

    int h = wh-y ;
    if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN){

        /*glPushMatrix();*/
        /*glTranslatef(x , h , 0);*/
        glRasterPos2i(x,h);
        glCopyPixels(0,0,image->sizeX, image->sizeY,GL_COLOR);
        //glPopMatrix();}
        if(btn == GLUT_LEFT_BUTTON && state == GLUT_UP) {

          // check if it is in right place to stuck it else will retuen to original place  
      }
    }glFlush();

}
//-----------------------------------------------------------------------------------------------
void intiate(){

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D(0,ww,0,wh);
}
//-----------------------------------------------------------------------------------------------
void main()
{
glutInitDisplayMode (GLUT_RGB);  
glutInitWindowSize (ww, wh);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Inside my Body");
intiate();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
} 
genpfault
  • 51,148
  • 11
  • 85
  • 139

1 Answers1

1

1-how to move the image according to coordinates from the mouse? i know how to translate an object that i drow like polygon but not images?

First and foremost: Please don't use glDrawPixels. Use textures instead, applied to regular geometry. OpenGL doesn't deal with user input, so you'll have to implement the mouse interaction yourself. Never place OpenGL drawing commands in the input event handler. In the input event handler, change some variables, set flags, etc. but don't draw. With the flags set, issue a redraw so that you can draw the updates.

2- how to check if its in right plce and make it stuck there

OpenGL is a drawing API, not a scene graph. You tell it what to draw and that's it. If things are not looking like you're expecting them, then you're not drawing them right.

Pro-Tip: Place all OpenGL commands in the drawing routine. Do not call any OpenGL functions from anywhere else. The stuff you do in your initiate function actually belongs in the display function.

Stephen
  • 3,515
  • 5
  • 27
  • 33
datenwolf
  • 159,371
  • 13
  • 185
  • 298