implement an opengl code to load two textures in two viewports, add FBO concept also to…
- User
implement an opengl code to load two textures in two viewports, add FBO concept also to this code
- Assistant
To implement an OpenGL code that loads two textures into two different viewports and incorporates the Framebuffer Object (FBO) concept, follow these steps. This example will use modern OpenGL (OpenGL 3.3+). It will also use GLEW for managing OpenGL extensions and GLFW for window management.
Make sure you have GLFW and GLEW installed in your development environment.
Here is a complete example code:
#include <GL/glew.h> #include <GLFW/glfw3.h> #include <iostream> // Vertex shader source code const char* vertexShaderSource = R"glsl( #version 330 core layout(location = 0) in vec3 aPos; layout(location = 1) in vec2 aTexCoord; out vec2 TexCoord; void main() { gl_Position = vec4(aPos, 1.0); TexCoord = aTexCoord; } )glsl"; // Fragment shader source code const char* fragmentShaderSource = R"glsl( #version 330 core out vec4 FragColor; in vec2 TexCoord; uniform sampler2D texture1; void main() { FragColor = texture(texture1, TexCoord); } )glsl"; // Function to compile the shaders and create a shader program GLuint createShaderProgram() { // Compile vertex shader GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); glCompileShader(vertexShader); // Check for compile errors GLint success; glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); if (!success) { GLchar infoLog[512]; glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); std::cerr << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; } // Compile fragment shader GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); glCompileShader(fragmentShader); // Check for compile errors glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); if (!success) { GLchar infoLog[512]; glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; } // Link shaders to create a shader program GLuint shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glLinkProgram(shaderProgram); // Check for linking errors glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); if (!success) { GLchar infoLog[512]; glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl; } // Clean up shaders (we no longer need them once linked into the program) glDeleteShader(vertexShader); glDeleteShader(fragmentShader); return shaderProgram; } // Function to create a texture from an image file (placeholder function) GLuint createTexture(const char* path) { // In a real application, you'd use an image loading library like stb_image to load the texture. // For now, we'll just create a simple colored texture placeholder. GLuint textureID; glGenTextures(1, &textureID); glBindTexture(GL_TEXTURE_2D, textureID); unsigned char pixelData[3] = { 255, 0, 0 }; // Red pixel for placeholder glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); return textureID; } // Function to initialize a framebuffer object (FBO) and attach a texture to it GLuint createFBO(GLuint texture) { GLuint fbo; glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cerr << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << std::endl; glBindFramebuffer(GL_FRAMEBUFFER, 0); return fbo; } int main() { // Initialize GLFW if (!glfwInit()) { std::cerr << "Failed to initialize GLFW" << std::endl; return -1; } // Set OpenGL version to 3.3 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Create a GLFW window GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL Window", NULL, NULL); if (!window) { std::cerr << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } // Make the window's context current glfwMakeContextCurrent(window); // Initialize GLEW to setup the OpenGL Function pointers glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { std::cerr << "Failed to initialize GLEW" << std::endl; return -1; } // Viewport settings glViewport(0, 0, 800, 600); // Enable depth testing glEnable(GL_DEPTH_TEST); // Create and compile shaders and shader program GLuint shaderProgram = createShaderProgram(); // Vertex data for a rectangle float vertices[] = { // positions // texture coords 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f }; unsigned int indices[] = { 0, 1, 3, // first triangle 1, 2, 3 // second triangle }; GLuint VBO, VAO, EBO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); // Position attribute glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); // Texture coordinate attribute glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1); // Load textures GLuint texture1 = createTexture("path/to/your/texture1.png"); // FBO for texture1 GLuint fbo1 = createFBO(texture1); // Main render loop while (!glfwWindowShouldClose(window)) { // Process events glfwPollEvents(); // Clear the screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Use our shader program glUseProgram(shaderProgram); // Bind our texture glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture1); glUniform1i(glGetUniformLocation(shaderProgram, "texture1"), 0); // Render to viewport 1 glBindFramebuffer(GL_FRAMEBUFFER, 0); // Stop rendering to the FBO and render to the screen glViewport(0, 0, 400, 600); glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // Render to viewport 2 glViewport(400, 0, 400, 600); glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // Swap buffers glfwSwapBuffers(window); } // Cleanup glDeleteBuffers(1, &VBO); glDeleteBuffers(1, &EBO); glDeleteVertexArrays(1, &VAO); glDeleteProgram(shaderProgram); // Terminate GLFW glfwTerminate(); return 0; }To run this code:
- Ensure you have the
GLFWandGLEWlibraries installed and linked correctly. - Replace
"path/to/your/texture1.png"with the actual path to your texture.
This code sets up an OpenGL context, compiles shaders, creates a VAO/VBO/EBO, loads a texture, and uses an FBO. It uses two viewports to render the same texture in two different areas of the window.
The code assumes a placeholder for texture loading. In an actual application, you can use a library like
stb_imageto load image files into textures. - Ensure you have the