Posts

Showing posts with the label OpenGl

Installing Glut

Image
Download the glut from this link:-

OpenGl - Displays a BARN shape on the XY plane.

Image
Source Code: 1: #include <windows.h> 2: #include <gl/Gl.h> 3: #include <gl/glut.h> 4: #include <cmath> 5: 6: int WinWidth = 500, WinHeight = 500; 7: 8: typedef struct{float x;float y;}Point2D; 9: typedef struct{float x;float y;float z;}Point3D; 10: 11: 12: Point3D COP = {100,100,-200}; 13: 14: Point3D A,B,C,D,E,F,G,H,I,J; // Vertices of the BARN 15: 16: void InitBarn(){ 17: A.x = 200; A.y = 100; A.z = 200; 18: B.x = 200; B.y = 00; B.z = 200; 19: C.x = 400; C.y = 00; C.z = 200; 20: D.x = 400; D.y = 100; D.z = 200; 21: E.x = 300; E.y = 200; E.z = 200; 22: 23: F.x = 200; F.y = 100; F.z = 400; 24: G.x = 200; G.y = 00; G.z = 400; 25: H.x = 400; H.y = 00; H.z = 400; 26: I.x = 400; I.y = 100; I.z = 400; 27: J.x = 300; J.y = 200; J.z = 400; 28: } 29: 30: void constructBarn(Point2D A, Point2D B, Point2D C, Point2D D...

Rotate a square with respect to the origin

#include <windows.h> #include <gl/Gl.h> #include <gl/glut.h> #include <cmath> int screenheight = 600; int screenwidth = 800; int flag = 0; double angle = 30;             typedef struct{     float x;     float y; }Point2D; Point2D p1,p2,p3,p4; void DrawSquare(Point2D pt1, Point2D pt2,Point2D pt3, Point2D pt4){        glPointSize(2.0);        glBegin(GL_LINE_LOOP);        glVertex2i(pt1.x, pt1.y);    glVertex2i(pt3.x, pt3.y);   glVertex2i(pt2.x, pt2.y);   glVertex2i(pt4.x, pt4.y);        glEnd();        glPointSize(5.0);        glBegin(GL_POINTS);        glVertex2i(pt2.x, pt2.y);        glEnd();        glFlush(); } Point2D translate(Point2D p, float tx, float ty){   ...

Rotate a square with respect to its centre

Image
#include <windows.h> #include <gl/Gl.h> #include <gl/glut.h> #include <cmath> int screenheight = 600; int screenwidth = 800; bool flag = true; int X1,Y1,X2,Y2,click; float len; float m; double angle = 30;                //The angle for the rotation (in degrees) typedef struct{     float x;     float y; }Point2D; Point2D p1,p2,p3,p4; void DrawLineSegment(Point2D pt1, Point2D pt2){        glPointSize(1.0);        glBegin(GL_LINES);        glVertex2i(pt1.x, pt1.y);        glVertex2i(pt2.x, pt2.y);        glEnd();        glPointSize(6.0);        glBegin(GL_POINTS);   glVertex2i(pt1.x, pt1.y);        glVertex2i(pt2.x, pt2.y);        glEnd();       ...

Rotate a line

#include <windows.h> #include <gl/Gl.h> #include <gl/glut.h> #include <cmath> int screenheight = 600; int screenwidth = 800; bool flag = true; double angle = 30; //The angle for the rotation (in degrees) typedef struct{ float x; float y; }Point2D; Point2D p1,p2; void DrawLineSegment(Point2D pt1, Point2D pt2){ glPointSize(1.0); glBegin(GL_LINES); glVertex2i(pt1.x, pt1.y); glVertex2i(pt2.x, pt2.y); glEnd(); glPointSize(6.0); glBegin(GL_POINTS); glVertex2i(pt2.x, pt2.y); glEnd(); glFlush(); } Point2D translate(Point2D p, float tx, float ty){ p.x =p.x; //.....wite the equations for translation p.y = p.y; //.....wite the equations for translation return p; } Point2D rotate(Point2D p, float ang){ ang = ang * 3.14 / 180.0; //angle in radians Point2D ptemp; ptemp.x = p.x * cos(ang) - p.y * sin(ang...

