13 Replies - 4484 Views - Last Post: 09 February 2012 - 03:13 PM Rate Topic: -----

#1 Amb154  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 29-April 11

Key buffering problem in OpenGL

Posted 08 February 2012 - 04:40 PM

Hello, I'm trying to implement key buffering in my program, but it doesn't seem to be doing anything at all.

//PROGRAM VARIABLES
void dec(void)
{
}
 
bool keyStates[256] = { 0 }; // Create an array of boolean values of length 256 (0-255)  
bool keySpecialStates[256] = { 0 };// Create an arroy of boolean values of length 246 (0-245)
 
bool currentColor = false; // current color of screen
float scenePlacement1 =  0.0f;
float scenePlacement2 =  0.0f;
float scenePlacement3 = -5.0f;
float xRotateAngle = 0.0f;
float yRotateAngle = 0.0f;
float zRotateAngle = 0.0f;
float xScale = 1;
float yScale = 1;
float zScale = 1;
GLfloat fogDensity = .05;
GLfloat fogColor[4] = {0.0, 0.0, 0.0, 1.0};
float fogLvl = 0;
GLuint texture = 0;
 
float xPos = 0, yPos = 0, zPos = 0, xRot = 0, yRot = 0, angle = 0.0;
 
 
//PROGRAM VARIABLES
 
GLuint LoadTexture( const char* filename, int width, int height)
{
        unsigned char * data;
    FILE * file;
 
        file = fopen( filename, "rb" ); // We need to open our file
    if ( file == NULL ) return 0; // If our file is empty, set our texture to empty
 
        data = (unsigned char *)malloc( width * height * 3 ); // Assign the nessecary memory for the texture
 
        fread( data, width * height * 3, 1, file ); // read in our file
    fclose( file ); //close our file, no point leaving it open
 
        glGenTextures( 1, &texture ); // then we need to tell OpenGL that we are generating a texture
    glBindTexture( GL_TEXTURE_2D, texture ); // now we bind the texture that we are working with
        glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); // set texture environment parameters The parameter GL_MODULATE will blend the texture with whatever is underneath, setting it to GL_DECAL will tell the texture to replace whatever is on the object.
 
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR );
 
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); //Here we are setting the parameter to repeat the texture instead of clamping the texture to the edge of our shape.
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
 
        gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data );
    free( data ); // free the texture
    return texture; // return the texture data
}
 
void FreeTexture( GLuint texture )
{
  glDeleteTextures( 1, &texture ); // Delete our texture, simple enough.
}
 
void camera(void)
{
        glRotatef(xRot, 1.0f, 0.0f, 0.0f);
        glRotatef(yRot, 0.0f, 1.0f, 0.0f);
        glTranslatef(-xPos, -yPos, -zPos);
}
 
