我正在尝试在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 :(得分:0)
从void
的调用前删除关键字glReadPixels
,然后移动它,使其位于您要调用它的位置的函数内,可能位于mouse()
里面左键单击if
块。