使用opengl和cimg渲染高度图时遇到麻烦

时间:2016-09-23 20:39:56

标签: opengl cimg

我有一项任务,我必须链接2张图片;一个灰度图像及其彩色版本。我必须从彩色图像传输所有彩色像素,以在我的窗口中将图像显示为3D对象,并使用灰度图像来确定每个像素的高度(渐变)。我已经在这个工作超过5天,并且永远无法在窗口中呈现任何内容。无论我做什么,我总是得到一个空白的屏幕。

以下是我用来加载图片的内容:

// Initialize images and vectors
CImg<unsigned char> gray_image("pictures/gray.bmp");
CImg<unsigned char> color_image("pictures/color.bmp");

// Doesn't matter if it's gray_image or color_image, since they both have the same size
int mapHeight = gray_image.height();
int mapWidth = gray_image.width();

// Vertex Data container, storing vertex positions from gray_image
// This vector will store data in the format of: |X, Y, Z|
vector<GLfloat>vertexData;
for (int i = 0; i < mapHeight; i++)
{
    for (int j = 0; j < mapWidth; j++)
    {
        vertexData.push_back((GLfloat)gray_image(j)); // X-Axis
        vertexData.push_back((GLfloat)gray_image(i)); // Y-Axis
        vertexData.push_back((GLfloat)gray_image(0)); // Z-Axis, TEMPORARILY SET TO 0!
    }
}

// Vertex Color container, storing vertex colors from color_image
// This vector will store data in the format of: |R, G, B|
vector<GLfloat>vertexColor;
for (int i = 0; i < mapHeight; i++)
{
    for (int j = 0; j < mapWidth; j++)
    {
        vertexColor.push_back((GLfloat)color_image(j, i, 0, 0)); // R   
        vertexColor.push_back((GLfloat)color_image(j, i, 0, 1)); // G
        vertexColor.push_back((GLfloat)color_image(j, i, 0, 2)); // B
    }
}

// Vertex Indices container, storing vertex indices from gray_image
// This vector will store the 3 indices of the 2 triangles (upper and lower triangle) which form each quad

vector<GLuint>vertexIndices;
for (int i = 0; i < mapHeight - 1; i++)
{
    for (int j = 0; j < mapWidth - 1; j++)
    {
        int index = (mapWidth * j) + i;

        // Top triangle
        vertexIndices.push_back(index);                // V0
        vertexIndices.push_back(index + mapWidth + 1); // V3
        vertexIndices.push_back(index + 1);            // V1

        // Bottom triangle
        vertexIndices.push_back(index);                // V0
        vertexIndices.push_back(index + mapWidth);     // V2
        vertexIndices.push_back(index + mapWidth + 1); // V3
    }
}

// Initialize VAO, EBO, and VBOs
GLuint VAO, VBOData, VBOColor, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBOData);
glGenBuffers(1, &VBOColor);
glGenBuffers(1, &EBO);

// Bind VAO
glBindVertexArray(VAO);

// Bind and implement VBO for vertex positions
glBindBuffer(GL_ARRAY_BUFFER, VBOData);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), &vertexData, GL_STATIC_DRAW);

// Bind and implement EBO for indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertexIndices), &vertexIndices, GL_STATIC_DRAW);

// Connecting X, Y, and Z to shader
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);

// Unbind VBOData (without unbinding EBO)
glBindBuffer(GL_ARRAY_BUFFER, 0);

// Bind and implement VBO for colors
glBindBuffer(GL_ARRAY_BUFFER, VBOColor);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexColor), &vertexColor, GL_STATIC_DRAW);

// Connecting RGB to shader
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);

// Unbind VBOColor
glBindBuffer(GL_ARRAY_BUFFER, 0);

// Unbind VAO
glBindVertexArray(0); 

// ----------Rendering (Game Loop)----------

while (!glfwWindowShouldClose(window))
{
    // Calculate deltatime of current frame
    GLfloat currentFrame = glfwGetTime();
    deltaTime = currentFrame - lastFrame;
    lastFrame = currentFrame;

    // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
    glfwPollEvents();
    camera_key_callback();

    // Render
    // Clear the colorbuffer
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Camera and View transformation
    glm::mat4 model;
    glm::mat4 view;
    glm::mat4 projection;
    model = glm::rotate(model, -55.0f, glm::vec3(1.0f, 0.0f, 0.0f));
    view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
    projection = glm::perspective(fov, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f);

    // Get their uniform location
    GLint modelLoc = glGetUniformLocation(shaderProgram, "model");
    GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
    GLint projLoc = glGetUniformLocation(shaderProgram, "projection");

    // Pass the matrices to the shader
    glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
    glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
    glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));

    glBindVertexArray(VAO);
    glDrawElements(GL_TRIANGLES, sizeof(vertexIndices), GL_UNSIGNED_INT, 0); // Replaces glDrawArrays() WHAT TO DO FOR SECOND PARAMETER?!
    glBindVertexArray(0);

    // Swap the screen buffers
    glfwSwapBuffers(window);
}

顶点着色器:

#version 330 core

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;

out vec3 ourColor;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    gl_Position = projection * view * model * vec4(position.x, position.y,    position.z, 1.0);
    ourColor = color;
}

片段着色器:

#version 330 core

in vec3 ourColor;

out vec4 color;

void main()
{
    color = vec4(ourColor, 1.0f);
}

由于

1 个答案:

答案 0 :(得分:0)

绘制调用的size参数错误。 sizeof(vertexIndices)返回基础类型的大小。在这种情况下,这是sizeof(vector<GLuint>)因此是矢量对象的大小而不是存储在矢量中的数据。你真正想要的是vertexIndices.size()