小编Neo*_*ana的帖子

我的法线贴图有什么问题?我认为这是我的切线

编辑:你可能想从"编辑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)

c++ opengl normals bump-mapping

18
推荐指数
1
解决办法
1680
查看次数

如何检查std :: variant是否可以保存某种类型

我有一个班里有一个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::disjunctionstd::is_same

喜欢

std::enable_if<std::disjunction<std::is_same<T,/*Variant Types???*/>...>> …
Run Code Online (Sandbox Code Playgroud)

c++ c++17

18
推荐指数
3
解决办法
2674
查看次数

当单独的文件中的类时,使用asio独立的Segfault

以下是我能得到的最小例子.它确实需要在单独的文件中,因为这似乎是导致分段错误错误的原因.

我正在使用带有Asio独立1.10.6的Mingw x32 4.8.1.我还测试了TDM GCC 4.7.1和M​​ingw 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)

c++ boost boost-asio

16
推荐指数
1
解决办法
1474
查看次数

如何在GLSL中渲染无限的2D网格?

理想情况下,我想要做的是绘制一个四边形并让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)

opengl glsl

14
推荐指数
1
解决办法
9790
查看次数

什么时候应该使用C ++定宽整数类型,它们如何影响性能?

通常,我想大多数人会使用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我的代码中几乎没有零发生。

我认为这有三个原因:

  1. 这使我的意图清晰地传达给其他人(即,我不希望这个数字大于这个数字,否则它不会是负数)
  2. 代码更具可移植性。如果我想使用16位整数类型,则每个编译器将为16位
  3. 节省内存。尽管我认为在现代PC上这并不重要。

但是我担心这会降低性能,因为您通常的消费级CPU会以std::int32_t更好的本机宽度类型运行。

那么,什么时候应该使用固定宽度类型,它们如何影响性能?

c++ c++11

10
推荐指数
1
解决办法
179
查看次数

如何检索要在模板中使用的函数的返回类型?

我有一个函数在某处调用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)

但我不想指定参数值,因为它们是静态的.

c++

9
推荐指数
2
解决办法
1182
查看次数

为什么enable_if不在这里工作?

我有这个代码,我的期望是()基于模板参数的类型会有两个不同版本的运算符.

#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)'

c++ templates

7
推荐指数
1
解决办法
255
查看次数

在传递给库函数之前检查值是否更好,或者捕获抛出的异常?

假设我正在使用一个具有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)

c++ exception-handling

6
推荐指数
1
解决办法
100
查看次数

还有哪些方法可以绘制物体的轮廓?

我有一个非常简单的案例。我想绘制一个物体的轮廓,在这种情况下我认为它们只是球体,但我不想依赖于此。

我发现了一些方法,例如:

  1. 将对象绘制到模板缓冲区
  2. 打开线框模式
  3. 用粗线绘制物体
  4. 在上面画出真实的物体

我使用这种方法的问题是我的模型有很多顶点,这需要我绘制三次。我的帧速率明显下降。

还有其他方法可以做到这一点吗?我的下一个猜测是在最终渲染上绘制圆圈作为后期处理效果,因为我只查看球体。但我宁愿这样做不仅仅是为了领域。

我可以在现有的着色器中做一些勾画轮廓的事情吗?

我还希望当对象位于其他对象后面时出现轮廓。

我正在使用 OpenGL 4.3。

opengl

6
推荐指数
1
解决办法
1万
查看次数

gprof 不为需要合理时间执行的程序产生任何输出

我知道以前有人问过类似的问题,但是:

  1. 关于gprof 产生空输出,我在 Windows 10 上使用来自 MSYS2 的 GCC 10.2.0,即它是 MingW64 发行版。我还添加-no-pie了链接器参数,但仍然没有结果。GNU Prof 的版本是 2.36.1
  2. 关于gprof 没有给出任何输出,我正在一个明确需要时间执行的程序上执行它。

该程序是本教程中的代码,复制如下:

//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)

gcc gprof mingw32 mingw-w64 msys2

5
推荐指数
1
解决办法
299
查看次数