OpenGL programming (c++) The program should generate a triangle that should move in response to the left mouse button being held down and the mouse moved. The figure should stop at the defined window boundaries (N,S,E,W) making sure that the entire figure is always present in your window.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

OpenGL programming (c++)

The program should generate a triangle that should move in response to the left mouse button being held down and the mouse moved. The figure should stop at the defined window boundaries (N,S,E,W) making sure that the entire figure is always present in your window.

Expert Solution
Step 1

To create an OpenGL program in C++ that generates a moving triangle in response to mouse events, you can follow these steps:

 

1)Include the necessary header files for OpenGL and GLUT:

 

#include <GL/glut.h>

#include <GL/gl.h>

2)Define the initial positions of the triangle.

GLfloat triangleX = 0.0;

GLfloat triangleY = 0.0;

 

 

3)Create a function that draws the triangle:

 

void drawTriangle()

{

    glPushMatrix();

    glTranslatef(triangleX, triangleY, 0.0);

    glBegin(GL_TRIANGLES);

    glVertex3f(-0.5, -0.5, 0.0);

    glVertex3f(0.5, -0.5, 0.0);

    glVertex3f(0.0, 0.5, 0.0);

    glEnd();

    glPopMatrix();

}

4)Create a function that responds to mouse events

 

void mouse(int button, int state, int x, int y)

{

    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)

    {

        // Save the initial position of the mouse

        lastX = x;

        lastY = y;

    }

}

 

5)Create a function that moves the triangle when the mouse is movedCreate a function that moves the triangle when the mouse is moved

 

 

void motion(int x, int y)

{

// Calculate the distance the mouse moved

GLfloat deltaX = x - lastX;

GLfloat deltaY = y - lastY;

 

// Update the position of the triangle

triangleX += deltaX / windowWidth;

triangleY -= deltaY / windowHeight;

 

// Make sure the triangle stays within the window boundaries

if (triangleX > 0.5)

{

triangleX = 0.5;

}

else if (triangleX < -0.5)

{

triangleX = -0.5;

}

if (triangleY > 0.5)

{

triangleY = 0.5;

}

else if (triangleY < -0.5)

{

triangleY = -0.5;

}

 

// Save the current position of the mouse

lastX = x;

lastY = y;

 

// Redraw the scene

glutPostRedisplay();

}

 

 

 

 

 

 

steps

Step by step

Solved in 2 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

 lastX = x; and lastY = y;

they are giving me an error "undefined" how do I fix it?

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education