Click here to Skip to main content
16,014,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I draw 1 quads.It is success. When I re-size the window, the mouse position is bad. It does not draw a line where the mouse is. I want to use glPerspective,gluLookAt

Thanks for the help:
my code:
#include <stdio.h>
#include <glut.h>
int numLines;
typedef enum state{waitingforclick,clickedonce};

typedef struct point
{
int x;
int y;
}point;

 point line[256][2];

 int gState=waitingforclick;
 bool lineisvalid=false;
 int gHeight;
 float gColor[3]={0,1,0};
 
 void drawlines()
 {
    glColor3fv(gColor);
    glBegin(GL_QUADS);
    for(int i = 0;i<=numLines; i++)
    {
    glVertex2i(line[i][0].x,gHeight -line[i][0].y);
    glVertex2i(line[i][1].x,gHeight-line[i][1].y);
 }
 glEnd();
 }

 void display()
 {
    glClear(GL_COLOR_BUFFER_BIT);
    drawlines();
    glutSwapBuffers();
 }
//for menu.you should ignore it
 void menufunc(int val)
 {
    switch(val)
    {
    case 0:
        gColor[0] = 1;
        gColor[1] = 0;
        gColor[2] = 0;
        break;
    case 1:
        gColor[0] = 0;
        gColor[1] = 1;
        gColor[2] = 0;
        break;
    case 2:
        gColor[0] = 0;
        gColor[1] = 0;
        gColor[2] = 1;
        break;
    }
 }
//for menu.you should ignore it
 void createMenu ()
 {
    int menu = glutCreateMenu(menufunc);
    glutAddMenuEntry("Red", 0);
    glutAddMenuEntry("green", 1);
    glutAddMenuEntry("Blue", 2);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
 }
 void init()
 {
    glClearColor(0,0,0,1);
    glMatrixMode(GL_PROJECTION); 
    //I can change glOrtho by gluPerspective ?
    glOrtho(-1, 1.0, -1, 1.0, -1.0, 1.0);
    numLines=- 1;
    glMatrixMode(GL_MODELVIEW);
    createMenu();
 }

 void reshape(int width, int height)
 {
    gHeight = height;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //I can change gluOrtho2D by gluLookAt ?
    gluOrtho2D(0, width, 0, height);
    glMatrixMode(GL_MODELVIEW);
 }


void mouseclick(int button, int state,int x, int y)
 {
    //when mouse click
    if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
    {
        switch (gState)
        {
            case waitingforclick:
                printf("1 clicked");
                ++numLines;
                //line position
                line[numLines][0].x = x;
                line[numLines][0].y = y;
                line[numLines][1].x = x;
                line[numLines][1].y = y;
                line[numLines][2].x = x;
                line[numLines][3].y = y;
                gState++;
                break;
            case clickedonce:
                printf("2 clicked");
                line[numLines][1].x = x;
                line[numLines][1].y = y;
                gState = waitingforclick;
                break;
        }
    }
    glutPostRedisplay ();
 }

 void mousedrag(int x, int y)
 {
     if(gState == clickedonce)
     {
         line[numLines][1].x = x;
         line[numLines][1].y = y;
     }
     glutPostRedisplay();
 }

 int main(int argc, char ** argv)
 {
     glutInit(& argc, argv);
     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
     glutInitWindowPosition(100,100);
     glutInitWindowSize(500,500);
     glutCreateWindow("App");
     glutReshapeFunc(reshape);
     glutDisplayFunc(display);
     glutMouseFunc(mouseclick);
     glutPassiveMotionFunc(mousedrag);
     init();
     glutMainLoop();
     return 0;
 }
Posted
Updated 7-Jul-11 22:09pm
v4
Comments
Slacker007 7-Jul-11 6:52am    
Edited for readability.

1 solution

If I have axis x,y,z,I can get distance mouse2d to axes?how?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900