gpt4 book ai didi

c++ - 尝试在不使用转换概念的情况下呈现此代码

转载 作者:行者123 更新时间:2023-11-30 03:13:57 25 4
gpt4 key购买 nike

我试图在不使用变换函数的情况下改变三角形的位置,每次只改变 x 的位置,

这是我在主 while 循环中的代码

float MyPoints[] = { 0.1 , 0.2, 0.3, 0.4, 0.5 , 0.6, 0.7, 0.8, 0.9};
int offset = (-1, 1);
for (int i = 0; i < sizeof(MyPoints); i++) {
offset += MyPoints[i];
ourShader.Use();
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);// unbind
}

这是在着色器中

out vec3 ourColor;
out vec2 TexCoord;
uniform vec4 offset;

void main()
{
gl_Position = vec4(position.x + offset, position.y, position.z, 1.0f);
ourColor = color;
TexCoord = texCoord;
}

编辑

这是我在主 while 循环中的代码

    float offset = 1.0f;
float step = 0.001f; //move
int i=0;
// Loop until window closed (Game loop)
while (!glfwWindowShouldClose(mainWindow))
{
// Get + Handle user input events
glfwPollEvents();

//Render
// Clear the colorbuffer
glClearColor(0.0f, 0.1f, 0.2f, 1.0f);
//glPointSize(400.0f);
glClear(GL_COLOR_BUFFER_BIT);

// Call Shader Program
//Rendering the first triangle

GLuint program =ourShader.Program ; // program object from "ourShader"
GLint offset_loc = glGetUniformLocation(program, "offset");

float MyPoints[] = { -0.1 , -0.2,-0.3,-0.4,-0.5 ,-0.6,-0.7,-0.8,-0.9 };

int noPoints = sizeof(MyPoints) / sizeof(float);
ourShader.Use();
for (int i = 0; i < noPoints; i++) {
glUniform1f(offset_loc, MyPoints[i] + offset);
}
offset += step;

if (MyPoints[i] + offset >= 1.0f || MyPoints[i] + offset <= -1.0f)
step *= -1.0f;

//update uniform data

glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(mainWindow);
glBindVertexArray(0);// unbind

}

这是在着色器中

out vec3 ourColor;
out vec2 TexCoord;
uniform float offset;

void main()
{
gl_Position = vec4(position.x + offset, position.y, position.z, 1.0f);
ourColor = color;
TexCoord = texCoord;
}

Edit 代码从 (-1.0) 移动到窗口的中间到末尾

最佳答案

首先,数组中元素的数量是sizeof(MyPoints)/sizeof(float)

统一变量offset的类型必须是float:

uniform float offset;

您必须通过 glGetUniformLocation 获取统一变量 offset 的位置并通过例如设置制服的值glUniform1f :

GLuint program = ; // program object from "ourShader"
GLint offset_loc = glGetUniformLocation(program, "offset");

float MyPoints[] = { 0.1 , 0.2, 0.3, 0.4, 0.5 , 0.6, 0.7, 0.8, 0.9};
int noPoints = sizeof(MyPoints) / sizeof(float);

// bind vertex array
glBindVertexArray(VAO);

// install program
ourShader.Use();

float offset = -1.0f;
for (int i = 0; i < noPoints; i++) {

// set value of the uniform (after program is installed)
offset += MyPoints[i];
glUniform1f(offset_loc, offset);

// draw one triangle
glDrawArrays(GL_TRIANGLES, 0, 3);
}
glBindVertexArray(0);

如果您想让三角形移动,那么您必须更改每一帧中每个单独三角形的偏移量。例如:

float offset = 0.0f;
float step = 0.01f;
while (!glfwWindowShouldClose(mainWindow))
{
// [...]

ourShader.Use();
glUniform1f(offset_loc, offset);
glDrawArrays(GL_TRIANGLES, 0, 3);

// [...]

// change offset
offset += step;
if (offset >= 1.0f || offset <= -1.0f)
step *= -1.0f; // reverse direction
}

关于c++ - 尝试在不使用转换概念的情况下呈现此代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58315745/

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