openGL加载obj文件+绘制大脑表层+高亮染色

2023-11-10 05:30

本文主要是介绍openGL加载obj文件+绘制大脑表层+高亮染色,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

绘制大脑表层并高亮染色的工作是以openGL加载obj文件为基础的,这里是我们用到的原始程序:只能加载一个obj文件的demo。

然而,一个完整的大脑表层是由很多分区组成的,因此我们的程序需要支持两个功能:

  • 同时加载多个obj文件。
  • 每个大脑分区obj文件保持其相对位置。

明白了需求后,我们就可以开始修改代码了~

glmUnitize函数的作用是单位化,也就是把模型通过平移和缩放变换限制到3维坐标系中点为中心的一个单位正方体区域内。所以控制obj显示位置是在glmUnitize()函数中,源代码如下:

 1 /* public functions */
 2 
 3 /* glmUnitize: "unitize" a model by translating it to the origin and
 4  * scaling it to fit in a unit cube around the origin.  Returns the
 5  * scalefactor used.
 6  *
 7  * model - properly initialized GLMmodel structure 
 8  */
 9   GLfloat
10 glmUnitize(GLMmodel* model, GLfloat center[3])
11 {
12   GLuint  i;
13   GLfloat maxx, minx, maxy, miny, maxz, minz;
14   GLfloat cx, cy, cz, w, h, d;
15   GLfloat scale;
16 
17   assert(model);
18   assert(model->vertices);
19 
20   /* get the max/mins */
21   maxx = minx = model->vertices[3 + X];
22   maxy = miny = model->vertices[3 + Y];
23   maxz = minz = model->vertices[3 + Z];
24   for (i = 1; i <= model->numvertices; i++) {
25     if (maxx < model->vertices[3 * i + X])
26       maxx = model->vertices[3 * i + X];
27     if (minx > model->vertices[3 * i + X])
28       minx = model->vertices[3 * i + X];
29 
30     if (maxy < model->vertices[3 * i + Y])
31       maxy = model->vertices[3 * i + Y];
32     if (miny > model->vertices[3 * i + Y])
33       miny = model->vertices[3 * i + Y];
34 
35     if (maxz < model->vertices[3 * i + Z])
36       maxz = model->vertices[3 * i + Z];
37     if (minz > model->vertices[3 * i + Z])
38       minz = model->vertices[3 * i + Z];
39   }
40 
41   /* calculate model width, height, and depth */
42   w = _glmAbs(maxx) + _glmAbs(minx);
43   h = _glmAbs(maxy) + _glmAbs(miny);
44   d = _glmAbs(maxz) + _glmAbs(minz);
45 
46   /* calculate center of the model */
47   cx = (maxx + minx) / 2.0;
48   cy = (maxy + miny) / 2.0;
49   cz = (maxz + minz) / 2.0;
50 
51   /* calculate unitizing scale factor */
52   scale = 2.0 / _glmMax(_glmMax(w, h), d);
53 
54   /* translate around center then scale */
55   for (i = 1; i <= model->numvertices; i++) {
56     model->vertices[3 * i + X] -= cx;
57     model->vertices[3 * i + Y] -= cy;
58     model->vertices[3 * i + Z] -= cz;
59     model->vertices[3 * i + X] *= scale;
60     model->vertices[3 * i + Y] *= scale;
61     model->vertices[3 * i + Z] *= scale;
62   }
63 
64   center[0] = cx;
65   center[1] = cy;
66   center[2] = cz;
67   return scale;
68 }
glmUnitize

这里我们要修改两个内容:

  • 删除改变位置的代码
  • 改变缩放比例,各个obj文件的缩放因子需要保持一致。

这是修改后的代码:

 1 /* public functions */
 2 
 3 /* glmUnitize: "unitize" a model by translating it to the origin and
 4  * scaling it to fit in a unit cube around the origin.  Returns the
 5  * scalefactor used.
 6  *
 7  * model - properly initialized GLMmodel structure 
 8  */
 9   GLfloat
