我真的希望你们可以提供帮助,如果可以的话,我不想问很多问题,但这个问题确实伤害了我的大脑。提前谢谢。
我最近投身于现代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
渲染:(为简单起见,我省略了初始化/绑定其他缓冲区的数据)
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
Fragment Shader:
#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;
}
答案 0 :(得分:1)
我能够使用
解决问题glDisable(GL_MULTISAMPLE);
感谢MorphingDragon的答案,我只是张贴作为其他人的答案。
虽然MorphingDragon没有给我直接答案,但他指出了我正确的方向,所以我相信他值得赞扬。