0% found this document useful (0 votes)
25 views15 pages

CG Prog

Uploaded by

darkvaderkx007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views15 pages

CG Prog

Uploaded by

darkvaderkx007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CG Record Programs

1. Change Background Programs

#include<glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void init()
{
glClearColor(0.7, 0.5, 0.0, 0.0);
}
void main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Change Background Color");

init();
glutDisplayFunc(display);
glutMainLoop();
}

2. Points Example

#include <glut.h>

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(20.0);
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_POINTS);
glVertex2i(10,10);
glVertex2i(10,290);
glVertex2i(200,150);
glVertex2i(390,290);
glVertex2i(390,10);
glEnd();
glFlush();
}
void init() {
glClearColor(0.0,0.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 399.0, 0.0, 299.0);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 300);
glutCreateWindow("Points around corners");
init();
glutDisplayFunc(display);
glutMainLoop();
}

3. Lines: Normal line, line loop, line strip

#include <glut.h>

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(2.0);

glColor3f(1.0, 0.0, 0.0);


glBegin(GL_LINES);
glVertex2i(50, 50);
glVertex2i(200, 200);
glEnd();

glColor3f(0.0, 1.0, 0.0);


glBegin(GL_LINE_STRIP);
glVertex2i(250, 50);
glVertex2i(300, 150);
glVertex2i(350, 50);
glVertex2i(400, 150);
glEnd();

glColor3f(0.0, 0.0, 1.0);


glBegin(GL_LINE_LOOP);
glVertex2i(100, 300);
glVertex2i(150, 400);
glVertex2i(200, 350);
glVertex2i(150, 250);
glEnd();

glFlush();
}

void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Line Types: Normal Line, Line Strip, Line Loop");

init();
glutDisplayFunc(display);
glutMainLoop();
}

4. Triangles: Normal triangle, triangle strip, triangle fan

#include <glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 0.0, 0.0);


glBegin(GL_TRIANGLES);
glVertex2f(100, 300);
glVertex2f(150, 400);
glVertex2f(200, 300);
glEnd();

glColor3f(0.0, 1.0, 0.0);


glBegin(GL_TRIANGLE_STRIP);
glVertex2f(100, 100);
glVertex2f(150, 200);
glVertex2f(200, 100);
glVertex2f(250, 200);
glVertex2f(300, 100);
glEnd();

glColor3f(0.0, 0.0, 1.0);


glBegin(GL_TRIANGLE_FAN);
glVertex2f(400, 300);
glVertex2f(350, 350);
glVertex2f(450, 350);
glVertex2f(500, 300);
glVertex2f(450, 250);
glVertex2f(350, 250);
glEnd();

glFlush();
}

void init() {
glClearColor(1.0, 1.0, 1.0, 0.0);
gluOrtho2D(0.0, 600.0, 0.0, 600.0);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("Triangle Types: Normal Triangle, Strip and Fan Example");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

5. DDA Algorithm to draw line

#include <glut.h>
#include <math.h>

void dda(int x1, int y1, int x2, int y2) {


int dx = x2 - x1;
int dy = y2 - y1;

int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);

float xInc = dx / (float)steps;


float yInc = dy / (float)steps;

float x = x1;
float y = y1;

glBegin(GL_POINTS);
for (int i = 0; i <= steps; i++) {
glVertex2i(round(x), round(y));
x += xInc;
y += yInc;
}
glEnd();
glFlush();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
dda(100, 150, 250, 300);
}

void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0.0, 0.0, 0.0);
glPointSize(2.0);
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("DDA Line Drawing Algorithm");

init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

6. Bresenham's Algorithm to draw line

#include <glut.h>
#include<cmath>

void bresenhamLine(float x1, float y1, float x2, float y2) {


float dx = abs(x2 - x1);
float dy = abs(y2 - y1);
float m = dy / dx, d1, d2;
int x = x1, y = y1;
int* req = NULL;
if (m >= 1.0)
{
d1 = dx;
d2 = dy;
req = &y;
}
else
{
d1 = dy;
d2 = dx;
req = &x;
}
int steps = (int)abs(d2);
float p = d1 - d2;
glBegin(GL_POINTS);
for (int i = 0; i < steps; i++)
{
glVertex2i(x, y);
if (p < 0)
{
p += d1;
*req++;
}
else
{
p += d1 - d2;
x++;
y++;
}
}
glEnd();
glFlush();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
bresenhamLine(100, 150, 250, 300);
glFlush();
}

