Simple 2D hut design in computer graphics using c++


/*Faisal Porag
*/

Source Code:

#include <GL/glut.h>


void display(void)
{
/* clear all pixels */
glClear(GL_COLOR_BUFFER_BIT);

/* draw white polygon (rectangle) with corners at
* (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
*/
glColor3f(0.0, 1.0, 0.0);
//glBegin(GL_POLYGON);
//glBegin(GL_POINTS);
//glBegin(GL_LINES);
//glBegin(GL_TRIANGLES);
/*
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
*/

/*
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
*/

//code for one single line
/*glVertex3f(0.25, 0.25, 0.0);//starting point
glVertex3f(0.75, 0.25, 0.0);//Ending point

//code for one single line
glVertex3f(0.25, 0.25, 0.0);//starting point
glVertex3f(0.50, 0.75, 0.0);//Ending point

//code for one single line
glVertex3f(0.50, 0.75, 0.0);//starting point
glVertex3f(0.75, 0.25, 0.0);//Ending point
*/


//Code for create a small hut
glBegin(GL_LINES);
//1st line
//up horizontal line
glVertex3f(0.25,0.65,0.0);//first point
glVertex3f(0.50,0.65,0.0);//second point

//down horizontal line
glVertex3f(0.28,0.30,0.0);//first point
glVertex3f(0.50,0.30,0.0);//second point

//Create door

//small room door
//leftside vertical
glVertex3f(0.36,0.30,0.0);
glVertex3f(0.36,0.42,0.0);

//right side vertical
glVertex3f(0.41,0.30,0.0);
glVertex3f(0.41,0.42,0.0);

//door's horizontal line
glVertex3f(0.36,0.42,0.0);
glVertex3f(0.41,0.42,0.0);
//end of the door


//Create small room window

//leftside vertical
glVertex3f(0.30,0.50,0.0);
glVertex3f(0.30,0.60,0.0);

//right side vertical
glVertex3f(0.36,0.50,0.0);
glVertex3f(0.36,0.60,0.0);

//window's up horizontal line
glVertex3f(0.30,0.50,0.0);
glVertex3f(0.36,0.50,0.0);

//window's up horizontal line
glVertex3f(0.30,0.60,0.0);
glVertex3f(0.36,0.60,0.0);

//window's partition
//vertical middle line
glVertex3f(0.33,0.50,0.0);
glVertex3f(0.33,0.60,0.0);
//end of the window



//vertical line leftside
glVertex3f(0.28,0.30,0.0);//first point
glVertex3f(0.28,0.65,0.0);//second point

//vertical line rightside
glVertex3f(0.50,0.30,0.0);//first point
glVertex3f(0.50,0.65,0.0);//second point

//small angle
//leftside angle line
glVertex3f(0.25,0.65,0.0);//first point
glVertex3f(0.38,0.85,0.0);//second point

//rightside angle line
glVertex3f(0.50,0.65,0.0);//first point
glVertex3f(0.38,0.85,0.0);//second point


//middle line
//glVertex3f(0.48,0.65,0.0);//first point
//glVertex3f(0.48,0.75,0.0);//second point

//another part
//up horizantal line for large room
glVertex3f(0.38,0.85,0.0);//first point
glVertex3f(0.70,0.85,0.0);//second point

//third angle for large room
glVertex3f(0.70,0.85,0.0);//second point
glVertex3f(0.78,0.65,0.0);//second point

//down  horizantal line for large room
glVertex3f(0.50,0.65,0.0);//first point
glVertex3f(0.78,0.65,0.0);//second point

//vertical line for large room (right side)
glVertex3f(0.75,0.65,0.0);
glVertex3f(0.75,0.30,0.0);


//horizantal line for large room
glVertex3f(0.75,0.30,0.0);
glVertex3f(0.50,0.30,0.0);

//Create door

//large room door
//leftside vertical
glVertex3f(0.60,0.30,0.0);
glVertex3f(0.60,0.42,0.0);

//right side vertical
glVertex3f(0.65,0.30,0.0);
glVertex3f(0.65,0.42,0.0);

//door's horizontal line
glVertex3f(0.60,0.42,0.0);
glVertex3f(0.65,0.42,0.0);
//end of door

//Create window

//create large room window
//leftside vertical
glVertex3f(0.65,0.50,0.0);
glVertex3f(0.65,0.60,0.0);

//right side vertical
glVertex3f(0.73,0.50,0.0);
glVertex3f(0.73,0.60,0.0);

//window's up horizontal line
glVertex3f(0.65,0.60,0.0);
glVertex3f(0.73,0.60,0.0);

//window's up horizontal line
glVertex3f(0.65,0.50,0.0);
glVertex3f(0.73,0.50,0.0);

//window's partition
//vertical middle line
glVertex3f(0.69,0.50,0.0);
glVertex3f(0.69,0.60,0.0);
//end of window


//border of the hut
//for small room horizomtal
glVertex3f(0.28,0.25,0.0);//first point
glVertex3f(0.50,0.25,0.0);//second point

//middle
glVertex3f(0.28,0.275,0.0);//first point
glVertex3f(0.50,0.275,0.0);//second point

//for small room vertical
glVertex3f(0.28,0.30,0.0);
glVertex3f(0.28,0.25,0.0);

//another for small room vertical
glVertex3f(0.50,0.275,0.0);
glVertex3f(0.50,0.25,0.0);

//for large room
glVertex3f(0.75,0.25,0.0);
glVertex3f(0.50,0.25,0.0);

//middle
glVertex3f(0.75,0.275,0.0);
glVertex3f(0.50,0.275,0.0);

//for small room vertical
glVertex3f(0.75,0.30,0.0);
glVertex3f(0.75,0.25,0.0);

glEnd();
/* don’t wait!
* start processing buffered OpenGL routines
*/
glFlush();
}
void init(void)
{
/* select clearing (background) color */
glClearColor(0.0, 0.0, 0.0, 0.0);
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
/*
* Declare initial window size, position, and display mode
* (single buffer and RGBA). Open window with “hello”
* in its title bar. Call initialization routines.
* Register callback function to display graphics.
* Enter main loop and process events.
*/

//main method
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(550, 350);
glutInitWindowPosition(350, 100);
glutCreateWindow("Hut");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0; /* ISO C requires main to return int. */

}

Output:


try this source code for design a simple 2d hut design.

মন্তব্যসমূহ

Popular Posts

HashMap in Java