How do I input keyboard function in OpenGL

Hello,

I am trying to solve my OpenGL pyramid project. If anyone can please help me with a code as soon as possible.

I have to do when I build the code it will come black window After that perform the following task:

  1. F1+r = 60’ rotation(static)
  2. F2+r =45’ rotation(continuous x-axis)
  3. CTRL+w = White
  4. B = Black

Thanks for your concern and help.

#include <stdlib.h>

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

// all variables initialized to 1.0, meaning
// the triangle will initially be white
float red=1.0f, blue=1.0f, white=1.0f;

// angle for rotating triangle
float angle = 0.0f;

void changeSize(int w, int h) {

	// Prevent a divide by zero, when window is too short
	// (you cant make a window of zero width).
	if (h == 0)
		h = 1;
	float ratio =  w * 1.0 / h;

        // Use the Projection Matrix
	glMatrixMode(GL_PROJECTION);

        // Reset Matrix
	glLoadIdentity();

	// Set the viewport to be the entire window
	glViewport(0, 0, w, h);

	// Set the correct perspective.
	gluPerspective(45.0f, ratio, 0.1f, 100.0f);

	// Get Back to the Modelview
	glMatrixMode(GL_MODELVIEW);
}

void renderScene(void) {

	// Clear Color and Depth Buffers
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Reset transformations
	glLoadIdentity();

	// Set the camera
	gluLookAt(	0.0f, 0.0f, 10.0f, 0.0f, 0.0f,  0.0f, 0.0f, 1.0f,  0.0f);

	glRotatef(angle, 1.0f, 1.0f, 0.0f);

	glColor3f(red,white,blue);
	glBegin(GL_TRIANGLES);
	  //glColor3f(1.0, 1.0, 1.0);     // Red
      glVertex3f( 0.0f, 1.0f, 0.0f);
     // glColor3f(1.0, 1.0, 1.0);    // Green
      glVertex3f(-1.0f, -1.0f, 1.0f);
      //glColor3f(1.0, 1.0, 1.0);     // Blue
      glVertex3f(1.0f, -1.0f, 1.0f);
 
      // Right
     // glColor3f(0.0, 1.0, 1.0);     // Red
      glVertex3f(0.0f, 1.0f, 0.0f);
      //glColor3f(0.0, 1.0, 1.0);   // Blue
      glVertex3f(1.0f, -1.0f, 1.0f);
      //glColor3f(0.0, 1.0, 1.0);    // Green
      glVertex3f(1.0f, -1.0f, -1.0f);
 
      // Back
      //glColor3f(0.0, 0.5, 0.0);    // Red
      glVertex3f(0.0f, 1.0f, 0.0f);
    // glColor3f(0.0, 0.5, 0.0);    // Green
      glVertex3f(1.0f, -1.0f, -1.0f);
    // glColor3f(0.0, 0.5, 0.0);     // Blue
      glVertex3f(-1.0f, -1.0f, -1.0f);
 
      // Left
    // glColor3f(0.6, 0.6, 0.6);       // Red
      glVertex3f( 0.0f, 1.0f, 0.0f);
     // glColor3f(0.663, 0.663, 0.663);      // Blue
      glVertex3f(-1.0f,-1.0f,-1.0f);
    // glColor3f(0.663, 0.663, 0.663);      // Green
      glVertex3f(-1.0f,-1.0f, 1.0f);

	glEnd();

	angle+=0.1f;

	glutSwapBuffers();
}

void processNormalKeys(unsigned char key, int x, int y) {

	if (key == 27)
		exit(0);
}

void processSpecialKeys(int key, int x, int y) {

	switch(key) {

			if(key=='1')
	{
		angle+=10;
	}
	if(key=='0')
	{
		angle+=1;
	}

		case GLUT_KEY_F1 :
				red = 1.0;
				white = 0.0;
				blue = 0.0;
				; break;
		case GLUT_KEY_F2 :
				red = 0.0;
				white = 1.0;
				blue = 0.0; break;
		case GLUT_KEY_F3 :
				red = 0.0;
				white = 0.0;
				blue = 1.0; break;
	}
}

int main(int argc, char **argv) {

	// init GLUT and create window
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(640,480);
	glutCreateWindow("Lighthouse3D- GLUT Tutorial");

	// register callbacks
	glutDisplayFunc(renderScene);
	glutReshapeFunc(changeSize);
	glutIdleFunc(renderScene);

	// here are the new entries
	glutKeyboardFunc(processNormalKeys);
	glutSpecialFunc(processSpecialKeys);

	// enter GLUT event processing cycle
	glutMainLoop();

	return 1;
}

Hi,

looks like this is more of a general C++ or OpenGL question, instead of a Cinder-related one. Maybe you have a better chance of getting an answer on the OpenGL forums.

In Cinder, if you want to listen to keyboard events, simply override the keyDown() and keyUp() methods of your main application class, then do something like this:

void MyApp::keyDown( KeyEvent &event )
{
    switch( event.getCode() ) {
        case KeyEvent::KEY_w:
            if( event.isControlDown() ) {
                mCurrentColor = Color::white(); // where mCurrentColor is of type ci::Color
            }
        break;
    }
}

-Paul

Hi paul,

I was trying to post OpenGL forum. But new user registration baned, why? I don’t know.

Would you please correct my code, I tried that method but doesn’t work.

Sincerely,
Anup Roy

Hi Anup,

I’m happy to answer any Cinder-related questions. But I can’t help you with your non-Cinder project.

-Paul

OKay Thank you Paul.