Displacement map in vertex shader

I’m new to opengl
I would like to make a terrain using a vertex shader.
I mapped a displacement map (geo_texture) on to a 256*256 vertices plane(mesh)
vertex shader:

out vec3 color;
out vec2 texCoord;

uniform mat4 model;
uniform mat4 proj_view;
uniform sampler2D geo_texture;

void main()
{
   color = aPos;
   texCoord = aTexCoord;

   vec4 displacement = texture(geo_texture, texCoord);
   vec3 pos = aPos;
    pos.y += normalize( displacement.x);  

   gl_Position = proj_view * model * vec4(pos, 1.0);   
}

and this is my texture class

Texture::Texture(const char *fileName, GLuint shader_program_id, const char *uniName){
    uniformName = uniName;

    int width_texture, height_texture, channel_texture;
    unsigned char* bytes = stbi_load(fileName, &width_texture, &height_texture, &channel_texture, 0);
    const char* failureReason = stbi_failure_reason();
    if(failureReason!=nullptr){
        glfwSetErrorCallback(glfw_error_callback);
    }

    GLuint texture;
    glGenTextures(1, &texture);
    glActiveTexture(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width_texture, height_texture, 0, GL_RGB, GL_UNSIGNED_BYTE, bytes );

    glGenerateMipmap(GL_TEXTURE_2D);

    stbi_image_free(bytes);
    glBindTexture(GL_TEXTURE_2D, 0);
    glGetUniformLocation(shader_program_id, uniformName);
    uni_texture = texture;
}

and main:

int main(int argc, char** argv)
{   
    if (argc>=2) chdir(argv[1]);

    
    
    glfwInit();

    // set glfw's OpenGL to VERSION=3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    // set glfw's to CORE profile (modern functions ONLY)
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    GLFWwindow* window = glfwCreateWindow(window_width, window_height, extract_version(argv[0]), NULL, NULL);
    
    // event handling
    glfwSetErrorCallback(glfw_error_callback);
    glfwSetWindowCloseCallback(window, glfw_window_close_callback);
    glfwMakeContextCurrent(window);

    //load GLAD to configures OpenGL
    gladLoadGL();
    // adjust viewport
    glViewport(0, 0, window_width, window_height);

    // Generates Shader object using shaders defualt.vert and default.frag
    Shader geo_shader = Shader(geo_vert, geo_frag);
    Shader blade_shader = Shader(blade_vert, blade_frag);


    // camera
    float fov = glm::radians(45.0f), near = 0.1f, far = 1000.0f;
    glm::vec3 eye_position = glm::vec3(0.0f, 0.5f, 150.0f), eye_direction = glm::vec3(0.0f, 0.0f, -1.0f);
    Camera cam = Camera(eye_position, eye_direction, window_width, window_height, fov, near, far);

    //model
    ObjLoader obj = ObjLoader();
    Mesh geo_mesh = obj.load_obj_from_file(geo_obj, false, false, true);
    std::vector<glm::mat4> instance_mats;


    srand(static_cast<unsigned int>(time(nullptr)));
    
    float rotation =45.0f;
    glm::vec3 scale = glm::vec3(0.1f);
    glm::mat4 proj_view = cam.get_matrix();
    for(auto vert: geo_mesh.vertices){
        int random_number = rand() % 100;
        if(random_number>96){
        glm::mat4 transformation = glm::mat4(1.0f);     
        transformation = glm::translate(transformation, vert.position);
        instance_mats.push_back( transformation);}  
    }
    int a = instance_mats.size();
    Mesh blade_mesh = obj.load_obj_from_file(blade_obj, false, false, true,  instance_mats.size(), instance_mats);
    
    //enable depth buffer
    glEnable(GL_DEPTH_TEST);
    

    // texture
    Texture geo_texture = Texture(geo_texture_img, geo_shader.ID, "geo_texture");
    Texture grass_texture = Texture(blade_texture_img, blade_shader.ID, "grass_texture");
    
//////////////////////////////////////////////////////////////////////////////////////////////////////    

    while(!glfwWindowShouldClose(window)){
        // Specify the color of the background
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        // Clean the back buffer and assign the new color to it
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        // every assigning stuff go after activate
        glm::mat4 model_orientation = glm::mat4(1.0f);
        model_orientation = glm::rotate(model_orientation, glm::radians(rotation), glm::vec3(1.0f, 0.0f, 0.0f));
        geo_mesh.draw(geo_shader,cam, model_orientation, geo_texture);
        // blade_mesh.draw(blade_shader, cam, model_orientation, grass_texture);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }


/////////////////////////////////////////////////////////////////////////////////////////////////////////

    // delete everything
    geo_texture.remove();
    grass_texture.remove();
    geo_shader.remove();
    blade_shader.remove();
    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
}

the recent output shows the mesh being displayed but the vertices are not displaced. So I suspect there should be some mistakes in the setting of the texture, or the rendering sequence?

New contributor

NewDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật