简体   繁体   English

在类似2D openGL图形的环境中移动“相机”

[英]Moving “camera” in a 2D openGL graph-like environment

I'm using a pathfinding algorithm(D* lite, author: James Neufeld) that I found with a graphical visualization. 我正在使用通过图形可视化找到的寻路算法(D * lite,作者:James Neufeld)。 For the robot's functionality the graphical visualization isn't needed but for developing it is a great tool. 对于机器人的功能,不需要图形化可视化,但是对于开发它来说,这是一个很好的工具。 It's working great, but by default you can only see the first quadrant of the cartesian plane. 效果很好,但是默认情况下,您只能看到笛卡尔平面的第一象限。 I need to be able to see the 4 quadrants. 我需要能够看到4个象限。 Changing the variable "scale" in the code works like zooming in/out but it stays in the 1st quadrant anyways. 在代码中更改变量“ scale”的作用类似于放大/缩小,但无论如何它始终位于第一象限。 I don't want to shift all the points(foreach coordinate: x+=100 and y+=100) to move to that quadrant, graphically it would work but it would mess up the robots data and would consume more resources(running on RaspberryPi 3 that's also using a LiDAR, servo, filtering and analyzing points, and many other things). 我不想移动所有点(foreach坐标:x + = 100和y + = 100)移动到该象限,虽然可以工作,但是会弄乱机器人数据并消耗更多资源(在RaspberryPi 3上运行)还使用了LiDAR,伺服,滤波和分析点以及许多其他功能)。

How can I specify the viewpoint? 如何指定视点? I need to change the viewpoint parallel to the screen. 我需要平行于屏幕更改视点。 I've tried glTranslatef() and gluLookAt() functions but I can't seem to make it work, I'm also not sure of where should I use those functions in the code. 我已经尝试过glTranslatef()和gluLookAt()函数,但似乎无法使其正常工作,我也不确定应该在代码中的哪些位置使用这些函数。

This is the program running, each square is an (x,y) coordinate: 这是正在运行的程序,每个正方形都是(x,y)坐标:

每个正方形都是(x,y)坐标

area I can see vs. area I need to see : 我可以看到的区域与我需要看到的区域: 我可以看到的区域与我需要看到的区域

#ifdef MACOS
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h> 
#else
#include <GL/glut.h>
#include <GL/gl.h> 
#include <GL/glu.h>
#endif

#include <unistd.h>
#include "Dstar.h"
#include <stdio.h>

int hh, ww;

int window; 
Dstar *dstar;

int scale = 6;
int mbutton = 0;
int mstate = 0;

bool b_autoreplan = true;

void InitGL(int Width, int Height)
{
  hh = Height;
  ww = Width;

  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  glClearDepth(1.0);    

  glViewport(0,0,Width,Height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0,Width,0,Height,-100,100);
  glMatrixMode(GL_MODELVIEW);

}

void ReSizeGLScene(int Width, int Height)
{
  hh = Height;
  ww = Width;

  glViewport(0,0,Width,Height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0,Width,0,Height,-100,100);
  glMatrixMode(GL_MODELVIEW);

}

void DrawGLScene()
{

  usleep(100);

  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  glLoadIdentity();
  glPushMatrix();

  glScaled(scale,scale,1);

  if (b_autoreplan) dstar->replan();

  dstar->draw();

  glPopMatrix();
  glutSwapBuffers();

}


void keyPressed(unsigned char key, int x, int y) 
{
  usleep(100);

  switch(key) {
  case 'q':
  case 'Q':
    glutDestroyWindow(window); 
    exit(0);
    break;
  case 'r':
  case 'R':
    dstar->replan();
    break;
  case 'a':
  case 'A':
    b_autoreplan = !b_autoreplan;
    break;
  case 'c':
  case 'C':
    dstar->init(40,50,140, 90);
    break;
  }

}

void mouseFunc(int button, int state, int x, int y) {

  y = hh -y+scale/2;
  x += scale/2;

  mbutton = button;

  if ((mstate = state) == GLUT_DOWN) {
    if (button == GLUT_LEFT_BUTTON) {
      dstar->updateCell(x/scale, y/scale, -1);
    } else if (button == GLUT_RIGHT_BUTTON) {
      dstar->updateStart(x/scale, y/scale);
    } else if (button == GLUT_MIDDLE_BUTTON) {
      dstar->updateGoal(x/scale, y/scale);
    }
  }
}

void mouseMotionFunc(int x, int y)  {

  y = hh -y+scale/2;
  x += scale/2;

  y /= scale;
  x /= scale;

  if (mstate == GLUT_DOWN) {
    if (mbutton == GLUT_LEFT_BUTTON) {
      dstar->updateCell(x, y, -1);
    } else if (mbutton == GLUT_RIGHT_BUTTON) {
      dstar->updateStart(x, y);
    } else if (mbutton == GLUT_MIDDLE_BUTTON) {
      dstar->updateGoal(x, y);
    }
  }

}

int main(int argc, char **argv) {

  glutInit(&argc, argv);  
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);  
  glutInitWindowSize(1000, 800);  
  glutInitWindowPosition(50, 20);  

  window = glutCreateWindow("Dstar Visualizer");  

  glutDisplayFunc(&DrawGLScene);  
  glutIdleFunc(&DrawGLScene);
  glutReshapeFunc(&ReSizeGLScene);
  glutKeyboardFunc(&keyPressed);
  glutMouseFunc(&mouseFunc);
  glutMotionFunc(&mouseMotionFunc);

  InitGL(800, 600);

  dstar = new Dstar();
  dstar->init(2,2,90, 40);

  printf("----------------------------------\n");
  printf("Dstar Visualizer\n");
  printf("Commands:\n");
  printf("[q/Q] - Quit\n");
  printf("[r/R] - Replan\n");
  printf("[a/A] - Toggle Auto Replan\n");
  printf("[c/C] - Clear (restart)\n");
  printf("left mouse click - make cell untraversable (cost -1)\n");
  printf("middle mouse click - move goal to cell\n");
  printf("right mouse click - move start to cell\n");
  printf("----------------------------------\n");

  glutMainLoop();  

  return 1;
}

try glTranslatef(GLfloat x, GLfloat y, GLfloat z); 尝试glTranslatef(GLfloat x,GLfloat y,GLfloat z); after scaling to right size. 缩放到合适的大小后。 Moving camera would be glLookAt but I would just translate scene. 移动相机将是glLookAt,但我只是翻译场景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM