如何在Opengl中写入文件 [英] How to write file in Opengl

查看:61
本文介绍了如何在Opengl中写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用键盘设置Z值
现在,我有新问题
如何使用fwrite将输出顶点值写入文件?
正常
浮点顶点[] [] = {};
浮动边缘[] [] = {};
然后使用for和glVertex3dv()绘制框
如果输出文件,则读取文件->设置顶点,边值?
感谢好朋友

I use the keyboard to set Z value
Now,I have new problem
How to use fwrite output vertex value into file ?
normal
float vertex[][]={};
float edge[][]={};
then use for and glVertex3dv() to draw box
if output file then read file->set vertex,edge value?
thanks to good friends

推荐答案

那是什么问题?
您已经存储了顶点,对吗?

只需定义文件的外观和编写方式,逐步浏览列表中的顶点即可.

例如

So what''s the problem?
You have your vertices stored, right?

Just define the way you want the file to look and write it, stepping through the vertices in your list.

E.g

struct vert{
 double x,y,z;
}

FILE *fp;
vert *vertList = _whateverIsAppropriate_;
int numberfVertices = _whateverIsAppropriate_
fp = fopen(outputFilename, "w");
fwrite(fp, (int*)&numberOfVertices, sizeof(int));
for (curVertex=0; i<numberofvertices;>{ 
 fwrite(fp, vertList[curVertex].x, sizeof(double));
 fwrite(fp, vertList[curVertex].y, sizeof(double));
 fwrite(fp, vertList[curVertex].z, sizeof(double));
}
fclose(fp);



您已经以不同的方式存储了顶点,当然必须根据需要访问它们.与边缘相同-您必须确定一种将其存储在文件中的方法,然后添加代码来执行此操作. -也许您需要存储顶点,边和多边形?

昨天和今天,我一直在使用(lightwave)obj文件格式.也许看一下(*)一些示例文件或(*)格式规范将对阅读有所帮助.例如,考虑一个简单的多维数据集的.OBJ文件:



You''ve stored your vertices differently, and will of course have to access them as required. Same with the edges - you''ll have to determine a way of storing them in the file and then add code to do that too. - Perhaps you''ll need to store verts, edges and polys?

I''ve been playing with the (lightwave)obj file format yesterday and today. Perhaps having a look at (*)some example files or (*) the format specification would be helpful reading. As an example, consider the .OBJ file for a simple cube:

# Blender3D v249 OBJ File:
# www.blender3d.org
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 1.000000 1.000000
v 0.999999 -1.000001 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
usemtl Material
s off
f 1 2 3 4
f 5 8 7 6
f 1 5 6 2
f 2 6 7 3
f 3 7 8 4
f 5 1 4 8


嗯,加载保存的数据的过程(或多或少)与保存过程相反.如果将其另存为MD2或3DS兼容格式,则可以使用MD2或3DS加载程序进行加载.我提供的代码不是什么种类的代码.当我输入最后一个答案时,我编排了格式. :)

(它仅进入IDE 5秒钟,以便我可以正确缩进-我确定我在某处错过了一个&运算符或两个).

为了加载以我之前指定的方式保存的数据,以下将是有效的:

Well, the process of loading the saved data is (more-or-less) the reverse of the saving process. If you save it as an MD2 or 3DS compatible format, you may load with and MD2 or 3DS loader. The code I presented is nothing of the sort. I made up the format as I typed my last answer. :)

(It only went into the IDE for 5 seconds so that I could properly indent it - I''m sure I''ve missed an & operator or two somewhere)

In order to load the data that was saved in the way I specified earlier, the following would be effective:

struct vert
{
    double x,y,z;
}

FILE *fp;
vert *vertList;
int numberfVertices;
fp = fopen(inputFilename, "r");
fread(fp, sizeof(int), 1, &numberOfVertices)
vertList = (vert*)malloc(numberOfVertices * sizeof(vert));

for (curVertex=0; curVertex<numberOfVertices; curVertex++)
{
    fread(fp, sizeof(double), 1, vertList[curVertex].x;
    fread(fp, sizeof(double), 1, vertList[curVertex].y;
    fread(fp, sizeof(double), 1, vertList[curVertex].z;
}

fclose(fp);




自然地,您可能会发现不需要对每个顶点执行3次写入/读取-一个语句可以简单地在一个语句中从磁盘读取/写入整个结构:




Naturally, you may observe that there is no need to perform 3 writes/reads per vertex - one may simply read/write the entire struct from/to disk in a single statement:

fwrite(fp, &vertList[curVertex], sizeof(vert));
fread(fp, sizeof(vert), 1, &vertList[curVertex]);



我能闻到作业的味道吗?



I can''t smell homework can i?


谢谢,好朋友
抱歉,我迟到了
很棒,简单.
那么,当读取文件时,我可以使用代码md2,3ds加载吗?
还是简单的是通过fread更改fwrite然后设置vertList [curVertex]?
问题是如何很好地加载?
thanks,good friends
sorry,I late
it''s wonderful,simple.
So,when read file,I can use code md2,3ds load ?
or simple is change fwrite by fread then set vertList[curVertex] ?
Problem is how to nice load ?


这篇关于如何在Opengl中写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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