我想從我的遊戲中的像素中獲取顏色並將此顏色保存在變量中。如何在opengl中做到這一點?如何從像素獲取顏色? OpenGL
5
A
回答
9
#include <GL/glut.h>
#include <iostream>
using namespace std;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10, 10, -10, 10, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glScalef(5,5,5);
glBegin(GL_TRIANGLES);
glColor3ub(255,0,0);
glVertex2f(-1,-1);
glColor3ub(0,255,0);
glVertex2f(1,-1);
glColor3ub(0,0,255);
glVertex2f(1,1);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
y = glutGet(GLUT_WINDOW_HEIGHT) - y;
unsigned char pixel[4];
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
cout << "R: " << (int)pixel[0] << endl;
cout << "G: " << (int)pixel[1] << endl;
cout << "B: " << (int)pixel[2] << endl;
cout << endl;
}
int main(int argc, char **argv)
{
glutInitWindowSize(640,480);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("glReadPixels()");
glutDisplayFunc(display);
glutPassiveMotionFunc(motion);
glutMainLoop();
return 0;
}
5
struct{ GLubyte red, green, blue; } pixel;
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &pixel);
1
#include <math.h>
#include <gl/glut.h>
struct Point {
GLint x;
GLint y;
};
struct Color {
GLfloat r;
GLfloat g;
GLfloat b;
};
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 640, 0, 480);
}
//this function is used for getting color of pixel
Color getPixelColor(GLint x, GLint y) {
Color color;
glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, &color);
return color;
}
void setPixelColor(GLint x, GLint y, Color color) {
glColor3f(color.r, color.g, color.b);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
void floodFill(GLint x, GLint y, Color oldColor, Color newColor) {
Color color;
color = getPixelColor(x, y);
if(color.r == oldColor.r && color.g == oldColor.g && color.b == oldColor.b)
{
setPixelColor(x, y, newColor);
floodFill(x+1, y, oldColor, newColor);
floodFill(x, y+1, oldColor, newColor);
floodFill(x-1, y, oldColor, newColor);
floodFill(x, y-1, oldColor, newColor);
}
return;
}
void onMouseClick(int button, int state, int x, int y)
{
Color newColor = {1.0f, 0.0f, 0.0f};
Color oldColor = {1.0f, 1.0f, 1.0f};
floodFill(320, 240, oldColor, newColor);
}
void draw_circle(Point pC, GLfloat radius) {
GLfloat step = 1/radius;
GLfloat x, y;
for(GLfloat theta = 0; theta <= 360; theta += step) {
x = pC.x + (radius * cos(theta));
y = pC.y + (radius * sin(theta));
glVertex2i(x, y);
}
}
void display(void) {
Point pt = {320, 240};
GLfloat radius = 50;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
draw_circle(pt, radius);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(200, 200);
glutCreateWindow("Open GL");
init();
glutDisplayFunc(display);
glutMouseFunc(onMouseClick);
glutMainLoop();
return 0;
}
相關問題
- 1. 從圖像中獲取像素顏色
- 2. WriteableImage - 如何獲取像素的顏色?
- 3. FreeImage:獲取像素顏色
- 4. GDAL獲取像素顏色
- 5. Java - 獲取像素顏色
- 6. 從Kinect Depth Stream獲取像素顏色
- 7. 從CCRenderTexture獲取像素的顏色
- 8. 從NSImage獲取像素和顏色
- 9. 從圖片中獲取像素顏色?
- 10. 如何從圖像中獲取像素的顏色?
- 11. 如何獲取圖像中像素的顏色(加載灰色)?
- 12. 獲取像素顏色不準確
- 13. 如何從Openlayers 3圖層獲取像素的顏色值?
- 14. Android,如何從Canvas獲取像素顏色?
- 15. 如何從圖形獲取像素顏色g
- 16. 如何從SKSpriteNode或SKTexture獲取像素顏色?
- 17. 如何從React Native的屏幕上的像素獲取顏色?
- 18. 如何從WriteableBitmap獲取像素的顏色?
- 19. 如何從圖片框中獲取特定像素的顏色?
- 20. SpriteKit:如何從SKSpriteNode中的點獲取像素顏色?
- 21. 硒 - 如何獲得像素顏色?
- 22. 嘗試獲取像素顏色
- 23. 獲取UIImage的像素顏色
- 24. Java獲取像素的顏色LIVE
- 25. 使用Magick獲取像素顏色++
- 26. 獲取AVCaptureSession或AVCaptureVideoPreviewLayer的像素顏色
- 27. 獲取rMagick中像素的顏色
- 28. 獲取像素的顏色在Android的
- 29. Dart - 獲取ImageElement的像素顏色
- 30. iPhone:如何獲取圖像的每個像素的顏色?
漂亮的代碼,短按預期工作。您已經在顯示過程中放置了用於檢查/顯示像素顏色的代碼。它工作的很快,但是當繪圖更復雜時,程序無法正常工作。把上面的代碼放在運動過程中怎麼樣,像[這裏]?(http://en.wikibooks.org/wiki/Fractals/Computer_graphic_techniques/2D/gfile#Viewer_for_programmers) – Adam
@Adam:Yup,編輯。 – genpfault