void renderShape(void)
{
        //glColor3f(1.0f, 0.0f, 0.0f); // For One full color
        texture = LoadTexture("C:/Users/Adrian/Documents/Visual Studio 2010/Projects/OpenGlGame/Debug/texture.raw", 256, 256);
        //glutWireCube(1);
        //glutSolidCube(1);
        //glutWireSphere(1, 50, 50);
        //glutSolidSphere(1, 50, 50);
        //glutWireCone(1, 1, 50, 50);  
        //glutSolidCone(1, 1, 50, 50);
        //glutWireTorus(5, 5, 5, 5);  
        //glutSolidTorus(5, 5, 5, 5);  
        //glutWireTeapot(1);  
        //glutSolidTeapot(1);
 
 
 
        glBegin(GL_QUADS);              // Draw The Cube Using quads
   
        //glColor3f(0.0f,1.0f,0.0f);    // Color Blue
   
       
        glTexCoord3d(0.0, 0.0, 0.0);
        glVertex3f( 1.0f, 1.0f,-1.0f);  // Top Right Of The Quad (Top)
   
        glTexCoord3d(1.0, 0.0, 0.0);
        glVertex3f(-1.0f, 1.0f,-1.0f);  // Top Left Of The Quad (Top)
   
        glTexCoord3d(1.0, 1.0, 0.0);
        glVertex3f(-1.0f, 1.0f, 1.0f);  // Bottom Left Of The Quad (Top)
   
        glTexCoord3d(0.0, 1.0, 0.0);
        glVertex3f( 1.0f, 1.0f, 1.0f);  // Bottom Right Of The Quad (Top)
   
        //glColor3f(1.0f,0.5f,0.0f);    // Color Orange
   
       
        glTexCoord3d(0.0, 0.0, 0.0);
        glVertex3f( 1.0f,-1.0f, 1.0f);  // Top Right Of The Quad (Bottom)
   
        glTexCoord3d(1.0, 0.0, 0.0);
        glVertex3f(-1.0f,-1.0f, 1.0f);  // Top Left Of The Quad (Bottom)
   
        glTexCoord3d(1.0, 1.0, 0.0);
        glVertex3f(-1.0f,-1.0f,-1.0f);  // Bottom Left Of The Quad (Bottom)
   
        glTexCoord3d(0.0, 1.0, 0.0);
        glVertex3f( 1.0f,-1.0f,-1.0f);  // Bottom Right Of The Quad (Bottom)
   
       
        //glColor3f(1.0f,0.0f,0.0f);    // Color Red   
   
 
        glTexCoord3d(0.0, 0.0, 0.0);
        glVertex3f( 1.0f, 1.0f, 1.0f);  // Top Right Of The Quad (Front)
   
        glTexCoord3d(1.0, 0.0, 0.0);
        glVertex3f(-1.0f, 1.0f, 1.0f);  // Top Left Of The Quad (Front)
   
        glTexCoord3d(1.0, 1.0, 0.0);
        glVertex3f(-1.0f,-1.0f, 1.0f);  // Bottom Left Of The Quad (Front)
   
        glTexCoord3d(0.0, 1.0, 0.0);
        glVertex3f( 1.0f,-1.0f, 1.0f);  // Bottom Right Of The Quad (Front)
   
 
        //glColor3f(1.0f,1.0f,0.0f);    // Color Yellow
   
        glTexCoord3d(1.0, 1.0, 0.0);
        glVertex3f( 1.0f,-1.0f,-1.0f);  // Top Right Of The Quad (Back)
   
        glTexCoord3d(0.0, 1.0, 0.0);
        glVertex3f(-1.0f,-1.0f,-1.0f);  // Top Left Of The Quad (Back)
   
        glTexCoord3d(0.0, 0.0, 0.0);
        glVertex3f(-1.0f, 1.0f,-1.0f);  // Bottom Left Of The Quad (Back)
   
        glTexCoord3d(1.0, 0.0, 0.0);
        glVertex3f( 1.0f, 1.0f,-1.0f);  // Bottom Right Of The Quad (Back)
   
       
        //glColor3f(0.0f,0.0f,1.0f);    // Color Blue
   
       
        glTexCoord3d(0.0, 0.0, 0.0);
        glVertex3f(-1.0f, 1.0f, 1.0f);  // Top Right Of The Quad (Left)
   
        glTexCoord3d(1.0, 0.0, 0.0);
        glVertex3f(-1.0f, 1.0f,-1.0f);  // Top Left Of The Quad (Left)
   
        glTexCoord3d(1.0, 1.0, 0.0);
        glVertex3f(-1.0f,-1.0f,-1.0f);  // Bottom Left Of The Quad (Left)
   
        glTexCoord3d(0.0, 1.0, 0.0);
        glVertex3f(-1.0f,-1.0f, 1.0f);  // Bottom Right Of The Quad (Left)
 
 
        //glColor3f(1.0f,0.0f,1.0f);    // Color Violet
   
        glTexCoord3d(0.0, 0.0, 0.0);
        glVertex3f( 1.0f, 1.0f,-1.0f);  // Top Right Of The Quad (Right)
   
        glTexCoord3d(1.0, 0.0, 0.0);
        glVertex3f( 1.0f, 1.0f, 1.0f);  // Top Left Of The Quad (Right)
   
        glTexCoord3d(1.0, 1.0, 0.0);
        glVertex3f( 1.0f,-1.0f, 1.0f);  // Bottom Left Of The Quad (Right)
   
        glTexCoord3d(0.0, 1.0, 0.0);
        glVertex3f( 1.0f,-1.0f,-1.0f);  // Bottom Right Of The Quad (Right)
  glEnd();                      // End Drawing The Cube
}
 
