gpt4 book ai didi

c++ - OpenGL 点 Sprite 渲染为点而不是纹理

转载 作者:行者123 更新时间:2023-11-28 02:36:20 24 4
gpt4 key购买 nike

这是我的代码

顶点着色器

#version 430

in vec3 position;
uniform mat4 MVP;

void main()
{
gl_Position = MVP * vec4(position, 1.0);
}

片段着色器

#version 430

out vec4 outputColor;
uniform sampler2D tex;

void main()
{
outputColor = texture(tex, gl_PointCoord);
}

初始化

void init()
{
glPointSize(20.0f);
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

bmp.Open("C:\\users\\alon\\desktop\\star.bmp");
bmp.ReadPixels();
unsigned char *pixels = new unsigned char[bmp.NeededBufferSize()];
bmp.AssignPixels(pixels);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bmp.GetWidth(), bmp.GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
delete[] pixels;

glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

GLuint VaoId;
glGenVertexArrays(1, &VaoId);
glBindVertexArray(VaoId);

position_index = glGetAttribLocation(program, "position");
MVP_location = glGetUniformLocation(program, "MVP");

// x y z s t
GLfloat vertices[] = {-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
-0.5f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f};

glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(position_index, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
}

渲染

void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnableVertexAttribArray(position_index);
glBindBuffer(GL_ARRAY_BUFFER, VBO);

float c;
if(bTime)
c = (float)std::clock() * 2.0f / CLOCKS_PER_SEC;
else
c = 0.0f;

glm::mat4 modelview = glm::rotate(-c * 50.0f, glm::vec3(0.0f, 1.0f, 0.0f));
modelview = glm::translate(glm::vec3(0.0f + fTranslateX, 0.0f, -1.75f + fTranslateZ)) * modelview;
glm::mat4 projection = glm::perspective(60.0f, 16.0f/9.0f, 0.10f, 100.0f); //perspective
glm::mat4 MVP = projection * modelview;

glUniformMatrix4fv(MVP_location, 1, GL_FALSE, &MVP[0][0]);

glDrawArrays(GL_POINTS, 0, 6);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(position_index);

glutSwapBuffers();

if(glGetError() != GL_NO_ERROR)
exit(1);
}

星图为:

Star image

输出:

Screenshot

我做错了什么吗?

最佳答案

在兼容性配置文件(或 3.1 之前的任何 GL 版本)中,您想要的行为仅在启用 GL_POINT_SPRITE 时发生。在核心配置文件中,该状态已被删除,gl_PointCoord 始终表现得好像启用了 GL_POINT_SPRITE

要解决您的问题,您必须调用 glEnable (GL_POINT_SPRITE)

关于c++ - OpenGL 点 Sprite 渲染为点而不是纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27299707/

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