opengl rotation issue

hello,
Im drawing a XYZ indictor in my partview window using gluCylinder. I can get Z axis looking right but I cant seem to rotate the cylinder to the Y or X axis:

{ // Z axis in blue
glTranslatef(0.0f,0.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
gluCylinder(quadratic,0.05f,0.05f,0.5f,20,10);
// draw a cone at the end of the line
glTranslatef(0.0f,0.0f,0.5f);
gluCylinder(quadratic,0.10f,0.0f,0.18f,20,1);
}
{ y axis is green/yellow
glTranslatef(0.0f,0.0f,0.0f);
glRotatef(-90,0.0f,0.0f,1.0f);
glColor3f(0.0f,1.0f,0.0f);
gluCylinder(quadratic,0.05f,0.05f,0.5f,20,10);
// draw a cone at the end of the line
glTranslatef(0.0f,0.0f,0.5f);
gluCylinder(quadratic,0.10f,0.0f,0.18f,20,1);
}
Im sure im missing something obvious.

Thanks in advance
Derrek

Could you post the code before, after, and between the brackets?

Your first problem is with glRotate. Check the manual to see what those parameters are. Do you know what the numbers 0,0,1 mean, i.e. what they are supposed to do? After you fix that, you’ll find you have another small, problem. But one step at a time.

Your first problem is with glRotate. Check the manual to see what those parameters are. Do you know what the numbers 0,0,1 mean, i.e. what they are supposed to do? After you fix that, you’ll find you have another small, problem. But one step at a time. [/QUOTE]
that 's not a total code.
why not paste a complete code.
to run

Hello,
Here is the complete code, I think Im using glrotatef correctly but Ill double check,

GLUquadricObj *quadratic; // Storage For Our Quadratic Objects ( NEW )

// CVisualWorkShopView

IMPLEMENT_DYNCREATE(CVisualWorkShopView, CView)

BEGIN_MESSAGE_MAP(CVisualWorkShopView, CView)
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CVisualWorkShopView::OnFilePrintPreview)
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_CREATE()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()

// CVisualWorkShopView construction/destruction

CVisualWorkShopView::CVisualWorkShopView()
{
m_fPosX = 0.0f; // X position of model in camera view
m_fPosY = 0.0f; // Y position of model in camera view
m_fZoom = 10.0f; // Zoom on model in camera view
m_fRotX = 0.0f; // Rotation on model in camera view
m_fRotY = 0.0f; // Rotation on model in camera view
m_bIsMaximized = false;

}

CVisualWorkShopView::~CVisualWorkShopView()
{
}

BOOL CVisualWorkShopView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

return CView::PreCreateWindow(cs);

}

// CVisualWorkShopView drawing

void CVisualWorkShopView::OnDraw(CDC* /pDC/)
{

glLoadIdentity();

glTranslatef(0.0f, 0.0f, -m_fZoom);
glTranslatef(m_fPosX, m_fPosY, 0.0f);
glRotatef(m_fRotX, 1.0f, 0.0f, 0.0f);
glRotatef(m_fRotY, 0.0f, 1.0f, 0.0f);

// Clear color and depth buffer bits
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		
// Draw OpenGL scene
oglDrawScene();

// Swap buffers
SwapBuffers(hdc);

// CVisualWorkShopDoc* pDoc = GetDocument();
// ASSERT_VALID(pDoc);
// if (!pDoc)
// return;

}

// CVisualWorkShopView printing

void CVisualWorkShopView::OnFilePrintPreview()
{
AFXPrintPreview(this);
}

BOOL CVisualWorkShopView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}

void CVisualWorkShopView::OnBeginPrinting(CDC* /pDC/, CPrintInfo* /pInfo/)
{
// TODO: add extra initialization before printing
}

void CVisualWorkShopView::OnEndPrinting(CDC* /pDC/, CPrintInfo* /pInfo/)
{
// TODO: add cleanup after printing
}

void CVisualWorkShopView::OnRButtonUp(UINT nFlags, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}

void CVisualWorkShopView::OnContextMenu(CWnd* pWnd, CPoint point)
{
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
}

// CVisualWorkShopView diagnostics

