gpt4 book ai didi

c++ - 我的 OpenGL 纹理/几何体上的线条伪影

转载 作者:行者123 更新时间:2023-11-30 05:48:36 24 4
gpt4 key购买 nike

我真的希望你们能帮上忙,如果可以的话我不想问太多问题,但是这个问题真的很伤我的脑子。提前致谢。

我最近开始研究现代 OpenGL(3.3),并且一直在尝试构建一个简单的 2D 游戏。这一切都是 3D 的,但我将三角形用于单个四边形(用于 Sprite )。

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

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

enter image description here

垂直:

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

渲染:(为简单起见,我省略了用于初始化/绑定(bind)其他缓冲区的数据)

glUseProgram(SHADERPROGRAM_IM_USING);

//Bind buffers here

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_Texture);
glUniform1i(m_TextureID, 0); //TextureID is Initiated in my Init function

glDrawArrays(GL_TRIANGLES, 0, m_New_Vertices.size());

//Unbind buffers here

片段着色器:

#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;
}

加载纹理:

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;
}

最佳答案

我能够通过使用来解决问题

glDisable(GL_MULTISAMPLE);

感谢 MorphingDragon 的回答,我只是作为对其他人的回答发布。

虽然 MorphingDragon 没有给我直接的答案,但他为我指明了正确的方向,所以我相信他值得信任。

关于c++ - 我的 OpenGL 纹理/几何体上的线条伪影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28120356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com