Help me with glReadPixel and mouse PPLLLLEEEAAASSSEEE

No one have responded to my past posts. I assume someone does not know how to help me because of no code posted. So here is the code. How do you put mouse and glReadPixels in code to get return rgb value from mouse pointer on GL window for the deadcolor to make it transparent.

/*

Color Keying

Author : Nate ‘m|d’ Miller
Email : nkmiller@calpoly.edu
Web : http://nate.scuzzy.net
Date : 03/10/00

This simple program loads a .raw image and makes the color (0, 0, 255)
transparent. The .raw image is assumed to be a 24-bit image. The image is
read into a buffer that is of size w * h * 3, the 3 is because each pixel has a
Red, Green and Blue component. Once the image has been read in, a destination
buffer is created that is of size w * h * 4, the 4 is because of the alpha
channel. The image buffer is then copied into the destination buffer pixel by
pixel. If the current pixel has the color (0, 255, 0) it is given an alpha
value of 0 in the destination buffer, a value which will make the pixel
transparent. For all other colors an alpha value of 255 is given to the pixel
in the destination buffer which will make it visible. The end result is that
pixels that have the color (0, 255, 0) are not drawn when we use the proper
blending mode.

Make sure you link too glu32.lib glut32.lib and opengl32.lib when you
compile this!
*/
#include <stdlib.h>
#include <stdio.h>
#include <gl/glut.h>

typedef float vec3_t[3];
typedef unsigned char byte;

int winW = 640;
int winH = 480;
const char *appName = “Color Keying”;

vec3_t eye = {0, 0, 30};
vec3_t rot = {0, 0, 0};
vec3_t rotAmnt = {1.2f, 0.4f, -0.75f};

int texId = 13; // texture id to use
int imageDims[2] = {128, 128}; // image dimensions, raw doesn’t have this info
int imageSize = imageDims[0] * imageDims[1];
int deadColor[3] = {0, 255, 0}; // neon green gets killed
int blend = 1;

/*
Notice I used GL_NEAREST below and not GL_LINEAR. With GL_LINEAR the green
color bleeds into surrounding pixels giving the outer edge of the image a green
tint. GL_LINEAR prevents this from happening.
*/
void ImageUpload(byte image)
{
glBindTexture(GL_TEXTURE_2D, texId);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
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_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
/

This is the most important call here. We use GL_RGBA because we just
added an alpha channel to the image so it is no longer RGB.
*/
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, imageDims[0], imageDims[1], 0,
GL_RGBA, GL_UNSIGNED_BYTE, image);
}

byte *ImageAddAlpha(byte *src)
{
// allocate buffer for a RGBA image
byte *dest = new byte[imageSize * 4];

// walk through the original image and copy it over to the dest image
for (int i = 0, j = 0; i < imageSize * 3; i += 3, j += 4)
{
/*
If the pixels color matches our deadColor we give it a alpha value of
0 which equates to a transparent pixel. Otherwise the alpha value of
the pixel is set to 255, full alpha.
*/
if (src == deadColor[0] && src[i + 1] == deadColor[1] &&
src[i + 2] == deadColor[2])
dest[j + 3] = 0;
else
dest[j + 3] = 255;

dest[j] = src[i];
dest[j + 1] = src[i + 1];
dest[j + 2] = src[i + 2];
}

return dest;
}

void LoadRaw(char *name)
{
FILE *in = fopen(name, “r”);
byte *buffer = new byte[imageSize * 3]; // allocate buffer for a RGB image
byte *image = 0;

if (!in)
{
printf(“Unable to find %s
!”, name);
return;
}

// read the image in
fread(buffer, 1, imageSize * 3, in);
fclose(in);

// release all our memory, image has been uploaded
image = ImageAddAlpha(buffer);
delete [] buffer;
ImageUpload(image);
delete [] image;
}

void glutDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(-eye[0], -eye[1], -eye[2]);
glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
glRotatef(rot[2], 0.0f, 0.0f, 1.0f);

glBindTexture(GL_TEXTURE_2D, texId);

glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(-20.0f, -20.0f, 0.0f);
glTexCoord2f(1, 0);
glVertex3f(20.0f, -20.0f, 0.0f);
glTexCoord2f(1, 1);
glVertex3f(20.0f, 20.0f, 0.0f);
glTexCoord2f(0, 1);
glVertex3f(-20.0f, 20.0f, 0.0f);
glEnd();

glutSwapBuffers();
}

void glutResize(int w, int h)
{
if (!h)
return;

winW = w;
winH = h;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, winW, winH);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(90, (double) winW / winH, 1, 4096);

glutPostRedisplay();
}

void glutIdle(void)
{
rot[0] += rotAmnt[0];
rot[1] += rotAmnt[1];
rot[2] += rotAmnt[2];

if (rot[0] > 360 | | rot[0] < -360)
rot[0] = 0;
if (rot[1] > 360 | | rot[1] < -360)
rot[1] = 0;
if (rot[2] > 360 | | rot[2] < -360)
rot[2] = 0;

glutPostRedisplay();
}

void glutKeyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
case ‘q’:
case ‘Q’:
exit(1);
break;
case ‘b’:
case ‘B’:
blend = !blend;

if (blend)
glEnable(GL_BLEND);
else
glDisable(GL_BLEND);
break;
}
}

void glInit (void)
{
glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);

LoadRaw(“image.raw”);

printf("Q - Quit
");
printf("B - Enables/Disables blending
");
}

void main (void)
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(winW,winH);
glutCreateWindow(appName);
glutDisplayFunc(glutDisplay);
glutReshapeFunc(glutResize);
glutKeyboardFunc(glutKeyboard);
glutIdleFunc(glutIdle);

glInit ();

glutMainLoop();
}

Not sure I’m following you. Do you want to read the pixel value under the mouse cursor, and make that color in the texture transparent?

Well, to read the color value from the framebuffer, do like this.

GLubyte color[3];
glReadPixels(mouseX, winH - 1 - mouseY, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, color);

And you alreay have the code to make the texture transparent.

Yes that is what I mean. Is the code you just posted a full code for mouse and glreadpixel?

Waldoo

Am i missing something here… you arent drawing anything in deadcolor, How can you be assured that there are pixels with deadcolor unless you draw something in deadcolor?

-Sundar

Right I just want to be able to select color with mouse to get return rgb value from glreadpixel for deadcolor to transparent it.

Deadcolor is variable to command which color to transparent it. Thats why I want mouse and glreadpixel code to do the pick color job.

Waldoo

The code I posted is all you need to get the color from the mouse position, assuming you already know where the mouse is. I have no clue how you get the mouse cooridnates, since you haven’t said what OS and other APIs you are using.

Win98 and visual C++