#include <GL/glut.h> // Initialization function void init() { glClearColor(0.5f, 0.8f, 1.0f, 1.0f); // Background color (sky blue) glEnable(GL_DEPTH_TEST); // Enable depth testing } // Display function void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); // Camera settings gluLookAt(0.0, 5.0, 10.0, // Eye position 0.0, 0.0, 0.0, // Look-at point 0.0, 1.0, 0.0); // Up direction // Draw a simple chair glPushMatrix(); glTranslatef(-3.0, 0.0, 0.0); drawChair(); glPopMatrix(); // Draw a simple table glPushMatrix(); glTranslatef(3.0, 0.0, 0.0); drawTable(); glPopMatrix(); // Swap buffers glutSwapBuffers(); } // Simple chair drawing function void drawChair() { glPushMatrix(); glColor3f(0.4f, 0.2f, 0.1f); glScalef(1.0f, 0.1f, 1.0f); glutSolidCube(2.0f); // Seat glPopMatrix(); glPushMatrix(); glColor3f(0.4f, 0.2f, 0.1f); glTranslatef(0.0f, 1.0f, -0.9f); glScalef(1.0f, 2.0f, 0.1f); glutSolidCube(2.0f); // Backrest glPopMatrix(); } // Simple table drawing function void drawTable() { glPushMatrix(); glColor3f(0.6f, 0.3f, 0.0f); glScalef(3.0f, 0.1f, 2.0f); glutSolidCube(2.0f); // Tabletop glPopMatrix(); glPushMatrix(); glColor3f(0.6f, 0.3f, 0.0f); glTranslatef(2.8f, -1.0f, 1.8f); glScalef(0.1f, 2.0f, 0.1f); glutSolidCube(2.0f); // Table leg glPopMatrix(); glPushMatrix(); glColor3f(0.6f, 0.3f, 0.0f); glTranslatef(-2.8f, -1.0f, 1.8f); glScalef(0.1f, 2.0f, 0.1f); glutSolidCube(2.0f); // Table leg glPopMatrix(); glPushMatrix(); glColor3f(0.6f, 0.3f, 0.0f); glTranslatef(2.8f, -1.0f, -1.8f); glScalef(0.1f, 2.0f, 0.1f); glutSolidCube(2.0f); // Table leg glPopMatrix(); glPushMatrix(); glColor3f(0.6f, 0.3f, 0.0f); glTranslatef(-2.8f, -1.0f, -1.8f); glScalef(0.1f, 2.0f, 0.1f); glutSolidCube(2.0f); // Table leg glPopMatrix(); } // Reshape function void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, (double)w / (double)h, 1.0, 100.0); glMatrixMode(GL_MODELVIEW); } // Main function int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(800, 600); glutCreateWindow("OpenGL Environment"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}