PDA

View Full Version : Rotation



Rajiv Mishra
08-31-2001, 07:46 PM
I have developed an application in vc++ and OpenGL. In which menu have a tag Cube When you press it one cube will display on screen ,same time if you press it again one more cube will display on screen but in some other position.Now by right click(mouse) on the first object ,it's start rotating but same time the second one has disappear.Which i don't want,Please help me.

With Regards
rajiv

Leyder Dylan
09-01-2001, 01:19 AM
Post you code.

ByteZero
09-01-2001, 08:56 AM
Hi here is my program code that use Menu's
I hope that you can solve your problem with my code http://www.opengl.org/discussion_boards/ubb/biggrin.gif

#include <windows.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <stdio.h>
#include <math.h>

#ifndef __GL_PI
#define GL_PI 3.1415f
#endif
GLfloat rotPoints= 0.0f;
GLfloat rotPoly = 0.0f;
GLfloat rotLines = 0.0f;
GLfloat rotLineStrip = 0.0f;
GLfloat rotLineLoop = 0.0f;
GLfloat rotTriangle = 0.0f;
GLfloat rotTriangleStrip = 0.0f;
GLfloat rotTriangleFan = 0.0f;
GLfloat rotQuads = 0.0f;
GLfloat rotQuadStrip = 0.0f;

typedef enum{
Points, Lines, LineLoop, LineStrip, Poly, Quads, QuadStrip, Triangle, TriangleFan,
TriangleStrip, Quit
}Primitives;


//(R)ender (C)ontext festlegen
//Initialisierung des Programms !!!
GLint SetupRC(GLvoid)
{
//Hintergrundfarbe wird auf schwarz gesetzt
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

//SMOOTH Shading zaubert die Farben besser um ein Polygon
glShadeModel(GL_SMOOTH); //Einschalten von SMOOTH Shading

//Test des Z-Buffers einschalten
glClearDepth(1.0f); //Z-Buffer wird mit dem Wert gefüllt
glEnable(GL_DEPTH_TEST); //Einschalten des Depth Test
glDepthFunc(GL_LEQUAL); //Vergleich der einkommenden Z-Werte

//Farbe der Objecte
glColor3f(1.0f, 1.0f, 0.0f); //rot
return TRUE; //Initialisierung OKAY
}

GLvoid DrawGL(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Löschen des Farb- und des Z- Buffers
glPushMatrix(); //die OBERSTE Matrix (die auf dem Stack liegt) wird kopiert und nochmal auf
//den Stack gepackt
// glutPostRedisplay();//Screen wird neu gezeichnet
glPopMatrix(); //die OBERSTE Matrix wird von Stack entfernt
glutSwapBuffers(); //vertauscht die Buffer (für double buffering benötigt)
}

//Zeichenen der Punkte
GLvoid DrawPoints(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(rotPoints, 0.0f, 1.0f, 1.0f);
rotPoints += 1.0f;
glBegin(GL_POINTS);
// x, y, z
glVertex3f(0.0f, 0.0f, 0.0f); //Punkt 1
glVertex3f(50.0, 0.0f, -50.0f);//Punkt 2
glVertex3f(0.0f, 60.0f, -30.0f);//Punkt 3
glVertex3f(-20.0f, 40.0f, 30);//Punkt 4
glVertex3f(30.0f, -40.0f, 0.0f);//Punkt 5
glVertex3f(10.0f, 10.0f, 10.0f);//Punkt 6
glEnd();

glutPostRedisplay();
glPopMatrix();
glutSwapBuffers();
}

//Zeichenen der Linien
GLvoid DrawLines(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(rotLines, 1.0f, 0.0f, 1.0f);
rotLines += 1.0f;

glBegin(GL_LINES); //Beginn Line 1
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(50.0f, 50.0f, 0.0f);
glEnd(); //End Line 1

glBegin(GL_LINES); //Beginn Lines 2
glVertex3f(-10.0f, -10.0f, -10.0f);
glVertex3f(-40.0f, -40.0f, -40.0f);
glEnd();//End Lines 2

glBegin(GL_LINES);//Beginn Line 3
glVertex3f(0.0f, 40.0f, 20.0f);
glVertex3f(-60.0f, 40.0f, 20.0f);
glEnd(); //End Line 3

glutPostRedisplay();
glPopMatrix();
glutSwapBuffers();
}

//Zeichenen des Polygons
GLvoid DrawPoly(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(rotPoly, 0.0f, 1.0f, 1.0f);
rotPoly += 1.0f;

glBegin(GL_POLYGON);
glVertex3f(20.0f, -10.0f, 0.0f); //Vertex 0
glVertex3f(50.0f, 50.0f, 0.0f);//Vertex 1
glVertex3f(-50.0f, 50.0f, 0.0f);//Vertex 2
glVertex3f(-80.0f, 20.0f, 0.0f);//Vertex 3
glVertex3f(-50.0f, -20.0f, 0.0f);//Vertex 4
glEnd();

glutPostRedisplay();
glPopMatrix();
glutSwapBuffers();
}