10 glmUnitize(GLMmodel* model, GLfloat center[3])
11 {
12   GLuint  i;
13   GLfloat maxx, minx, maxy, miny, maxz, minz;
14   GLfloat cx, cy, cz, w, h, d;
15   GLfloat scale;
16 
17   assert(model);
18   assert(model->vertices);
19 
20   /* get the max/mins */
21   maxx = minx = model->vertices[3 + X];
22   maxy = miny = model->vertices[3 + Y];
23   maxz = minz = model->vertices[3 + Z];
24   for (i = 1; i <= model->numvertices; i++) {
25     if (maxx < model->vertices[3 * i + X])
26       maxx = model->vertices[3 * i + X];
27     if (minx > model->vertices[3 * i + X])
28       minx = model->vertices[3 * i + X];
29 
30     if (maxy < model->vertices[3 * i + Y])
31       maxy = model->vertices[3 * i + Y];
32     if (miny > model->vertices[3 * i + Y])
33       miny = model->vertices[3 * i + Y];
34 
35     if (maxz < model->vertices[3 * i + Z])
36       maxz = model->vertices[3 * i + Z];
37     if (minz > model->vertices[3 * i + Z])
38       minz = model->vertices[3 * i + Z];
39   }
40 
41   /* calculate model width, height, and depth */
42   w = _glmAbs(maxx) + _glmAbs(minx);
43   h = _glmAbs(maxy) + _glmAbs(miny);
44   d = _glmAbs(maxz) + _glmAbs(minz);
45 
46   /* calculate center of the model */
47   cx = (maxx + minx) / 2.0;
48   cy = (maxy + miny) / 2.0;
49   cz = (maxz + minz) / 2.0;
50 
51   /* calculate unitizing scale factor */
52   //scale = 2.0 / _glmMax(_glmMax(w, h), d);
53   scale = 0.01;
54   /* translate around center then scale */
55   for (i = 1; i <= model->numvertices; i++) {
56    /* model->vertices[3 * i + X] -= cx;
57     model->vertices[3 * i + Y] -= cy;
58     model->vertices[3 * i + Z] -= cz;*/
59     model->vertices[3 * i + X] *= scale;
60     model->vertices[3 * i + Y] *= scale;
61     model->vertices[3 * i + Z] *= scale;
62   }
63 
64   center[0] = cx;
65   center[1] = cy;
66   center[2] = cz;
67   return scale;
68 }
View Code

现在我们要解决同时加载多个obj文件的问题。

  • pModel改为pModel数组,全局变量cnt记录当前加载到哪个obj文件。
  • 遍历obj文件夹下的所有obj文件,并依次加载。

 核心代码如下:

 1                 case 'o':
 2         case 'O':
 3         {
 4             string path="C:\\test";
 5             _finddata_t file_info;
 6             string current_path = path + "/*.obj";
 7             int handle = _findfirst(current_path.c_str(), &file_info);
 8             do
 9             {
10                 
11                 string rt = "C:\\test\\";
12                 string fn= rt + file_info.name;
13                 memset(FileName, '\0', sizeof(FileName));
14                 for (int i = 0; i < fn.length(); i++)
15                 {
16                     FileName[i] = fn[i];
17                 }
18                 if (pModel[cnt] == NULL)
19                 {
20                     pModel[cnt] = glmReadOBJ(FileName);
21                     // Generate normal for the model
22                     glmFacetNormals(pModel[cnt]);
23                     // Scale the model to fit the screen
24                     glmUnitize(pModel[cnt], modelCenter);
25                     // Init the modelview matrix as an identity matrix
26                     glMatrixMode(GL_MODELVIEW);
27                     glLoadIdentity();
28                     glGetDoublev(GL_MODELVIEW_MATRIX, pModelViewMatrix);
29                     cnt++;
30                 //    break;
31                 }
32 
33             } while (!_findnext(handle, &file_info));
34 
35             _findclose(handle);
36         }
37 
38         break;
View Code
 1 void display()
 2 {
 3     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
 4     glMatrixMode(GL_MODELVIEW);
 5     glLoadIdentity();
 6     glTranslated( 0.0, 0.0, -5.0 );
 7     glMultMatrixd( pModelViewMatrix );
 8     for (int i = 0; i < cnt; i++)
 9     {
10         if (pModel[i] != NULL)
11         {
12             glmDraw(pModel[i], GLM_FLAT);
13         }
14     }
15     glutSwapBuffers();
16 }
Display