void init (void) {
    glEnable (GL_DEPTH_TEST);
    //glEnable (GL_LIGHTING);
    //glEnable (GL_LIGHT0);
        glEnable (GL_FOG);
        glFogi   (GL_FOG_MODE, GL_EXP2);
        glFogfv  (GL_FOG_COLOR, fogColor);
        glFogf   (GL_FOG_DENSITY, fogDensity);
        glHint   (GL_FOG_HINT, GL_NICEST);
        glFogf   (GL_FOG_START, 1);
        glFogf   (GL_FOG_END, 10);
 
}
 
void display (void)
{      
        glClearColor(0.f, 0.f, 0.f, 1.f); // Clear the background of our window to red  
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the color buffer
        glLoadIdentity(); // Load the identity matrix to reset drawing loactions
        glClearDepth(1);
        glEnable( GL_TEXTURE_2D );
        camera();
 
        init();
        glTranslatef(scenePlacement1, scenePlacement2, scenePlacement3); // Move scene back 5 units. ( +Right/-Left, +Up/-Down, +Backward/-Forward )
        glRotatef(yRotateAngle, 0.0f, 1.0f, 0.0f);
        glRotatef(zRotateAngle, 0.0f, 0.0f, 1.0f);
        glRotatef(xRotateAngle, 1.0f, 0.0f, 0.0f);
        glScalef(xScale, yScale, zScale);
       
        renderShape();
 
        glutSwapBuffers(); // Flushes everything above to the window
}
 
void reshape (int width, int height)
{
        glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Sets viewport to size of the window
        glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed  
        glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we don't get any artifacts (cleaning up)
       
        gluPerspective(60, (GLfloat)width / (GLfloat)height, 0.1, 100.0); // FOV, Aspect ratio, Near and Far rendering values.
 
        glMatrixMode(GL_MODELVIEW); // Switch back to Model matrix to draw objects.
}
 
void keyOperations(void)
{
        std::fill_n( keyStates, 256, false);
 
        if (keyStates['a'])
        {
                glTranslatef(-5, -2, -5);
                display();
        }
}
 
void keySpecialOperations(void)
{  
 
        if (keySpecialStates[GLUT_KEY_LEFT])
        {
                glTranslatef(-5, -2, -5);
                display();
        }  
}  
 
void keyPressed (unsigned char key, int x, int y)
{              
        keyStates[key] = true; // Set the state of the key to pressed
}
 
void keyUp (unsigned char key, int x, int y)
{
        keyStates[key] = false; // Set the state of the key to unpressed
}
 
void keySpecial (int key, int x, int y)
{
        keySpecialStates[key] = true; // Set the state of the key to pressed
}
 
void keySpecialUp (int key, int x, int y)
{
        keySpecialStates[key] = false; // Set the state of the key to unpressed
}
 
int main(int argc, char **argv)
{
        glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);
 
        glutInit(&argc, argv); // Initialize GLUT
        glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); // Sets up a single buffered display / Alpha
        glutInitWindowSize (500, 500); // Window size
        glutInitWindowPosition (100, 100); // Window position
        glutCreateWindow ("OpenGL Window"); // Create window
       
        init();
        glutReshapeFunc(reshape); // Uses our reshape function for reshaping the window
        glutDisplayFunc(display); // Uses our display function for display
        glutKeyboardFunc(keyPressed); // Uses our keyPressed function for keyboard interaction
        glutKeyboardUpFunc(keyUp);// Uses our keyUp function for keyboard interaction when keys are released
        glutSpecialFunc(keySpecial);
        glutSpecialUpFunc(keySpecialUp);
 
 
        glutMainLoop(); // Enter main display loop
}


Is This A Good Question/Topic? 0
  • +

Replies To: Key buffering problem in OpenGL

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,172
  • Joined: 14-February 08

Re: Key buffering problem in OpenGL

Posted 09 February 2012 - 02:37 AM

Ok so what have you tried? Have you set break points in your key pressed function? This will enabled you to see whether the function is actually being called or not, you could also cout the value of the key that's being passed in.
Was This Post Helpful? 0
  • +
  • -

#3 Amb154  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 29-April 11

Re: Key buffering problem in OpenGL

Posted 09 February 2012 - 04:23 AM

