OpenGL gluLookat无法在着色器上使用 [英] OpenGL gluLookat not working with shaders on

查看:202
本文介绍了OpenGL gluLookat无法在着色器上使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想使用gluLookat并在其上放置着色器时,当我关闭着色器时,它不会移动相机".

When i want to use gluLookat and have the shaders on it doesnt move the "camera" when i turn the shaders off it works correctly.

我的着色器中是否缺少某些东西,我无法弄清楚是什么.

Is there something missing in my shaders, i cant figure out what.

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); 
}

将相机"移动到我希望着色器打开的位置

Move the "camera" to where i want it to be with shaders on

推荐答案

使用着色器程序时,当前矩阵不会神奇地处理顶点坐标属性.着色器程序必须执行顶点坐标的转换.

When you use a shader program, then the vertex coordinate attributes are not magically processed, by the current matrices. The shader program has to do the transformations of the vertex coordinates.

您有两种可能性,要么使用兼容性配置文件上下文,要么使用较低的glsl版本(例如1.10).

You've 2 possibilities, either you use a compatibility profile context and use a lower glsl version (e.g. 1.10).

然后,您可以使用内置的统一 gl_ModelViewProjectionMatrix (请参见 GLSL 1.10加密)和固定功能矩阵堆栈将起作用:

Then you can use the built in uniform gl_ModelViewProjectionMatrix (see GLSL 1.10 secification) and the fixed function matrix stack will work:

#version 110
attribute vec4 position

// varying vec4 Color;

void main()
{ 
     // ...  

     gl_Position = gl_ModelViewProjectionMatrix * position;
}

但是请注意,此方法自几十年来就已被弃用.请参见固定功能管道

But note this is deprecated since decades. See Fixed Function Pipeline and Legacy OpenGL.

我建议使用 OpenGL数学之类的库进行计算 lookAt() 统一变量:

I recommend to use a library like OpenGL Mathematics to calculate the view matrix by lookAt() and a uniform variable:

#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);

    // [...]
}

统一位置由布局限定符明确设置. (location = 7).
glUniformMatrix4fv 将制服的值设置为默认统一块中的指定位置.这必须在 glUseProgram安装了密封胶之后进行.

The uniform location is set explicite by a 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.

这篇关于OpenGL gluLookat无法在着色器上使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