gluPerspective and gluLookAt 在OpenGL-ES中的替代

Code:
void perspective(double fovy, double aspect, double zNear, double zFar)
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

   double xmin, xmax, ymin, ymax;

   ymax = zNear * tan(fovy * M_PI / 360.0);
   ymin = -ymax;
   xmin = ymin * aspect;
   xmax = ymax * aspect;


   glFrustumf(xmin, xmax, ymin, ymax, zNear, zFar);
   
   
   
	glMatrixMode(GL_MODELVIEW);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	

	glDepthMask(GL_TRUE);
}
Code:
void orthographic()		   
{                   
	glMatrixMode(GL_PROJECTION);		   
	glLoadIdentity();					   
	glOrthof( 0, 320, 480, 0, 1, 0 );				
	glMatrixMode(GL_MODELVIEW);				
	glLoadIdentity();					
	glDepthMask(GL_FALSE);
}
Code:
	perspective(85, 480/320, 0.01,10);
Code:
 void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez,

           GLfloat centerx, GLfloat centery, GLfloat centerz,

           GLfloat upx, GLfloat upy, GLfloat upz)

 {

    GLfloat m[16];

    GLfloat x[3], y[3], z[3];

    GLfloat mag;

 

    /* Make rotation matrix */

 

    /* Z vector */

    z[0] = eyex - centerx;

    z[1] = eyey - centery;

    z[2] = eyez - centerz;

    mag = sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);

    if (mag) {                   /* mpichler, 19950515 */

       z[0] /= mag;

       z[1] /= mag;

       z[2] /= mag;

    }

 

    /* Y vector */

    y[0] = upx;

    y[1] = upy;

    y[2] = upz;

 

    /* X vector = Y cross Z */

    x[0] = y[1] * z[2] - y[2] * z[1];

    x[1] = -y[0] * z[2] + y[2] * z[0];

    x[2] = y[0] * z[1] - y[1] * z[0];

 

    /* Recompute Y = Z cross X */

    y[0] = z[1] * x[2] - z[2] * x[1];

    y[1] = -z[0] * x[2] + z[2] * x[0];

    y[2] = z[0] * x[1] - z[1] * x[0];

 

    /* mpichler, 19950515 */

    /* cross product gives area of parallelogram, which is < 1.0 for

     * non-perpendicular unit-length vectors; so normalize x, y here

     */

 

    mag = sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);

    if (mag) {

       x[0] /= mag;

       x[1] /= mag;

       x[2] /= mag;

    }

    mag = sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);

    if (mag) {

       y[0] /= mag;

       y[1] /= mag;

       y[2] /= mag;

    }

 

 #define M(row,col)  m[col*4+row]

    M(0, 0) = x[0];

    M(0, 1) = x[1];

    M(0, 2) = x[2];

    M(0, 3) = 0.0;

    M(1, 0) = y[0];

    M(1, 1) = y[1];

    M(1, 2) = y[2];

    M(1, 3) = 0.0;

    M(2, 0) = z[0];

    M(2, 1) = z[1];

    M(2, 2) = z[2];

    M(2, 3) = 0.0;

    M(3, 0) = 0.0;

    M(3, 1) = 0.0;

    M(3, 2) = 0.0;

    M(3, 3) = 1.0;

 #undef M

    glMultMatrixf(m);

 

    /* Translate Eye to Origin */

    glTranslatef(-eyex, -eyey, -eyez);

 

 }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在QT使用OpenGL渲染一个OBJ模型,可以按照以下步骤进行: 1. 创建一个QT项目,选择OpenGL窗口模板。 2. 在OpenGL窗口初始化OpenGL和QT的集成,可以使用以下代码: ```c++ void GLWidget::initializeGL() { initializeOpenGLFunctions(); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_COLOR_MATERIAL); } void GLWidget::resizeGL(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, (double)w / (double)h, 0.01, 100.0); glMatrixMode(GL_MODELVIEW); } void GLWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -5.0f); glRotatef(rotationX, 1.0, 0.0, 0.0); glRotatef(rotationY, 0.0, 1.0, 0.0); drawModel(); } ``` 3. 加载OBJ模型,并将其渲染到OpenGL窗口,可以使用以下代码: ```c++ void GLWidget::loadModel(QString filename) { model = glmReadOBJ(filename.toStdString().c_str()); glmUnitize(model); glmFacetNormals(model); glmVertexNormals(model, 90); } void GLWidget::drawModel() { if (model != NULL) { glmDraw(model, GLM_SMOOTH); } } ``` 4. 在QT的主窗口添加一个QPushButton,点击按钮后调用loadModel()函数加载OBJ模型。 ```c++ void MainWindow::on_btnLoad_clicked() { QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), ".", tr("OBJ Files (*.obj)")); if (!filename.isEmpty()) { ui->glWidget->loadModel(filename); } } ``` 5. 运行程序,点击按钮加载OBJ模型并在OpenGL窗口显示。 以上就是在QT使用OpenGL渲染OBJ模型的基本步骤,需要注意的是,这里使用了OpenGL Utility Toolkit(GLUT)和OpenGL Mathematics(GLM)库来帮助加载OBJ模型和进行矩阵变换等操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值