Friday, January 3, 2020

How to draw triangle in c programming language using openGL utilities like glut and glew

Triangle in openGL


code:

//necessary header
#include<stdlib.h>
#include<math.h>
#include<Windows.h>
//Opengl utilities
#include <GL\glew.h>
#include<GL/GLU.h>
#include<GL/GL.h>
#include <GL\freeglut.h>
//function
void render(void) {
//clear
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
//reset
glLoadIdentity();
//each pixel size
glPointSize(100.0);
//draw things
glBegin(GL_TRIANGLES);
glColor3f(1,0,0);
glVertex2f(-0.5, -0.5);
glColor3f(0,1,0);
glVertex2f(0.5, -0.5);
glColor3f(0,0,1);
glVertex2f(0, 0.5);
glEnd();
//display for swaping buffer yield smooth animation
glutSwapBuffers();
}
void initBackground() {
glClearColor(0.0, 0.0, 0.0, 1);
glColor3f(1.0, 1.0, 1.0);
}
void keyboard( unsigned char c, int x, int y) {
if (c == 'a') {
exit(0);
}
}
void mouse(int buttom, int state, int x, int y) {
if (buttom == GLUT_RIGHT_BUTTON) {
exit(0);
}
}
//main function
int main( int argc, char* argv[]) {
//openGL initialization code (optional)
glutInit(&argc, argv);
//Specify the display mode -RBG or color index, single or double buffer
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE);
//Creating window named "Triangle in openGL"
//with starting point (100,100)
//with screen resolution 640*480
glutInitWindowPosition(100,100);
glutInitWindowSize(640, 480);
glutCreateWindow("Triangle in openGL");
//Register the call back functions
glutDisplayFunc(render);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
//for changing background properties
initBackground();
//Program goes into infinite loop waiting for events
glutMainLoop();
return 0;
}

No comments:

Post a Comment