#ifdef _DEBUG
void CVisualWorkShopView::AssertValid() const
{
CView::AssertValid();
}

void CVisualWorkShopView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}

CVisualWorkShopDoc* CVisualWorkShopView::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CVisualWorkShopDoc)));
return (CVisualWorkShopDoc*)m_pDocument;
}
#endif //_DEBUG

void CVisualWorkShopView::OnPaint()
{
ValidateRect(NULL);
//CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CView::OnPaint() for painting messages

}

void CVisualWorkShopView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);

if (0 >= cx || 0 >= cy || nType == SIZE_MINIMIZED) return;

// Map the OpenGL coordinates.
glViewport(0, 0, cx, cy);

// Projection view
glMatrixMode(GL_PROJECTION);

glLoadIdentity();

// Set our current view perspective
gluPerspective(35.0f, (float)cx / (float)cy, 0.01f, 2000.0f);

// Model view
glMatrixMode(GL_MODELVIEW);

switch (nType)
{
	// If window resize token is "maximize"
	case SIZE_MAXIMIZED:
	{
		// Get the current window rect
		GetWindowRect(m_rect);

		// Move the window accordingly
		MoveWindow(6, 6, cx - 14, cy - 14);

		// Get the new window rect
		GetWindowRect(m_rect);

		// Store our old window as the new rect
		m_oldWindow = m_rect;

		break;
	}

	// If window resize token is "restore"
	case SIZE_RESTORED:
	{
		// If the window is currently maximized
		if (m_bIsMaximized)
		{
			// Get the current window rect
			GetWindowRect(m_rect);

			// Move the window accordingly (to our stored old window)
			MoveWindow(m_oldWindow.left, m_oldWindow.top - 18, m_originalRect.Width() - 4, m_originalRect.Height() - 4);

			// Get the new window rect
			GetWindowRect(m_rect);

			// Store our old window as the new rect
			m_oldWindow = m_rect;
		}
	
		break;
	}
}

}
// TODO: Add your message handler code here

int CVisualWorkShopView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;

static PIXELFORMATDESCRIPTOR pfd =
{
	sizeof(PIXELFORMATDESCRIPTOR),      // size of this pfd
	1,                                  // version number
	PFD_DRAW_TO_WINDOW |                // support window
	PFD_SUPPORT_OPENGL |                // support OpenGl
	PFD_DOUBLEBUFFER,                   // double buffered
	PFD_TYPE_RGBA,                      // RGBA type
	32,                                 // bit depth
	0, 0, 0, 0, 0, 0,                   // color bits ignored
	0,                                  // no alpha buffer
	0,                                  // shift bit ignored
	0,                                  // no accumulation buffer
	0, 0, 0, 0,                         // accum bits ignored
	16,                                 // z-buffer depth
	0,                                  // no stencil buffer
	0,                                  // no auxiliary buffer
	0,                                  // main layer
	0,                                  // reserved
	0, 0, 0,                            // layer masks ignored
};

// Basic Setup:
quadratic=gluNewQuadric();							// Create A Pointer To The Quadric Object (Return 0 If No Memory) (NEW)
gluQuadricNormals(quadratic, GLU_SMOOTH);			// Create Smooth Normals (NEW)
gluQuadricTexture(quadratic, GL_TRUE);				// Create Texture Coords (NEW)

// Get device context only once.
hdc = GetDC()->m_hDC;

// Pixel format.
m_nPixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, m_nPixelFormat, &pfd);

// Create the OpenGL Rendering Context.
hrc = wglCreateContext(hdc);

// preform default settings of render mode
OnCreateGL();

// free the target DeviceContext(window)
wglMakeCurrent(hdc, hrc);

// Turn on backface culling
glFrontFace(GL_CCW);
glCullFace(GL_BACK);

// Turn on depth testing
glDepthFunc(GL_LEQUAL);

// Send draw request
OnDraw(NULL);

return 0;

}

void CVisualWorkShopView::OnCreateGL()
{
// Turn on depth testing
glEnable(GL_DEPTH_TEST);

// Set color to use when clearing the background.
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);// black color

// set clear Z-buffer value
glClearDepth(1.0f);

}