GL_TRIANGLE_FAN Example In OpenGl

#include <GL/glut.h>       void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,0.0); glBegin(GL_TRIANGLE_FAN); glVertex3f(0.1, 0.1,0.0); glVertex3f(0.6, 0.1,0.0); glVertex3f(0.8,0.3,0.0); glVertex3f(0.6,0.6,0.0); glVertex3f(0.1,0.6,0.0); glVertex3f(0.0,0.3,0.0); glEnd(); glFlush(); } void init() { glClearColor(0.0,0.5,0.2,0.8); } int main(int argc, char** argv) { glutCreateWindow("simple 3D"); glutDisplayFunc(display); init(); glutMainLoop(); }

GL_POLYGON Example In OpenGl

#include <GL/glut.h>       void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,0.0); glBegin(GL_POLYGON); glVertex3f(0.1, 0.1,0.0); glVertex3f(0.6, 0.1,0.0); glVertex3f(0.8,0.3,0.0); glVertex3f(0.6,0.6,0.0); glVertex3f(0.1,0.6,0.0); glVertex3f(0.0,0.3,0.0); glEnd(); glFlush(); } void init() { glClearColor(0.0,0.5,0.2,0.8); } int main(int argc, char** argv) { glutCreateWindow("simple 3D"); glutDisplayFunc(display); init(); glutMainLoop(); }

GL_QUAD_STRIP Example In OpenGl

#include <GL/glut.h>       void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,0.0); glBegin(GL_QUAD_STRIP); glVertex3f(0.2, 0.2,0.0); glVertex3f(0.8, 0.2,0.0); glVertex3f(0.2,0.4,0.0); glVertex3f(0.8,0.4,0.0); glVertex3f(0.2,0.8,0.0); glVertex3f(0.8,0.8,0.0); glEnd(); glFlush(); } void init() { glClearColor(0.0,0.5,0.2,0.8); } int main(int argc, char** argv) { glutCreateWindow("simple 3D"); glutDisplayFunc(display); init(); glutMainLoop(); }

GL_QUADS Example In OpenGl

#include <gl glut.h="">          void display() { glClear(GL_COLOR_BUFFER_BIT);  glColor3f(1.0,1.0,0.0); glBegin(GL_QUADS); glVertex3f(0.2, 0.2,0.0); glVertex3f(0.8, 0.2,0.0); glVertex3f(0.6,0.4,0.0); glVertex3f(0.4,0.4,0.0); glVertex3f(0.4,0.6,0.0); glVertex3f(0.6,0.6,0.0); glVertex3f(0.8,0.8,0.0); glVertex3f(0.2,0.8,0.0); glEnd(); glFlush();  } void init() { glClearColor(0.0,0.5,0.2,0.8); } int main(int argc, char** argv) { glutCreateWindow("simple 3D");  glutDisplayFunc(display); init(); glutMainLoop(); }

GL_TRIANGLES Example In OpenGl

Image
#include <GL/glut.h>       void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,0.0); glBegin(GL_TRIANGLES); glVertex3f(0.2, 0.2,0.0); glVertex3f(0.8, 0.2,0.0); glVertex3f(0.2, 0.5,0.0); //glVertex3f(0.8, 0.5,0.0); //glVertex3f(0.2, 0.8,0.0); //glVertex3f(0.8, 0.8,0.0); glEnd(); glFlush(); } void init() { glClearColor(0.0,0.5,0.2,0.8); } int main(int argc, char** argv) { glutCreateWindow("simple 3D"); glutDisplayFunc(display); init(); glutMainLoop(); }

GL_TRIANGLE_STRIP Example In OpenGl