void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0.0, 0.0, 0.0);
glPointSize(2.0);
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Bresenham's Line Drawing Algorithm");

init();
glutDisplayFunc(display);
glutMainLoop();
}

7. Quadrilateral

#include <glut.h>

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(1.0, 0.5, 0.5);
glVertex2i(100, 100);
glVertex2i(300, 100);
glVertex2i(250, 300);
glVertex2i(50, 300);
glEnd();

glFlush();
}

void init() {
glClearColor(1.0, 1.0, 1.0, 0.0);
gluOrtho2D(0.0, 499.0, 0.0, 499.0);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("Quadrilateral");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

8. Mesh/Grid example

#include<glut.h>
void drawGrid()
{
glBegin(GL_LINES);
for (int i = 0; i <= 20; i++)
{
glVertex2i(i, 0);
glVertex2i(i, 20);
glVertex2i(0, i);
glVertex2i(20, i);
}
glEnd();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawGrid();
glFlush();
}

void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0.0, 0.0, 0.0);
glLineWidth(2.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 10.0, 0.0, 10.0);
}
void main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Mesh/Grid Example");
glutDisplayFunc(display);
init();
glutMainLoop();
}

9. Display text

#include <glut.h>

void output(GLfloat x, GLfloat y, const char* text) {


const char* p;
glPushMatrix();
glTranslated(x, y, 0);
glScaled(0.2, 0.2, 0);
for (p = text; *p; p++) {
glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
}
glPopMatrix();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
output(10, 300, "B.N.M Institute of Technology");
glFlush();
}

void myinit() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(1.0, 0.0, 0.0);
gluOrtho2D(0.0, 499.0, 0.0, 499.0);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL Text Output");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}

10. 3D Sierpinski Gasket(Tetrahedron)

#include <stdio.h>
#include <stdlib.h>
#include <glut.h>

typedef float point[3];

point v[4] = { {0.0, 0.0, 10.0}, {0.0, 10.0, -10.0}, {-10.0, -10.0, -10.0}, {10.0,
-10.0, -10.0} };
int n;

void triangle(point a, point b, point c) {


glBegin(GL_POLYGON);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}

void divide_triangle(point a, point b, point c, int m) {


point p1, p2, p3;
if (m > 0) {
for (int i = 0; i < 3; i++) {
p1[i] = (a[i] + b[i]) / 2;
p2[i] = (a[i] + c[i]) / 2;
p3[i] = (b[i] + c[i]) / 2;
}
divide_triangle(a, p1, p2, m - 1);
divide_triangle(c, p2, p3, m - 1);
divide_triangle(b, p3, p1, m - 1);
}
else {
triangle(a, b, c);
}
}

void tetrahedron(int m) {
glColor3f(1.0, 0.0, 0.0);
divide_triangle(v[0], v[1], v[2], m);
glColor3f(0.0, 1.0, 0.0);
divide_triangle(v[3], v[2], v[1], m);
glColor3f(0.0, 0.0, 1.0);
divide_triangle(v[0], v[3], v[1], m);
glColor3f(0.0, 0.0, 0.0);
divide_triangle(v[0], v[2], v[3], m);
}

void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
tetrahedron(n);
glFlush();
}

void myReshape(int w, int h) {


glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-12.0, 12.0, -12.0 * (GLfloat)h / (GLfloat)w, 12.0 * (GLfloat)h /
(GLfloat)w, -12.0, 12.0);
else
glOrtho(-12.0 * (GLfloat)w / (GLfloat)h, 12.0 * (GLfloat)w / (GLfloat)h,
-12.0, 12.0, -12.0, 12.0);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}

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


printf("No. of divisions? ");
scanf_s("%d", &n);

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("3D Gasket");

glClearColor(1.0, 1.0, 1.0, 1.0);


glEnable(GL_DEPTH_TEST);

glutReshapeFunc(myReshape);
glutDisplayFunc(display);