void CVisualWorkShopView::OnMouseMove(UINT nFlags, CPoint point)
{
int diffX = (int)(point.x - m_fLastX);
int diffY = (int)(point.y - m_fLastY);
m_fLastX = (float)point.x;
m_fLastY = (float)point.y;

// Left mouse button
if (nFlags & MK_LBUTTON)
{
	m_fRotX += (float)0.5f * diffY;

	if ((m_fRotX > 360.0f) || (m_fRotX < -360.0f))
	{
		m_fRotX = 0.0f;
	}

	m_fRotY += (float)0.5f * diffX;

	if ((m_fRotY > 360.0f) || (m_fRotY < -360.0f))
	{
		m_fRotY = 0.0f;
	}
}

// Right mouse button
else if (nFlags & MK_RBUTTON)
{
	m_fZoom -= (float)0.1f * diffY;
}

// Middle mouse button
else if (nFlags & MK_MBUTTON)
{
	m_fPosX += (float)0.05f * diffX;
	m_fPosY -= (float)0.05f * diffY;
}

OnDraw(NULL);

CView::OnMouseMove(nFlags, point);

}

void CVisualWorkShopView::DrawBackGround(void)
{
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glBegin(GL_QUADS);					    // Start Drawing A Quad
    glColor4f(0.2f,0.2f,0.6f,1.0f); 
	glVertex3f(-1.0f, 1.0f, -1.0f);		// Top Left Of The Quad
	glVertex3f( 1.0f, 1.0f, -1.0f);		// Top Right Of The Quad
    glColor4f(0.9f,0.9f,1.0f,1.0f);
	glVertex3f( 1.0f,-1.0f, -1.0f);		// Bottom Right Of The Quad
	glVertex3f(-1.0f,-1.0f, -1.0f);		// Bottom Left Of The Quad
glEnd();

}

void CVisualWorkShopView::oglDrawScene(void)
{

glDepthMask(GL_FALSE);
glDisable(GL_DEPTH_TEST);

glPushMatrix();
glLoadIdentity();
DrawBackGround();
glPopMatrix();

// And reenable those
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);

// Wireframe Mode

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

// Draw3DQuadratic();
/*
glBegin(GL_QUADS);
glColor3f(0.f,0.f,1.f);
// Front Side
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);

		// Back Side
		glVertex3f(-1.0f, -1.0f, -1.0f);
		glVertex3f(-1.0f,  1.0f, -1.0f);
		glVertex3f( 1.0f,  1.0f, -1.0f);
		glVertex3f( 1.0f, -1.0f, -1.0f);

		// Top Side
		glVertex3f( 1.0f, 1.0f,  1.0f);
		glVertex3f( 1.0f, 1.0f, -1.0f);
		glVertex3f(-1.0f, 1.0f, -1.0f);
		glVertex3f(-1.0f, 1.0f,  1.0f);

		// Bottom Side
		glVertex3f(-1.0f, -1.0f, -1.0f);
		glVertex3f( 1.0f, -1.0f, -1.0f);
		glVertex3f( 1.0f, -1.0f,  1.0f);
		glVertex3f(-1.0f, -1.0f,  1.0f);

		// Right Side
		glVertex3f( 1.0f,  1.0f,  1.0f);
		glVertex3f( 1.0f, -1.0f,  1.0f);
		glVertex3f( 1.0f, -1.0f, -1.0f);
		glVertex3f( 1.0f,  1.0f, -1.0f);

		// Left Side
		glVertex3f(-1.0f, -1.0f, -1.0f);
		glVertex3f(-1.0f, -1.0f,  1.0f);
		glVertex3f(-1.0f,  1.0f,  1.0f);
		glVertex3f(-1.0f,  1.0f, -1.0f);
glEnd();

*/

