C++ OpenGL纹理/几何体上的线条瑕疵

C++ OpenGL纹理/几何体上的线条瑕疵,c++,opengl,opengl-es,C++,Opengl,Opengl Es,我真的希望你们能帮忙,如果可以的话,我不想问太多问题,但这真的伤了我的大脑。提前谢谢 我最近潜入了现代OpenGL(3.3),我一直在尝试构建一个简单的2D游戏。这一切都是3D的,但我使用三角形作为单四边形(精灵) 一切正常,但当我决定实现alpha通道时,我得到了一条从每个顶点开始的线——基本上是所有几何体上的~1px线框框架 这是场景:(这是一个非常放大的场景) 顶点: m_New_Vertices.push_back(glm::vec3(-1.0, -1.0, m_Location.z)

我真的希望你们能帮忙,如果可以的话,我不想问太多问题,但这真的伤了我的大脑。提前谢谢

我最近潜入了现代OpenGL(3.3),我一直在尝试构建一个简单的2D游戏。这一切都是3D的,但我使用三角形作为单四边形(精灵)

一切正常,但当我决定实现alpha通道时,我得到了一条从每个顶点开始的线——基本上是所有几何体上的~1px线框框架

这是场景:(这是一个非常放大的场景)

顶点:

m_New_Vertices.push_back(glm::vec3(-1.0, -1.0, m_Location.z));
m_New_Vertices.push_back(glm::vec3(1.0, -1.0, m_Location.z));
m_New_Vertices.push_back(glm::vec3(-1.0, 1.0, m_Location.z)); 
m_New_Vertices.push_back(glm::vec3(-1.0, 1.0, m_Location.z));
m_New_Vertices.push_back(glm::vec3(1.0, -1.0, m_Location.z)); 
m_New_Vertices.push_back(glm::vec3(1.0, 1.0, m_Location.z));
纹理加载:

m_Texture = LoadTexture("FILE.png", NULL, NULL); //see below for function defenition
渲染:(为了简单起见,我省略了初始化/绑定其他缓冲区的数据)

片段着色器:

#version 330 core
// Interpolated values from the vertex shaders
in vec2 UV;
in vec3 Position_worldspace;
in vec3 Normal_cameraspace;
in vec3 EyeDirection_cameraspace;
in vec3 LightDirection_cameraspace;

// Ouput data
out vec4 color;

// Values that stay constant for the whole mesh.
uniform sampler2D myTextureSampler;
uniform mat4 MV;
uniform vec3 LightPosition_worldspace;

void main(){

vec3 LightColor = vec3(1, 1, 1);
float LightPower = 50.0;

vec4 MaterialDiffuseColor;
MaterialDiffuseColor.rgb = texture2D(myTextureSampler, UV).rgb;
MaterialDiffuseColor.a = texture2D(myTextureSampler, UV).a;
vec3 MaterialAmbientColor = vec3(0.3, 0.3, 0.3) * MaterialDiffuseColor.rgb;
vec3 MaterialSpecularColor = vec3(0.3, 0.3, 0.3);

float distance = length(LightPosition_worldspace - Position_worldspace);

vec3 n = normalize(Normal_cameraspace);
vec3 l = normalize(LightDirection_cameraspace);

float cosTheta = clamp(dot(n, l), 0, 1);

vec3 E = normalize(EyeDirection_cameraspace);
vec3 R = reflect(-l, n);

float cosAlpha = clamp(dot(E, R), 0, 1);

color.rgb = MaterialAmbientColor + (MaterialDiffuseColor.rgb) * LightColor * LightPower * cosTheta / (distance*distance)
+ MaterialSpecularColor * LightColor * LightPower * pow(cosAlpha, 5) / (distance*distance);
color.a = MaterialDiffuseColor.a;

// Output color = color of the texture at the specified UV
//color = texture2D( myTextureSampler, UV ).rgb;
}
LoadTexture:

GLuint LoadTexture(const char * file_name, int *width, int *height)
{
    png_byte header[8];

    FILE *fp = fopen(file_name, "rb");
    if (fp == 0)
    {
        perror(file_name);
        return 0;
    }

    // read the header
    fread(header, 1, 8, fp);

    if (png_sig_cmp(header, 0, 8))
    {
        fprintf(stderr, "error: %s is not a PNG.\n", file_name);
        fclose(fp);
        return 0;
    }

    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (!png_ptr)
    {
        fprintf(stderr, "error: png_create_read_struct returned 0.\n");
        fclose(fp);
        return 0;
    }

    // create png info struct
    png_infop info_ptr = png_create_info_struct(png_ptr);
    if (!info_ptr)
    {
        fprintf(stderr, "error: png_create_info_struct returned 0.\n");
        png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
        fclose(fp);
        return 0;
    }

    // create png info struct
    png_infop end_info = png_create_info_struct(png_ptr);
    if (!end_info)
    {
        fprintf(stderr, "error: png_create_info_struct returned 0.\n");
        png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
        fclose(fp);
        return 0;
    }

    // the code in this if statement gets called if libpng encounters an error
    if (setjmp(png_jmpbuf(png_ptr))) {
        fprintf(stderr, "error from libpng\n");
        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
        fclose(fp);
        return 0;
    }

    // init png reading
    png_init_io(png_ptr, fp);

    // let libpng know you already read the first 8 bytes
    png_set_sig_bytes(png_ptr, 8);

    // read all the info up to the image data
    png_read_info(png_ptr, info_ptr);

    // variables to pass to get info
    int bit_depth, color_type;
    png_uint_32 temp_width, temp_height;

    // get info about png
    png_get_IHDR(png_ptr, info_ptr, &temp_width, &temp_height, &bit_depth, &color_type,
        NULL, NULL, NULL);

    if (width){ *width = temp_width; }
    if (height){ *height = temp_height; }

    //printf("%s: %lux%lu %d\n", file_name, temp_width, temp_height, color_type);

    if (bit_depth != 8)
    {
        fprintf(stderr, "%s: Unsupported bit depth %d.  Must be 8.\n", file_name, bit_depth);
        return 0;
    }

    GLint format;
    switch (color_type)
    {
    case PNG_COLOR_TYPE_RGB:
        format = GL_RGB;
        break;
    case PNG_COLOR_TYPE_RGB_ALPHA:
        format = GL_RGBA;
        break;
    default:
        fprintf(stderr, "%s: Unknown libpng color type %d.\n", file_name, color_type);
        return 0;
    }

    // Update the png info struct.
    png_read_update_info(png_ptr, info_ptr);

    // Row size in bytes.
    int rowbytes = png_get_rowbytes(png_ptr, info_ptr);

    // glTexImage2d requires rows to be 4-byte aligned
    rowbytes += 3 - ((rowbytes - 1) % 4);

    // Allocate the image_data as a big block, to be given to opengl
    png_byte * image_data = (png_byte *)malloc(rowbytes * temp_height * sizeof(png_byte) + 15);
    if (image_data == NULL)
    {
        fprintf(stderr, "error: could not allocate memory for PNG image data\n");
        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
        fclose(fp);
        return 0;
    }

    // row_pointers is for pointing to image_data for reading the png with libpng
    png_byte ** row_pointers = (png_byte **)malloc(temp_height * sizeof(png_byte *));
    if (row_pointers == NULL)
    {
        fprintf(stderr, "error: could not allocate memory for PNG row pointers\n");
        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
        free(image_data);
        fclose(fp);
        return 0;
    }

    // set the individual row_pointers to point at the correct offsets of image_data
    for (unsigned int i = 0; i < temp_height; i++)
    {
        row_pointers[temp_height - 1 - i] = image_data + i * rowbytes;
    }

    // read the png into image_data through row_pointers
    png_read_image(png_ptr, row_pointers);

    // Generate the OpenGL texture object
    GLuint texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, format, temp_width, temp_height, 0, format, GL_UNSIGNED_BYTE, image_data);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    // clean up
    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    free(image_data);
    free(row_pointers);
    fclose(fp);
    return texture;
}
GLuint LoadTexture(常量字符*文件名,int*宽度,int*高度)
{
png_字节头[8];
文件*fp=fopen(文件名,“rb”);
如果(fp==0)
{
perror(文件名);
返回0;
}
//阅读标题
fread(标题1、8、fp);
if(png_sig_cmp(头,0,8))
{
fprintf(stderr,“错误:%s不是PNG。\n”,文件名);
fclose(fp);
返回0;
}
png_structp png_ptr=png_create_read_struct(png_LIBPNG_VER_STRING,NULL,NULL);
如果(!png_ptr)
{
fprintf(stderr,“错误:png_create_read_struct返回0。\n”);
fclose(fp);
返回0;
}
//创建png信息结构
png_infop info_ptr=png_create_info_struct(png_ptr);
如果(!info_ptr)
{
fprintf(stderr,“错误:png_create_info_struct返回0。\n”);
png_destroy_read_struct(&png_ptr,(png_infopp)NULL,(png_infopp)NULL);
fclose(fp);
返回0;
}
//创建png信息结构
png_infop end_info=png_create_info_struct(png_ptr);
如果(!end_info)
{
fprintf(stderr,“错误:png_create_info_struct返回0。\n”);
png_destroy_read_struct(&png_ptr,&info_ptr,(png_infopp)NULL);
fclose(fp);
返回0;
}
//如果libpng遇到错误,将调用此if语句中的代码
if(setjmp(png_jmpbuf(png_ptr))){
fprintf(stderr,“来自libpng的错误”);
png_destroy_read_struct(&png_ptr,&info_ptr,&end_info);
fclose(fp);
返回0;
}
//初始化png读取
png_init_io(png_ptr,fp);
//让libpng知道您已经读取了前8个字节
png_set_sig_字节(png_ptr,8);
//读取图像数据之前的所有信息
png_read_info(png_ptr,info_ptr);
//要传递以获取信息的变量
int位深度、颜色类型;
png单元32温度宽度、温度高度;
//获取有关png的信息
png获取IHDR(png ptr、信息ptr、温度宽度、温度高度、位深度和颜色类型,
空,空,空);
如果(宽度){*width=temp_width;}
如果(高度){*height=temp_height;}
//printf(“%s:%lux%lu%d\n”,文件名、温度宽度、温度高度、颜色类型);
if(位深度!=8)
{
fprintf(stderr,“%s:不支持的位深度%d。必须为8。\n”,文件名,位深度);
返回0;
}
闪烁格式;
开关(彩色)
{
案例PNG\U颜色\U类型\U RGB:
格式=GL_RGB;
打破
案例PNG\U颜色\U类型\U RGB\U ALPHA:
格式=GL_RGBA;
打破
违约:
fprintf(stderr,“%s:未知的libpng颜色类型%d.\n”,文件名,颜色类型);
返回0;
}
//更新png信息结构。
png_读取_更新_信息(png_ptr,info_ptr);
//行大小(字节)。
int rowbytes=png_get_rowbytes(png_ptr,info_ptr);
//glTexImage2d要求行是4字节对齐的
行字节数+=3-((行字节数-1)%4);
//将图像数据作为一个大块分配给opengl
png_字节*图像_数据=(png_字节*)malloc(行字节*临时高度*大小(png_字节)+15);
if(图像_数据==NULL)
{
fprintf(stderr,“错误:无法为PNG图像数据分配内存\n”);
png_destroy_read_struct(&png_ptr,&info_ptr,&end_info);
fclose(fp);
返回0;
}
//行指针用于指向图像数据,以便使用libpng读取png
png_字节**行指针=(png_字节**)malloc(临时高度*大小)(png_字节*);
if(行指针==NULL)
{
fprintf(stderr,“错误:无法为PNG行指针分配内存\n”);
png_destroy_read_struct(&png_ptr,&info_ptr,&end_info);
免费(图像_数据);
fclose(fp);
返回0;
}
//将单个行指针设置为指向图像数据的正确偏移
对于(无符号整数i=0;i
我通过使用

glDisable(GL_MULTISAMPLE);
感谢MorphingDragon提供的答案,我只是在为其他人发布一个答案


虽然MorphingDragon没有给我直接的答案,但他为我指出了正确的方向,因此我相信他值得表扬。

您是否在OpenGL中启用了多边形平滑功能?如果你尝试禁用它。谢谢,我不知道这么小的一段代码就能解决这么大的问题。一开始我还以为是抗锯齿。我更改了我的nvidia设置,但没有结果。我还注意到,如果我更换了显示器(比如我将输出HMDI插入到电视屏幕),工件将
glDisable(GL_MULTISAMPLE);