PDA

View Full Version : Cube and Pyramid 3D Object



Ismailmiller
02-12-2017, 01:52 AM
Hi,

I have a question. I managed to run a program to create 2 objects; pyramid and cube but it includes colours. I was wondering which part of my coding I should remove to remove the colours and make the cube and pyramid white only? I am still a beginner only.

/
// Test
// CGI
//

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdio.h>
#include <stdlib.h>

static int drawCube = 1; // option to draw the cube
static int drawPyramid = 1; // option to draw the pyramid

static int cubeRotating = 0; // on/off rotaion of the cube
static int cubeRotateAngle = 0; // current rotation angle of the cube

// Draw a pyramid of size 2, centered at (0,0,0)
void pyramid(int wireframe) {

if (wireframe) {
// Set wireframe mode
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.7f, 0.7f, 0.7f);
} else {
// Set fill-mode
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(1, 1, 1);
}

// Draw the base square
glBegin(GL_QUADS);
glVertex3f(-1, 0, -1);
glVertex3f(-1, 0, 1);
glVertex3f( 1, 0, 1);
glVertex3f( 1, 0, -1);
glEnd();

// Draw four side triangles
glBegin(GL_TRIANGLE_FAN);

// the commond point of the four triangles
glVertex3f(0, 1.4, 0);

// Base points of each triangle
glVertex3f(-1, 0, -1);
glVertex3f(-1, 0, 1);

glVertex3f(-1, 0, 1);
glVertex3f( 1, 0, 1);

glVertex3f( 1, 0, 1);
glVertex3f( 1, 0, -1);

glVertex3f( 1, 0, -1);
glVertex3f(-1, 0, -1);

glEnd();

}

// Draw a cube of size 1, with one corner at (0,0,0)
void cube(int wireframe) {

if (wireframe) {
// Set wireframe mode
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.7f, 0.7f, 0.7f);
} else {
// Set fill-mode
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}

glBegin(GL_QUADS);

// bottom side
if (!wireframe) glColor3f(0, 0, 0); // each point using a different color
glVertex3f(0, 0, 0);
if (!wireframe) glColor3f(0, 0, 1);
glVertex3f(0, 0, 1);
if (!wireframe) glColor3f(1, 0, 1);
glVertex3f(1, 0, 1);
if (!wireframe) glColor3f(1, 0, 0);
glVertex3f(1, 0, 0);

// top side
if (!wireframe) glColor3f(0, 1, 0);
glVertex3f(0, 1, 0);
if (!wireframe) glColor3f(0, 1, 1);
glVertex3f(0, 1, 1);
if (!wireframe) glColor3f(1, 1, 1);
glVertex3f(1, 1, 1);
if (!wireframe) glColor3f(1, 1, 0);
glVertex3f(1, 1, 0);

// front side
if (!wireframe) glColor3f(0, 0, 0);
glVertex3f(0, 0, 0);
if (!wireframe) glColor3f(0, 1, 0);
glVertex3f(0, 1, 0);
if (!wireframe) glColor3f(1, 1, 0);
glVertex3f(1, 1, 0);
if (!wireframe) glColor3f(1, 0, 0);
glVertex3f(1, 0, 0);

// back side
if (!wireframe) glColor3f(0, 0, 1);
glVertex3f(0, 0, 1);
if (!wireframe) glColor3f(0, 1, 1);
glVertex3f(0, 1, 1);
if (!wireframe) glColor3f(1, 1, 1);
glVertex3f(1, 1, 1);
if (!wireframe) glColor3f(1, 0, 1);
glVertex3f(1, 0, 1);

// left side
if (!wireframe) glColor3f(0, 0, 0);
glVertex3f(0, 0, 0);
if (!wireframe) glColor3f(0, 0, 1);
glVertex3f(0, 0, 1);
if (!wireframe) glColor3f(0, 1, 1);
glVertex3f(0, 1, 1);
if (!wireframe) glColor3f(0, 1, 0);
glVertex3f(0, 1, 0);

// right side
if (!wireframe) glColor3f(1, 0, 0);
glVertex3f(1, 0, 0);
if (!wireframe) glColor3f(1, 0, 1);
glVertex3f(1, 0, 1);
if (!wireframe) glColor3f(1, 1, 1);
glVertex3f(1, 1, 1);
if (!wireframe) glColor3f(1, 1, 0);
glVertex3f(1, 1, 0);

glEnd();
}

// Handle key-pressed even
void keyPressed(unsigned char key, int x, int y) {

switch (key) {
case 27: // ESC key
exit(0);
break;
case 'c': case 'C': // set options to draw the cube only
drawCube = 1;
drawPyramid = 0;
break;
case 'p': case 'P': // set options to draw the pyramid only
drawCube = 0;
drawPyramid = 1;
break;
case 'a': case 'A': // set options to draw both objects
drawCube = 1;
drawPyramid = 1;
break;
}

glutPostRedisplay();
}

// function to update rotation of the cube
void timer(int frame)
{
cubeRotateAngle += 1; // make the cube rotate
glutPostRedisplay(); // update the display

// set timer for update for next frame
if (cubeRotating) {
glutTimerFunc(40, timer, 0);
}
}

// Handle mouse-button pressed to turn on/off the animation
void mousePressed(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
cubeRotating = !cubeRotating;

if (cubeRotating) {
// Start the timer
glutTimerFunc(40, timer, 0);
}
}
}

// Draw the whole scene
void display(void) {

// Reset the drawing buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Draw the cube if requested
if (drawCube) {
glPushMatrix();

// Place the cube at some distance
glTranslatef(1.5f, 0, 1);

// Rotate the cube (as its current rotation angle)
glRotatef(cubeRotateAngle, 1, 1, 1);
// make the rotation axis going through the center of the cube
glTranslatef(-0.5f, -0.5f, -0.5f);

cube(0); // draw wireframe version
cube(1); // draw color version

glPopMatrix();
}

// Draw the pyramid if requested
if (drawPyramid) {

glPushMatrix();

// Pose the pyramid
glRotatef(30, 0, 1, 0);
glTranslatef(-1.5f, 0, -1);

pyramid(0); // draw wireframe version
pyramid(1); // draw color version

glPopMatrix();
}

glFlush();
glutSwapBuffers();
}

// Called when the window is resized.
void reshape(int w, int h) {

glViewport(0, 0, w, h);

// Setup projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w / (GLfloat) h, 0.1, 200.0);

// Setup model view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 3.0, 5.0, // look from
0.0, 0.0, 0.0, // look to
0.0, 1.0, 0.0);
}

// Initialize OpenGL parameters
void init() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // black background
glEnable(GL_DEPTH_TEST); // Enable depth-test to remove hidden faces
}

int main(int argc, char **argv) {

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Tutor-Marked Assignment");

// Setup callback functions
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyPressed);
glutMouseFunc(mousePressed);

init();

glutMainLoop();
return 0;
}







Best Regards,
Mohamed Ismail