Tôi muốn lấy màu từ pixel trong trò chơi của mình và lưu màu này thành biến. Làm thế nào để làm điều đó trong opengl?Cách lấy màu từ pixel? OpenGL
5
A
Trả lời
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;
}
Các vấn đề liên quan
- 1. Lấy màu pixel từ một hình ảnh
- 2. Cách lấy màu của pixel trong UIView?
- 3. lấy pixel từ bitmap
- 4. Đọc pixel từ Ứng dụng OpenGL/GLUT
- 5. Lấy màu pixel ở vị trí có pygame
- 6. Thay đổi màu pixel Python
- 7. Tại sao màu pixel không chính xác trong OpenGL ES 2.0 trên Android?
- 8. Lấy danh sách các giá trị pixel từ PIL
- 9. PHP - lấy tên màu từ rgb
- 10. Cách tốt nhất để truy cập pixel trong OpenGL?
- 11. Cách truy cập màu pixel trong một bitmap?
- 12. OpenGL ES: Cách kết cấu màu với màu
- 13. Nhận màu Pixel nhanh nhất?
- 14. màu nền trong OpenGL
- 15. Lấy giá trị alpha pixel cho UIImage
- 16. C# Lấy dữ liệu pixel hiệu quả từ System.Drawing.Bitmap
- 17. Lấy RGB của pixel bằng PIL
- 18. Lấy Đề án Màu từ Hình ảnh
- 19. Làm cách nào để tô màu một pixel?
- 20. Cách lấy dữ liệu pixel từ một hình ảnh bằng cách sử dụng R
- 21. Giá trị màu của pixel rmagick
- 22. canvas ImageData xóa các pixel màu trắng
- 23. Đọc các màu pixel của hình ảnh
- 24. Cách đơn giản để đọc các giá trị màu pixel từ hình ảnh PNG trên iPhone?
- 25. Điểm ảnh tuyệt đối OpenGL Pixel hoàn hảo
- 26. OpenGL xác định vị trí đỉnh theo pixel
- 27. Tìm màu pixel theo tọa độ cụ thể từ mẫu sampler2D bằng GLSL
- 28. Lấy giá trị pixel của tệp BMP
- 29. Cách lấy thông tin pixel bên trong trình đổ bóng phân đoạn?
- 30. Trình đánh bóng phân đoạn OpenGL biết pixel cần lấy mẫu như thế nào trong kết cấu?
đẹp mã, ngắn và làm việc như mong đợi. Bạn đã đặt mã để kiểm tra/hiển thị màu của pixel bên trong thủ tục hiển thị. Nó hoạt động nhanh, nhưng khi bản vẽ phức tạp hơn thì chương trình không hoạt động đúng cách. Điều gì về việc đưa mã trên vào trong thủ tục chuyển động, như [ở đây]? (Http://en.wikibooks.org/wiki/Fractals/Computer_graphic_techniques/2D/gfile#Viewer_for_programmers) – Adam
@Adam: Yup, edited. – genpfault