- 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/
我想开始使用OpenGl开发图形程序 为了开始,我正在关注 OpenGL 我遇到过使用 GLUT 和不使用 GLUT 进行编程,但作为 OpenGL 的新手,我更加困惑如何使用它? 最佳答案 GLUT
我的应用程序在使用 wglCreateContext 上下文的 Windows 上仅使用 gl/glu。 该应用程序有自己的 key 管理器,现在我喜欢为输入框和选择列表实现 GUI 框架。 我找到了
我正在用 C++ 进行一些 OpenGL 编程。 这是我的代码的一部分: #include #include #include #include #include Properties.
我正在使用 Win32 API 处理 OpenGL。因此我正在使用 wgl (Wiggle)。一切安好。除非我想使用 FREEGLUT 库中的一些形状。例如,茶壶。我正在查看 freeglut 的源代
我正在尝试在 Haskell 中制作图形,并且一直在使用 Haskell.org 的教程 (http://www.haskell.org/haskellwiki/OpenGLTutorial1)。但是
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
我正在用 C 语言开发一个带有 GLUT 的应用程序。我有一个基本的 GLUT Canvas ,我可以在其中绘制 OpenGL 内容。 当其他 X11 窗口与 GLUT Canvas 重叠然后被删除时
我最近重新设计了我的应用程序,以便在 Idle 函数中完成数据收集。该程序从 argv[1] 读取文件并从该文件中解析出命令。这部分似乎都可以正常工作,并且解析的数据是准确的。 问题出在显示功能上。命
我遇到了一个奇怪的问题,当我以零延迟调用它时,glutTimerFunc 似乎随机停止工作。 这是我的代码: #include #include #include int x = 0; void
我正在处理一项作业,要求我仅使用隐藏线渲染图像。我在网上找到了有关如何删除隐藏线的内容,但我一直未能成功地弄清楚如何反转代码以使其仅绘制隐藏线。 通常,当我访问 Stack Overflow 时,我喜
我想将一个对象(立方体)乘以一定的数字,在本例中假设为 25,我确实有一个立方体的代码,并且它可以工作,但我不知道如何制作更多。我是 GLUT 的新人。 #include #include #in
嘿, 我正在尝试使用 C++ 使用 OpenGL 和 CUDA 创建场景,但是当我尝试传递函数进行初始化时收到错误消息。这是我的标题: class SimpleScene { public:
我有一个我在 6 个多月前编写的程序,它使用了 GLUT 库。 6 个月前我写的时候它运行得非常好。从那以后我就没有使用过它,也没有以任何方式编辑过代码。 该程序有一些立方体以一些随机速度四处漂浮,它
我有一个过剩的问题,我想要一个回调来知道用户何时移动我的应用程序的窗口,但我没有在过剩中发现任何东西。 我的目标是只在 Windows 上制作一个应用程序,是否可以使用 MFC 保留我的过剩代码来做到
我正在尝试在 GLUT c++ 上编写“Classic Snake”,但遇到了一些问题。 例如,当我的蛇向右移动时,我将目的地更改为向上,同时将其更改为向右蛇进入自身内部(我禁止将目的地从右更改为左)
我正在尝试使用以下摄像机代码实现摄像机控制的场景:http://www.swiftless.com/tutorials/opengl/camera2.html 还有书中的风景代码,随机生成地形。通过使
上周,我尝试了几个在阅读 GLUT 教程时发现的示例,一切都运行良好。 现在,当我重试这些相同的示例时,我得到了一个奇怪的行为:我的 GLUT 窗口显示了窗口所在位置的桌面部分(所以如果 GLUT 窗
我正在尝试使用以下方法将纹理渲染到平面: unsigned char image[HEIGHT][WIDTH][3]; ... GLuint textureId; glGenTextures(1, &
我编写了模拟选择排序操作的程序。我添加了函数 myKeyboard 来退出程序,但是由于使用函数 sleep() 来模拟动画,myKeyboard 仅在排序完成后才起作用。有什么方法可以替换函数 sl
当我编译后尝试运行这段代码时,我得到的只是一个没有内容的窗口边框,那么错误在哪里? 注意:我用的是ubuntu和gcc编译 gcc -lglut Simple.c -o Simple The out
我是一名优秀的程序员,十分优秀!