Sign in to follow this  
horizon981

GLFW Input Problem.

Recommended Posts

horizon981    100
Hi. I was making a simple application --- Rotate a cube based on keyboard input. I'm using GLFW. The problem is that no key is getting detected. This is the code I'm using to detect keys:
// Main rendering loop
      do
      {
         // Call our rendering function
          Draw();

          // Swap front and back buffers (we use a double buffered display)
          glfwSwapBuffers();
            
          //Set up Rotation
          if( glfwGetKey(GLFW_KEY_RIGHT)==GLFW_PRESS)
           yRot+=0.04;
           
          if( glfwGetKey(GLFW_KEY_LEFT)== GLFW_PRESS)
           yRot-=0.04;
           
          if( glfwGetKey(GLFW_KEY_UP)== GLFW_PRESS)
           xRot+=0.04;
           
          if( glfwGetKey(GLFW_KEY_DOWN)== GLFW_PRESS)
           xRot-=0.04;
           
          if( glfwGetKey(GLFW_KEY_PAGEUP)== GLFW_PRESS)
           zRot+=0.04;
           
          if( glfwGetKey(GLFW_KEY_PAGEDOWN)== GLFW_PRESS)
           zRot-=0.04;
                                
          // Check if the escape key was pressed, or if the window was closed
          running = !glfwGetKey( GLFW_KEY_ESC ) &&
                    glfwGetWindowParam( GLFW_OPENED );
      }
      while( running );

Well, nothing is really happening. If you need the complete code, plz tell me. Thanks in advance.

Share this post


Link to post
Share on other sites
Dorvo    272
Hmmm.. that is quite a pickle.
Assuming the window was properly set up, that code should work.
What I do is disable auto polling events in GLFW, and poll for them myself.
You could try this out.


void processInput()
{
glfwPollEvents(); // poll for any events


if (glfwGetKey(GLFW_KEY_ESC) == GLFW_PRESS)
glfwCloseWindow();
}

void drawScene()
{
// TODO: any GL-specific drawing

glfwSwapBuffers();
}

void main()
{
glfwInit();

glfwOpenWindow(640, 480, 8, 8, 8, 8, 24, 8, GLFW_WINDOW); // shouldn't have been GLFW_WINDOWED. I just noticed that.
glfwSetWindowTitle("Try this out.");

glfwDisable(GLFW_AUTO_POLL_EVENTS); // disable auto-polling events


while (GL_TRUE == glfwGetWindowParam(GLFW_OPENED))
{
processInput();
drawScene();
}

glfwTerminate();
}





I use this as a basic shell for any test app I make, and it works perfectly fine.

Share this post


Link to post
Share on other sites
horizon981    100
Well, not only input is not working...
Even simple rotation commands like
glRotatef(30.0,1.0f,0.0f,0.0f);

are not having any effect.
Here's the complete code:


#include <stdlib.h> // For malloc() etc.
#include <stdio.h> // For printf(), fopen() etc.
#include <math.h> // For sin(), cos() etc.
#include <GL/glfw.h> // For GLFW, OpenGL and GLU


float xRot=30.0,yRot=0.0,zRot=0.0;

