Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help completing this program in C++ that will translate, rotate and scale a 3D model. It will use the left mouse button to

I need help completing this program in C++ that will translate, rotate and scale a 3D model. It will use the left mouse button to roate, the mouse wheel to scale, and use keys to translate Where their are comments asking to //Write your own code below/above is where I need help completing the code nothing else.

// trackball.cpp : This file contains the 'main' function. Program execution begins and ends there. //

#include "pch.h"

#include #include using namespace std;

#include #include

#include #include #include

#include "objLoader.h"

#define KEY_LEFT 100 #define KEY_UP 101 #define KEY_RIGHT 102 #define KEY_DOWN 103

#ifndef M_PI #define M_PI 3.1415926535897932384626433832795 #endif

int winWidth = 1024; int winHeight = 1024; bool firstTime = true;

WavefrontObj *obj_data;

// Trackball parameters initialization float angle = 0.0, axis[3], trans[3];

bool trackingMouse = false; bool redrawContinue = false; bool trackballMove = false;

float lastPos[3] = { 0.0, 0.0, 0.0 }; int curx, cury; int startX, startY;

// Translation & Rotation float x_trans = 0.0f; // translate object in x direction float y_trans = 0.0f; // translate object in y direction float zoom = 1.0f; // zoom for scaling

void Init(int w, int h) { glViewport(0, 0, w, h); glShadeModel(GL_SMOOTH); // Set Smooth Shading glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Background Color glClearDepth(1.0f); // Depth buffer setup glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Use perspective correct interpolation if available

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glLoadIdentity(); // Reset The Projection Matrix double aspect = (double)h / w; glFrustum(-5, 5, -5 * aspect, 5 * aspect, 10, 500); // Define perspective projection frustum //gluPerspective(30, w/h, 10, 74); glTranslated(0.0, 0.0, -24); // Viewing transformation

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix glLoadIdentity(); // Reset The Modelview Matrix

if (firstTime) { glEnable(GL_LIGHTING); float frontColor[] = { 0.2f, 0.7f, 0.7f, 1.0f };

glMaterialfv(GL_FRONT, GL_AMBIENT, frontColor); glMaterialfv(GL_FRONT, GL_DIFFUSE, frontColor); glMaterialfv(GL_FRONT, GL_SPECULAR, frontColor); glMaterialf(GL_FRONT, GL_SHININESS, 100);

float lightDirection[] = { 2.0f, 0.0f, 1.0f, 0 }; float ambientIntensity[] = { 0.1f, 0.1f, 0.1f, 1.0f }; float lightIntensity[] = { 0.9f, 0.9f, 0.9f, 1.0f }; float lightSpec[] = { 1.0f, 1.0f, 1.0f, 1 }; glLightfv(GL_LIGHT0, GL_AMBIENT, ambientIntensity); glLightfv(GL_LIGHT0, GL_DIFFUSE, lightIntensity); glLightfv(GL_LIGHT0, GL_POSITION, lightDirection); glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpec); glEnable(GL_LIGHT0); firstTime = false; } }

void Draw() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer

// Write your own code below (Hint:Translation Rotation & Scaling)

// Write your own code above

if (obj_data != NULL) obj_data->Draw(); else glutSolidTeapot(1.0); //draw a teapot when no argument is provided

glFlush(); glutSwapBuffers();

// Write your own code below (Hint:reset translate and scale value)

// Write your own code above }

// // GLUT keypress function // void Specialkey(int key, int x, int y) { // Write your own code below (Hint: write response for keypress for up/down/left/right arrow, which has been #define as KEY_UP/KEY_DOWN/KEY_LEFT/KEY_RIGHT)

// Write your own code above

glutPostRedisplay(); }

void onKeyPress(unsigned char key, int x, int y) {

if (key == 'p') { obj_data->mode = GL_LINE_LOOP; } else if (key == 's') { glShadeModel(GL_SMOOTH); // Set Smooth Shading obj_data->mode = GL_POLYGON; } else if (key == 'f') { glShadeModel(GL_FLAT); // Set Smooth Shading obj_data->mode = GL_POLYGON; } else if (key == 'q') { delete obj_data; exit(0); }

glutPostRedisplay(); }

void MouseWheel(int wheel, int direction, int x, int y) { // Write your own code below (Hint: set zoom for mouse wheel)

// Write your own code above glutPostRedisplay();

}

void mouseMotion(int x, int y) { // Write your own code below

// Write your own code above glutPostRedisplay(); }

void mouseButton(int button, int state, int x, int y) { // Write your own code below (Hint:holding down left button allows user to rotate cube)

if (button == GLUT_LEFT_BUTTON) { if (state == GLUT_DOWN) switch(state) { case GLUT_DOWN: y = winHeight - y; trackingMouse = true; redrawContinue = false; startX = x; startY = y; curx = x; cury = y; trackballMove = true; break;

case GLUT_UP: trackingMouse = false; if (startX != x || startY != y) redrawContinue = true; else { angle = 0.0; redrawContinue = false; trackballMove = false; } break; } }

// Write your own code above }

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

// glut initialization functions: glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(winWidth, winHeight); glutInitWindowPosition(100, 100); glutCreateWindow("ImageViewer");

Init(winWidth, winHeight);

// display, onMouseButton, mouse_motion, onKeyPress, and resize are functions defined above glutDisplayFunc(Draw); glutKeyboardFunc(onKeyPress); glutSpecialFunc(Specialkey); glutMouseFunc(mouseButton); glutMotionFunc(mouseMotion); glutMouseWheelFunc(MouseWheel); glutReshapeFunc(Init);

if (argc >= 2) obj_data = new WavefrontObj(argv[1]); else obj_data = NULL;

// start glutMainLoop -- infinite loop glutMainLoop();

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

Intelligent Databases Object Oriented Deductive Hypermedia Technologies

Authors: Kamran Parsaye, Mark Chignell, Setrag Khoshafian, Harry Wong

1st Edition

0471503452, 978-0471503453

More Books

Students also viewed these Databases questions