You are on page 1of 8

1.

Line Drawing using GL_LINES


glBegin(GL_LINES);
glVertex2f(0.2,0.1);
glVertex2f(0.2,0.6);
glVertex2f(0.5,0.6);
glVertex2f(0.8,0.3);
glEnd();

2. Line Drawing using GL_LINE_STRIP


glBegin(GL_LINE_STRIP);
glVertex2f(0.2,0.1);
glVertex2f(0.2,0.6);
glVertex2f(0.5,0.6);
glVertex2f(0.8,0.3);
glEnd();

3. Line Drawing using GL_LINE_LOOP


glBegin(GL_LINE_LOOP);
glVertex2f(0.2,0.1);
glVertex2f(0.2,0.6);
glVertex2f(0.5,0.6);
glVertex2f(0.8,0.3);
glEnd();

4. Polygon Drawing using GL_POLYGON with blue color


#include<GL/glut.h>
#include<cmath>
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2f(0.2,0.1);
Full code to
glVertex2f(0.2,0.6);
draw the above
glVertex2f(0.5,0.6); shapes.

glVertex2f(0.8,0.3); Only change the


primitives
glEnd();
glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400,400);
glutCreateWindow("Circle Darwing");
glClearColor(1.0,1.0,1.0,1.0);
glutDisplayFunc(display);
glutMainLoop();
}
5. Half Circle drawing using cartesian coordinates with r=0.5

#include <GL/glut.h>

#include <cmath>

const int windowWidth = 500;

const int windowHeight = 500;

//int rad=5;

void display() {

// GLint rad=5;

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0.0, 0.0, 0.0); // Set color to white

glBegin(GL_LINE_LOOP);

for (int i = 0; i <= 180; i++) {

float x = -0.5 + i / 180.0; // Specify x-coordinate directly

float y = sqrt(pow(0.5,2) - pow(x,2)); // Compute y-coordinate using the equation of a circle

glVertex2f(x, y);

glEnd();
glFlush();

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(windowWidth, windowHeight);

glutInitWindowPosition(100, 100);

glutCreateWindow("OpenGL Half Circle in Code::Blocks");

glClearColor(1.0, 1.0, 1.0, 1.0); // Set clear color to black

glutDisplayFunc(display);

glutMainLoop();

return 0;

6. full Circle drawing using cartesian coordinates with r=0.5


#include <GL/glut.h>

#include <cmath>

const int windowWidth = 500;

const int windowHeight = 500;

void display() {

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0.0, 0.0, 0.0); // Set color to white

glBegin(GL_LINE_LOOP);

for (int i = 0; i <= 360; i++) {

float angle = i * 2*3.14159265 / 360.0; // Convert degrees to radians (use 2𝜋 for full circle)

float x = 0.5 * cos(angle);

float y = 0.5 * sin(angle);

glVertex2f(x, y);

glEnd();

glFlush();

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(windowWidth, windowHeight);

glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL Half Circle ");

glClearColor(1.0, 1.0, 1.0, 1.0); // Set clear color to black

glutDisplayFunc(display);

glutMainLoop();

return 0;

7. When you change the GL_LINE_LOOP to GL_POLYGON the output filled with solid color
like bellow

8. To draw Rectangles using glRect*.


 Where * symbol represents data types of a given polygon
 These can be float, double, vector, integer

o glRecti(8,0,8,8)

o glRectf(0.8,0.0,0.8,0.8)

o glRectv(p1, p2); // GLint p1[]= {0.8,0.0}; Glint p2[] = {0.8,0.8};


o
Full code
#include<GL/glut.h>
#include<cmath>
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,1.0);
glRecti(-0.8,0,0.8,0.8); In this code we used
glFlush(); glRectf. And you try
the others one.
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400,400);
glutCreateWindow("Circle Darwing");
glClearColor(1.0,1.0,1.0,1.0);
glutDisplayFunc(display);
glutMainLoop();
}
9. exercise by yourself the following.
A) GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN
B) GL_QUADS, GL_QUAD_STRIP

You might also like