//Zeichnen des Quads
GLvoid DrawQuad(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(rotQuads, 1.0f, 1.0f, 0.0f);
rotQuads += 1.0f;

glBegin(GL_QUADS);
glVertex3f(0.0f, 0.0f, 0.0f);//Vertex 1
glVertex3f(60.0f, 0.0f, 0.0f);//Vertex 2
glVertex3f(60.0f, 60.0f, 0.0f);//Vertex 3
glVertex3f(0.0f, 60.0f, 0.0f);//Vertex 4
glEnd();

glutPostRedisplay();
glPopMatrix();
glutSwapBuffers();
}

//Zeichnen des Dreiecks
GLvoid DrawTriangle(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(rotTriangle, 1.0f, 1.0f, 0.0f);
rotTriangle += 1.0f;

glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(60.0f, 0.0f, 0.0f);
glVertex3f(30.0f, 60.0f, 0.0f);
glEnd();

glutPostRedisplay();
glPopMatrix();
glutSwapBuffers();
}

//Zeichnen des LineStrip
GLvoid DrawLineStrip(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(rotLineStrip, 0.0f, 1.0f, 1.0f);
rotLineStrip += 1.0f;

glBegin(GL_LINE_STRIP);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(40.0f, 20.0f, 0.0f);
glVertex3f(40.0f, 60.0f, 0.0f);
glVertex3f(-10.0f, 40.0f, 0.0f);
glEnd();

glutPostRedisplay();
glPopMatrix();
glutSwapBuffers();
}

//Zeichnen des TriangleFan
GLvoid DrawTriangleFan(GLvoid)
{
GLfloat winkel, x, y;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(rotTriangleFan, 0.0f, 1.0f, 1.0f);
rotTriangleFan += 1.0f;

glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 0.0f, 0.0f);
// 2.0f steht für je 1 Hälfte des Kreises; GL_PI/100.0f bestimmt die Anzahl der "Ecken" des Fan's
for(winkel = 0.0f; winkel < (2.0f*GL_PI); winkel += (GL_PI/100.0f))
{
//x und y Berechung
x = 50.0f * sin(winkel);
y = 50.0f * cos(winkel);

glVertex2f(x, y);
}
glEnd();

glutPostRedisplay();
glPopMatrix();
glutSwapBuffers();
}

//Zeichnen des TriangleStrip
GLvoid DrawTriangleStrip(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(rotTriangleStrip, 1.0f, 0.0f, 1.0f);
rotTriangleStrip += 1.0f;

glBegin(GL_TRIANGLE_STRIP);
glVertex3f(0.0f, 0.0f, 0.0f); //V1
glVertex3f(50.0f, 0.0f, 0.0f);//V2
glVertex3f(25.0f, 50.0f, 0.0f);//V3
glVertex3f(70.0f, 60.0f, 0.0f);//V4
glVertex3f(25.0f, 90.0f, 0.0f);//V5
glEnd();

glutPostRedisplay();
glPopMatrix();
glutSwapBuffers();
}

//Zeichnen des LineLoop
GLvoid DrawLineLoop(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(rotLineLoop, 1.0f, 0.0f, 1.0f);
rotLineLoop += 1.0f;

glBegin(GL_LINE_LOOP);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, -20.0f, 0.0f);
glVertex3f(40.0f, -20.0f, 0.0f);
glVertex3f(50.0f, 30.0f, 0.0f);
glEnd();

glutPostRedisplay();
glPopMatrix();
glutSwapBuffers();
}

//Zeichnen des QuadStrip
GLvoid DrawQuadStrip(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(rotQuadStrip , 1.0f, 1.0f, 1.0f);
rotQuadStrip += 1.0f;

glBegin(GL_QUAD_STRIP);
glVertex3f(0.0f, 0.0f, 0.0f);//V 0
glVertex3f(0.0f, -30.0f, 0.0f);//V 1
glVertex3f(40.0f, 0.0f, 0.0f);//V 2
glVertex3f(40.0f, -30.0f, 0.0f);//V 3
glVertex3f(80.0f, 0.0f, 0.0f);//V 4
glVertex3f(80.0f, -30.0f, 0.0f);//V 5
glEnd();

glutPostRedisplay();
glPopMatrix();
glutSwapBuffers();
}