#include <GL/glut.h>          void display() { glClear(GL_COLOR_BUFFER_BIT);  glColor3f(1.0,1.0,0.0); glBegin(GL_TRIANGLE_STRIP); glVertex3f(0.2, 0.2,0.0); glVertex3f(0.8, 0.2,0.0); glVertex3f(0.2, 0.5,0.0); glVertex3f(0.8, 0.5,0.0); glVertex3f(0.2, 0.8,0.0); glVertex3f(0.8, 0.8,0.0); glEnd(); glFlush();  } void init() { glClearColor(0.0,0.5,0.2,0.8); } int main(int argc, char** argv) { glutCreateWindow("simple 3D");  glutDisplayFunc(display); init(); glutMainLoop(); }

GL_LINE_LOOP Example In OpenGl

#include <GL/glut.h>       void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,0.0); glBegin(GL_LINE_LOOP); glVertex3f(0.2, 0.2,0.0); glVertex3f(0.8, 0.2,0.0); glVertex3f(0.2, 0.5,0.0); glVertex3f(0.8, 0.5,0.0); glVertex3f(0.2, 0.8,0.0); glVertex3f(0.8, 0.8,0.0); glEnd(); glFlush(); } void init() { glClearColor(0.0,0.5,0.2,0.8); } int main(int argc, char** argv) { glutCreateWindow("simple 3D"); glutDisplayFunc(display); init(); glutMainLoop(); }

GL_LINE_STRIP Example In OpenGl

#include <GL/glut.h>          void display() { glClear(GL_COLOR_BUFFER_BIT);  glColor3f(1.0,1.0,0.0); glBegin(GL_LINE_STRIP); glVertex3f(0.2, 0.2,0.0); glVertex3f(0.8, 0.2,0.0); glVertex3f(0.2, 0.5,0.0); glVertex3f(0.8, 0.5,0.0); glVertex3f(0.2, 0.8,0.0); glVertex3f(0.8, 0.8,0.0); glEnd(); glFlush();  } void init() { glClearColor(0.0,0.5,0.2,0.8); } int main(int argc, char** argv) { glutCreateWindow("simple 3D");  glutDisplayFunc(display); init(); glutMainLoop(); }

Simple line drawing in OpenGl

#include <GL/glut.h>          void display(void) { glClear(GL_COLOR_BUFFER_BIT);  glColor3f(1.0,1.0,0.0); glBegin(GL_LINES); glVertex2f(0.5, -0.5); glVertex2f(-0.5, 0.5); glEnd(); glFlush();  } void init(){ glClearColor(0.0,0.5,0.2,0.8); } int main(int argc, char** argv) { glutCreateWindow("simple line");  glutDisplayFunc(display); init(); glutMainLoop(); }

GL_POINTS In OpenGl

#include <GL/glut.h> void display(void) { glClear(GL_COLOR_BUFFER_BIT); glPointSize(3.0); glBegin(GL_POINTS); glColor3f(1.0,1.0,0.0); glVertex2f(-0.5, -0.5); glColor3f(1.0,0.0,0.0); glVertex2f(-0.5, 0.5); glColor3f(0.0,0.0,1.0); glVertex2f(0.5, 0.5); glColor3f(0.0,1.0,0.0); glVertex2f(0.5, -0.5); glEnd(); glFlush(); } void init(){} int main(int argc, char** argv) { glutCreateWindow("simple"); glutDisplayFunc(display); init(); glutMainLoop(); }

Line drawing with mouse click event in OpenGl

#include <GL/glut.h>  int ww = 600, wh = 400; int first = 0; int xi, yi, xf, yf; void drawLine(int x1, int y1, int x2, int y2) { glClear(GL_COLOR_BUFFER_BIT); glLineWidth(5.0); glBegin(GL_LINES); glVertex2i(x1, y1); glVertex2i(x2, y2); glEnd(); glFlush(); } void display() { glClearColor(1.0, 1.0, 0.0, 1.0); glColor3f(1.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); } void mouse(int btn, int state, int x, int y) { if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) { switch(first) { case 0: xi = x; yi = (wh-y); first = 1; break; case 1: xf = x; yf = (wh-y); drawLine(xi,yi,xf,yf); first = 0; break; } } } void myinit() { glViewport(0,0,ww,wh); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh); glMatrixMode(GL_MODELVIEW); } int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode (GLU...