glutMainLoop();

11. Scenery Example

#include <glut.h>
#include <math.h>

// Function to draw a circle (for sun, cloud, etc.)


void drawCircle(float x, float y, float radius) {
int i;
int triangleAmount = 100;
GLfloat twicePi = 2.0f * 3.14159265f;

glBegin(GL_TRIANGLE_FAN);
glVertex2f(x, y);
for (i = 0; i <= triangleAmount; i++) {
glVertex2f(
x + (radius * cos(i * twicePi / triangleAmount)),
y + (radius * sin(i * twicePi / triangleAmount))
);
}
glEnd();
}
void drawSunRays(float x, float y, float radius, int numRays) {
glBegin(GL_LINES);
for (int i = 0; i < numRays; i++) {
float angle = (2.0f * 3.14159f / numRays) * i;
float x1 = x + radius * cos(angle);
float y1 = y + radius * sin(angle);
glVertex2f(x, y); // Center of the sun
glVertex2f(x1, y1); // End of the ray
}
glEnd();
}
void drawCrow(float x, float y) {
glColor3f(0.0f, 0.0f, 0.0f); // Dark color for crow
glLineWidth(2.0);
glBegin(GL_LINES);

// Draw the small "V" shape


glVertex2f(x - 0.02f, y); // Left point of "V"
glVertex2f(x, y - 0.05f); // Bottom point of "V"

glVertex2f(x, y - 0.05f); // Bottom point of "V"


glVertex2f(x + 0.02f, y); // Right point of "V"

glEnd();
}

// Function to draw the house


void drawHouse(float x, float y, float scale, float r, float g, float b) {
// House base
glColor3f(r, g, b); // House color
glBegin(GL_QUADS);
glVertex2f(x - scale, y);
glVertex2f(x + scale, y);
glVertex2f(x + scale, y + scale * 0.75f);
glVertex2f(x - scale, y + scale * 0.75f);
glEnd();
// Roof
glColor3f(0.4f, 0.2f, 0.0f); // Brown roof
glBegin(GL_TRIANGLES);
glVertex2f(x - scale * 1.2f, y + scale * 0.75f);
glVertex2f(x + scale * 1.2f, y + scale * 0.75f);
glVertex2f(x, y + scale * 1.5f);
glEnd();

glColor3f(0.5f,0.25f,0.0f); // Door color (brown)


float doorWidth = scale * 0.4f; // Width of the door
float doorHeight = scale * 0.6f; // Height of the door
glBegin(GL_QUADS);
glVertex2f(x - doorWidth / 2, y); // Bottom-left
glVertex2f(x + doorWidth / 2, y); // Bottom-right
glVertex2f(x + doorWidth / 2, y + doorHeight); // Top-right
glVertex2f(x - doorWidth / 2, y + doorHeight); // Top-left
glEnd();
}

// Function to draw a tree


void drawTree(float x, float y, float scale) {
// Tree trunk
glColor3f(0.55f, 0.27f, 0.07f); // Brown trunk
glBegin(GL_QUADS);
glVertex2f(x - scale * 0.1f, y);
glVertex2f(x + scale * 0.1f, y);
glVertex2f(x + scale * 0.1f, y + scale * 0.5f);
glVertex2f(x - scale * 0.1f, y + scale * 0.5f);
glEnd();

// Tree leaves (triangle)


glColor3f(0.0f, 0.5f, 0.0f); // Green leaves
glBegin(GL_TRIANGLES);
glVertex2f(x - scale * 0.5f, y + scale * 0.5f);
glVertex2f(x + scale * 0.5f, y + scale * 0.5f);
glVertex2f(x, y + scale);
glEnd();
}

// Function to draw the boat


void drawBoat(float x, float y) {
// Boat base
glColor3f(0.0f, 0.0f, 0.5f); // Dark blue
glBegin(GL_QUADS);
glVertex2f(x - 0.3f, y);
glVertex2f(x + 0.3f, y);
glVertex2f(x + 0.2f, y - 0.1f);
glVertex2f(x - 0.2f, y - 0.1f);
glEnd();
// Sail
glColor3f(1.0f, 0.0f, 0.0f); // Red sail
glBegin(GL_TRIANGLES);
glVertex2f(x, y);
glVertex2f(x, y + 0.2f);
glVertex2f(x + 0.2f, y);
glEnd();

// Boat mast
glColor3f(0.0f, 0.0f, 0.0f); // Black mast
glBegin(GL_LINES);
glVertex2f(x, y);
glVertex2f(x, y + 0.25f);
glEnd();
}

