CG/Assignment C-6b (3D Cube).cpp

95 lines
1.9 KiB
C++
Raw Normal View History

2023-12-07 10:26:55 +05:30
#include<GL/glut.h>
void mydisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); //resets modelview matrix
glTranslatef(0.0,0.0,-10.0);
//glScalef(2.0,2.0,0.0);
//glRotatef(20.0,1.0,0.0,0.0);
glRotatef(-45.0,0.0,1.0,0.0);
glBegin(GL_QUADS);
//front (red)
glColor3f(1.0,0.0,0.0);
glVertex3f(-1.0,1.0,1.0);
glVertex3f(-1.0,-1.0,1.0);
glVertex3f(1.0,-1.0,1.0);
glVertex3f(1.0,1.0,1.0);
//back (green)
glColor3f(0.0,1.0,0.0);
glVertex3f(1.0,1.0,-1.0);
glVertex3f(1.0,-1.0,-1.0);
glVertex3f(-1.0,-1.0,-1.0);
glVertex3f(-1.0,1.0,-1.0);
//right (blue)
glColor3f(0.0,0.0,1.0);
glVertex3f(1.0,1.0,1.0);
glVertex3f(1.0,-1.0,1.0);
glVertex3f(1.0,-1.0,-1.0);
glVertex3f(1.0,1.0,-1.0);
//left (yellow)
glColor3f(1.0,1.0,0.0);
glVertex3f(-1.0,1.0,-1.0);
glVertex3f(-1.0,-1.0,-1.0);
glVertex3f(-1.0,-1.0,1.0);
glVertex3f(-1.0,1.0,1.0);
//top (cyan)
glColor3f(0.0,1.0,1.0);
glVertex3f(-1.0,1.0,-1.0);
glVertex3f(-1.0,1.0,1.0);
glVertex3f(1.0,1.0,1.0);
glVertex3f(1.0,1.0,-1.0);
//bottom (magenta)
glColor3f(1.0,0.0,1.0);
glVertex3f(-1.0,-1.0,-1.0);
glVertex3f(-1.0,-1.0,1.0);
glVertex3f(1.0,-1.0,1.0);
glVertex3f(1.0,-1.0,-1.0);
glEnd();
glFlush();
}
void init()
{
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
}
void reshape(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); //resets projection matrix
gluPerspective(60,1,2.0,50.0);
// glOrtho(-5,5,-5,5,-5,5);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("3D transformations");
glutDisplayFunc(mydisplay);
glutReshapeFunc(reshape);
init();
glutMainLoop();
}