void Draw( void )
{
int width, height; // Window dimensions
double t; // Time (in seconds)

// Get current time
t = glfwGetTime();

// Get window size
glfwGetWindowSize( &width, &height );

// Make sure that height is non-zero to avoid division by zero
height = height < 1 ? 1 : height;

// Set viewport
glViewport( 0, 0, width, height );

// Clear color and depht buffers
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

// Set up projection matrix
glMatrixMode( GL_PROJECTION ); // Select projection matrix
glLoadIdentity(); // Start with an identity matrix
gluPerspective( // Set perspective view
65.0, // Field of view = 65 degrees
(double)width/(double)height, // Window aspect (assumes square pixels)
1.0, // Near Z clipping plane
100.0 // Far Z clippling plane
);

// Set up modelview matrix
glMatrixMode( GL_MODELVIEW ); // Select modelview matrix
glLoadIdentity(); // Start with an identity matrix
gluLookAt( // Set camera position and orientation
0.0, 0.0, 5.0, // Camera position (x,y,z)
0.0, 0.0, 0.0, // View point (x,y,z)
0.0, 1.0, 0.0 // Up-vector (x,y,z)
);

// Here is where actual OpenGL rendering calls would begin...
glBegin(GL_QUADS);
glColor3f(0.1f,0.6f,0.8f);
glVertex3f(-1.0f,1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,1.0f);
glVertex3f(1.0f,-1.0f,1.0f);
glVertex3f(1.0f,1.0f,1.0f);

glColor3f(0.2f,0.2f,0.6f);
glVertex3f(1.0f,1.0f,1.0f);
glVertex3f(1.0f,-1.0f,1.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,1.0f,-1.0f);

glColor3f(0.7f,0.8f,0.4f);
glVertex3f(-1.0f,1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,1.0f,-1.0f);

glColor3f(1.0f,1.0f,1.0f);
glVertex3f(-1.0f,1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);;

glColor3f(0.9f,0.5f,0.5f);
glVertex3f(-1.0f,1.0f,1.0f);
glVertex3f(1.0f,1.0f,1.0f);
glVertex3f(1.0f,1.0f,-1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);

glColor3f(0.8f,0.4f,0.5f);
glVertex3f(-1.0f,-1.0f,1.0f);
glVertex3f(1.0f,-1.0f,1.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glEnd();

glRotatef(xRot,1.0f,0.0f,0.0f);
glRotatef(yRot,0.0f,1.0f,0.0f);
glRotatef(zRot,0.0f,0.0f,1.0f);



}

int main( int argc, char **argv )
{
int ok; // Flag telling if the window was opened
int running; // Flag telling if the program is running
// Initialize GLFW
glfwInit();

// Open window
ok = glfwOpenWindow(
640, 480, // Width and height of window
8, 8, 8, // Number of red, green, and blue bits for color buffer
8, // Number of bits for alpha buffer
24, // Number of bits for depth buffer (Z-buffer)
0, // Number of bits for stencil buffer
GLFW_WINDOW // We want a desktop window (could be GLFW_FULLSCREEN)
);

// If we could not open a window, exit now
if( !ok )
{
glfwTerminate();
return 0;
}

// Set window title
glfwSetWindowTitle( "My OpenGL program" );

// Enable sticky keys
glfwEnable( GLFW_STICKY_KEYS );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Main rendering loop
do
{
// Call our rendering function
Draw();

// Swap front and back buffers (we use a double buffered display)
glfwSwapBuffers();

//Set up Rotation
if( glfwGetKey(GLFW_KEY_RIGHT)==GLFW_PRESS)
yRot+=0.04;

if( glfwGetKey(GLFW_KEY_LEFT)== GLFW_PRESS)
yRot-=0.04;

if( glfwGetKey(GLFW_KEY_UP)== GLFW_PRESS)
xRot+=0.04;

if( glfwGetKey(GLFW_KEY_DOWN)== GLFW_PRESS)
xRot-=0.04;

if( glfwGetKey(GLFW_KEY_PAGEUP)== GLFW_PRESS)
zRot+=0.04;

if( glfwGetKey(GLFW_KEY_PAGEDOWN)== GLFW_PRESS)
zRot-=0.04;

// Check if the escape key was pressed, or if the window was closed
running = !glfwGetKey( GLFW_KEY_ESC ) &&
glfwGetWindowParam( GLFW_OPENED );
}
while( running );

// Terminate GLFW
glfwTerminate();

// Exit program
return 0;
}




I'm at my wit's end......
Pleaseeeeeeee HELP ME OUT.

Share this post


Link to post
Share on other sites
horizon981    100
Ah!!!!!!!!
What an idiot I have been!!
rick_appleton: Thanks a lot!! You saved me from a lot of frustration.
Silly mistake.
But now the problem is: the sides of the cube are not opaque.
I can see through them.
I've tried changing
glClearColor(0.0f,0.0f,0.0f,0.0f);
to
glClearColor(0.0f,0.0f,0.0f,1.0f);

But it doesn't work.
What to do?

Share this post


Link to post
Share on other sites
Kalidor    1087
I think that your problem is not that the cube's faces are transparent, but that some are being drawn on top of others that should be behind it. You are asking for a depth buffer and clearing it, but I don't see you enabling depth testing anywhere. You may also want to enable backface culling.

Share this post


Link to post
Share on other sites
oggialli    217
And the code to do that would be glEnable(GL_DEPTH_TEST). I didn't check if you have taken vertex winding into account when specifying the cube for rendering but if you did, you can use backface culling too. It would be glEnable(GL_CULL).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this