OpenGL是否围绕坐标旋转? [英] OpenGL rotate around a coordinate?

查看:72
本文介绍了OpenGL是否围绕坐标旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我正在用OpenGL编写演示.在其中一个场景中,我有几个围绕自身中心旋转的立方体(x,y,z).要绘制立方体,我获取中心(x,y,z)的坐标,并从-1,-1,-1到1、1、1绘制一个立方体(您知道了)

无论如何,当旋转时,所有多维数据集都围绕0、0、0旋转.我该如何绕着x,y,z旋转?

资料来源:

PartCubes.cpp

Hi I am writing a demo in OpenGL. In one of the scenes, i am having a few cubes that rotate around the center of themselves(x, y, z). To draw the cube, i get the coordinates for the center(x, y, z), and draw a cube from -1, -1, -1 to 1, 1, 1(you get the idea)

Anyway, when rotating, all the cubes rotate around 0, 0, 0. How can i rotate around x, y, z instead?

Source:

PartCubes.cpp

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include "GLWrapper.h"
#include "Nevermind/GLNMD.h"
#include "PartCubes.h"
#include "Box.h"

using namespace GLWrapper;

PartCubes::PartCubes(void)
{
	xpos = 0;
	ypos = 0;
	zpos = 0;
}

PartCubes::~PartCubes(void)
{
}
void PartCubes::draw(float time)
{
	srand(666);
	for(int i = 0; i < 400; i++)
	{
		float x = (float)rand()/(float)RAND_MAX;
		float y = (float)rand()/(float)RAND_MAX;
		float z = (float)rand()/(float)RAND_MAX;
		x = x * 20;
		y = y * 20;
		z = z * 20;
		float xrot = (float)rand()/(float)RAND_MAX;
		xrot = (xrot * 2) * time;
		//float x = 0;
		//float y = 0;
		//float z = 0;
		drawCube(x + xpos, y + ypos, z + zpos, xrot);
	}

}
void PartCubes::drawCube(float x, float y, float z, float xrot)
{
	glTranslatef(-x,-y,-z);  
	glRotatef(xrot, 1, 0, 0);
	glBegin(GL_QUADS);
		glColor3f(1.0, 0.0, 0.0);
        glVertex3f(1.0 + x, -1.0 + y, -1.0 + z);
        glVertex3f(1.0 + x, -1.0 + y, 1.0 + z);
        glVertex3f(-1.0 + x, -1.0 + y, 1.0 + z);
        glVertex3f(-1.0 + x, -1.0 + y, -1.0 + z);
        glVertex3f(1.0 + x, 1.0 + y, -0.999999 + z);
        glVertex3f(-1.0 + x, 1.0 + y, -1.0 + z);
        glVertex3f(-1.0 + x, 1.0 + y, 1.0 + z);
        glVertex3f(0.999999 + x, 1.0 + y, 1.000001 + z);
        glVertex3f(1.0 + x, -1.0 + y, -1.0 + z);
        glVertex3f(1.0 + x, 1.0 + y, -0.999999 + z);
        glVertex3f(0.999999 + x, 1.0 + y, 1.000001 + z);
        glVertex3f(1.0 + x, -1.0 + y, 1.0 + z);
        glVertex3f(1.0 + x, -1.0 + y, 1.0 + z);
        glVertex3f(0.999999 + x, 1.0 + y, 1.000001 + z);
        glVertex3f(-1.0 + x, 1.0 + y, 1.0 + z);
        glVertex3f(-1.0 + x, -1.0 + y, 1.0 + z);
        glVertex3f(-1.0 + x, -1.0 + y, 1.0 + z);
        glVertex3f(-1.0 + x, 1.0 + y, 1.0 + z);
        glVertex3f(-1.0 + x, 1.0 + y, -1.0 + z);
        glVertex3f(-1.0 + x, -1.0 + y, -1.0 + z);
        glVertex3f(1.0 + x, 1.0 + y, -0.999999 + z);
        glVertex3f(1.0 + x, -1.0 + y, -1.0 + z);
        glVertex3f(-1.0 + x, -1.0 + y, -1.0 + z);
        glVertex3f(-1.0 + x, 1.0 + y, -1.0 + z);
    glEnd();
	glTranslatef(x,y,z);
}



PartCubes.h:



PartCubes.h:

class PartCubes
{
public:
	PartCubes(void);
	~PartCubes(void);
	void draw(float time);
	float xpos;
	float ypos;
	float zpos;
	void drawCube(float x, float y, float z, float xrot);
};

推荐答案

1.使用glTranslate(-x,-y,-z)函数相对于(0,0,0)
移动对象 2.调用glRotate()相对于(0,0,0)旋转对象
3.调用glTranslate(x,y,z)将对象移回到相对于(x,y,z)的位置.

确保相对于(0,0,0)绘制模型,并让O​​penGL完成移动模型的工作.

1. Use the glTranslate(-x, -y, -z) function to move the object relative to (0,0,0)
2. Call glRotate() to rotate the objects, relative to (0,0,0)
3. Call glTranslate(x, y, z) to move the objects back to a position relative to (x,y,z).

Be sure that you draw your model relative to (0,0,0), and let OpenGL do the work of moving it around.

void PartCubes::drawCube(float x, float y, float z, float xrot)
{
    glPushMatrix();

    static float xoff = 1.0f;
    static float yoff = 0.5f;
    static float zoff = 0.0f;

    glTranslatef(x,y,z);
    glRotatef(xrot, 0, 1, 0);
    glTranslatef(xoff,yoff,zoff);  

    drawCube();

    glTranslatef(x,y,z);

    glPopMatrix();
}

void PartCubes::drawCube(void)
{
	glBegin(GL_QUADS);

	glColor3f(1.0f, 0.0f, 0.0f);
        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);
        glVertex3f(1.0f, 1.0f, -0.0f);
        glVertex3f(-1.0f, 1.0f, -1.0f);
        glVertex3f(-1.0f, 1.0f, 1.0f);
        glVertex3f(0.0f, 1.0f, 1.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(1.0f, 1.0f, -0.0f);
        glVertex3f(0.0f, 1.0f, 1.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
        glVertex3f(0.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);
        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, -0.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(-1.0f, 1.0f, -1.0f);
    glEnd();
}


围绕Origin(0,0,0)进行旋转.

因此,如果需要绕原点以外的点旋转,则需要反向平移该量,然后进行旋转,然后再次平移到该点.很简单.

如果您的对象是凸形,则可以轻松找到中心,对吗?然后使用glTranslate(-object.x,-object.y-object.z)向后平移,然后使用glRotate(...)进行旋转,然后再次使用相同的glTranslate(object.x,object.y, object.z).

希望这对您有帮助
http://cgmath.blogspot.com/
Rotations are done around Origin(0,0,0).

So if you need to rotate around a point other than origin, you need to back translate by that amount, and carry out the rotation then again translate to that point. It is pretty straight forward.

If your object is a convex shape, you can find the center easily right ? Then back translate using glTranslate(-object.x,-object.y-object.z) then do rotation using glRotate(...) then again translate to the original intended location using same glTranslate(object.x,object.y,object.z).

Hopes this helps
http://cgmath.blogspot.com/


使用矩阵.这样就可以了
Use matrixes. That made it work


这篇关于OpenGL是否围绕坐标旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