- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以我有一个小的.obj
解析器,可以解析顶点并将其绘制在屏幕上:
void loadObj(char *fname)
{
FILE *fp;
int read;
GLfloat x, y, z;
char ch;
_model = glGenLists(1);
fp = fopen(fname, "r");
if (!fp)
{
printf("can't open file %s\n", fname);
exit(1);
}
glPointSize(2.0);
glNewList(_model, GL_COMPILE);
{
glPushMatrix();
glBegin(GL_POINTS);
while (!(feof(fp)))
{
read = fscanf(fp, "%c %f %f %f", &ch, &x, &y, &z);
if (read == 4 && ch == 'v')
{
glVertex3f(x, y, z);
}
}
glEnd();
}
glPopMatrix();
glEndList();
fclose(fp);
}
void drawModel()
{
glPushMatrix();
glTranslatef(0, 0.00, 0.00);
glColor3f(1.0, 0.23, 0.27);
glScalef(10, 10, 10);
glRotatef(_modelRot, 0, 1, 0);
glCallList(_model);
glPopMatrix();
}
重点是,输出只是顶点,像这样:
我如何修改它以在不添加第 3 方库的情况下至少显示点之间的 3D 形式?这是我正在寻找的东西:
谢谢。如果需要,可以提供更多代码。
最佳答案
您的对象解析器和渲染调用未完成。
.obj 文件的第一部分包含顶点数据。包括位置、纹理坐标和法线数据。第二部分包含信息,即顶点如何相互连接。
# List of geometric vertices, with (x,y,z[,w]) coordinates, w is optional and defaults to 1.0.
v 0.123 0.234 0.345 1.0 # first vertex
v ... # second vertex
...
# List of texture coordinates, in (u, v [,w]) coordinates, these will vary between 0 and 1, w is optional and defaults to 0.
vt 0.500 1 [0] # first texture coordinate
vt ... # second
...
# List of vertex normals in (x,y,z) form; normals might not be unit vectors.
vn 0.707 0.000 0.707 # first normal
vn ... # second
...
# Parameter space vertices in ( u [,v] [,w] ) form; free form geometry statement ( see below )
vp 0.310000 3.210000 2.100000
vp ...
...
# Polygonal face element (see below)
f 1 2 3 # face of the first, second and third vertex
f 3/1 4/2 5/3 # face of the third, fourth and fifth vertex with the first second and third texture coordinate
f 6/4/1 3/5/3 7/6/5 # face of sixth, thrid and seventh vertex, fourth, fifth and sixth texture coordinate and first thrid and fifth normal
f 7//1 8//2 9//3 # similar to the line over but without texture coordinates
f ...
...
列表取自这里: https://en.wikipedia.org/wiki/Wavefront_.obj_file
OpenGl 无法以 .obj 文件允许的方式将顶点数据映射在一起。所以你必须制作一个包含所有顶点的数据结构,一个用于所有纹素,一个用于所有法线。
然后您可以解析面并构建顶点数据,通过收集正确的位置、纹素和法线来构建完整的面。
在那一步之后,您可以使用此组合通过 GL_TRIANGLES 或 GL_QUADS 绘制图元。
这是我不久前为 OpenGl 4.x 编写的加载器
#pragma once
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <glm/glm.hpp>
#include <GL/glew.h>
#include "System/Log.hpp"
#include "Graphics/Primitives/Object.hpp"
#include "Graphics/Primitives/Material.hpp"
namespace Loader {
template <class ObjectT = Graphics::Primitives::Object>
class ObjectLoader {
private:
const std::string fullPath;
std::vector<Graphics::Primitives::VertexGroup> objects;
std::map<std::string, Graphics::Primitives::Material> materials;
std::string prefixPath;
std::vector<glm::vec3> verticies;
std::vector<glm::vec2> texels;
std::vector<glm::vec3> normals;
std::vector<glm::uvec3> faces;
std::vector<glm::vec3> index_verticies;
std::vector<glm::vec2> index_texels;
std::vector<glm::vec3> index_normals;
Graphics::Primitives::Material material;
std::map<std::string, GLuint> indexDb;
std::string getPrefixPath();
std::vector<std::string> explode(std::string str, char delimiter = ' ');
glm::vec3 stringsToVec(const std::vector<std::string> parts, unsigned int begin);
void loadMaterial(std::string fileName);
Graphics::Primitives::VertexGroup flush();
public:
ObjectLoader(std::string fileName);
ObjectT load();
};
template <class ObjectT>
std::string ObjectLoader<ObjectT>::getPrefixPath() {
unsigned int lastSlash = 0;
for(int i = fullPath.size(); i > 0; i--) {
if(fullPath[i] == '/') {
lastSlash = i;
break;
}
}
std::string prefixPath = fullPath.substr(0, lastSlash);
prefixPath += "/";
return prefixPath;
}
template <class ObjectT>
std::vector<std::string> ObjectLoader<ObjectT>::explode(std::string str, char delimiter) {
std::vector<std::string> result;
std::stringstream data(str);
std::string line;
while(std::getline(data,line,delimiter)) {
result.push_back(line);
}
return result;
}
template <class ObjectT>
glm::vec3 ObjectLoader<ObjectT>::stringsToVec(const std::vector<std::string> parts, unsigned int begin) {
glm::vec3 result;
if(parts.size() > begin + 2) {
result.x = std::atof(parts[begin].c_str());
result.y = std::atof(parts[begin+1].c_str());
result.z = std::atof(parts[begin+2].c_str());
} else
if(parts.size() > begin) {
result.x = std::atof(parts[begin].c_str());
result.y = std::atof(parts[begin].c_str());
result.z = std::atof(parts[begin].c_str());
}
return result;
}
template <class ObjectT>
void ObjectLoader<ObjectT>::loadMaterial(std::string fileName) {
std::ifstream materialFile(fileName);
std::string line;
Graphics::Primitives::Material material;
std::string materialName;
bool initialised = false;
while(std::getline(materialFile, line)) {
//System::Log::msg << " " << line << std::endl;
std::vector<std::string> parts = explode(line);
if(parts.size() > 0) {
if(parts[0] == "newmtl") {
if(initialised) {
materials.insert(std::make_pair(materialName, material));
System::Log::msg << "Loaded material: " << materialName << std::endl;
}
materialName = parts[1];
initialised = true;
material = Graphics::Primitives::Material();
} else
if(parts[0] == "Ns") {
if(parts.size() > 1) {
material.specularExponent = std::atof(parts[1].c_str());
}
} else
if(parts[0] == "Ka") {
material.ambientReflectance = stringsToVec(parts,1);
} else
if(parts[0] == "Kd") {
material.diffuseReflectance = stringsToVec(parts,1);
} else
if(parts[0] == "Ks") {
material.specularReflectance = stringsToVec(parts,1);
} else
//if(parts[0] == "Ke") {
//No idea what this value means, maybe transmission filter aka Tf?
//} else
if(parts[0] == "Ni") {
//Optical density ignored for now
} else
if(parts[0] == "d") {
material.dissolve = std::atof(parts[1].c_str());
} else
if(parts[0] == "map_Ka") {
material.textureStack.push_back(Graphics::Ogl::loadTexture(parts[1]));
} else
if(parts[0] == "map_Kd") {
material.textureStack.push_back(Graphics::Ogl::loadTexture(parts[1]));
} else
if(parts[0] == "map_Ks") {
material.textureStack.push_back(Graphics::Ogl::loadTexture(parts[1]));
}
}
}
materials.insert(std::make_pair(materialName, material));
System::Log::msg << "Loaded material: " << materialName << std::endl;
}
template <class ObjectT>
Graphics::Primitives::VertexGroup ObjectLoader<ObjectT>::flush()
{
Graphics::Ogl::VertexArrayObject vao = Graphics::Ogl::makeVertexArrayObject(
std::vector<Graphics::Ogl::ArrayBufferObject>({
Graphics::Ogl::makeArrayBufferObject(index_verticies),
Graphics::Ogl::makeArrayBufferObject(index_normals),
Graphics::Ogl::makeArrayBufferObject(index_texels),
Graphics::Ogl::makeIndexBufferObject(faces)
})
);
System::Log::msg << "Flushing buffers: vertecies(" << index_verticies.size()
<< "), texels(" << index_texels.size()
<< "), normals(" << index_normals.size()
<< "), faces(" << faces.size() << ")" << std::endl;
Graphics::Primitives::VertexGroup obj(vao, material);
glCheckError();
faces.clear();
index_verticies.clear();
index_texels.clear();
index_normals.clear();
indexDb.clear();
return obj;
}
template <class ObjectT>
ObjectLoader<ObjectT>::ObjectLoader(std::string fileName) :
fullPath(fileName),
prefixPath(getPrefixPath())
{}
template <class ObjectT>
ObjectT ObjectLoader<ObjectT>::load() {
System::Log::msg << "Loading Object from file: " << fullPath << std::endl;
std::ifstream objectFile(fullPath);
std::string line;
while(std::getline(objectFile,line)) {
//System::Log::msg << line << std::endl;
if(line[0] == '#' || line[0] == 'o' || line[0] == 'g') {
continue;
}
std::vector<std::string> substrs = explode(line);
if(substrs.size() == 0) {
continue;
}
if( substrs[0] == "v") {
//Add new Vertex to index buffer
glm::vec3 vertex;
if(substrs.size() > 3) {
vertex.x = std::stof(substrs[1]);
vertex.y = std::stof(substrs[2]);
vertex.z = std::stof(substrs[3]);
verticies.push_back(vertex);
} else {
System::Log::err << "Vertex with less than 3 coordinates." << std::endl;
}
} else if(substrs[0] == "vt") {
//Add new Texel to index buffer
glm::vec2 texel;
if(substrs.size() > 2) {
texel.x= std::stof(substrs[1]);
texel.y = std::stof(substrs[2]);
texels.push_back(texel);
} else {
System::Log::err << "Texel with less than 2 coordinates." << std::endl;
}
} else if(substrs[0] == "vn") {
//Add new Normal to index buffer
glm::vec3 normal;
if(substrs.size() > 3) {
normal.x = std::stof(substrs[1]);
normal.y = std::stof(substrs[2]);
normal.z = std::stof(substrs[3]);
normals.push_back(normal);
} else {
System::Log::err << "Normal with less than 3 coordinates." << std::endl;
}
} else if(substrs[0] == "f") {
if(texels.size() == 0) texels.push_back(glm::vec2(0,0));
if(normals.size() == 0) normals.push_back(glm::vec3(0,0,0));
//Lookup in index db;
glm::uvec3 face;
for(unsigned int faceIndex = 1; faceIndex < 4; faceIndex++) {
std::string vtn = substrs[faceIndex];
try {
//Try to find index combination in db
face[faceIndex-1] = indexDb.at(substrs[faceIndex]);
//Index found
} catch (std::exception e) {
//Index not found, now to the hard part
//Create new index in indexDb
GLuint newFace = indexDb.size();
face[faceIndex-1] = newFace;
indexDb.insert(std::make_pair(substrs[faceIndex], newFace));
//Create new vtn triple in buffers
std::vector<std::string> components = explode(substrs[faceIndex],'/');
if(components[1].size() == 0) components[1] = "0";
if(components[2].size() == 0) components[2] = "0";
auto clipValue = [](std::string& number) -> GLuint {
GLuint result = std::atoi(number.c_str());
if(result > 0) result--;
return result;
};
const GLuint vi = clipValue(components[0]);
const GLuint ti = clipValue(components[1]);
const GLuint ni = clipValue(components[2]);
if(verticies.size() > vi) {
index_verticies.push_back(verticies[vi]);
} else {
System::Log::err << "Error: Invalid vertex index. (Index="<< vi <<", LoadedVerticies=" << verticies.size() << ")" << std::endl;
}
if(texels.size() > ti) {
index_texels.push_back(texels[ti]);
}
if(normals.size() > ni) {
index_normals.push_back(normals[ni]);
}
}
}
//Add new Face to Mesh
faces.push_back(face);
//System::Log::msg << "Face: " << face.x << " " << face.y << " " << face.z << std::endl;
} else if(substrs[0] == "usemtl") {
//Flush last mesh
if(faces.size() > 0) {
objects.push_back(flush());
}
//Use new material
material = materials.at(substrs[1]);
System::Log::msg << "Use Material:" << substrs[1] << std::endl;
} else if(substrs[0] == "s") {
//Smoothing
//TODO:
} else if(substrs[0] == "mtllib") {
//Load new materials
loadMaterial(prefixPath+substrs[1]);
} else {
System::Log::err << "Unknown prefix in file" << std::endl;
}
}
if(faces.size() > 0) {
objects.push_back(flush());
}
System::Log::msg << "Done loading object." << std::endl;
return ObjectT(objects);
}
} // End of namespace Loader
关于c++ - .obj 解析器 + 渲染 GLUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42600709/
我正在尝试创建键值对并推送到数组,但我得到的只是 [Obj Obj]、[Obj Obj]。我创建了一个传递名称和值的函数,用于将键和值分配给 JavaScript 对象。这是我的代码。不确定我错过了什
似乎在 for 循环的上下文中,关于对象的语法发生了变化。 为什么 console.log() 不应该运行相同的东西?第一个按预期运行,第二个呈现错误“steve is not defined”: v
在 Ady Osmani 的 blog post关于 js 中的命名空间,他提到了 5 个常见做法来测试先前定义的命名空间/对象是否存在以防止覆盖。我在这里复制我关注的 3 个: var myAppl
有没有办法(我怀疑它涉及继承和多态)来区分OBJ o, OBJ& o, const OBJ& o?我希望在 3 个不同的程序中使用相同的代码,并使用相同的方法名称调用不同的方法。 int main()
我正在寻找一个正则表达式来分割这种内容: obj.method(obj.attr,obj.attr2) 我希望拆分返回一个数组: ["obj", "method(obj.attr, obj.attr2
我想知道这些方法中哪种更好: var Obj = Obj || {}; 或 if (Obj === undefined || typeof Obj !== 'object') { Obj = {}
我正在尝试将一个值推送到数组的属性,如下所示 var obj = {}; obj.a = (obj.a || []).push(10); console.log( typeof obj.a );
为了避免误解,我们首先要就某些词语的含义达成一致。以下含义并非普遍接受的含义,我仅建议将它们作为此问题的背景。 function -- Function 的一个实例。它有一个与其关联的过程。 obje
我总是不确定哪个是正确的以及该使用哪个。 通常我会进行(obj == null)检查。我认为最好直接问。 我应该使用以下哪一项: if (obj == null) { alert(
我正在处理一些使用 pygraph 模块的类,当我使用 add_node() 方法时,它总是出现“node xxx already in graph”。所以我尝试使用 deepcopy() 创建一个新
在 this page您可以看到以下示例,了解如何实现数组的indexOf: if (!Array.prototype.indexOf) { Array.prototype.indexOf = f
(1) 和 (2) 之间是否存在任何重要差异(语义、性能相关等)? var obj = obj || {}; var obj = Object(obj); 上下文。第一个是我在 Django 的模板和
我想知道 obj !== obj 什么时候可以为真? 这是我在书上看到的一行代码,我很纳闷。 var result = class2type[(obj == null || obj !== obj)]
我有时会看到这种模式...... obj.method.call(obj, arg) 我不明白为什么它不同于... obj.method(arg) 为什么要使用第一种模式? 我的天啊,似乎引起了很
我刚刚在一段 React 代码中发现了以下结构(名称已更改): 据我了解,bind 只是执行相应的函数,并将函数的 this 设置为第一个参数,并向其传递更多参数。由于 func 已经是我们想要的
当我们查看Underscore.js源码时,我们可以看到如下内容: _.isObject = function (obj) { return obj === Object(obj);
我在将项目发布到本地系统时收到此错误 Copying file obj\Debug\build.force to obj\Release\Package\PackageTmp\obj\Debug\bu
我有一个类型为 Expression> 的现有表达式;它包含类似 cust => cust.Name 的值. 我还有一个父类,其字段类型为 T .我需要一个接受上述作为参数并生成一个以父类 ( TMo
我在当前目录中有 add.c sub.c 并通过 makefile 编译它们。 我做了以下事情: program 1: objs=$(patsubst %.cpp, %.o, $(wildcard *
这个问题在这里已经有了答案: Is there a difference between copy initialization and direct initialization? (9 个回答)
我是一名优秀的程序员,十分优秀!