// Function to draw the full scene


void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Sky background
glColor3f(0.53f, 0.81f, 0.92f); // Light blue
glBegin(GL_QUADS);
glVertex2f(-1.0f, -0.2f);
glVertex2f(1.0f, -0.2f);
glVertex2f(1.0f, 1.0f);
glVertex2f(-1.0f, 1.0f);
glEnd();

drawCrow(0.1f, 0.5f); // Crow 1


drawCrow(-0.2f, 0.85f); // Crow 2
drawCrow(0.4f, 0.85f); // Crow 3

// Ground
glColor3f(0.0f, 0.5f, 0.0f); // Green
glBegin(GL_QUADS);
glVertex2f(-1.0f, -0.2f);
glVertex2f(1.0f, -0.2f);
glVertex2f(1.0f, -1.0f);
glVertex2f(-1.0f, -1.0f);
glEnd();

// Water
glColor3f(0.0f, 0.5f, 1.0f); // Blue water
glBegin(GL_QUADS);
glVertex2f(-1.0f, -0.6f);
glVertex2f(1.0f, -0.6f);
glVertex2f(1.0f, -1.0f);
glVertex2f(-1.0f, -1.0f);
glEnd();

// Sun
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
drawSunRays(0.15f, 0.7f, 0.15f, 12); // 12 rays
drawCircle(0.15f, 0.7f, 0.1f);

// Clouds (original three)


glColor3f(1.0f, 1.0f, 1.0f); // White
drawCircle(0.5f, 0.7f, 0.08f);
drawCircle(0.6f, 0.735f, 0.08f);
drawCircle(0.7f, 0.7f, 0.08f);

drawCircle(-0.3f, 0.735f, 0.08f);


drawCircle(-0.2f, 0.7f, 0.08f);
drawCircle(-0.4f, 0.7f, 0.08f);

// Mountains
glColor3f(0.6f, 0.3f, 0.1f); // Brown mountains
glBegin(GL_TRIANGLES);
glVertex2f(-1.0f, -0.2f);
glVertex2f(-0.8f, 0.3f);
glVertex2f(-0.6f, -0.2f);

glEnd();

glBegin(GL_TRIANGLES);
glVertex2f(-0.5f, -0.2f);
glVertex2f(-0.3f, 0.35f);
glVertex2f(-0.1f, -0.2f);
glEnd();

glBegin(GL_TRIANGLES);
glVertex2f(0.0f, -0.2f);
glVertex2f(0.2f, 0.25f);
glVertex2f(0.4f, -0.2f);
glEnd();

glBegin(GL_TRIANGLES);
glVertex2f(0.5f, -0.2f);
glVertex2f(0.7f, 0.3f);
glVertex2f(0.9f, -0.2f);
glEnd();

// Draw houses
drawHouse(-0.7f, -0.2f, 0.15f, 0.8f, 0.0f, 0.0f); // Red house
drawHouse(-0.2f, -0.2f, 0.15f, 0.0f, 0.8f, 0.0f); // Green house
drawHouse(0.3f, -0.2f, 0.15f, 0.0f, 0.0f, 0.8f); // Blue house
drawHouse(0.8f, -0.2f, 0.15f, 1.0f, 0.5f, 0.f); // Yellow house

// Draw trees
drawTree(-0.9f, -0.2f, 0.15f);
drawTree(-0.4f, -0.2f, 0.15f);
drawTree(0.1f, -0.2f, 0.15f);
drawTree(0.6f, -0.2f, 0.15f);

// Draw a boat on the water


drawBoat(0.0f, -0.75f);

// Refresh
glFlush();
}

// Initialize OpenGL
void init() {
glClearColor(0.53f, 0.81f, 0.92f, 1.0f); // Sky blue background
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Scenery Example");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

You might also like