Jump to content
  • Advertisement
Sign in to follow this  

OpenGL glReadPixels() isn't working

This topic is 4141 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry for the code dump, but I've been fighting with this all day and I can't seem to figure out what's wrong. I can see a white quad in the middle of the screen, but all the .bmps that are written are just black. If I don't call glClear() the bitmaps have what looks like the window under the SDL window, except the colors are inverted and I can see a black box where the quad should be. Any ideas?
#include <iostream>
#include <sstream>
#include <GL/glew.h>
#include <SDL/SDL.h>
#include "utils.h"

int main(int argc, char** argv)
	int width = 640;
	int height = 480;


	SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );


	SDL_SetVideoMode(width, height, 32, SDL_OPENGL);


	gluPerspective(45.0f, (float)width/height, 1.0f, 1000.0f);

	gluLookAt(0, 0, 0, 0, 0, 1.0f, 0, 1.0f, 1);
	glViewport(0, 0, width, height);

	char* pixels = new char[width * height * 3];

	glClearColor(0, 0, 0, 0.0f);

	for (int i = 0; i < 10; i++)

			glVertex3f(1.0f, 1.0f, 10);
			glVertex3f(-1.0f, 1.0f, 10);
			glVertex3f(-1.0f, -1.0f, 10);
			glVertex3f(1.0f, -1.0f, 10);

		memset(pixels, 128, width * height * 3);
		glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
		std::stringstream ss;
		ss << "blah_" << i << ".bmp";
		WriteBitmap(ss.str(), pixels, width, height, 3);

		GLenum err = glGetError();
		if (err != GL_NO_ERROR)
			std::cout << "zomg!" << std::endl;

	return 0;

Share this post

Link to post
Share on other sites
char* pixels = new char[width * height * 3]; != GL_UNSIGNED_BYTE

u need GLubyte * pixels = new GLubyte

Share this post

Link to post
Share on other sites
Cripes. I figured it out. The problem was where I copy/pasted a version of WriteBitmap() that accepted an array of floats to create the char array version, and didn't remove the scaling-1.0-to-255 code.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!