| |
 |
OpenGL Tutorial on switching to Fullscreen/GameMode
|
|
If you would like to see this site updated, please help out and
|
In this opengl tutorial I will be teaching you how to set your games into fullscreen mode using glut. This is often used to increase the amount of frames per second in a game, along with setting the quality of the picture. A lower resolution in fullscreen mode means a worse looking picture. Fullscreen mode in GLUT is probably one of the easiest things you can do
just before you call the line to enter glut fullscreen mode you choose
which settings to use when in glut fullscreen mode.
Such as the resolution,
color depth and the refresh rate.
we do this with:
glutGameModeString( "990x768:32@75" );
this is setting the resolution to 990x768, you can also use 800x600, 640x480, etc
next it is saying to use a color depth of 32 bits, you can also use 24, 16, 8, etc
then it is telling the program to run the screen refresh rate at 75hertz, others are 65, 70, 80, 85, etc
after that when then tell GLUT to enter the fullscreen mode which it callse Game Mode.
we do this with:
glutEnterGameMode();
then when we want to exit the program we say:
glutLeaveGameMode(); to set the screen back to how it was
and that was it. simple wasn't it?
If you have any questions, just email me at swiftless@gmail.com :-)
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.
| | | #include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
//angle of rotation
float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 90, angle=0.0;
//draw the cubes, they make a fancy shape from above :P
void cube (void) {
float i;
for (i=0;i<50;i++)
{
glTranslated(1, 0, 1);
glPushMatrix();
glutSolidCube(2); //draw the cube
glPopMatrix();
}
}
void init (void) {
glEnable (GL_DEPTH_TEST); //enable the depth testing
glEnable (GL_LIGHTING); //enable the lighting
glEnable (GL_LIGHT0); //enable LIGHT0, our Diffuse Light
glShadeModel (GL_SMOOTH); //set the shader to smooth shader
}
void camera (void) {
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);
glTranslated(-xpos,-ypos,-zpos);
}
void display (void) {
glClearColor (0.0,0.0,0.0,1.0); //clear the screen to black
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); / /clear the color buffer and the depth buffer
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); / /camera position, x,y,z, looking at x,y,z, Up Positions of the camera
camera();
cube(); //call the cube drawing function
glutSwapBuffers(); //swap the buffers
angle++; //increase the angle
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h); //set the viewport to the current window specifications
glMatrixMode (GL_PROJECTION); //set the matrix to projection
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0); //set the perspective (angle of sight, width, height, , depth)
glMatrixMode (GL_MODELVIEW); //set the matrix back to model
}
void keyboard (unsigned char key, int x, int y) {
if (key=='q')
{
xrot += 1;
if (xrot >360) xrot -= 360;
}
if (key=='z')
{
xrot -= 1;
if (xrot < -360) xrot += 360;
}
if (key=='w')
{
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos += float(sin(yrotrad)) ;
zpos -= float(cos(yrotrad)) ;
ypos -= float(sin(xrotrad)) ;
}
if (key=='s')
{
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos -= float(sin(yrotrad));
zpos += float(cos(yrotrad)) ;
ypos += float(sin(xrotrad));
}
if (key=='d')
{
yrot += 1;
if (yrot >360) yrot -= 360;
}
if (key=='a')
{
yrot -= 1;
if (yrot < -360)yrot += 360;
}
if (key==27)
{
glutLeaveGameMode(); //set the resolution how it was
exit(0); //quit the program
}
}
int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH); //set the display to Double buffer, with depth
glutGameModeString( "1024x768:32@75" ); //the settings for fullscreen mode
glutEnterGameMode(); //set glut to fullscreen using the settings in the line above
init (); //call the init function
glutDisplayFunc (display); //use the display function to draw everything
glutIdleFunc (display); //update any variables in display, display can be changed to anyhing, as long as you move the variables to be updated, in this case, angle++;
glutReshapeFunc (reshape); //reshape the window accordingly
glutKeyboardFunc (keyboard); //check the keyboard
glutMainLoop (); //call the main loop
return 0;
}
|
Download C++ Source Code for this Tutorial
Comments:
| Name: Baby Stuey |
| Date: 2010-03-02 19:09:37 |
Comment:
Explicit casting must be done like so:
xpos += (float)( sin(yrotrad) );
(You're missing the parentheses around float, 6 errors)
|
| Name: Baby Stuey |
| Date: 2010-03-02 21:18:22 |
Comment:
A review of casting. The above got me thinking if the cast to float was even necessary (it shouldn't be)...
#include <stdio.h> #include <math.h>
#define PI 3.141592654
// Numbers without a decimal point are considered integers // Numbers with a decimal point are considered floats, with or without digits to the right of the decimal.
// fp is short for floating-point // a 'truncated result' is a floating-point value that has lost all of its significant figures to the right of the decimal.
// Calculations involving at least one floating-point value will yield a floating point result. // Calculations involving only integer values will yield an integer result.
int main(void) { printf("\n\tDivision involving whole integer values:\nResult of 10 / 3 should be 3.333...\n"); // DON'Ts printf("%d \n", (5. / 3.) ); // = ???? (result of fp division is piped through the wrong format specifier, result is unpredictable) printf("%0.3f\n", (5 / 3) ); // = 1.667 (result of integer division is piped through the wrong format specifier, result is correct, but this is bad form) // Common OOPSes! printf("%0.3f\n", (float)(5 / 3) ); // = 1.000 (result of integer division cast to float, result is truncated) printf("%0.3f\n", (float)(5. / 3.) ); // = 1.667 (result of fp division redundantly cast to float, result is correct) printf("%0.3f\n", ((int)5. / 3.) ); // = 1.667 (5. cast to an int, but promoted back to float, result is correct) // DOs printf("%d \n", (5 / 3) ); // = 1 (result of integer division is truncated, but correct) printf("%d \n", (int)(5. / 3.) ); // = 1 (result of fp division cast to int - result is truncated) printf("%0.3f\n", ( (float)5 / 3) ); // = 1.667 (integer 5 cast to a float for fp division, result is correct) printf("%0.3f\n", ( 5 / (float)3) ); // = 1.667 (integer 3 cast to a float for fp division, result is correct) printf("%0.3f\n", (5. / 3.) ); // = 1.667 (both 5. and 3. are floats already, no cast needed, result is correct) printf("%0.3f\n", (5 / 3.) ); // = 1.667 (5 automatically promoted to float, no cast needed, result is correct) printf("%0.3f\n", (5. / 3) ); // = 1.667 (3 automatically promoted to float, no cast needed, result is correct)
// sin() takes a value in radians and returns the resulting sine as a floating-point value. printf("\n\tSine of a floating point value:\nResult of sin( PI/3 ) should be sqrt(3)/2 = 0.866...\n"); printf("%0.3f\n", sin(PI/3) ); // = 0.866 (fp input, result of sin() correct) printf("%d \n", (int)sin(PI/3) ); // = 0 (fp input, result of sin() cast to int, truncating result) printf("%0.3f\n", (float)sin(PI/3) ); // = 0.866 (fp input, result of sin() redundantly cast to float, but correct) printf("%0.3f\n", sin( (int)PI /3) ); // = 0.841 (int input, result of sin() is correct for sin(1))
printf("\n\tSine of a whole integer value:\nResult of sin(1) should be = 0.841...\n"); printf("%0.3f\n", sin(1) ); // = 0.841 (int input, result of sin() is correct) printf("%0.3f\n", sin(1.) ); // = 0.841 (fp input, result of sin() is correct) printf("%d \n", (int)sin(1) ); // = 0 (int input, cast to int, truncating result) printf("%d \n", (int)sin(1.) ); // = 0 (fp input, cast to int, truncating result) printf("%0.3f\n", (float)sin(1) ); // = 0.841 (int input, result of sin() redundantly cast to float, but correct) printf("%0.3f\n", (float)sin(1.) ); // = 0.841 (fp input, result of sin() redundantly cast to float, but correct)
printf("\n\tSine of the result of dividing two whole integer values:\nResult of sin(1.5) should be = 0.997...\n"); printf("%0.3f\n", sin(3/2) ); // = 0.841 (int input, result of sin() is correct for sin(1)) printf("%0.3f\n", sin(3./2) ); // = 0.997 (fp input, result of sin() is correct) printf("%0.3f\n", sin(3/2.) ); // = 0.997 (fp input, result of sin() is correct) }
|
Name Email
|
 |