Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Waldoo

Help me with glReadPixel and mouse PPLLLLEEEAAASSSEEE

This topic is 5965 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

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\n!", 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\n"); printf("B - Enables/Disables blending\n"); } 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(); }

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!