J'ai une affectation que je dois lier 2 images; une image en niveaux de gris et sa version colorée. Je dois transférer tous les pixels colorés de l'image colorée pour afficher l'image dans ma fenêtre en tant qu'objet 3D, et utiliser l'image en niveaux de gris pour déterminer la hauteur (gradient) de chaque pixel. J'ai travaillé dessus pendant plus de 5 jours, et je ne peux jamais rien obtenir à rendre dans ma fenêtre. Je reçois toujours un écran vide, peu importe ce que je fais.Problème avec le rendu d'une table de hauteur en utilisant opengl et cimg
Voici ce que j'utilise pour charger les images:
// 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);
}
Vertex shader:
#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;
}
shader Fragment:
#version 330 core
in vec3 ourColor;
out vec4 color;
void main()
{
color = vec4(ourColor, 1.0f);
}
Merci