在OpenGL中使用glpecspective,gluLookAt [英] use glpecspective, gluLookAt in OpenGL

查看:95
本文介绍了在OpenGL中使用glpecspective,gluLookAt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我画1个四边形,这是成功的.当我调整窗口大小时,鼠标位置不好.它不会在鼠标所在的位置画一条线.我想使用glPerspective,gluLookAt

感谢您的帮助:
我的代码:

#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;
 }

解决方案

如果我有x,y,z轴,我可以得到mouse2d到轴的距离?如何?

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;
 }

解决方案

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


这篇关于在OpenGL中使用glpecspective,gluLookAt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