[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to rotate, translate and zoom objects using mouse in OpenGL (C#)

Posted on 2005-04-21
7
Medium Priority
?
26,324 Views
Last Modified: 2013-12-06
Dear expert,
I need some example (VB .NET, C#, C++) on how to rotate, translate and zoom objects in OpenGL using mouse and general. I am using orthogonal view.

Thanks in advance, Svemir.
0
Comment
Question by:svemir
  • 5
  • 2
7 Comments
 
LVL 3

Accepted Solution

by:
Barca earned 1000 total points
ID: 13864206
glRotate, glTanslate


for zoom, you can use glLookAt

http://www.eecs.tulane.edu/www/Terry/OpenGL/Introduction.html
0
 
LVL 3

Expert Comment

by:Barca
ID: 13864265
there is a sample code for glroatae, gl Translate and zoom by using alLootAt


#include <glut.h>

bool rotationenable = false;
bool scaleenable = false;
bool translateenable = false;


static float angle[3] ={0.0,0.0,0.0};

//scale factors
static float sx = 1.0, sy = 1.0 , sz = 1.0;

//coordinates of cube
float Vertices[8][3] = {
    {-1.0,-1.0,1.0},{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0},
    {-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}};

int vector[] = {0,0,0};

void drawFace(int a,int b,int c,int d)
{
    glVertex3fv(Vertices[a]);
      glVertex3fv(Vertices[b]);
      glVertex3fv(Vertices[c]);
    glVertex3fv(Vertices[d]);
}

void cube ()
{
   glBegin(GL_QUADS);

            //front
         glColor3f(1.0, 0.0, 0.0); //red
       drawFace(0,1,2,3);

         //back
         glColor3f(0.0, 1.0, 0.0); //green
         drawFace(7,6,5,4);

            //top
         glColor3f(0.0, 0.0, 1.0); //blue
         drawFace(3,2,6,7);

         //bottom
         glColor3f(1.0, 1.0, 0.0); //yellow
         drawFace(1,0,4,5);

         //left
         glColor3f(1.0, 0.0, 1.0); //pink
         drawFace(0,3,7,4);

         //right
         glColor3f(0.0, 1.0, 1.0); //cyan
         drawFace(2,1,5,6);

   glEnd();
}

static float x=0.0f,y=0.0f,z=50.0f;
static float lx=0.0f,ly=0.0f,lz=-1.0f;

void display(void){
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

      gluLookAt(x, y, z,
                  x + lx,y + ly,z + lz,
                    0.0f,1.0f,0.0f);

      if (translateenable == true)
            glTranslated(vector[0], vector[1], vector[2]);
      
      if (rotationenable == true){
            glRotatef(angle[0], 1.0, 0.0, 0.0);
            glRotatef(angle[1], 0.0, 1.0, 0.0);
            glRotatef(angle[2], 0.0, 0.0, 1.0);
      }

      if (scaleenable == true)
            glScalef(0.5, 1.5, 0.7);

      glScalef(sx, sy, sz);
      

      cube();

    glutSwapBuffers();
      
}


void processMenuEvents(int option) {

      switch (option) {
            case (1):
                  translateenable = true;
                  vector[0] += 1;
                  vector[1] += 2;
                  vector[2] += 3;
                  break;
            case (2):
                  rotationenable = true;
                  angle[0] += 45.0;
                  break;
            case (3):
                  rotationenable = true;
                  angle[1] += 45.0;
                  break;
            case (4):
                  rotationenable = true;
                  angle[2] += 45.0;
                  break;
            case (5):
                  scaleenable = true;
                  break;
            case (6):
                  translateenable = false;
                  rotationenable = false;
                  scaleenable = false;
                  angle[0] =0.0;
                  angle[1] =0.0;
                  angle[2]=0.0;
                  sx = 1.0;
                  sy = 1.0;
                  sz = 1.0;
                  break;
            default:
            break;
      }
}


//create menu
void createGLUTMenus() {

int menu,submenu;

      submenu = glutCreateMenu(processMenuEvents);
      glutAddMenuEntry("X",2);
      glutAddMenuEntry("Y",3);
      glutAddMenuEntry("Z",4);

      menu = glutCreateMenu(processMenuEvents);
      glutAddMenuEntry("Translate",1);
      glutAddSubMenu("Rotate Menu",submenu);
      glutAddMenuEntry("Scale",5);
      glutAddMenuEntry("Reset",6);
      glutAttachMenu(GLUT_RIGHT_BUTTON);
   
}

//function to move the camera
void moveMeFlat(int i) {
      x = x + i*(lx)*0.1;
      z = z + i*(lz)*0.1;
      glLoadIdentity();
      gluLookAt(x, y, z,
                  x + lx,y + ly,z + lz,
                    0.0f,1.0f,0.0f);
}

void keyboard(unsigned char key,int x, int y){
      switch(key){
      case 'f':
            moveMeFlat(1);
            break;
      case 'b':
            moveMeFlat(-1);
            break;
      case '+':            
            sx += 1.0;
            sy += 1.0;
            sz += 1.0;            
            break;
      case '-':
            if(sx >1.0 && sy >1.0 && sz>1.0){
                  sx -= 1.0;
                  sy -= 1.0;
                  sz -= 1.0;
            }
            break;
      default:
            break;
      }
}

static float ratio;


//function for resize display window
void reshape ( int w, int h )
{
   glViewport     ( 0, 0, w, h );
   glMatrixMode   ( GL_PROJECTION );
   glLoadIdentity ( );
   if ( h==0 )
      gluPerspective ( 60, ( float ) w, 1.0, 5000.0 );
   else
      gluPerspective ( 60, ( float ) w / ( float ) h, 1.0, 5000.0 );
   glMatrixMode   ( GL_MODELVIEW );
   glLoadIdentity ( );

}

void main(int argc, char ** argv) {
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
      glutInitWindowPosition(100,100);
      glutInitWindowSize(500,500);
      glutCreateWindow("sample1");

      glutKeyboardFunc(keyboard);

      createGLUTMenus();

      glutDisplayFunc(display);
      glutIdleFunc(display);      

      glutReshapeFunc(reshape);

      glEnable(GL_DEPTH_TEST);

      glutMainLoop();
}
0
 

Author Comment

by:svemir
ID: 13864296
Question was how to do this using mouse!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Expert Comment

by:Barca
ID: 13864395
try

glutMouseFunc(mouse);

void
mouse(int button, int state, int x, int y)
{
   
    if (state == GLUT_DOWN) {
    /* store a single hit. */
 

}


glutMotionFunc(motion);

void
motion(int x, int y)
{
/* store the coordinates while mouse is dragging . */

}
0
 
LVL 3

Expert Comment

by:Barca
ID: 13864402
or just

glutMouseFunc(mouse);

void
mouse(int button, int state, int x, int y)
{
   
    if (state == GLUT_DOWN) {
    /* store a single hit. */

if (state == GLUT_UP) {
    /* store a single hit. */
 

}
0
 

Author Comment

by:svemir
ID: 13868549
I can not find glutMouseFunc in csgl library. Does it exists in C#GL?
0
 
LVL 3

Expert Comment

by:Barca
ID: 13870051
The following URL was the lastest verison GLUT for windows (Microsoft version) is also on the 372 Resources page or at:

P.S: It was the same version with Alex posted, but with detail description documents with different development platform, such as MS-VS

GLUT for windows (Microsoft version) is also on the 372 Resources page or at:
glut-3.7.6-bin.zip (117 KB)

http://www.xmission.com/~nate/glut/glut-3.7.6-bin.zip

GLUT for Win32 dll, lib and header file (everything you need to get started programming with GLUT).
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question