使用Qt5和glew在openGL中渲染纹理

时间:2015-09-17 09:09:33

标签: qt opengl glew

我正在尝试使用Qt5和glew在openGL中渲染纹理四边形。 特别是我使用的是QopenGLWidget。

我正在使用的代码是从https://open.gl/textures修改的 主要变化是使用三角形条而不是元素缓冲区 并使用我自己的像素阵列而不是加载图像。

我已经测试过更改,他们使用glfw3工作。

在Qt中我只能看到一个没有颜色的黑色四边形。 这只发生在我尝试使用纹理时,如果我删除着色器的纹理部分,它会渲染颜色。

我的着色器编译正常,没有错误(尽管下面的代码中没有错误检查)。

texture(tex, Texcoord)

只是输出零。

所以我的问题是,为什么它只渲染一个黑色四边形?

GLfloat tmpdata[] = {
//pos               //colour            //texCoord
-0.5f, 0.5f,        1.0f, 0.0f, 0.0f,   0.0f, 0.0f, // Top-left
0.5f, 0.5f,         0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // Top-right
-0.5f, -0.5f,       1.0f, 1.0f, 1.0f,   0.0f, 1.0f,  // Bottom-left
0.5f, -0.5f,        0.0f, 0.0f, 1.0f,   1.0f, 1.0f // Bottom-right
};

GLRenderer::GLRenderer(QWidget *parent)
    :
    QOpenGLWidget(parent)
{
// Shader sources
VertSource =
    "#version 150 core\n"
    "in vec2 position;"
    "in vec3 color;"
    "in vec2 texcoord;"
    "out vec3 Color;"
    "out vec2 Texcoord;"
    "void main() {"
    "   Color = color;"
    "   Texcoord = texcoord;"
    "   gl_Position = vec4(position, 0.0, 1.0);"
    "}";
FragSource =
    "#version 150 core\n"
    "in vec3 Color;"
    "in vec2 Texcoord;"
    "out vec4 outColor;"
    "uniform sampler2D tex;"
    "void main() {"
    "   outColor = texture(tex, Texcoord) * vec4(Color, 1.0);"
    // "   outColor = vec4(Color, 1.0);"
    //"   outColor = texture(tex, Texcoord);"
     "}";
}

GLRenderer::~GLRenderer()
{
    glDeleteProgram(shaderProgram);
    glDeleteShader(fragmentShader);
    glDeleteShader(vertexShader);
    glDeleteBuffers(1, &vbo);
    glDeleteVertexArrays(1, &vao);
}

void GLRenderer::initializeGL()
{
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (err != GLEW_OK)
{
    printf("%s", glewGetErrorString(err));
}

// Create Vertex Array Object
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

// Create a Vertex Buffer Object and copy the vertex data to it
GLuint vbo;
glGenBuffers(1, &vbo);

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(tmpdata), tmpdata, GL_STATIC_DRAW);

// Create and compile the vertex shader
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &VertSource, NULL);
glCompileShader(vertexShader);

// Create and compile the fragment shader
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &FragSource, NULL);
glCompileShader(fragmentShader);


// Link the vertex and fragment shader into a shader program
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glBindFragDataLocation(shaderProgram, 0, "outColor");
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);

// Specify the layout of the vertex data
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), 0);

GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));

GLint texAttrib = glGetAttribLocation(shaderProgram, "texcoord");
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(5 * sizeof(GLfloat)));

// Load texture
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);

// Black/white checkerboard
float pixels[] = {
    1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f,
    0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f
};
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_FLOAT, pixels);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}

void GLRenderer::paintGL()
{
    // Clear the screen to black
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}

0 个答案:

没有答案