//Zum ReSizen der Scene
//h = height
//w = width
GLvoid ReSizeScene(GLsizei w, GLsizei h)
{
GLfloat Entf = 100.0f; //Entfernungsvariable

if(h == 0)
{
h = 1;
}

glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

//Clipping volume (left, right, bottom, top, near, far)
if(w <= h)
{
glOrtho(-Entf, Entf, -Entf*h/w, Entf*h/w, -Entf, Entf);
}
else
{
glOrtho(-Entf*w/h, Entf*w/h, -Entf, Entf, -Entf, Entf);
}

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

//Tastatur Routine
GLvoid Keyboard(unsigned char key, int x, int y)
{
//printf("Key is %d\n ", key); //Ausgabe der gedrückten Tasten
switch(key)
{ //begin switch
case 27: //wenn ESC, dann Programmende
printf("Programm ENDE !!!\n");
exit(0); //Beenden erfolgreich
break;
case 119: //W
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
printf("Wireframe Mode ENABLE !!!\n");
break;

case 115: //S
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
printf("Solid Mode ENABLE !!!\n");
} //end switch
}

void menu(int value)
{
switch(value)
{ //Begin Switch
case Points:
printf("Points active !!!\n");
glutIdleFunc(DrawPoints);
break;

case Lines:
printf("Lines active !!!\n");
glutIdleFunc(DrawLines);
break;

case LineStrip:
printf("Line Strip active !!!\n");
glutIdleFunc(DrawLineStrip);
break;

case LineLoop:
printf("Line Loop active !!!\n");
glutIdleFunc(DrawLineLoop);
break;

case Poly:
printf("Polygon active !!!\n");
glutIdleFunc(DrawPoly);
break;

case Quads:
printf("Quads active !!!\n");
glutIdleFunc(DrawQuad);
break;

case QuadStrip:
printf("Quad Strip active !!!\n");
glutIdleFunc(DrawQuadStrip);
break;

case Triangle:
printf("Triangle active !!!\n");
glutIdleFunc(DrawTriangle);
break;

case TriangleFan:
printf("Triangle Fan active !!!\n");
glutIdleFunc(DrawTriangleFan);
break;

case TriangleStrip:
printf("Triangle Strip active !!!\n");
glutIdleFunc(DrawTriangleStrip);
break;

case Quit:
printf("Quit Program, OKAY\n");
exit(0);
break;
} //End Switch
}

//Hauptfuntion
int main(int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowPosition (320, 200);
glutInitWindowSize(640, 480);
glutCreateWindow("Primitive Demo");
glutDisplayFunc(DrawGL);
glutReshapeFunc(ReSizeScene);
glutKeyboardFunc(Keyboard);
glutCreateMenu(menu); //Createn des Menüs
glutAddMenuEntry("POINTS", Points); //adden von Menü Einträgen
glutAddMenuEntry("LINES", Lines);
glutAddMenuEntry("LINE LOOP", LineLoop);
glutAddMenuEntry("LINE STRIP", LineStrip);
glutAddMenuEntry("TRIANGLE", Triangle);
glutAddMenuEntry("TRIANGLE FAN", TriangleFan);
glutAddMenuEntry("TRIANGLE STRIP", TriangleStrip);
glutAddMenuEntry("QUADS", Quads);
glutAddMenuEntry("QUAD STRIP", QuadStrip);
glutAddMenuEntry("POLYGON", Poly);
glutAddMenuEntry("-------------", 0);
glutAddMenuEntry("Quit", Quit);
glutAttachMenu(GLUT_RIGHT_BUTTON);
SetupRC();
glutMainLoop();
return 0;
}

Rajiv Mishra
09-02-2001, 07:38 PM
Hello ByteZero,
you code is not the solution of my problem,
can you change you code so that,first time when you select line it start rotating than you select triangle or any other option from your menu ,this time triangle start rotating without lines be disappear.
with thanks
rajiv

ByteZero
09-02-2001, 08:22 PM
Hi

Okay I'll try to change the code !!!
But please post your own code so that I can see what's your mistake http://www.opengl.org/discussion_boards/ubb/biggrin.gif

ok I'll poste my changed code so fast as I can...

c u

Rajiv Mishra
09-02-2001, 08:43 PM
Hello ByteZero,
I have already said that there is no such error in my code but the thing which i want to do is create problem,what happen when i press mouse on object for contiuous rotation,
it's call display function continusly with some increament in angle of glRotate() function,As you people know display function has contain glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT),function,it's clear the whole hole screen.and all other object which is on screen become disappear.

DaViper
09-03-2001, 05:55 AM
It would be much easier for us, if you would post the part of your code in which the rotation and drawing of the cube takes place.

My best guees it that your are updating the Modelview Matrix and after that your are redrawing both bues, so one cube is roating around itself and the otherone is rotating around it.

greetz
Chris

Rajiv Mishra
09-03-2001, 07:53 PM
Hello daViper,
Many -many thanks,Please refer the code of ByteZero in this thread and change what ever I mentioned in the previous post.

in ByteZero code when you right click on the window you get one menu click any things that start rotating.Now when you again perfoemed right click first one disappear.this is the problem,what i want first one still rotate continuously,and also the new selected object start rotation in some other place in that window.

ByteZero
09-03-2001, 08:52 PM
Ok I've try to change the code http://www.opengl.org/discussion_boards/ubb/smile.gif
I'll hope this post will kill your problem.

Okay go to the DrawGL Routine and kick glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); out of the Code.

Now when you start the Program select an Primitive and and it would rotate and now when press the right mouse button again the Primitive stop to rotate !!!
I hope that this is the solution for your problem.

otherwise post or mail me your code to what's going on there.

c u

Rajiv Mishra
09-03-2001, 09:02 PM
What mr ByteZero,this is not solution,please read problem clearly.