2012-10-26 84 views
-1

我正在嘗試在OpenGL中爲我正在編寫的程序製作顏色選擇器。 我不斷收到錯誤「變量或字段」glReadPixels宣佈無效「 我已經研究,並找不到錯誤的意思,這使得它很難糾正。OpenGL顏色選擇器/變量或字段'glReadPixels'宣佈爲空

#ifdef __APPLE__ 
# include <OpenGL/gl.h> 
# include <OpenGL/glu.h> 
# include <GLUT/glut.h> 
#else 
# include <GL/gl.h> 
# include <GL/glu.h> 
# include <GL/glut.h> 
#endif 

#include <iostream> 
#include <cmath> 
using namespace std; 

#define WIDTH 750 
#define HEIGHT 750 

int i=0,mousex, mousey; 

unsigned char pick[3]; 

float A = pick[0]/255; 
float B = pick[1]/255; 
float C = pick[2]/255; 

bool mouseleftdown = false; 



void hex(void){ 
glBegin(GL_POLYGON); 
    glColor3f(1,0,0);   //red 
    glVertex2f(0, 2);   //top 
    glColor3f(1,.38,.01);  //orange 
    glVertex2f(2, 1);   //top right 
    glColor3f(1,1,0);   //yellow 
    glVertex2f(2, -1);   //bottom right 
    glColor3f(0,1,0);   //green 
    glVertex2f(0, -2);   //bottom 
    glColor3f(0,0,1);   //blue 
    glVertex2f(-2, -1);   //bottom left 
    glColor3f(.8,0,.8);   //purple 
    glVertex2f(-2, 1);   //top left 
glEnd(); 

glEnd(); 
} 

void square(void){ 
glBegin(GL_POLYGON); 
glVertex2i(1, -1); 
glVertex2i(1, 1); 
glVertex2i(-1, 1); 
glVertex2i(-1, -1); 
glEnd(); 
} 

void mouse(int button, int state, int x, int y){ 
// Save the left button state 
if (button == GLUT_LEFT_BUTTON) 
{ 
    mouseleftdown = (state == GLUT_DOWN); 
    glutPostRedisplay(); // Left button has changed; redisplay! 
} 

// Save the mouse position 
mousex = x; 
mousey = y; 

} 

void glReadPixels(mousex , mousey , 1 , 1 , GL_RGB , GL_UNSIGNED_BYTE , pick); 


void draw(void){ 
glClear(GL_COLOR_BUFFER_BIT); 

glPushMatrix(); 
glColor3f(A,B,C); 
glScalef(750,750,1); 
square(); 
glPopMatrix(); 

glPushMatrix(); 
glScalef(20,20,1); 
    hex(); 
glPopMatrix(); 

glFlush(); 

} 

void my_init(void){ 
glClearColor(0, 0, 0, 0);    //sets clear color 
glLineWidth(4); 
gluOrtho2D(-100, 100, -100, 100);  //sets origin 
} 

int main(int argc, char* argv[ ]){ 
glutInit(&argc, argv); 
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); 
glutInitWindowSize(WIDTH, HEIGHT); 
glutCreateWindow("Color Picker"); 
glutDisplayFunc(draw); 
glutMouseFunc(mouse); 

my_init(); 

glClearColor(1, 1, 1, 0); 

glutMainLoop();       //listens for events 

return 0; 
} 
+0

你期待與'glReadPixels'上的行有什麼關係?它沒有任何功能。 – user1118321

回答

0

在通話前取下關鍵字voidglReadPixels,然後移動它,這樣它在你要調用它的地方在函數內部,想必裏面mouse()內部左鍵單擊if塊。

+0

我做到了,但仍然無法正常運行。每當我關閉應該保存RGB值的數組時,我會得到一個問號。 –

+0

如何打印出陣列? –