OpenGL转换不会生效
OpenGL translations do not take effect
这些问题可能很基本,请耐心听我说。
我已经构建了一个非常小的Qt程序来处理OpenGL,并尝试学习渲染方法,并且已经有了一个彩色三角形(扩展了本教程http://qt-project.org/wiki/How_to_use_OpenGL_Core_Profile_with_Qt)。我现在的问题是,我试图应用于三角形的任何变换都无济于事。最重要的是,将参数更改为glOrtho似乎对显示没有任何影响,而且我不知道将参数设置为glViewport应该如何工作:使它们变小似乎会使3D世界的渲染区域变大。
代码:
这是我的顶点/索引数组(vec3位置,vec3颜色)
static GLfloat tri[] = { -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f };
static GLuint tri_ind[] = { 0, 1, 2 };
static int SIZEOF_TRI = 2 * 3 * 3 * sizeof(GLfloat);
static int SIZEOF_TRI_IND = 3 * sizeof(GLuint);
static int NUM_TRI_VERTS = 3;
我的OpenGL小部件将Qt的缓冲区对象用于顶点和索引缓冲区:
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
GLWidget( const QGLFormat& format, QWidget* parent = 0 );
protected:
virtual void initializeGL();
virtual void resizeGL( int w, int h );
virtual void paintGL();
virtual void keyPressEvent( QKeyEvent* e );
private:
bool prepareShaderProgram( const QString& vertexShaderPath,
const QString& fragmentShaderPath );
QGLShaderProgram m_shader;
QGLBuffer m_vertexBuffer;
QGLBuffer m_indexBuffer;
};
构造函数初始化缓冲区:
GLWidget::GLWidget( const QGLFormat& format, QWidget* parent )
: QGLWidget( format, parent ),
m_vertexBuffer( QGLBuffer::VertexBuffer ),
m_indexBuffer( QGLBuffer::IndexBuffer )
{
}
initializeGL初始化其余部分:
void GLWidget::initializeGL()
{
QGLFormat glFormat = QGLWidget::format();
if ( !glFormat.sampleBuffers() )
qWarning() << "Could not enable sample buffers";
// Set the clear color to black
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
// Prepare a complete shader program...
QString vert = QCoreApplication::applicationDirPath() + "/simple.vert";
QString frag = QCoreApplication::applicationDirPath() + "/simple.frag";
if ( !prepareShaderProgram( vert, frag ) )
return;
// Bind the shader program so that we can associate variables from
// our application to the shaders
if ( !m_shader.bind() )
{
qWarning() << "Could not bind shader program to context";
return;
}
// We need us some vertex data. Start simple with a triangle ;-)
GLfloat* points = tri;
m_vertexBuffer.create();
m_vertexBuffer.setUsagePattern( QGLBuffer::StaticDraw );
if ( !m_vertexBuffer.bind() )
{
qWarning() << "Could not bind vertex buffer to the context";
return;
}
m_vertexBuffer.allocate( points, SIZEOF_TRI );
// Indices
GLuint* indices = tri_ind;
m_indexBuffer.create();
m_indexBuffer.setUsagePattern( QGLBuffer::StaticDraw );
if ( !m_indexBuffer.bind() )
{
qWarning() << "Could not bind vertex buffer to the context";
return;
}
m_indexBuffer.allocate( indices, SIZEOF_TRI_IND );
// Enable the "vertex" attribute to bind it to our currently bound
// vertex buffer.
m_shader.setAttributeBuffer( "vertex", GL_FLOAT, 0, 3, 6 * sizeof(GLfloat) );
m_shader.enableAttributeArray( "vertex" );
m_shader.setAttributeBuffer( "color", GL_FLOAT, 3 * sizeof(GLfloat), 3, 6 * sizeof(GLfloat) );
m_shader.enableAttributeArray( "color" );
}
paintGL进行平移并绘制顶点:
void GLWidget::paintGL()
{
// Clear the buffer with the current clearing color
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// Draw stuff
glLoadIdentity();
glTranslatef(10.0, 10.0, 0.0);
glDrawArrays( GL_TRIANGLES, 0, NUM_TRI_VERTS );
}
这个三角形看起来和我添加翻译之前完全一样。
此外,在resizeGL中(调整小部件的大小时),将参数更改为glOrtho对显示没有影响。在+-5和+-10之间没有差异。如果我将glViewport的宽度和高度分别设置为100,然后分别设置为200,则当值为200时,三角形(在左下角)看起来比值为100时更大。我本以为视口大小越大,里面的对象就越小,但也许我错过了什么。
void GLWidget::resizeGL( int w, int h )
{
// Set the viewport to window dimensions
glViewport( 0, 0, w, qMax( h, 1 ) );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0, 10.0, -10.0, 10.0, -5.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
有人能给我一些正确的方向吗?
谢谢。
编辑:顶点着色器:
#version 330
in vec3 vertex;
in vec3 color;
out vec3 Color;
void main( void )
{
Color = color;
gl_Position = vec4(vertex, 1.0);
}
片段着色器:
#version 330
layout(location = 0, index = 0) out vec4 fragColor;
in vec3 Color;
void main( void )
{
fragColor = vec4(Color, 1.0);
}
编写着色器是为了在OpenGL-3.3或更高版本的核心配置文件中工作。您使用的是不推荐使用的固定函数管道方法(glMatrixMode、glLoadIdentity、glTranslate等),这些方法在核心配置文件中不起作用。使用像GLM、Eigen或linmath.h这样的实矩阵数学库来计算矩阵,然后将它们传递给着色器统一。
- 了解 GLM- openGL 中的相机转换
- 将QOpenGLWidget子类转换为使用Metal而不是OpenGL的子类是否可行?
- OpenGL 转换无法使用 GLM 矩阵
- 将旧管道转换为现代 openGL 时出现问题
- 在 OpenGL 中将笛卡尔世界坐标转换为球面局部坐标
- 在 OpenGL 中转换应用顺序让我感到困惑
- 如何在OpenGL中使用关键事件来转换对象
- 在 OpenGL 中计算矩阵时,转换的正确顺序是什么?
- OpenGL 将 ClipCoord 转换为 ScreenCoord
- 将 OpenGL 坐标系统转换为自定义并返回
- 如何将图像转换为灰度 opengl c++
- 如何将鼠标坐标正确转换为 opengl 坐标?
- Opengl 精度转换错误编译错误 E0415
- 使用Emscripten将OpenGL ES转换回主机
- 如何将这些OpenGL着色器转换为Android NDK的GLES3的OpenGL ES着色器
- 如何正确地使用opengl转换的矩阵进行数学运算
- C++ / OpenGL:纹理到像素图示例 - 缩小转换误差
- 使用SFML,现代OpenGL转换从2D点到3D
- 在使用OpenGl中使用转换反馈的GL_SEPARATE_ATTRIB时,如何读取多个缓冲区
- 在不同的坡度上转换openGL中的对象