Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help creating two more triangles for the bottom of my 3d pyramid on OpenGL. Please help #include #include /* Global variables */ char

I need help creating two more triangles for the bottom of my 3d pyramid on OpenGL. Please help

#include

#include

/* Global variables */

char title[] = "3D Pyramid";

/* Initialize OpenGL Graphics */

void initGL() {

glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque

glClearDepth(1.0f);// Set background depth to farthest

glEnable(GL_DEPTH_TEST);// Enable depth testing for z-culling

}

/* Handler for window-repaint event. Called back when the window first appears and

whenever the window needs to be re-painted. */

void display() {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers

glMatrixMode(GL_MODELVIEW);// To operate on model-view matrix

// Render a pyramid consists of 4 triangles

glLoadIdentity();// Reset the model-view matrix

glTranslatef(-1.5f, 0.0f, -6.0f);// Move left and into the screen

glBegin(GL_TRIANGLES);// Begin drawing the pyramid with 4 triangles

// Front

glColor3f(1.0f, 0.0f, 0.0f);// Red

glVertex3f(0.0f, 1.0f, 0.0f);

glColor3f(0.0f, 1.0f, 0.0f);// Green

glVertex3f(-1.0f, -1.0f, 1.0f);

glColor3f(0.0f, 0.0f, 1.0f);// Blue

glVertex3f(1.0f, -1.0f, 1.0f);

// Right

glColor3f(1.0f, 0.0f, 0.0f);// Red

glVertex3f(0.0f, 1.0f, 0.0f);

glColor3f(0.0f, 0.0f, 1.0f);// Blue

glVertex3f(1.0f, -1.0f, 1.0f);

glColor3f(0.0f, 1.0f, 0.0f);// Green

glVertex3f(1.0f, -1.0f, -1.0f);

// Back

glColor3f(1.0f, 0.0f, 0.0f);// Red

glVertex3f(0.0f, 1.0f, 0.0f);

glColor3f(0.0f, 1.0f, 0.0f);// Green

glVertex3f(1.0f, -1.0f, -1.0f);

glColor3f(0.0f, 0.0f, 1.0f);// Blue

glVertex3f(-1.0f, -1.0f, -1.0f);

// Left

glColor3f(1.0f, 0.0f, 0.0f);// Red

glVertex3f(0.0f, 1.0f, 0.0f);

glColor3f(0.0f, 0.0f, 1.0f);// Blue

glVertex3f(-1.0f, -1.0f, -1.0f);

glColor3f(0.0f, 1.0f, 0.0f);// Green

glVertex3f(-1.0f, -1.0f, 1.0f);

glEnd();// Done drawing the pyramid

glutSwapBuffers();// Swap the front and back frame buffers (double buffering)

}

/* Handler for window re-size event. Called back when the window first appears and

whenever the window is re-sized with its new width and height */

void reshape(GLsizei width, GLsizei height) {// GLsizei for non-negative integer

// Compute aspect ratio of the new window

if (height == 0) height = 1;// To prevent divide by 0

GLfloat aspect = (GLfloat)width / (GLfloat)height;

// Set the viewport to cover the new window

glViewport(0, 0, width, height);

// Set the aspect ratio of the clipping volume to match the viewport

glMatrixMode(GL_PROJECTION);// To operate on the Projection matrix

glLoadIdentity();// Reset

// Enable perspective projection with fovy, aspect, zNear and zFar

gluPerspective(45.0f, aspect, 0.1f, 100.0f);

}

/* Main function: GLUT runs as a console application starting at main() */

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(title);// Create window with the given title

glutDisplayFunc(display);// Register callback handler for window re-paint event

glutReshapeFunc(reshape);// Register callback handler for window re-size event

initGL();// Our own OpenGL initialization

glutMainLoop();// Enter the infinite event-processing loop

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Android 4 Application Development

Authors: Reto Meier

3rd Edition

1118223853, 9781118223857

More Books

Students also viewed these Programming questions