I'm not sure what you mean by break points, but i did try to cout the a key and the left key and it didn't send the cout to the console.
Also, I won't be on today for a while so my response will probably be late.
Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,172
  • Joined: 14-February 08

Re: Key buffering problem in OpenGL

Posted 09 February 2012 - 07:15 AM

You should really know what breakpoints are if you are at a level where you feel you can dive into OpenGL. What IDE are you using?
Was This Post Helpful? 0
  • +
  • -

#5 Amb154  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 29-April 11

Re: Key buffering problem in OpenGL

Posted 09 February 2012 - 07:38 AM

I'm using visual studio 2010.
Was This Post Helpful? 0
  • +
  • -

#6 Amb154  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 29-April 11

Re: Key buffering problem in OpenGL

Posted 09 February 2012 - 07:45 AM

View PostAmb154, on 09 February 2012 - 07:38 AM, said:

I'm using visual studio 2010.


Also, this probably doesn't have to do with it, but are there any specific header files i need for this?
Was This Post Helpful? 0
  • +
  • -

#7 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,172
  • Joined: 14-February 08

Re: Key buffering problem in OpenGL

Posted 09 February 2012 - 09:23 AM

Visual studio is awesome for debugging and you should be learning how to do it. Personally I'm not going to set up a GLUT project to run your code, someone else might but this is a good time for you to learn some debugging. Look up breakpoints and the call stack.
Was This Post Helpful? 0
  • +
  • -

#8 Amb154  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 29-April 11

Re: Key buffering problem in OpenGL

Posted 09 February 2012 - 01:18 PM

View Poststayscrisp, on 09 February 2012 - 09:23 AM, said:

Visual studio is awesome for debugging and you should be learning how to do it. Personally I'm not going to set up a GLUT project to run your code, someone else might but this is a good time for you to learn some debugging. Look up breakpoints and the call stack.


I've put a breakpoint in both the keyPressed function and the keyOperations function.
The keyPressed function is getting the information just fine, however the keyOperation function isn't doing anything.
Was This Post Helpful? 0
  • +
  • -

#9 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,172
  • Joined: 14-February 08

Re: Key buffering problem in OpenGL

Posted 09 February 2012 - 02:21 PM

You do know that you are not calling keyOperation anywhere?
Was This Post Helpful? 0
  • +
  • -

#10 Amb154  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 29-April 11

Re: Key buffering problem in OpenGL

Posted 09 February 2012 - 02:25 PM

View Poststayscrisp, on 09 February 2012 - 02:21 PM, said:

You do know that you are not calling keyOperation anywhere?


I realized that just before you posted.*facepalm*
For some reason, the course I'm doing didn't specify that, and it seemed to escape my mind.

Sorry to interrupt your day with my trivial ways.
Thanks for all the help though. You guys here at dream.in.code are awesome.
Was This Post Helpful? 0
  • +
  • -

#11 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,172
  • Joined: 14-February 08

Re: Key buffering problem in OpenGL

Posted 09 February 2012 - 02:38 PM

Don't worry about it :) Is it working now? Just as an aside, GLUT is pretty confusing and not too good for anything except small demos. You should move onto SDL, it is a lot better and much easier to use. Just let me know if you need any more information on getting started on this.
Was This Post Helpful? 0
  • +
  • -

#12 Amb154  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 29-April 11

Re: Key buffering problem in OpenGL

Posted 09 February 2012 - 02:56 PM

Yeah, it's working. I'm in the middle of a course for OpenGL including glut right now, should i finish that and move to SDL? Also, got any good usage tutorials for SDL?
Was This Post Helpful? 0
  • +
  • -

#13 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,172
  • Joined: 14-February 08

Re: Key buffering problem in OpenGL

Posted 09 February 2012 - 03:07 PM

Well you could start with the tutorials listed in my signature, but they are really just 2D and more to teach OOP concepts. GLUT is fine for beginning but its input and event code is not very good. it also stops you from creating your own game loop due to having to use its own functions to do anything.
Was This Post Helpful? 0
  • +
  • -

#14 Amb154  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 29-April 11

Re: Key buffering problem in OpenGL

Posted 09 February 2012 - 03:13 PM

I'll definitely look at those. Thanks for the help. :bigsmile:
I'll probably post here more, but until then, nice talking to you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1