glBegin(GL_LINES);
	
    // red x axis arrow
    glColor3f(1.f,0.f,0.f);
	glVertex3f(0.0f,0.0f,0.0f);
	glVertex3f(1.0f,0.0f,0.0f);
    // x letter & arrowhead
	glVertex3f(1.1f,0.1f,0.0f);
	glVertex3f(1.3f,-0.1f,0.0f);
	glVertex3f(1.3f,0.1f,0.0f);
	glVertex3f(1.1f,-0.1f,0.0f);
	glVertex3f(1.0f,0.0f,0.0f);
	glVertex3f(0.9f,0.1f,0.0f);
	glVertex3f(1.0f,0.0f,0.0f);
	glVertex3f(0.9f,-0.1f,0.0f);

    // green y axis arrow
	glColor3f(0.f,1.f,0.f);
	glVertex3f(0.0f,0.0f,0.0f);
	glVertex3f(0.0f,1.0f,0.0f);
	// y letter & arrowhead
	glVertex3f(-0.1f,1.3f,0.0f);
	glVertex3f(0.f,1.2f,0.0f);
	glVertex3f(0.1f,1.3f,0.0f);
	glVertex3f(0.f,1.2f,0.0f);
	glVertex3f(0.f,1.2f,0.0f);
	glVertex3f(0.f,1.1f,0.0f);
	glVertex3f(0.0f,1.0f,0.0f);
	glVertex3f(0.1f,0.9f,0.0f);
	glVertex3f(0.0f,1.0f,0.0f);
	glVertex3f(-0.1f,0.9f,0.0f);
	
	// blue z axis arrow
	glColor3f(0.f,0.f,1.f);
	glVertex3f(0.0f,0.0f,0.0f);
	glVertex3f(0.0f,0.0f,1.0f);
	// z letter & arrowhead
	glVertex3f(0.0f,-0.1f,1.3f);
	glVertex3f(0.0f,0.1f,1.3f);
	glVertex3f(0.0f,0.1f,1.3f);
	glVertex3f(0.0f,-0.1f,1.1f);
	glVertex3f(0.0f,-0.1f,1.1f);
	glVertex3f(0.0f,0.1f,1.1f);
	glVertex3f(0.0f,0.0f,1.0f);
	glVertex3f(0.0f,0.1f,0.9f);
	glVertex3f(0.0f,0.0f,1.0f);
	glVertex3f(0.0f,-0.1f,0.9f);
glEnd();

glBegin(GL_LINES);

for (int i = -10; i <= 10; i++)
{
	glColor3f(0.5f, 1.0f, 0.5f); 
    glVertex3f((float)i, -2, -10);
    glVertex3f((float)i, -2, 10);
    glVertex3f(-10, -2, (float)i);
    glVertex3f(10, -2, (float)i);
}

glEnd();

// draw X-Y-Z origin
DrawXYZOrigin();

}

void CVisualWorkShopView::DrawOglLine( float x1, float y1, float x2, float y2 )
{
float zval = 1.0f;

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushAttrib(GL_ENABLE_BIT|GL_LINE_BIT|GL_CURRENT_BIT|GL_LIGHTING_BIT|GL_TEXTURE_BIT);
glPushMatrix();

// AfxMessageBox(“hello from draw line”,0,0);
glBegin(GL_LINES);
glColor3f(1.f,0.f,0.f);
glVertex3f(x1,y2,zval);
glVertex3f(x2,y2,zval);
glEnd();
glFlush();
glPopMatrix();
glPopAttrib();

}

void CVisualWorkShopView::draw_ogl_circle( int x1, int y1, int rad, int color )
{

}

void CVisualWorkShopView::Draw3DQuadratic(void)
{
glColor3f(1.0f,0.f,0.f);
// glTranslatef(2.0f,1.5f,-2.0);
gluCylinder(quadratic,1.0f,1.0f,3.0f,32,32);
// AfxMessageBox(“I got called”,0,0);
glTranslatef(2.0f,0.0f,1.0f);
glColor4f(0.5f,0.0f,1.0f,0.5f);
gluSphere(quadratic,1.0f,20,20);
}

