OpenGL gluLookat 无法使用着色器
OpenGL gluLookat not working with shaders on
当我想使用 gluLookat 并且在其上安装着色器时 "camera" 当我关闭着色器时它可以正常工作。
我的着色器中是否缺少某些东西,我不知道是什么。
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.8, 0.8, 0.8, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.5,0,1,0.5,0,0,0,1,0);
glColor3f(0, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDrawArrays(GL_LINES, 6, 6);
glDrawArrays(GL_TRIANGLES, 12,6);
glDrawArrays(GL_LINES, 18, 4);
glDrawArrays(GL_TRIANGLES, 22, 6);
glColor3f(1, 0.7, 0);
glDrawArrays(GL_TRIANGLES, 28, 6);
glFlush();
}
顶点着色器:
#version 450 core // 420, 330 core , compatibility
in vec4 position
out vec4 Color;
void main()
{
gl_Position = position;
}
片段着色器:
#version 450 core // 420, 330 core , compatibility
in vec4 Color;
layout(location=0) out vec4 fColor;
void main()
{
fColor = vec4(0,0,0,0);
}
将 "camera" 移动到我希望它在着色器开启的位置
当您使用着色器程序时,当前矩阵不会神奇地处理顶点坐标属性。着色器程序必须进行顶点坐标的转换。
您有 2 种可能性,要么使用兼容性配置文件上下文,要么使用较低的 glsl 版本(例如 1.10)。
然后你可以使用内置的统一 gl_ModelViewProjectionMatrix
(see GLSL 1.10 secification) 并且固定函数矩阵堆栈将起作用:
#version 110
attribute vec4 position
// varying vec4 Color;
void main()
{
// ...
gl_Position = gl_ModelViewProjectionMatrix * position;
}
但请注意,几十年来这已被弃用。参见 Fixed Function Pipeline and Legacy OpenGL。
我建议使用像 OpenGL Mathematics to calculate the view matrix by lookAt()
and a uniform 变量这样的库:
#version 450 core // 420, 330 core , compatibility
in vec4 position
// out vec4 Color;
layout(location = 7) uniform mat4 view_matrix;
void main()
{
gl_Position = view_matrix * position;
}
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// [...]
{
// [...]
glUseProgram(program);
glm::mat4 view = glm::lookAt(
glm::vec3(0.5f,0.0f,1.0f), glm::Vec3(0.5f,0.0f,0.0f), glm::Vec3(0.0f,1.0f,0.0f));
glUniformMatrix4fv(7, 1, GL_FALSE, glm::value_ptr(view);
// [...]
}
统一位置由 Layout qualifier (location = 7
) 显式设置。
glUniformMatrix4fv
sets the value of the uniform at the specified location in the default uniform block. This has to be done after the progroam was installed by glUseProgram
.
当我想使用 gluLookat 并且在其上安装着色器时 "camera" 当我关闭着色器时它可以正常工作。
我的着色器中是否缺少某些东西,我不知道是什么。
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.8, 0.8, 0.8, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.5,0,1,0.5,0,0,0,1,0);
glColor3f(0, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDrawArrays(GL_LINES, 6, 6);
glDrawArrays(GL_TRIANGLES, 12,6);
glDrawArrays(GL_LINES, 18, 4);
glDrawArrays(GL_TRIANGLES, 22, 6);
glColor3f(1, 0.7, 0);
glDrawArrays(GL_TRIANGLES, 28, 6);
glFlush();
}
顶点着色器:
#version 450 core // 420, 330 core , compatibility
in vec4 position
out vec4 Color;
void main()
{
gl_Position = position;
}
片段着色器:
#version 450 core // 420, 330 core , compatibility
in vec4 Color;
layout(location=0) out vec4 fColor;
void main()
{
fColor = vec4(0,0,0,0);
}
将 "camera" 移动到我希望它在着色器开启的位置
当您使用着色器程序时,当前矩阵不会神奇地处理顶点坐标属性。着色器程序必须进行顶点坐标的转换。
您有 2 种可能性,要么使用兼容性配置文件上下文,要么使用较低的 glsl 版本(例如 1.10)。
然后你可以使用内置的统一 gl_ModelViewProjectionMatrix
(see GLSL 1.10 secification) 并且固定函数矩阵堆栈将起作用:
#version 110
attribute vec4 position
// varying vec4 Color;
void main()
{
// ...
gl_Position = gl_ModelViewProjectionMatrix * position;
}
但请注意,几十年来这已被弃用。参见 Fixed Function Pipeline and Legacy OpenGL。
我建议使用像 OpenGL Mathematics to calculate the view matrix by lookAt()
and a uniform 变量这样的库:
#version 450 core // 420, 330 core , compatibility
in vec4 position
// out vec4 Color;
layout(location = 7) uniform mat4 view_matrix;
void main()
{
gl_Position = view_matrix * position;
}
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// [...]
{
// [...]
glUseProgram(program);
glm::mat4 view = glm::lookAt(
glm::vec3(0.5f,0.0f,1.0f), glm::Vec3(0.5f,0.0f,0.0f), glm::Vec3(0.0f,1.0f,0.0f));
glUniformMatrix4fv(7, 1, GL_FALSE, glm::value_ptr(view);
// [...]
}
统一位置由 Layout qualifier (location = 7
) 显式设置。
glUniformMatrix4fv
sets the value of the uniform at the specified location in the default uniform block. This has to be done after the progroam was installed by glUseProgram
.