🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

[FIXED] display issues (jumpy visuals)

Started by
1 comment, last by supercat1 18 years ago
i posted this in the opengl thread, but i'm not sure it is going to get any attention there. i got lots of help, but unfortunately no one is able to give me much insight into how to solve the issue i'm encountering. what is happening is that while i'm moving left and right with the keyboard and using the mouse to rotate the camera the objects i'm looking at appear to jump/vibrate. i'll post the code so that you can run it yourself and test it. i would personally really appreciate any insight into what is going on. it seems to me to be some math error, but i can't find it.

//  6/7/06
//  Andrew Monshizadeh
//  OpenGL 3d engine using glut

#include <GL/glut.h>
#include <cmath>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;

// window global variables
int WINDOW_WIDTH = 800;
int WINDOW_HEIGHT = 600;
int windowids[1];  // can be used to store multiple window ids in case there are multiple windows
char title[13] = "test engine";

// call back functions to be used
void initializecallbacks();
void displaycallback();
void keyboardcallback(unsigned char key, int x, int y);
void passivemotioncallback(int x, int y);
void reshapecallback(int w, int h);
void timercallback(int time);

// setup visual requirments
void initializevisuals();

// variables used for movement
const float piover180 = 0.0174532925f;  // pi / 180, for degree to radian transform
float heading; // direction character is facing
float xpos; // x-axis position of the player
float zpos; // z-axis position of the player
float ypos = 0.5; // y-axis position of the player
GLfloat	yrot; // y-axis rotation
GLfloat lookupdown = 0.0f; // value to look up or down
GLfloat	z=0.0f;				// Depth Into The Screen
int mousebasex = WINDOW_WIDTH/2;
int mousebasey = WINDOW_HEIGHT/2;

int main(int argc, char* argv[])
{
  glutInit(&argc, argv);
  initializevisuals();
  initializecallbacks();
  
  glutMainLoop();
  
  return 0;
}

//----------------------------------------------------------------------------//
//                              call back section                             //
//----------------------------------------------------------------------------//
void initializecallbacks()
{
  // setting up the call back functions
  glutDisplayFunc(displaycallback);  // display call back
  glutKeyboardFunc(keyboardcallback);  //  keyboard call back
  glutPassiveMotionFunc(passivemotioncallback);  // passive mouse motion call back
  glutReshapeFunc(reshapecallback); // screen reshape call back
  glutTimerFunc(15, timercallback, 0);  // timer call back
}

void keyboardcallback(unsigned char key, int x, int y)
{
  switch (key)
  {
    // miscellaneous input
    case 27: // 'esc'
      exit(0); // end the program
      break;
    
    // movement input
    case 'a':
      xpos -= (float)sin(heading*piover180 + 90.0f*piover180) * 0.05f;
      zpos -= (float)cos(heading*piover180 + 90.0f*piover180) * 0.05f;
			break;
		case 'd':
      xpos += (float)sin(heading*piover180 + 90.0f*piover180) * 0.05f;
      zpos += (float)cos(heading*piover180 + 90.0f*piover180) * 0.05f;
      break;
    case 'w':
      xpos -= (float)sin(heading*piover180) * 0.05f;
			zpos -= (float)cos(heading*piover180) * 0.05f;
			break;
		case 's':
      xpos += (float)sin(heading*piover180) * 0.05f;
      zpos += (float)cos(heading*piover180) * 0.05f;
  }
  
  int dims[4];
  glGetIntegerv(GL_VIEWPORT, dims);
  
  int dx = x - mousebasex;
  int dy = y - mousebasey;
  mousebasex = x;
  mousebasey = y;
  
//  if (x < 20)
//  {
//    glutWarpPointer(dims[2]/2, y);
//    mousebasex = dims[2]/2;
//  }
//  if (x > (dims[2]/2 - 20))
//  {
//    glutWarpPointer(dims[2]/2, y);
//    mousebasex = dims[2]/2;
//  }
  
  lookupdown += dy * 0.5f;
  heading -= dx * 0.5f;
  if(heading < 0.0f)
    heading = heading + 360.0f;
  else if(heading > 360.0f)
    heading = heading - 360.0f;
  yrot = heading;
}

void passivemotioncallback(int x, int y)
{
  int dims[4];
  glGetIntegerv(GL_VIEWPORT, dims);
  
  int dx = x - mousebasex;
  int dy = y - mousebasey;
  mousebasex = x;
  mousebasey = y;
  
  if (x < 20)
  {
    glutWarpPointer(dims[2]/2, y);
    mousebasex = dims[2]/2;
  }
  if (x > (dims[2] - 20))
  {
    glutWarpPointer(dims[2]/2, y);
    mousebasex = dims[2]/2;
  }
  
  lookupdown += dy * 0.5f;
  heading -= dx * 0.5f;
  if(heading < 0.0f)
    heading = heading + 360.0f;
  else if (heading > 360.0f)
    heading = heading - 360.0f;
  yrot = heading;
}

void reshapecallback(int w, int h)
{
  glViewport(0, 0, w, h);
 
 	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix

	// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);

	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glLoadIdentity();									// Reset The Modelview Matrix
}

void timercallback(int time)
{
  glutPostRedisplay();
  glutTimerFunc(10, timercallback, 0);
}

void displaycallback()
{
  int dims[4];
  glGetIntegerv(GL_VIEWPORT, dims);
  
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();									// Reset The View

	GLfloat xtrans = -xpos;
	GLfloat ztrans = -zpos;
	GLfloat ytrans = 0; // -walkbias-0.25f;
	GLfloat sceneroty = 360.0f - yrot;
	
  glRotatef(lookupdown,1.0f,0,0);
	glRotatef(sceneroty,0,1.0f,0);
	glTranslatef(xtrans, ytrans, ztrans);
	
	glBegin(GL_QUADS);
		glColor3f(0.0f,1.0f,0.0f);			// Set The Color To Green
		glVertex3f( 0.5f, 0.5f,-0.5f);			// Top Right Of The Quad (Top)
		glVertex3f(-0.5f, 0.5f,-0.5f);			// Top Left Of The Quad (Top)
		glVertex3f(-0.5f, 0.5f, 0.5f);			// Bottom Left Of The Quad (Top)
		glVertex3f( 0.5f, 0.5f, 0.5f);			// Bottom Right Of The Quad (Top)
		
		glColor3f(1.0f,0.5f,0.0f);			// Set The Color To Orange
		glVertex3f( 0.5f,-0.5f, 0.5f);			// Top Right Of The Quad (Bottom)
		glVertex3f(-0.5f,-0.5f, 0.5f);			// Top Left Of The Quad (Bottom)
		glVertex3f(-0.5f,-0.5f,-0.5f);			// Bottom Left Of The Quad (Bottom)
		glVertex3f( 0.5f,-0.5f,-0.5f);			// Bottom Right Of The Quad (Bottom)
		
		glColor3f(1.0f,0.0f,0.0f);			// Set The Color To Red
		glVertex3f( 0.5f, 0.5f, 0.5f);			// Top Right Of The Quad (Front)
		glVertex3f(-0.5f, 0.5f, 0.5f);			// Top Left Of The Quad (Front)
		glVertex3f(-0.5f,-0.5f, 0.5f);			// Bottom Left Of The Quad (Front)
		glVertex3f( 0.5f,-0.5f, 0.5f);			// Bottom Right Of The Quad (Front)
		
		glColor3f(1.0f,1.0f,0.0f);			// Set The Color To Yellow
		glVertex3f( 0.5f,-0.5f,-0.5f);			// Bottom Left Of The Quad (Back)
		glVertex3f(-0.5f,-0.5f,-0.5f);			// Bottom Right Of The Quad (Back)
		glVertex3f(-0.5f, 0.5f,-0.5f);			// Top Right Of The Quad (Back)
		glVertex3f( 0.5f, 0.5f,-0.5f);			// Top Left Of The Quad (Back)
		
		glColor3f(0.0f,0.0f,1.0f);			// Set The Color To Blue
		glVertex3f(-0.5f, 0.5f, 0.5f);			// Top Right Of The Quad (Left)
		glVertex3f(-0.5f, 0.5f,-0.5f);			// Top Left Of The Quad (Left)
		glVertex3f(-0.5f,-0.5f,-0.5f);			// Bottom Left Of The Quad (Left)
		glVertex3f(-0.5f,-0.5f, 0.5f);			// Bottom Right Of The Quad (Left)
		
		glColor3f(1.0f,0.0f,1.0f);			// Set The Color To Violet
		glVertex3f( 0.5f, 0.5f,-0.5f);			// Top Right Of The Quad (Right)
		glVertex3f( 0.5f, 0.5f, 0.5f);			// Top Left Of The Quad (Right)
		glVertex3f( 0.5f,-0.5f, 0.5f);			// Bottom Left Of The Quad (Right)
		glVertex3f( 0.5f,-0.5f,-0.5f);			// Bottom Right Of The Quad (Right)
	glEnd();						// Done Drawing The Quad
	
	glFlush();
	glutSwapBuffers();
}


void initializevisuals()
{
  // creating the window and drawing buffers  
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); // create double buffer and z-depth buffer
  glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); // create window using predetermined window sizes
  glutInitWindowPosition(0,0);
  windowids[0]=glutCreateWindow(title);
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // set background color to white
	glClearDepth(1.0); // enables clearing of the depth buffer
  glEnable(GL_DEPTH_TEST); // enable depth testing
	glDepthFunc(GL_LESS);	// the type of depth test to do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// really nice perspective calculations, got from NeHe tutorial 10

  int dims[4];
  glGetIntegerv(GL_VIEWPORT, dims);

	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix

	// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f,(GLfloat)dims[2]/(GLfloat)dims[3],0.1f,100.0f);

	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glLoadIdentity();									// Reset The Modelview Matrix
}



any comments y'all have would be much appreciated. i took much of the movement code from the nehe tutorial #10. thanks! [Edited by - supercat1 on June 10, 2006 12:21:39 PM]
Advertisement
Don't know what you mean, maybe your pc is too weak, so you're getting low fps :)?
sorry, forgot to update this thread, but infact the issue has been solved. it came down to the fact that i had to disable keyboard repeats. turns out that because glut was polling the keyboard every cycle/interation through glutMainLoop() it was causing the stutter. so, when i took out the keyrepeat and implemented a method that allowed for delay-less input the whole problem disappeared.

and to think, i had a solution to the input delay for about 2 days before implementing it because i thought it was a minor issue. geesh. (insert rolling eye smiley here ...)

:D

This topic is closed to new replies.

Advertisement