2nd LAB
#include <GL/gl.h>
#include <GL/glut.h>
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
// =====================================
// RECTANGLE
// =====================================
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
// Proper rectangle
glVertex3f(0.08f, 0.08f, 0.0f);
glVertex3f(0.38f, 0.08f, 0.0f);
glVertex3f(0.38f, 0.32f, 0.0f);
glVertex3f(0.08f, 0.32f, 0.0f);
glEnd();
// =====================================
// HOUSE BODY
// =====================================
glColor3f(0.85f, 0.82f, 0.65f);
glBegin(GL_QUADS);
glVertex3f(0.52f, 0.08f, 0.0f);
glVertex3f(0.82f, 0.08f, 0.0f);
glVertex3f(0.82f, 0.35f, 0.0f);
glVertex3f(0.52f, 0.35f, 0.0f);
glEnd();
// =====================================
// ROOF
// =====================================
glColor3f(0.65f, 0.38f, 0.20f);
glBegin(GL_TRIANGLES);
glVertex3f(0.48f, 0.35f, 0.0f);
glVertex3f(0.86f, 0.35f, 0.0f);
glVertex3f(0.67f, 0.58f, 0.0f);
glEnd();
// =====================================
// FLOATING TRIANGLE
// =====================================
glColor3f(0.3f, 0.8f, 0.4f);
glBegin(GL_TRIANGLES);
glVertex3f(0.15f, 0.60f, 0.0f);
glVertex3f(0.35f, 0.60f, 0.0f);
glVertex3f(0.25f, 0.82f, 0.0f);
glEnd();
glFlush();
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -10.0, 10.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Fixed Shapes");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Windows:
#include <windows.h>
#include <iostream>
#include <stdlib.h>
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
using namespace std;
//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
}
}
//Initializes 3D rendering
void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL); //Enable color
glClearColor(0.7f, 0.9f, 1.0f, 1.0f); //Change the background to sky blue
}
//Called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
float _angle = 30.0f;
float _cameraAngle = 0.0f;
//Draws the 3D scene
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-_cameraAngle, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, -5.0f);
glPushMatrix();
glTranslatef(0.0f, -1.0f, 0.0f);
glRotatef(_angle, 0.0f, 0.0f, -1.0f);
glBegin(GL_QUADS);
//Trapezoid
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-0.7f, -0.5f, 0.0f);
glVertex3f(0.7f, -0.5f, 0.0f);
glVertex3f(0.7f, 0.5f, 0.0f);
glVertex3f(-0.7f, 0.5f, 0.0f);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(1.0f, 1.0f, 0.0f);
glRotatef(_angle, 0.0f, 1.0f, 0.0f);
glScalef(0.7f, 0.7f, 0.7f);
glBegin(GL_TRIANGLES);
glColor3f(0.0f, 1.0f, 0.0f);
//Pentagon
glVertex3f(-0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(-0.5f, 0.0f, 0.0f);
glVertex3f(-0.5f, 0.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
glVertex3f(-0.5f, 0.0f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(-1.0f, 1.0f, 0.0f);
glRotatef(_angle, 1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
//Triangle
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void update(int value) {
_angle += 2.0f;
if (_angle > 360) {
_angle -= 360;
}
glutPostRedisplay(); ////Tell GLUT that the scene has changed
glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
//Create the window
glutCreateWindow("Color");
initRendering();
//Set handler functions
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0); //Add a timer
glutMainLoop();
return 0;
}3nd LAB LINUX
#include <windows.h>
#include <iostream>
#include <stdlib.h>
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
using namespace std;
//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
}
}
//Initializes 3D rendering
void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL); //Enable color
glClearColor(0.7f, 0.9f, 1.0f, 1.0f); //Change the background to sky blue
}
//Called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
float _angle = 30.0f;
float _cameraAngle = 0.0f;
//Draws the 3D scene
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-_cameraAngle, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, -5.0f);
glPushMatrix();
glTranslatef(0.0f, -1.0f, 0.0f);
glRotatef(_angle, 0.0f, 0.0f, -1.0f);
glBegin(GL_QUADS);
//Trapezoid
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-0.7f, -0.5f, 0.0f);
glVertex3f(0.7f, -0.5f, 0.0f);
glVertex3f(0.7f, 0.5f, 0.0f);
glVertex3f(-0.7f, 0.5f, 0.0f);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(1.0f, 1.0f, 0.0f);
glRotatef(_angle, 0.0f, 1.0f, 0.0f);
glScalef(0.7f, 0.7f, 0.7f);
glBegin(GL_TRIANGLES);
glColor3f(0.0f, 1.0f, 0.0f);
//Pentagon
glVertex3f(-0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(-0.5f, 0.0f, 0.0f);
glVertex3f(-0.5f, 0.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
glVertex3f(-0.5f, 0.0f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(-1.0f, 1.0f, 0.0f);
glRotatef(_angle, 1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
//Triangle
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void update(int value) {
_angle += 2.0f;
if (_angle > 360) {
_angle -= 360;
}
glutPostRedisplay(); ////Tell GLUT that the scene has changed
glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
//Create the window
glutCreateWindow("Color");
initRendering();
//Set handler functions
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0); //Add a timer
glutMainLoop();
return 0;
}Printing Name: (IMRAN)
#include <GL/glut.h>
// Function to draw letters
void drawName() {
glLineWidth(3);
glBegin(GL_LINES);
// I
glVertex2f(-0.9, 0.5);
glVertex2f(-0.9, -0.5);
// M
glVertex2f(-0.7, -0.5);
glVertex2f(-0.7, 0.5);
glVertex2f(-0.7, 0.5);
glVertex2f(-0.6, 0.0);
glVertex2f(-0.6, 0.0);
glVertex2f(-0.5, 0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(-0.5, -0.5);
// R
glVertex2f(-0.3, -0.5);
glVertex2f(-0.3, 0.5);
glVertex2f(-0.3, 0.5);
glVertex2f(-0.1, 0.5);
glVertex2f(-0.1, 0.5);
glVertex2f(-0.1, 0.0);
glVertex2f(-0.1, 0.0);
glVertex2f(-0.3, 0.0);
glVertex2f(-0.3, 0.0);
glVertex2f(-0.1, -0.5);
// A
glVertex2f(0.1, -0.5);
glVertex2f(0.2, 0.5);
glVertex2f(0.2, 0.5);
glVertex2f(0.3, -0.5);
glVertex2f(0.15, 0.0);
glVertex2f(0.25, 0.0);
// N
glVertex2f(0.5, -0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.7, -0.5);
glVertex2f(0.7, -0.5);
glVertex2f(0.7, 0.5);
glEnd();
}
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0); // White color
drawName();
glFlush();
}
// Initialization
void init() {
glClearColor(0.0, 0.0, 0.0, 1.0); // Black background
gluOrtho2D(-1, 1, -1, 1);
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(800, 500);
glutCreateWindow("Draw Name using OpenGL");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}Print IMRAN with Line
#include <GL/glut.h>
// Helper to draw a single line
void line(float x1, float y1, float x2, float y2) {
glVertex2f(x1, y1);
glVertex2f(x2, y2);
}
void drawName() {
glLineWidth(3);
glBegin(GL_LINES);
// I
line(-0.9, 0.5, -0.9, -0.5);
// M
line(-0.7, -0.5, -0.7, 0.5);
line(-0.7, 0.5, -0.6, 0.0);
line(-0.6, 0.0, -0.5, 0.5);
line(-0.5, 0.5, -0.5, -0.5);
// R
line(-0.3, -0.5, -0.3, 0.5);
line(-0.3, 0.5, -0.1, 0.5);
line(-0.1, 0.5, -0.1, 0.0);
line(-0.1, 0.0, -0.3, 0.0);
line(-0.3, 0.0, -0.1, -0.5);
// A
line(0.1, -0.5, 0.2, 0.5);
line(0.2, 0.5, 0.3, -0.5);
line(0.15, 0.0, 0.25, 0.0);
// N
line(0.5, -0.5, 0.5, 0.5);
line(0.5, 0.5, 0.7, -0.5);
line(0.7, -0.5, 0.7, 0.5);
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0, 0, 0);
drawName();
glFlush();
}
void init() {
glClearColor(1, 1, 1, 1);
gluOrtho2D(-1, 1, -1, 1);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(800, 500);
glutCreateWindow("IMRAN");
init();
glutDisplayFunc(display);
glutMainLoop();
}