尝试在不使用转换概念的情况下呈现此代码
Trying to render this code without using transformation concept
>我正在尝试在不使用变换函数的情况下更改三角形的位置, 通过每次只更改 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;
}
编辑代码从 (-1.0( 到窗口的中间到末尾
移动首先数组中的元素数量是sizeof(MyPoints) / sizeof(float)
。
必须float
一致变量offset
的类型:
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++代码是如何在没有定义函数的情况下运行的
- 如何在没有函数的情况下编写此代码并使C++更简单?
- 在什么情况下,我想在 C/C++ 代码中使用内联汇编代码
- 可以在没有构建代码的情况下转到定义吗?
- 在没有引用传递资源的情况下,如何在java中简化这些代码
- 尝试在不使用转换概念的情况下呈现此代码
- 寻找有关为什么此C++代码在没有引用的情况下不起作用的解释
- 在运行时,何时完全初始化 std 库才能在不破坏代码的情况下使用它?
- 在存在错误代码的情况下输出参数与 NRVO
- 在给定相同的输入的情况下,某些代码怎么可能花费更多时间来运行,这似乎只是因为它处于循环中?
- 在链接的程序集文件中,我想从 c++ 调用代码访问变量.是否可以在不触发访问冲突的情况下执行此操作?
- 为什么我的代码在没有 chroot 函数的情况下工作,但使用 chroot 函数失败?
- 如何在不复制此代码的情况下将多个函数放入多个命名空间?
- 如何在 C++03 中没有重复代码的情况下在堆栈上创建一个非常量 C 字符串数组?
- 在没有额外代码的情况下链接两个独立类的最通用方法是什么?
- 如何在没有性能命中的情况下抽象SIMD代码来处理不同的数据类型
- 为什么 GCC 6.3 在没有显式 C++11 支持的情况下编译此大括号初始化列表代码
- 打包结构,而不是在没有代码重复的情况下打包
- 如何在不中断剩余代码的情况下仅C++中输入整数
- 如何在<typename> <long> 不更改给定代码的情况下将自定义向量与 STL 向量相互转换?