First Internals Optimized Code
First Internals Optimized Code
dda
#include<stdio.h>
#include<stdlib.h>
#include<GL/glut.h>
void display(void) {
float dx = x2 - x1;
float dy = y2 - y1;
float steps;
float x = x1;
float y = y1;
glPointSize(5.0);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
glFlush();
}
void myInit(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float margin = 10.0; // Adjust this margin as needed for better visibility
return 0;
}
glColor3f(1, 0, 0);
glPointSize(5);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
Sleep(100);
}
dx = x1 - x0;
dy = y1 - y0;
x = x0;
y = y0;
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(2.0);
bresenham(40, 120, 100, 20);
printf("done");
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(0, 200, 0, 200);
}
return 0;
}
3.Design a real world picture using primitives such as points, lines, triangles and
polygons.
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
void drawRiver() {
glBegin(GL_POLYGON);
glVertex2i(0, 50);
glVertex2i(500, 50);
glVertex2i(500, 0);
glVertex2i(0, 0);
glEnd();
}
void drawStars() {
glColor3f(1.0, 1.0, 1.0); // White color for stars
glPointSize(2.0);
glBegin(GL_POINTS);
for (int i = 0; i < numStars; ++i) {
int x = rand() % 500; // Random x-coordinate
int y = rand() % 750; // Random y-coordinate
glVertex2i(x, y);
}
glEnd();
}
void myDisplay() {
glClear(GL_COLOR_BUFFER_BIT);
// Draw Stars
drawStars();
// Draw Landscape
glColor3f(0.5, 0.5, 0.5); // Gray color for mountains
drawMountain(50, 100, 150);
drawMountain(200, 150, 100);
drawMountain(350, 120, 120);
glFlush();
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 500, 0, 500);
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
#include <stdio.h>
#include <Windows.h>
#include <GL/glut.h>
float vertices[4][3] = { {10, 10, -490}, {490, 10, -490}, {240, 490, -490}, {240,
150, 490} };
void init() {
glClearColor(1, 1, 1, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 500, 0, 500, -500, 500);
glMatrixMode(GL_MODELVIEW);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawTetra(vertices[0], vertices[1], vertices[2], vertices[3], 2);
glFlush();
}
2d triangle
#include<GL/glut.h>
#include<stdio.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
divide_triangle(y[0], y[1], y[2], n);
glEnd();
glFlush();
}
void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(-10, 60, -10, 60);
}
void midpointCircle() {
int x = 0;
int y = r;
float decision = 1 - r;
while (y > x) {
if (decision < 0) {
decision += 2 * x + 3;
}
else {
decision += 2 * (x - y) + 5;
y--;
}
x++;
drawCircle(xc, yc, x, y);
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
midpointCircle();
glFlush();
}
void myInit() {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0);
gluOrtho2D(0, 500, 0, 500);
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Circle Drawing Algorithm");
myInit();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
6.Develop a menu driven program to fill the polygon using scan line algorithm
#include<stdio.h>
#include<GL/glut.h>
void edgeDetect(int x1, int y1, int x2, int y2, int* le, int* re) {
float mx;
float x;
int i, temp;
x = x1;
for (i = y1; i <= y2; i++) {
if (x < le[i])
le[i] = x;
if (x > re[i])
re[i] = x;
x = x + mx;
}
}
void drawPixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
void scanfill(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
int le[500], re[500];
int i, y;
for (int i = 0; i < 500; i++) {
le[i] = 500;
re[i] = 0;
}
edgeDetect(x1, y1, x2, y2, le, re);
edgeDetect(x2, y2, x3, y3, le, re);
edgeDetect(x3, y3, x4, y4, le, re);
edgeDetect(x4, y4, x1, y1, le, re);
switch (ch)
{
case 1: glColor3f(1, 0, 0); break;
case 2: glColor3f(0, 0, 1); break;
case 3: glColor3f(0, 1, 0); break;
}
glBegin(GL_LINE_LOOP);
glVertex2i(x0, y0);
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glVertex2i(x3, y3);
glEnd();
glFlush();
scanfill(x0, y0, x1, y1, x2, y2, x3, y3);
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(10, 10);
glutCreateWindow("Scanfill");
init();
glutDisplayFunc(display);
glutCreateMenu(select);
glutAddMenuEntry("red", 1);
glutAddMenuEntry("blue", 2);
glutAddMenuEntry("green", 3);
glutAttachMenu(GLUT_LEFT_BUTTON);
glutMainLoop();
return 0;
}
7.Implement the program to draw a polygon that interact with interact with input
functions
//Submenu - optimised
#include<GL/glut.h>
glEnd();
glFlush();
}
void main(int argc, char** argv)
{
GLint subMenu;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(200, 200);
glutInitWindowSize(400, 400);
glutCreateWindow("Submenu Example");
init();
glutDisplayFunc(display);
subMenu = glutCreateMenu(colorSubMenu);
glutAddMenuEntry("Blue", 1);
glutAddMenuEntry("Green", 2);
glutAddMenuEntry("White", 3);
glutCreateMenu(mainMenu);
glutAddMenuEntry("Solid-Color Fill", 1);
glutAddMenuEntry("Color-Interpolation Fill", 2);
glutAddSubMenu("Color", subMenu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
}
8.Create and rotate a triangle about the origin and a fixed point.
GLint s1[] = { 150,200 };
GLint s2[] = { 250,200 };
GLint s3[] = { 250,400 };
GLshort angle = 15;
void before_rotation_pivot() {
glColor3ub(130, 133, 131);
glBegin(GL_LINE_LOOP);
glVertex2iv(s1);
glVertex2iv(s2);
glVertex2iv(s3);
glEnd();
}
void after_rotation_pivot() {
glColor3ub(247, 190, 47);
glTranslatef(s1[0], s1[1], 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(-s1[0], -s1[1], 0);
glBegin(GL_LINE_LOOP);
glVertex2iv(s1);
glVertex2iv(s2);
glVertex2iv(s3);
glEnd();
}
void rotate_pivot() {
glClear(GL_COLOR_BUFFER_BIT);
before_rotation_pivot();
after_rotation_pivot();
glFlush();
}
void before_rotation() {
glColor3ub(130, 133, 131);
glBegin(GL_LINE_LOOP);
glVertex2iv(q1);
glVertex2iv(q2);
glVertex2iv(q3);
glVertex2iv(q4);
glEnd();
}
void after_rotation() {
glColor3ub(0, 255, 0);
glRotatef(angle, 0, 0, 1);
glBegin(GL_LINE_LOOP);
glVertex2iv(q1);
glVertex2iv(q2);
glVertex2iv(q3);
glVertex2iv(q4);
glEnd();
}
void rotate_origin() {
glClear(GL_COLOR_BUFFER_BIT);
before_rotation();
after_rotation();
glFlush();
}
void scale_triangle() {
float scaleFactor = .3;
drawTriangle(v[0], v[1], v[2]); //Before
glScalef(scaleFactor, scaleFactor, scaleFactor);
drawTriangle(v[0], v[1], v[2]); //After
}
void rotate_triangle() {
drawTriangle(v[0], v[1], v[2]); //Before
glRotatef(45, 0, 0, 1);
drawTriangle(v[0], v[1], v[2]); //After
}
void translate_triangle() {
float translateX = -100, translateY = -80;
drawTriangle(v[0], v[1], v[2]); //Before
glTranslatef(translateX, translateY, 0);
drawTriangle(v[0], v[1], v[2]); //After
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
translate_triangle();
glFlush();
}
void init() {
glClearColor(1, 1, 1, 1);
gluOrtho2D(-250, 250, -250, 250);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(10, 10);
glutCreateWindow("Triangle Rotation");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}
float rectangleVertices[4][2] = { {50, 50}, {150, 50}, {150, 100}, {50, 100} };
float translationOffsetX = 50.0;
float translationOffsetY = 50.0;
void drawRectangle() {
glBegin(GL_QUADS);
for (int i = 0; i < 4; ++i) {
glVertex2f(rectangleVertices[i][0], rectangleVertices[i][1]);
}
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
// Original Rectangle
drawRectangle();
// Translated Rectangle
glColor3f(0.0, 0.0, 1.0); // Blue color
translateRectangle(translationOffsetX, translationOffsetY);
drawRectangle();
glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(0, 300, 0, 300);
}
return 0;
}
float rectangleVertices[4][2] = { {50, 50}, {150, 50}, {150, 100}, {50, 100} };
float scaleX=2, scaleY=2; // Rotation angle in degrees
void drawRectangle() {
glBegin(GL_QUADS);
glVertex2fv(rectangleVertices[0]);
glVertex2fv(rectangleVertices[1]);
glVertex2fv(rectangleVertices[2]);
glVertex2fv(rectangleVertices[3]);
glEnd();
}
void scaleRectangle(float xScale, float yScale) {
for (int i = 0; i < 4; i++) {
float x = rectangleVertices[i][0];
float y = rectangleVertices[i][1];
void display() {
glClear(GL_COLOR_BUFFER_BIT);
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(0, 500, 0, 500);
}
return 0;
}
float rectangleVertices[4][2] = { {50, 50}, {150, 50}, {150, 100}, {50, 100} };
float rotationAngle = 45.0; // Rotation angle in degrees
void drawRectangle() {
glBegin(GL_QUADS);
glVertex2fv(rectangleVertices[0]);
glVertex2fv(rectangleVertices[1]);
glVertex2fv(rectangleVertices[2]);
glVertex2fv(rectangleVertices[3]);
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(-200, 200, -200, 200);
}
return 0;
}
void init(void)
{
glClearColor(0.6, 0.3, 1, 0.4);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
void displayFcn(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 1.0);
glPointSize(3.0);
}
void winReshapeFcn(GLint newWidth, GLint newHeight)
{
glViewport(0, 0, newWidth, newHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)(newWidth), 0.0, (GLdouble)(newHeight));
winWidth = newWidth; winHeight = newHeight;
}
void plotPoint(GLint x, GLint y)
{
glPointSize(ptSize);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void curveDrawing(GLubyte curvePlotKey, GLint xMouse, GLint yMouse)
{
GLint x = xMouse;
GLint y = winHeight - yMouse;
switch (curvePlotKey)
{
case 'c': plotPoint(x, y);
break;
case '+': ptSize += 10;
break;
case '-': ptSize -= 10;
break;
default: break;
}
glFlush();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("Keyboard Curve-Drawing Example");
init();
glutDisplayFunc(displayFcn);
glutReshapeFunc(winReshapeFcn);
glutKeyboardFunc(curveDrawing);
glutMainLoop();
}
13.Using recti function plot the bar graph to show average temp in every month.
#include<GL/glut.h>
int temp[12] = { 22, 29, 17, 19, 35, 42, 50, 32, 21, 23, 18, 45 };
char month[12][3] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glColor3f(0, 0, 0);
glVertex2f(50, 50);
glVertex2f(480, 50);
glVertex2f(50, 50);
glVertex2f(50, 480);
glEnd();
glColor3f(1, 0, 0);
for (int i = 0; i < 12; i++)
{
glRecti(60 + 30 * i, 50, 80 + 30 * i, temp[i] * (480 / 50));
}
for (int i = 0; i < 12; i++)
{
glColor3f(0, 0, 0);
glRasterPos2f(60 + 30 * i, 30);
for (int j = 0; j < 3; j++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, month[i][j]);
}
}
glFlush();
}
void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(0, 500, 0, 500);
}
theory part
THEORY:
Points:
Attributes:
Color:
Attribute: glColor*
Description: Sets the color for subsequent primitives. Various functions like
glColor3f (for specifying RGB values) and glColor4f (including an alpha channel)
are used.
Vertex Coordinates:
Attribute: glVertex*
Description: Specifies the 2D or 3D coordinates of a vertex. Functions like
glVertex2f (for 2D) and glVertex3f (for 3D) are commonly used.
Line Width:
Attribute: glLineWidth
Description: Sets the width of lines drawn with GL_LINES and related primitives.
Polygon Mode:
Attribute: glPolygonMode
Description: Determines how polygons are rendered, allowing for wireframe (GL_LINE)
or filled (GL_FILL) modes.
Shading Model:
Attribute: glShadeModel
Description: Defines how colors are interpolated across primitives. Options include
GL_FLAT (constant color) and GL_SMOOTH (smooth color transition).
Texture Mapping:
Attributes: glTexCoord*
Description: Coordinates for texture mapping. Functions like glTexCoord2f are used
to assign texture coordinates.
4. SCANLINE ALGO:
The Scanline Algorithm is a graphics rendering technique used to fill the interiors
of polygons by scanning each horizontal line of the polygon and determining the
intersections of that line with the edges of the polygon. This algorithm is
particularly useful for rendering complex shapes with irregular boundaries.
(followed by algo)