PROGRAM
4: 3D
·
Header
File
·
Render
func
·
Handle
resize
·
Display
·
Timer
·
Main
func
1.
Header
File.(gl/glut.h , math.h, windows.h) // REFER 2d link: http://simplifiedopengl.blogspot.in/2016/08/program-3-write-program-to-implement-2d.html
2.
Initialise
rendering function for the 2d object// REFER 2d
3.
Create
Handle Resize // REFER 2d
4.
Display
Function // REFER 2d
void
drawScene()
{
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);//dark
blue
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(_angle,0.0f,0.0f,1.0f); //rotate
object by 30 degree with respect to y-axis
glTranslatef(0.0f, 0.0f, -10.0f);
//************************
//OBJ
//Right
face (x = 1.0f), Left face (x = -1.0f), Back face (z = -1.0f), Front face (z = 1.0f), Bottom face (y = -1.0f), top
face(z=1.0f)
glutSwapBuffers();
}
5. Timer
6. Main Function
SAMPLE
PROGRAM: 4 3D
//header
file same as all
GLfloat
angleCube = 0.0f; // Rotational angle
for cube [NEW]
void
initRendering() {
glEnable(GL_DEPTH_TEST); // Enable depth testing for z-culling
}
void
handleResize(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(double)w/(double)h,1.0,200.0);
}
void
display() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set
background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
glMatrixMode(GL_MODELVIEW); // To operate on model-view matrix
glLoadIdentity(); // Reset the model-view matrix
glTranslatef(-1.5f, 0.0f, -6.0f); // Move right and into the screen
glRotatef(angleCube, 0.0f, 1.0f, 1.0f); // Rotate about (1,1,1)-axis [NEW]
glBegin(GL_QUADS); // Begin drawing the color cube
with 6 quads
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
// Bottom face (y = -1.0f)
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
// Front face (z = 1.0f)
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
// Back face (z = -1.0f)
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, 1.0f,
1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f,
1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd();
// End of drawing color-cube
glutSwapBuffers(); // Swap the front and back frame buffers
(double buffering)
angleCube -= 0.15f;
}
void
update(int value) {
angleCube+=2.0f;
if(angleCube>360.f)
{
angleCube-=360;
}
glutPostRedisplay();
glutTimerFunc(25,update,0);
}
int
main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE); // Enable
double buffered mode
glutInitWindowSize(640, 480); // Set the window's initial width &
height
glutInitWindowPosition(50, 50); // Position
the window's initial top-left corner
glutCreateWindow("PROGRAM4"); // Create window with the given title
glutDisplayFunc(display); // Register callback handler for window
re-paint event
glutReshapeFunc(handleResize); // Register callback handler for window
re-size event
initRendering(); // Our own OpenGL
initialization
glutTimerFunc(0, update, 0); //
First timer call immediately [NEW]
glutMainLoop(); // Enter the infinite
event-processing loop
return 0;
}
No comments:
Post a Comment