现在我们要给加载的多个obj文件随机染色。

 1 void Color()
 2 {
 3     srand(unsigned(time(0)));
 4     for (int i = 0; i < maxn; i++)
 5     {
 6         
 7         rr[i] = random(0.0, 0.7);
 8         gg[i] = random(0.0, 0.7);
 9         bb[i] = random(0.0, 0.7);
10         cout << rr[i] << " " << gg[i] << " " << bb[i] << endl;
11     }
12 }
View Code
 1 /// Display the Object
 2 void display()
 3 {
 4     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
 5     
 6     glMatrixMode(GL_MODELVIEW);
 7     glLoadIdentity();
 8 
 9     glTranslated( 0.0, 0.0, -5.0 );
10     glMultMatrixd( pModelViewMatrix );
11     
12     glEnable(GL_COLOR_MATERIAL);
13     //glColorMaterial(GL_FRONT, GL_DIFFUSE);
14     
15     for (int i = 0; i < cnt; i++)
16     {
17         if (pModel[i] != NULL)
18         {
19             glColor3f(rr[i],gg[i],bb[i]);
20             glmDraw(pModel[i], GLM_FLAT|GLM_COLOR);
21         }
22     }
23     glDisable(GL_COLOR_MATERIAL); 
24     glutSwapBuffers();
25 }
View Code

到此为止吗,我们就完成了openGL加载obj文件+绘制大脑表层+高亮染色。点击下载:openGLhighlight.zip

转载于:https://www.cnblogs.com/itcsl/p/6838156.html

这篇关于openGL加载obj文件+绘制大脑表层+高亮染色的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/380621

相关文章

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法

《SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法》本文主要介绍了SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法,具有一定的参考价值,感兴趣的可以了解一下... 目录方法1:更改IDE配置方法2:在Eclipse中清理项目方法3:使用Maven命令行在开发Sprin

spring-boot-starter-thymeleaf加载外部html文件方式

《spring-boot-starter-thymeleaf加载外部html文件方式》本文介绍了在SpringMVC中使用Thymeleaf模板引擎加载外部HTML文件的方法,以及在SpringBoo... 目录1.Thymeleaf介绍2.springboot使用thymeleaf2.1.引入spring

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

SpringBoot项目启动后自动加载系统配置的多种实现方式

《SpringBoot项目启动后自动加载系统配置的多种实现方式》:本文主要介绍SpringBoot项目启动后自动加载系统配置的多种实现方式,并通过代码示例讲解的非常详细,对大家的学习或工作有一定的... 目录1. 使用 CommandLineRunner实现方式:2. 使用 ApplicationRunne

SpringBoot项目删除Bean或者不加载Bean的问题解决

《SpringBoot项目删除Bean或者不加载Bean的问题解决》文章介绍了在SpringBoot项目中如何使用@ComponentScan注解和自定义过滤器实现不加载某些Bean的方法,本文通过实... 使用@ComponentScan注解中的@ComponentScan.Filter标记不加载。@C

springboot 加载本地jar到maven的实现方法

《springboot加载本地jar到maven的实现方法》如何在SpringBoot项目中加载本地jar到Maven本地仓库,使用Maven的install-file目标来实现,本文结合实例代码给... 在Spring Boothttp://www.chinasem.cn项目中,如果你想要加载一个本地的ja

使用Python绘制蛇年春节祝福艺术图

《使用Python绘制蛇年春节祝福艺术图》:本文主要介绍如何使用Python的Matplotlib库绘制一幅富有创意的“蛇年有福”艺术图,这幅图结合了数字,蛇形,花朵等装饰,需要的可以参考下... 目录1. 绘图的基本概念2. 准备工作3. 实现代码解析3.1 设置绘图画布3.2 绘制数字“2025”3.3

使用Python绘制可爱的招财猫

《使用Python绘制可爱的招财猫》招财猫,也被称为“幸运猫”,是一种象征财富和好运的吉祥物,经常出现在亚洲文化的商店、餐厅和家庭中,今天,我将带你用Python和matplotlib库从零开始绘制一... 目录1. 为什么选择用 python 绘制?2. 绘图的基本概念3. 实现代码解析3.1 设置绘图画

最好用的WPF加载动画功能

《最好用的WPF加载动画功能》当开发应用程序时,提供良好的用户体验(UX)是至关重要的,加载动画作为一种有效的沟通工具,它不仅能告知用户系统正在工作,还能够通过视觉上的吸引力来增强整体用户体验,本文给... 目录前言需求分析高级用法综合案例总结最后前言当开发应用程序时,提供良好的用户体验(UX)是至关重要