Friday, January 3, 2020

How to draw polygon in c programming language using openGL utility toolkit like glut and glew

Polygon


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 initialization and definition
void display(void) {
//clearing all pixel
glClear(GL_COLOR_BUFFER_BIT);
//draw white polygon (rectangle) with corner at (0.25,0.25, 0.0) and (0.75,0.75,0.0)
glColor3f(1.0,1.0,1.0);
glBegin(GL_POLYGON);
glVertex3f(0.25,0.25,0.0);
glVertex3f(0.75,0.25,0.0);
glVertex3f(0.75,0.75,0.0);
glVertex3f(0,1,0);
glVertex3f(0.25,0.75,0.0);
glEnd();
//start processing openGL routine
glFlush();
}
void init(void) {
//for background
glClearColor(0.0,0.0,0.0,0.0);
//initializing viewing values
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// left, right, buttom, top, zNear, zFar
glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
}
//main function
//declare initial window size, position, and display mode (single buffered and RBGA)
//open window with "Polygon" in title bar
//call initialization routine
//register call back function
//enter main loop and process event
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(250,250);
glutInitWindowPosition(100,100);
glutCreateWindow("Polygon");
init();
glutDisplayFunc(display);
glutMainLoop();
return(0);
}

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;
}

How to draw Wired cube in c programming using openGL glut and glew utility toolkit

Wired Cube


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 initialization and definition
void init(void) {
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
}
void diplay(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
//clear the matrix
glLoadIdentity();
//viewing transformation
gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
//modeling transforming
glScalef(1.0,2.0,1.0);
glColor3f(1, 0, 0);
glutWireCube(2);
glFlush();
}
void reshape(int w, int h) {
glViewport(0,0,GLsizei(w), GLsizei(h));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0,1.0,-1.0,1.0,1.5,20);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Transformed Cube");
init();
glutDisplayFunc(diplay);
glutReshapeFunc(reshape);
glutMainLoop();
return(0);
}
view raw WiredCube.html hosted with ❤ by GitHub