N
N
n_hensy2016-03-13 18:20:25
OpenGL
n_hensy, 2016-03-13 18:20:25

OpenGL, how to link character jumps to mouse clicks?

1) I am not a programmer - full code is welcome. Links to useful articles on this subject are also very welcome.
2) In various articles and training videos, it is indicated how to link approach / movement in space using mouse clicks and keys. But to complete the task, it is necessary that when you click an object (a square, for example), bounce and then fall. Can you please tell me how to do this? C + OpenGL

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Makaleks, 2016-03-17
@Makaleks

Not deepened into the question (tried only with OpenGL v1.1), but there is a suggestion.
In the main application loop (1) you draw each object you need. The object has a field(2) describing its state. The event can change (3) the state, and before drawing, some change is made on the object (a small piece of bouncing) (4). Some states have a duration limit - the degree of completion is needed (for example, in time), i.e. during processing it is necessary to check: "maybe that's enough? if so, perform the conversion for the last time, and reset the state to 'inactive'". Everything about the question.
Possible clarifications:
1) Libraries providing OpenGL windowing and event registration may have a function like WaitEvents(), which will freeze the current thread until the event occurs, hence killing the animation. This method will have to be abandoned while the animation can come.
2) You can have a list of structures in the structure that describe the processing of the state. As states are activated/deactivated, the list can be increased/decreased.
3) Do you already know how to select an object on a mouse click? The OpenGL Red Book describes how to use the callback buffer : we pass an array to OpenGL, specify its dimensions. Using OpenGL Transforms Turn on the buffer. Further in the loop [ => load name (number) => draw object => ]. Nothing will appear on the screen itself, even if you do SwapBuffers (). Turn off the buffer - this function will return the number of hits - how many "names" were drawn. Once again, on an invisible screen, we zoomed in on the mouse pointer to a scale of the order of a pixel and drew all the objects on the scene. If something appears on the screen after calling glLoadName(i) , write the information to the buffer. Now in a buffer in a certain format it is written what was drawn with the add. information, such as the z -coordinate value (helps with complex 3D). Old code:

gliuButton* gliuCallBackStafGL(double mouseX, double mouseY)
{
  if (!gliuButtonStore.stackSize)// мой стек кнопок. кнопки хранятся блоками.
  {
    return;
  }

  glPushMatrix();	// сохраним матрицу, чтобы ничего не испортить
  GLuint buf[8] = { 0 };
  glSelectBuffer(8, buf);

  glLoadIdentity();	// координаты объектов я задавал в экранных координатах
          // => стандартные координаты [-1;1]x[-1;1] будут охватывать 4 пикселя
  glTranslatef(-mouseX, -mouseY, 0);

  glRenderMode(GL_SELECT);	// включаем буфер
  glInitNames();
  glPushName(0);		// не помню, для чего именно это нужно, но иначе не работает

  unsigned i = 0;
  // сложности с хранением блоками
  struct _gliuStackElemSet_buttonPtr *ptr = gliuButtonStore.current;
  gliuButton **cur;
  if (!ptr->_bsize)
  {
    ptr = ptr->_before;
  }
  cur = &ptr->_buf[ptr->_bsize - 1];

  for (i = 0; i < gliuButtonStore.stackSize; i++)
  {
    glLoadName(i);	// загрузить имя
    glRecti((*cur)->labelPart->x, (*cur)->labelPart->y, (*cur)->labelPart->x + (*cur)->labelPart->width, (*cur)->labelPart->y + (*cur)->labelPart->height);	// вместо кнопки - просто прямоугольник
    if (cur != ptr->_buf)
    {
      cur--;
    }
    else
    {
      ptr = ptr->_before;
    }
  }
  glPopName();

  // сколько попаданий?
  int hits = glRenderMode(GL_RENDER);

  if (hits>0)
  {
    ptr = gliuButtonStore.current;
    if (!ptr->_bsize)
    {
      ptr = ptr->_before;
    }
    cur = &ptr->_buf[ptr->_bsize - 1];

    for (i = 0; i < gliuButtonStore.stackSize; i++)
    {
      if (i == buf[3])
      {
        // именно в buf[3] хранится "имя"
        // полезно изучить всё содержимое buf
        break;
      }
      if (cur != ptr->_buf)
      {
        cur--;
      }
      else
      {
        ptr = ptr->_before;
      }
    }
    glPopMatrix();
    return *cur;
  }
  else 
  {
    glPopMatrix();
    return 0;
  }
}

pointer to the PS object
Do you want to move to C++? It implements several features that simplify development (classes, their methods, overloading, templates), for example, allow you to abstract the interface from the implementation (inheritance, virtual functions). There is literature on code organization techniques ("Design Patterns" by the Gang of Four). Compatible with C. Schildt's "Complete Reference to C++" will tell you about C and C++ - it is convenient to search for it. And yes, many C++ compilers already implement C++11 threads, which allows you to pause the application without connecting third-party libraries and Sleep (only! Windows) . PPS It seems that the answer is given, throw additional. questions in the comments.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question