shinku89 0 Newbie Poster

I try to make the triangle move smoothly but instead the triangle is gone when I enter move button. WHY? can someone please check and tell me what is the problem? it can move without dt( in my code for elapse time).

#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <time.h>
#include <iostream>



GLfloat d[3] = { 0.0,0.0,0.0};
// Some global variables used to measure the time
float  timeAtGameStart;
UINT64 ticksPerSecond;

float GetGameTime()
{
  UINT64 ticks;
  float time;

  // This is the number of clock ticks since start
  if( !QueryPerformanceCounter((LARGE_INTEGER *)&ticks) )
    ticks = (UINT64)timeGetTime();

  // Divide by frequency to get the time in seconds
  time = (float)(__int64)ticks/(float)(__int64)ticksPerSecond;

  // Subtract the time at game start to get
  // the time since the game started
  time -= timeAtGameStart;

  return time;
}
// Called once at the start of the game
void InitGameTime()
{
  // We need to know how often the clock is updated
  if( !QueryPerformanceFrequency((LARGE_INTEGER *)&ticksPerSecond) )
    ticksPerSecond = 1000;

  // If timeAtGameStart is 0 then we get the time since
  // the start of the computer when we call GetGameTime()
  timeAtGameStart = 0;
  timeAtGameStart = GetGameTime();
}

// Called every time you need the current game time


// We'll need some starting values
float lastGameTime = 0;
float position = 0;
float velocity = 1;

float dt;
// Here's where we compute how much 
// time has passed since last update
float GetDeltaTime()
{
  dt = GetGameTime() - lastGameTime;

  // Increment with dt instead of calling 
  // GetGameTime() to avoid loosing time
  lastGameTime += dt;

  return dt;
}


   
void display() {
  
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glPushMatrix();

    glTranslatef(d[0], d[1], 0.);
   
    glScalef(0.5,0.5,0.5);
    dt = GetDeltaTime();          // Each set of 3 vertices form a triangle
    glTranslatef(d[0],d[1],0);  
    glBegin(GL_TRIANGLES);
      glColor3f(0.0f, 1.0f, 0.0f); // Green
      glVertex2f(0.2f, -0.3f);
      glVertex2f(0.8f, -0.3f);
      glVertex2f(0.5f,  0.2f);
    glEnd();
  

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, d);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    
    glPopMatrix();

    glutSwapBuffers();
  
  
  
  
  
    
}


void special(int key, int x, int y)
{
   
   if( key == GLUT_KEY_UP )
    d[1] += 0.1f*dt;
    if( key == GLUT_KEY_DOWN )
    d[1] -= 0.1f*dt;
    if( key == GLUT_KEY_LEFT  )
    d[0] -= 0.1f*dt;
    if( key == GLUT_KEY_RIGHT )
    d[0] += 0.1f*dt;
    glutSwapBuffers();
    glutPostRedisplay();
}
 
// GLUT runs as a Console Application
int main(int argc, char** argv) {
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

   glutCreateWindow("simple triangle test");
   glutDisplayFunc(display);
          // Register callback handler for window re-paint
   glutSpecialFunc(special);

   glutMainLoop();
       // Enter infinitely event-processing loop
   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.