编辑:你可能想从"编辑3"开始,因为我已经解决了很多这个问题
这是我应用于icosphere的普通立方体贴图的屏幕截图:

我的立方体贴图icosphere的切线是使用以下代码生成的.m_indices在一个std::vector索引中std::vector的顶点m_vertices.
std::vector<glm::vec3> storedTan(m_vertices.size(),glm::vec3(0,0,0));
// tangents
for(int i = 0; i < m_indices.size(); i+=3)
{
int i1 = m_indices[i];
int i2 = m_indices[i+1];
int i3 = m_indices[i+2];
VertexData v1 = m_vertices[i1];
VertexData v2 = m_vertices[i2];
VertexData v3 = m_vertices[i3];
glm::vec3 p1 = glm::vec3(v1.position[0],v1.position[1],v1.position[2]);
glm::vec3 p2 = glm::vec3(v2.position[0],v2.position[1],v2.position[2]);
glm::vec3 p3 = glm::vec3(v3.position[0],v3.position[1],v3.position[2]);
glm::vec3 t1 = glm::vec3(v1.tcoords[0],v1.tcoords[1],v1.tcoords[2]);
glm::vec3 t2 = glm::vec3(v2.tcoords[0],v2.tcoords[1],v2.tcoords[2]);
glm::vec3 t3 = glm::vec3(v3.tcoords[0],v3.tcoords[1],v3.tcoords[2]);
std::function<glm::vec2(glm::vec3)> get_uv = [=](glm::vec3 STR)
{
float sc, …Run Code Online (Sandbox Code Playgroud) 我有一个班里有一个std::variant.此std::variant类型仅允许保存特定的类型列表.
我有模板函数,允许类的用户插入各种值std::unordered_map,映射包含此变体类型的值.即,如果用户的类型在特定的类型列表中,则仅允许用户插入值.但是,我不希望用户能够自己定义这个类型列表.
class GLCapabilities
{
public:
using VariantType = std::variant<GLint64>; // in future this would have other types
template <typename T>
std::enable_if_t<???> AddCapability(const GLenum parameterName)
{
if(m_capabilities.count(parameterName) == 0)
{
/*... get correct value of type T ... */
m_capabilities.insert(parameterName,value);
}
}
template<typename T>
std::enable_if_t<???,T> GetCapability(const GLenum parameterName) const
{
auto itr = m_capabilities.find(parameterName);
if(std::holds_alternative<T>(*itr))
return std::get<T>(*itr);
return T;
}
private:
std::unordered_map<GLenum,VariantType> m_capabilities;
};
Run Code Online (Sandbox Code Playgroud)
你会在上面看到哪里有???,我怎么检查?一些组合std::disjunction与std::is_same?
喜欢
std::enable_if<std::disjunction<std::is_same<T,/*Variant Types???*/>...>> …Run Code Online (Sandbox Code Playgroud) 以下是我能得到的最小例子.它确实需要在单独的文件中,因为这似乎是导致分段错误错误的原因.
我正在使用带有Asio独立1.10.6的Mingw x32 4.8.1.我还测试了TDM GCC 4.7.1和Mingw x64 4.8.1.Windows下的所有这些都产生相同的段错误.在Linux下使用最新版本的GCC没有这样的问题.
编辑:我刚刚完成了对Mingw-w64 5.2.0(它的Mingw-Builds版本)的测试.同样的问题.
我还测试了在新的虚拟机上使用TDM GCC 4.8.1进行编译并获得相同的段错误.编辑:我现在也在使用TDM GCC 4.8.1的完全不同的机器上进行了测试
在我使用的所有情况下-std=c++11,-g和-Wall.我也编译了-g并得到了相同的结果.我需要C++ 11标志,因为我不想依赖boost,就像asio一样.
在单个main.cpp文件中使用以下代码没有问题,程序似乎按预期运行.但是,如果我把每一个类到它自己*.hpp和*.cpp文件,我得到的一个segfault Server类的构造函数.
我进一步回去把所有东西都放回去main.cpp,开始逐一移动每个班级.segfault在最后一个类之后开始出现,Client被放入它自己的文件中.
另外,当我将所有类放入一个文件并移动它们等时,我确保任何不需要的目标文件都没有链接到我的.exe.
这段代码开始变得更大,但它已经缩小到了这个范围.
Server.hpp
#ifndef SERVER_HPP_INCLUDED
#define SERVER_HPP_INCLUDED
#include <string>
#include <memory>
#define ASIO_STANDALONE
#include <asio.hpp>
using namespace asio::ip;
namespace network
{
class Server
{
public:
Server(asio::io_service& ioService, uint16_t port);
private:
tcp::acceptor m_acceptor;
};
}
#endif // SERVER_HPP_INCLUDED
Run Code Online (Sandbox Code Playgroud)
Server.cpp
#include "Server.hpp" …Run Code Online (Sandbox Code Playgroud) 理想情况下,我想要做的是绘制一个四边形并让GLSL处理实际网格线的创建.
在我的尝试到目前为止顶点着色器:
#version 400
layout (location = 0) in vec4 in_position;
layout (location = 2) in vec3 in_UV;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
smooth out vec3 ex_UV;
smooth out vec3 ex_originalPosition;
void main()
{
gl_Position = projection * view * model * in_position;
ex_UV = in_UV;
ex_originalPosition = vec3(in_position.xyz);
}
Run Code Online (Sandbox Code Playgroud)
模型矩阵将四边形向上缩放一些大数,例如10,000.
#version 400
layout (location = 0) out vec4 color;
smooth in vec3 ex_UV;
smooth in vec3 ex_originalPosition;
uniform vec4 lineColor;
void main(void)
{
if(fract(ex_UV.x / …Run Code Online (Sandbox Code Playgroud) 通常,我想大多数人会使用int所有内容,并且有时会unsigned int在需要时使用它们。short int有时您可能会使用,可能用于网络流量等。
但是最近我已经开始std::size_t用于索引STL容器(应该如此),然后我开始std::uint8_t在创建四个8位色值(RGBA)而不是a的结构时找到用处char,因为它可以对我来说更有意义(这是一个数字,不是字符类型,也不是8位值,它是0-255之间的8位数字)。
然后在进行一些网络编程时,我发现我想确保传递的某些值是16位。根据https://en.cppreference.com/w/cpp/language/types,我不能假设a short int是16位,而只能是至少 16位。所以我找到了一个用途std::int16_t。
这使我逐渐开始在所有地方使用固定宽度类型。这使我真正考虑了我需要什么,需要多少范围等,不管它是否会变成负值。
所以现在,int我的代码中几乎没有零发生。
我认为这有三个原因:
但是我担心这会降低性能,因为您通常的消费级CPU会以std::int32_t更好的本机宽度类型运行。
那么,什么时候应该使用固定宽度类型,它们如何影响性能?
我有一个函数在某处调用x,返回一个已知值并具有已知参数:
int x(int y);
Run Code Online (Sandbox Code Playgroud)
我还有其他地方,我想创建一个容器来包含n这个函数的调用.然后我想多次执行它.
问题是,我不想依赖它作为int返回类型.我需要在编译时推断出返回类型.就像是:
std::vector<result_of<x(int)>::type> results;
Run Code Online (Sandbox Code Playgroud)
但我不想指定参数值,因为它们是静态的.
我有这个代码,我的期望是()基于模板参数的类型会有两个不同版本的运算符.
#include <string>
#include <type_traits>
template<typename T>
struct Impl
{
std::enable_if_t<!std::is_pointer<T>::value,T> operator()(const std::string& key, int node)
{
return static_cast<T>();
}
std::enable_if_t<std::is_pointer<T>::value,T> operator()(const std::string& key, int node)
{
return new T();
}
};
int main()
{
}
Run Code Online (Sandbox Code Playgroud)
相反,我得到一个错误编译:
'std::enable_if_t<std::is_pointer<_Tp>::value, T> Impl<T>::operator()(const string&, int)' cannot be overloaded with 'std::enable_if_t<(! std::is_pointer<_Tp>::value), T> Impl<T>::operator()(const string&, int)'
假设我正在使用一个具有to_int带字符串参数的函数的库.如果字符串是数字的字符表示,则此函数返回int,例如"23"将返回23.如果字符串不是数字,则抛出一个std::runtime_error.是否会更好:
if(is_all_digits(str))
x = to_int(str);
else
output("not an int, idiot. Try again");
Run Code Online (Sandbox Code Playgroud)
要么
try
{
x = to_int(str);
}
catch(...)
{
output("not an int, idiot. Try again");
}
Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的案例。我想绘制一个物体的轮廓,在这种情况下我认为它们只是球体,但我不想依赖于此。
我发现了一些方法,例如:
我使用这种方法的问题是我的模型有很多顶点,这需要我绘制三次。我的帧速率明显下降。
还有其他方法可以做到这一点吗?我的下一个猜测是在最终渲染上绘制圆圈作为后期处理效果,因为我只查看球体。但我宁愿这样做不仅仅是为了领域。
我可以在现有的着色器中做一些勾画轮廓的事情吗?
我还希望当对象位于其他对象后面时出现轮廓。
我正在使用 OpenGL 4.3。
我知道以前有人问过类似的问题,但是:
-no-pie了链接器参数,但仍然没有结果。GNU Prof 的版本是 2.36.1该程序是本教程中的代码,复制如下:
//test_gprof.c
#include<stdio.h>
void new_func1(void)
{
printf("\n Inside new_func1()\n");
int i = 0;
for(;i<0xffffffee;i++);
return;
}
void func1(void)
{
printf("\n Inside func1 \n");
int i = 0;
for(;i<0xffffffff;i++);
new_func1();
return;
}
static void func2(void)
{
printf("\n Inside func2 \n");
int i = 0;
for(;i<0xffffffaa;i++);
return;
}
int main(void)
{
printf("\n Inside main()\n");
int i = …Run Code Online (Sandbox Code Playgroud)