How do I keyboard input 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:

01. F1+r = 60' rotation(static)
02. F2+r =45' rotation(continuous x-axis)
03. CTRL+w = White
04. B = Black

Thanks for your concern and help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  #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;
}
Things you might try.. (3 is probably what you want)

1) gui, where part of the screen was the drawing area, and part of it was gui widgets like text input box
2) full screen, you can still pop up an entry box if your code has gui enabled. You may have to stop the draw thread for this, or force the entry box popup to be 'on top'


3) I think cin/getch etc all work fine. This is ok to do things like moving around in the graphics without needing to see text typed. It should work, but I havent done this in a very long time...

Last edited on
Topic archived. No new replies allowed.