## Sunday, August 24, 2014

### How to Draw Different Types of Triangles in OpenGL

To draw a triangle in opengl  we just need to use simple primitive functions. There are many ways you can draw but using primitive function is one of easy method. In this post we are going to draw few triangles using the different primitive functions defined in OpenGL graphics Library.

One of the blog reader requested to have the code for Triangle. Hence to help and all other reader in this post we going to see many types of triangles. We are going to have full source code with explanation. So get the code and spread the knowledge, don't forget to tell other and share the post.

### Draw First Type of Triangle in OpenGL

Here we are going to use the GL_TRINAGLES and glVertex() to draw our triangle in a simple manner. Her we have drawn two triangles -red and white, both as right angled triangle. You can change the vertexes to make others varities of Triangle.

#include "glut.h"

void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_TRIANGLES);
glColor3f(1,1,1);
glVertex3f(0,0,0);
glVertex3f(-1,0,0);
glVertex3f(0,1,0);

glColor3f(1,0,0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glVertex3f(0,-1,0);
glEnd();

glFlush();
}

int main(void)
{
glutCreateWindow("MY Triangle");
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}

#### Draw Striped Triangle in OpenGL C++

You may have heard about the Strip triangles, now we are going to see it. Just as in above are using glVertex() but this time GL_TRIANGLE_STRIP.

#include "glut.h"

void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_TRIANGLE_STRIP);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glVertex3f(0,1,0);
glVertex3f(0.9,0.9,0);
glVertex3f(1,-1,0);
glEnd();

glFlush();
}

int main(void)
{
glutCreateWindow("MY STRIP TRIANGLE");
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}

### Draw Triangle Fan in OpenGL

Now its time to Draw a Triangle Fan. You might have seen this as used in our Nuclear Power Plant Project in OpenGL, where the turbine have it.

#include "glut.h"

void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_TRIANGLE_STRIP);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glVertex3f(0,1,0);
glVertex3f(-1,0,0);
glVertex3f(-1,-1,0);
glEnd();

glFlush();
}

int main(void)
{
glutCreateWindow("Triangle Fans");
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}