Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<windows.h>
- #include <iostream>
- #include <stdlib.h>
- #include <GL/glut.h>
- using namespace std;
- //Initializes 3D rendering
- void initRendering() {
- //Makes 3D drawing work when something is in front of something else
- glEnable(GL_DEPTH_TEST);
- }
- //Called when the window is resized
- void handleResize(int w, int h) {
- //Tell OpenGL how to convert from coordinates to pixel values
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
- //Set the camera perspective
- glLoadIdentity(); //Reset the camera
- gluPerspective(45.0, //The camera angle
- (double)w / (double)h, //The width-to-height ratio
- 1.0, //The near z clipping coordinate
- 200.0); //The far z clipping coordinate
- }
- //Draws the 3D scene
- void drawScene() {
- //Clear information from last draw
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
- glLoadIdentity(); //Reset the drawing perspective
- glBegin(GL_TRIANGLES); //Begin triangle coordinates
- //Triangle
- glVertex3f(0.0,40.0,0.0);
- glVertex3f(15.0,-20.0,15.0);
- glVertex3f(-15.0,-20.0,15.0);
- glEnd(); //End triangle coordinates
- glBegin(GL_TRIANGLES); //Begin triangle coordinates
- //Triangle
- glVertex3f(0,40,0);
- glVertex3f(-15,-20,-15);
- glVertex3f(-15,-20,15);
- glEnd(); //End triangle coordinates
- glBegin(GL_TRIANGLES); //Begin triangle coordinates
- //Triangle
- glVertex3f(0.0,40.0,0.0);
- glVertex3f(-15.0,-20.0,-15.0);
- glVertex3f(15.0,-20.0,-15.0);
- glEnd(); //End triangle coordinates
- glBegin(GL_TRIANGLES); //Begin triangle coordinates
- //Triangle
- glVertex3f(0,40,0);
- glVertex3f(15,-20,-15);
- glVertex3f(15,-20,15);
- glEnd(); //End triangle coordinates
- glBegin(GL_TRIANGLES); //Begin triangle coordinates
- //Triangle
- glVertex3f(-15,-20,15);
- glVertex3f(-15.0,-20.0,15.0);
- glVertex3f(15.0,-20.0,15.0);
- glEnd(); //End triangle coordinates
- glBegin(GL_TRIANGLES); //Begin triangle coordinates
- //Triangle
- glVertex3f(15,-20,15);
- glVertex3f(-15,-20,-15);
- glVertex3f(15,-20,-15);
- glEnd(); //End triangle coordinates
- glutSwapBuffers(); //Send the 3D scene to the screen
- }
- int main(int argc, char** argv) {
- //Initialize GLUT
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(400, 400); //Set the window size
- //Create the window
- glutCreateWindow("Basic Shapes");
- initRendering(); //Initialize rendering
- //Set handler functions for drawing, keypresses, and window resizes
- glutDisplayFunc(drawScene);
- glutReshapeFunc(handleResize);
- glutMainLoop(); //Start the main loop
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement