c++ - 如何设置对象转换、属性?

标签 c++ opengl

我是 OpenGL 的新手,正在做在线类(class)的作业。我的第一个任务是用代码填充煎锅:旋转、缩放、平移、透视、左/右移动、上/下移动。我做到了,但在下一个任务中遇到了问题:设置转换。你能告诉我一个例子或一些链接如何做吗? 任何帮助将不胜感激。

  for (int i = 0 ; i < numobjects ; i++) {
    object* obj = &(objects[i]); 

    // Set up the object transformations 
    // And pass in the appropriate material properties
    // Again glUniform() related functions will be useful

    // Actually draw the object
    // We provide the actual glut drawing functions for you.  
    // Remember that obj->type is notation for accessing struct fields


    if (obj->type == cube) {
      glutSolidCube(obj->size); 
    }
    else if (obj->type == sphere) {
      const int tessel = 20; 
      glutSolidSphere(obj->size, tessel, tessel); 
    }
    else if (obj->type == teapot) {
      glutSolidTeapot(obj->size); 
    }

  }

const int maxobjects = 10 ; 
EXTERN int numobjects ; 
EXTERN struct object {
  shape type ; 
  GLfloat size ;
  GLfloat ambient[4] ; 
  GLfloat diffuse[4] ; 
  GLfloat specular[4] ;
  GLfloat emission[4] ; 
  GLfloat shininess ;
  mat4 transform ; 

} objects[maxobjects];

最佳答案

现代 OpenGL 中的变换是通过在着色器中使用统一矩阵来完成的。基本上,您必须查询转换变量 (glGetUniformLocation) 的统一位置,然后使用 glUniformMatrix4fvobj->transform 成员传递到该位置。 .

对于 Material 参数,工作流程基本相同,但使用适合类型的不同 glUniform* 调用。

关于c++ - 如何设置对象转换、属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32308833/

相关文章:

c++ - 什么 shared_ptr 策略用于异步方案?

c++ - SCons out of source build 仅适用于可执行程序,不适用于目标文件

c++ - 从高度图生成法线贴图?

c++ - glm 数据类型如何通过 OpenGL 缓冲区直接传递给 GPU?

c++ - 对象的内部表示

c++ - 为什么可以将友元函数定义放在类定义中?

xcode - MacOS Mojave Xcode 10 + OpenGL 在初始化窗口后未绘制

c++ - 战俘(1,信息)==南?

c++ - 如何正确跟踪 VBO 和 VAO 以进行清理?

c++修改函数内部的char