4_

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

#include <GL/glut.

h>

void drawCube() {

glutSolidCube(1.0);

void drawBox() {

glPushMatrix();

glScalef(1.0, 0.5, 2.0);

glutSolidCube(1.0);

glPopMatrix();

void drawCone() {

glutSolidCone(0.5, 1.0, 20, 20);

void display() {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();

glTranslatef(-2.0, 0.0, -5.0);

drawCube();

glPopMatrix();
glPushMatrix();

glTranslatef(0.0, 0.0, -5.0);

drawBox();

glPopMatrix();

glPushMatrix();

glTranslatef(2.0, 0.0, -5.0);

drawCone();

glPopMatrix();

glutSwapBuffers();

void init() {

glEnable(GL_DEPTH_TEST);

glClearColor(0.1f, 0.1f, 0.1f, 1.0f);

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutInitWindowSize(800, 600);

glutCreateWindow("OpenGL Objects");

init();

glutDisplayFunc(display);
glutMainLoop();

return 0;

#include <GL/glut.h>

#include <cmath>

// Function to draw a cube

void drawCube() {

glBegin(GL_QUADS);

// Front face

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

glVertex3f(-0.5f, -0.5f, 0.5f);

glVertex3f(0.5f, -0.5f, 0.5f);

glVertex3f(0.5f, 0.5f, 0.5f);

glVertex3f(-0.5f, 0.5f, 0.5f);

// Back face

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

glVertex3f(-0.5f, -0.5f, -0.5f);


glVertex3f(-0.5f, 0.5f, -0.5f);

glVertex3f(0.5f, 0.5f, -0.5f);

glVertex3f(0.5f, -0.5f, -0.5f);

// Left face

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

glVertex3f(-0.5f, -0.5f, -0.5f);

glVertex3f(-0.5f, -0.5f, 0.5f);

glVertex3f(-0.5f, 0.5f, 0.5f);

glVertex3f(-0.5f, 0.5f, -0.5f);

// Right face

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

glVertex3f(0.5f, -0.5f, -0.5f);

glVertex3f(0.5f, 0.5f, -0.5f);

glVertex3f(0.5f, 0.5f, 0.5f);

glVertex3f(0.5f, -0.5f, 0.5f);

glEnd();

// Function to draw a box

void drawBox() {

glPushMatrix();

glScalef(1.0f, 0.5f, 0.5f); // Scale to make it a box


drawCube();

glPopMatrix();

// Function to draw a cone

void drawCone() {

float radius = 0.5f;

float height = 1.0f;

int num_segments = 50;

glBegin(GL_TRIANGLE_FAN);

// Cone tip

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

glVertex3f(0.0f, height, 0.0f);

// Cone base

for (int i = 0; i <= num_segments; ++i) {

float angle = 2.0f * 3.1415926f * float(i) / float(num_segments);

float x = radius * cosf(angle);

float z = radius * sinf(angle);

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

glVertex3f(x, 0.0f, z);

glEnd();
}

// Display callback

void display() {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

gluLookAt(0.0, 0.0, 5.0, // Camera position

0.0, 0.0, 0.0, // Look at the origin

0.0, 1.0, 0.0); // Up direction

glPushMatrix();

glTranslatef(-1.5f, 0.0f, 0.0f); // Move cube left

drawCube();

glPopMatrix();

glPushMatrix();

glTranslatef(0.0f, 0.0f, 0.0f); // Center box

drawBox();

glPopMatrix();

glPushMatrix();

glTranslatef(1.5f, 0.0f, 0.0f); // Move cone right

drawCone();

glPopMatrix();
glutSwapBuffers();

// Initialize OpenGL settings

void init() {

glEnable(GL_DEPTH_TEST); // Enable depth testing for 3D rendering

glMatrixMode(GL_PROJECTION); // Set up the projection matrix

glLoadIdentity();

gluPerspective(45.0, 1.33, 1.0, 10.0); // Perspective projection

glMatrixMode(GL_MODELVIEW); // Return to model-view matrix

// Main function

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutInitWindowSize(800, 600);

glutCreateWindow("3D Shapes: Cube, Box, and Cone");

init();

glutDisplayFunc(display);

glutMainLoop();
return 0;

You might also like