Can't Get Spinning Cube To Render In OpenGL - c
I'm trying to get a spinning 3D cube but I can't get it to render. There's no error, but it's just a black screen. Also, there's no checking whether the Fragment shader is working but I can assure you it is I just don't have it included here.
Here's what my code looks like:
#include <glad/glad.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <cglm/cglm.h>
#include <cglm/mat4.h>
#include <cglm/vec3.h>
#include <stdio.h>
#include <stdlib.h>
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f};
GLuint elements[] = {
0, 1, 2,
2, 3, 0};
static const char *vertex_shader_text =
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec2 aTexCoord;\n"
"out vec2 TexCoord;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
" gl_Position = projection * view * model * vec4(aPos, 1.0f);\n"
" TexCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
"}";
static const char *fragment_shader_text =
"#version 330 core\n"
"varying vec3 color;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(color, 1.0);\n"
"}\n";
static void error_callback(int error, const char *description) {
fprintf(stderr, "Error: %s\n", description);
}
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
int main(void) {
GLFWwindow *window;
GLuint vbo, vertex_shader, fragment_shader, program;
GLint mvp_location, vpos_location, vcol_location;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (!window) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
fprintf(stderr, "Could Not Load GLFW\n");
exit(1);
}
glfwSwapInterval(1);
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
glCompileShader(vertex_shader);
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
glCompileShader(fragment_shader);
GLint status;
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &status);
if(!status){
char buffer[512];
glGetShaderInfoLog(vertex_shader, 512, NULL, buffer);
puts(buffer);
}
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
while (!glfwWindowShouldClose(window)) {
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float)height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
mat4 model = {{1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}};
mat4 view = {{1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}};
mat4 projection = {{1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}};
vec3 axis = {0.5f, 1.0f, 0.0f};
vec3 dist = {0.0f, 0.0f, -3.0f};
glm_rotate(model, glfwGetTime(), axis);
glm_translate(view, dist);
glm_perspective(0.79, width / height, 0.1f, 100.0f, projection);
unsigned int modelLoc = glGetUniformLocation(program, "model");
unsigned int viewLoc = glGetUniformLocation(program, "view");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, (const GLfloat *)model);
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, (const GLfloat *)view);
unsigned int projectionLoc = glGetUniformLocation(program, "projection");
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
You have to initialize the matrices with the Identity matrix:
mat4 model = {
{1.f, 0.f, 0.f, 0.f},
{0.f, 1.f, 0.f, 0.f},
{0.f, 0.f, 1.f, 0.f},
{0.f, 0.f, 0.f, 1.f}};
Also do so for view and projection.
The data types for the arguments of glm_rotate and glm_perspective must be float:
glm_rotate(model, (float)glfwGetTime(), axis);
glm_translate(view, dist);
glm_perspective((float)0.79, (float)width / (float)height, 0.1f, 100.0f, projection);
You also forgot to create the Index buffer:
glBindVertexArray(VAO);
// [...]
unsigned int IBO;
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
to install the program:
glUseProgram(program);
to set the projection matrix uniform.
unsigned int projectionLoc = glGetUniformLocation(program, "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, (const GLfloat *)projection);
Finally you must change the fragment shader. For example:
#version 330 core
in vec2 TexCoord;
void main()
{
gl_FragColor = vec4(TexCoord.xy, 0.5, 1.0);
};
Related
jfreechart bar color difference issue
arrayOfPaint[i] = new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F, Color.green); // arrayOfPaint[i] = new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, Color.red); } } } /* arrayOfPaint[0] = new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, Color.red); arrayOfPaint[1] = new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F, Color.green); arrayOfPaint[2] = new GradientPaint(0.0F, 0.0F, Color.blue, 0.0F, 0.0F, Color.blue); */ CategoryDataset paramCategoryDatasetCylinder = localDefaultCategoryDatasetCylinder; JFreeChart localJFreeChartCylinder = ChartFactory.createBarChart3D("", "Section", "Percentage", paramCategoryDatasetCylinder ,PlotOrientation.HORIZONTAL, true, true, false); CategoryPlot localCategoryPlotCylinder = (CategoryPlot)localJFreeChartCylinder.getPlot(); /* if(compCode.equalsIgnoreCase("0094000") && domainName.equalsIgnoreCase("L and T")){ CategoryAxis yAxis = (CategoryAxis)localCategoryPlotCylinder.getDomainAxis(); yAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); } */ localCategoryPlotCylinder.setBackgroundPaint(Color.white); localCategoryPlotCylinder.setRangeGridlinesVisible(true); localCategoryPlotCylinder.setRangeGridlinePaint(Color.BLACK); localJFreeChartCylinder.setBackgroundPaint(new GradientPaint(0.0F, 0.0F, Color.white, 350.0F, 0.0F, Color.white, true)); I have set GREEN color for both of the bars but I am getting difference in color. I am unable to figure out why it is happpening. Please help in this regard
Vulkan - Debugging A Camera Matrix Problem
The problem I'm having is that I'm exporting a scene from Blender and my scene is rotated 90 about the Y axis. It's clearly a matrix issue and I'm having problems visualizing the solution. Blender is configured to export with 'up' = (0, 1, 0) and 'forward' = (0, 0, 1). I'm using row major matrices as indicated by the following. mat4x4 cons_mat4x4_perspective(float fovy, float aspect_ratio, float near, float far) { mat4x4 result = cons_mat4x4_zero(); float half_tan_fovy = tan(fovy / 2.0f); /* in radians */ result.as_rows[0] = cons_vec4(1.0f / (aspect_ratio * half_tan_fovy), 0.0f, 0.0f, 0.0f); result.as_rows[1] = cons_vec4(0.0f, 1.0f / half_tan_fovy, 0.0f, 0.0f); result.as_rows[2] = cons_vec4(0.0f, 0.0f, -(far + near) / (far - near), -1.0f); result.as_rows[3] = cons_vec4(0.0f, 0.0f, -(2.0f * far * near) / (far - near), 0.0f); return result; } // Vulkan clip matrix clip = cons_mat4x4_floats(1.0f, 0.0f, 0.0f, 0.0f, 0.0f,-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 1.0f); mat4x4 cons_mat4x4_lookat(vec3 eye, vec3 center, vec3 up) { mat4x4 result = cons_mat4x4_identity(); vec3 f = normalize(vec3_sub(center, eye)); vec3 s = normalize(cross(f, up)); vec3 u = cross(s, f); result.as_rows[0] = cons_vec4(s.x, u.x, -f.x, 0.0f); result.as_rows[1] = cons_vec4(s.y, u.y, -f.y, 0.0f); result.as_rows[2] = cons_vec4(s.z, u.z, -f.z, 0.0f); result.as_rows[3] = cons_vec4(-vec3_dot(s, eye), vec3_dot(u, eye), vec3_dot(f, eye), 1.0f); return result; } My model matrix is the identity matrix and I'm creating a lookat matrix with the following parameters: vec3 _camera = cons_vec3(0.0f, 5.0f, 0.0f); vec3 _at = cons_vec3(0.0f, 0.0f, 0.0f); cons_mat4x4_lookat(_camera, _at, cons_vec3(0.0f, 1.0f, 0.0f));
How to properly initialize a model matrix in C?
I tried to convert the code of the question I asked over here. But when I try to run it, I run into an exception and it points to the mat4(which is an 2-d array of 4x4 floats) header of this(CGLM, which is use for OpenGL math in C as GLM is C++ only library) library. The C++ code works fine and runs perfectly but why does it crash in C. Here is the C snippet of the code where 'CGLM' is used(this is the code that crashes):- glUseProgram(shaderProgram); mat4 model = { { 1.0f, 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, 0.0f, 1.0f }, }; mat4 projection; glm_ortho(0.0f, (GLfloat)(WIDTH), (GLfloat)(HEIGHT), 0.0f, -1.0f, 1.0f, projection); vect2 scale = { 2.0f, 2.0f }; vect2 position = { 50.0f, 0.0f }; vec4 color = { 1.0f, 1.0f, 1.0f, 0.5f }; GLfloat rotation = 0.0f; glm_translate(model, (vec3){ position[0], position[1], 0.0f }); glm_translate(model, (vec3){ 0.5f * scale[0], 0.5f * scale[1], 0.0f }); glm_rotate(model, rotation, (vec3){ 0.0f, 0.0f, 1.0f }); // Why is this function crashing? glm_translate(model, (vec3){ -0.5f * scale[0], -0.5f * scale[1], 0.0f }); glm_scale(model, (vec3){ scale[0] * width, scale[1] * height, 1.0f }); glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, (GLfloat *)projection); glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, (GLfloat *)model); glUniform4f(glGetUniformLocation(shaderProgram, "spriteColor"), color[0], color[1], color[2], color[3]); Here is the C++ snippet of the code where GLM is used:- glUseProgram(shaderProgram); glm::mat4 model(1.0f); glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(WIDTH), static_cast<GLfloat>(HEIGHT), 0.0f, -1.0f, 1.0f); glm::vec2 scale = glm::vec2(2.0f, 2.0f); glm::vec2 position = glm::vec2(50.0f, 0.0f); glm::vec4 color = glm::vec4(1.0f, 1.0f, 1.0f, 0.5f); GLfloat rotation = 0.0f; model = glm::translate(model, glm::vec3(position, 0.0f)); model = glm::translate(model, glm::vec3(0.5f * scale.x, 0.5f * scale.y, 0.0f)); model = glm::rotate(model, rotation, glm::vec3(0.0f, 0.0f, 1.0f)); model = glm::translate(model, glm::vec3(-0.5f * scale.x, -0.5f * scale.y, 0.0f)); model = glm::scale(model, glm::vec3(scale * glm::vec2(width, height), 1.0f)); glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, glm::value_ptr(model)); glUniform4f(glGetUniformLocation(shaderProgram, "spriteColor"), color.x, color.y, color.z, color.w);
See CGLM documentation According to the getting started Aligment is Required: vec4 and mat4 requires 16 byte aligment because vec4 and mat4 operations are vectorized by SIMD instructions (SSE/AVX). So you need to initialize identity matrix like: mat4 model; glm_mat4_identity(model); .... mat4 projection; glm_mat4_identity(projection); glm_ortho(0.0f, (GLfloat)(WIDTH), (GLfloat)(HEIGHT), 0.0f, -1.0f, 1.0f, projection);
How to pass a GLfloat array through function in C++?
I'm trying to pass a GLfloat type array through a function but the new variable only take the 1st value of the array (0.5f in this case), what am i doing wrong? My Code: void main() { // some code GLfloat vertices[] = { // Positions // Colors // Texture Coords 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Top Right 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Bottom Right -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Bottom Left -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // Top Left }; passingArray(vertices); } void passingArray(GLfloat anotherVertices[]) { // this anotherVertices only take 1st value of the vertices array }
GL_ERROR: 0x0502
I hope to create an offScreen FBO, and use glReadPixels() to get the value of RGBA in this offScreen FBO. unfortunately, I got the error: GL_ERROR:0x0502 #import "ViewController.h" #define BUFFER_OFFSET(i) ((char *)NULL + (i)) #interface ViewController () { GLuint backFBO; GLuint defaultFBO; int fbo_width; int fbo_height; // GLuint _vertexArray; GLuint _vertexBuffer; GLuint _texCoordBuffer; GLuint depthBuffer; // GLKMatrix4 _modelViewProjectionMatrix; GLKMatrix3 _normalMatrix; float _rotation; } #property (nonatomic, strong) GLKBaseEffect *baseEffect; #property (nonatomic, strong) EAGLContext *context; - (void)setupGL; - (void)tearDownGL; - (void)setupFBO; #end the vertex data and normal data of object in onScreen FBO GLfloat gVertexData[] = { 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f }; #implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //1. setup context self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 ]; if (!self.context) { NSLog(#"Failed to create GLES Context"); } GLKView *view = (GLKView *)self.view; view.context = self.context; view.drawableDepthFormat = GLKViewDrawableDepthFormat24; [self setupGL]; } - (void)viewDidUnload { [super viewDidUnload]; [self tearDownGL]; if ([EAGLContext currentContext] == self.context) { [EAGLContext setCurrentContext:nil]; } self.context = nil; } - (void) setupGL { [EAGLContext setCurrentContext:self.context]; self.baseEffect = [[GLKBaseEffect alloc] init]; glEnable(GL_DEPTH_TEST); glGenVertexArraysOES(1, &_vertexArray); glBindVertexArrayOES(_vertexArray); glGenBuffers(1, &_vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(gVertexData), gVertexData, GL_STATIC_DRAW); glEnableVertexAttribArray(GLKVertexAttribPosition); glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0)); glEnableVertexAttribArray(GLKVertexAttribNormal); glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12)); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArrayOES(0); // initialize FBO [self setupFBO]; } - (void) tearDownGL { [EAGLContext setCurrentContext:self.context]; glDeleteBuffers(1, &_vertexBuffer); } - (void)update { float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height); GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f); self.baseEffect.transform.projectionMatrix = projectionMatrix; // Compute the model view matrix for the object rendered with ES2 GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -2.0f); modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotation, 1.0f, 1.0f, 1.0f); self.baseEffect.transform.modelviewMatrix = modelViewMatrix; _rotation += self.timeSinceLastUpdate * 0.5f; } - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { // render FBO tex [self renderFBO]; // reset to main framebuffer [((GLKView *) self.view) bindDrawable]; glViewport(0, 0, self.view.bounds.size.width, self.view.bounds.size.height); // render main glClearColor(0.65f, 0.65f, 0.65f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindVertexArrayOES(_vertexArray); [self.baseEffect prepareToDraw]; glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } #pragma mark - FBO // intialize FBO - (void)setupFBO { fbo_width = 512; fbo_height = 512; glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO); NSLog(#"default FBO: %d", defaultFBO); glGenFramebuffers(1, &backFBO); glGenRenderbuffers(1, &depthBuffer); glBindFramebuffer(GL_FRAMEBUFFER, backFBO); glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24_OES, fbo_width, fbo_height); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer); // FBO status check GLenum status; status = glCheckFramebufferStatus(GL_FRAMEBUFFER); switch(status) { case GL_FRAMEBUFFER_COMPLETE: NSLog(#"fbo complete"); break; case GL_FRAMEBUFFER_UNSUPPORTED: NSLog(#"fbo unsupported"); break; default: /* programming error; will fail on all hardware */ NSLog(#"Framebuffer Error"); break; } glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO); } // render FBO - (void)renderFBO { glBindFramebuffer(GL_FRAMEBUFFER, backFBO); glViewport(0,0, fbo_width, fbo_height); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); **//in here, I hope to get the value of RGBA in offScreen FBO** Byte pixelColor[4] = {0,}; glReadPixels(50, 50, fbo_width, fbo_height, GL_RGBA, GL_UNSIGNED_BYTE, pixelColor); glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO); } when I comment glReadPixels(), it's works well. Any help or advice would be much appreciated.
in your offScreen FBO, you should bind a GL_RENDERBUFFER, so you should add below code: //create a new FBO glBindFramebuffer(GL_FRAMEBUFFER, backFBO); // add GLuint _colorBuffer; glGenRenderbuffers(1, &_colorBuffer); glBindRenderbuffer(GL_RENDERBUFFER, _colorBuffer); glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8_OES, fbo_width , fbo_height); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER, _colorBuffer); I hope this will fix your error. good luck !!!