void CVisualWorkShopView::DrawXYZOrigin(void)
{
{
glTranslatef(0.0f,0.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
gluCylinder(quadratic,0.05f,0.05f,0.5f,20,10);
// draw a cone at the end of the line
glTranslatef(0.0f,0.0f,0.5f);
gluCylinder(quadratic,0.10f,0.0f,0.18f,20,1);
}
{
glPushMatrix();
glTranslatef(0.0f,0.0f,0.0f);
glRotatef(90,0.0f,0.0f,0.0f);
glColor3f(0.0f,1.0f,0.0f);
gluCylinder(quadratic,0.05f,0.05f,0.5f,20,10);
// draw a cone at the end of the line
glTranslatef(0.0f,0.0f,0.5f);
gluCylinder(quadratic,0.10f,0.0f,0.18f,20,1);
glPopMatrix ();
}
}

The code you posted originally was sufficient to diagnose the problem. I repeat - your problem is with the glRotate command. Read the documentation on what the calling parameters mean. Do you understand the explanations? Hand drawing some pictures always helps me.

BTW - the 2nd version of code you posted is different from the first. You changed the last 3 numbers in glRotate from 0,0,1 (which is wrong) to 0,0,0 (which is worse). Also, you added Push and Pop commands (a good idea). I assume you know that translating objects by 0,0,0 does nothing?

OK I reread the rotateF documentation and understand whats going on I have the cylinder oriented correctly but its starts drawing form the end of the previous cylinder. I thought the translate command was returning me to the origin.

void CVisualWorkShopView::DrawXYZOrigin(void)
{
{
glTranslatef(0.0f,0.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
gluCylinder(quadratic,0.05f,0.05f,0.5f,20,10);
// draw a cone at the end of the line
glTranslatef(0.0f,0.0f,0.5f);
gluCylinder(quadratic,0.10f,0.0f,0.18f,20,1);
}
{
glPushMatrix();
glRotatef(-90,1.0f,0.0f,0.0f);
glColor3f(0.0f,1.0f,0.0f);
gluCylinder(quadratic,0.05f,0.05f,0.5f,20,10);
// draw a cone at the end of the line
glTranslatef(0.0f,0.0f,0.5f);
gluCylinder(quadratic,0.10f,0.0f,0.18f,20,1);
glPopMatrix ();
}
}

Thanks for your help
Derrek

That’s obviously not the case as his original code had translates to top off the arrow but wasn’t resetting his matrix. So unless you assumed that wasn’t the problem (since all he/she originally said was that it wasn’t working), it certainly could have been a popping/pushing issue of the matrix stack, which as digga just mentioned is also a problem.

digga, glTranslate(0,0,0) does jack. It multiplies the current matrix by the identity matrix since you’re telling it to move 0,0,0 in xyz–not move TO 0,0,0. You can either push and pop your modelview matrix, or you can actually move back using negative values.

ex.

glTranslate(0,0,1)
// draw something
glTranslate(0,0,-1) // move back to where you were

or

glPushMatrix() // saves off matrix
glTranslate(0,0,1)
glPopMatrix() // restores latest saved matrix

Fixed it!!!
Thanks to everyone for their tips

void CVisualWorkShopView::DrawXYZOrigin(void)
{

	glPushMatrix();
	glColor3f(0.0f,0.0f,1.0f);
	gluCylinder(quadratic,0.05f,0.05f,0.5f,20,10);
	// draw a cone at the end of the line
	glTranslatef(0.0f,0.0f,0.5f);
	gluCylinder(quadratic,0.10f,0.0f,0.18f,20,1);

	glRotatef(-90,1.0f,0.0f,0.0f);
	glTranslatef(0.0f,0.5f,0.0f);
	glColor3f(0.0f,1.0f,0.0f);
	gluCylinder(quadratic,0.05f,0.05f,0.5f,20,10);
	// draw a cone at the end of the line
	glTranslatef(0.0f,0.0f,0.5f);
	gluCylinder(quadratic,0.10f,0.0f,0.18f,20,1);
 
	glRotatef(90,0.0f,0.5f,0.0f);
	glTranslatef(0.5f,0.0f,0.0f);
	glColor3f(1.0f,0.0f,0.0f);
	gluCylinder(quadratic,0.05f,0.05f,0.5f,20,10);
	// draw a cone at the end of the line
	glTranslatef(0.0f,0.0f,0.5f);
	gluCylinder(quadratic,0.10f,0.0f,0.18f,20,1);
    glPopMatrix ();

}

now if I can make it solid instead of wireframe Ill be done!

Derrek

Use the following code to make it solid:

glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );

Ehsan,
Thanks

Derrek

yw :slight_smile:
Good Luck with OpenGL :wink:

I also wrote some comments about OpenGL matrices and how they are applied here