我使用QUAD_STRIP显示不同颜色的方块和鼠标功能来捕捉颜色。捕获的颜色应设置为spehre。我可以读取颜色值,但我无法将读取颜色值设置为球体。在鼠标功能中给出时不显示球体。但是在display()函数中给出时会显示出来。
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include<windows.h>
float height=480,width=500;
GLUquadricObj *sphere = NULL;
GLubyte *img;
static void Init() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glShadeModel(GL_FLAT);
sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, GL_TRUE);
gluQuadricNormals(sphere, GLU_SMOOTH);
glClearColor(1,1,1,0);
}
static void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glColor3f(0.0f, 0.0f, 0.0f); // sets color to black.
glBegin(GL_QUAD_STRIP);
//1 q
glColor3f(.5,0.5,1);
glVertex2f(-1, 0.5 );
glVertex2f(-1, 0.75 );
glVertex2f(-0.75, 0.5 );
glVertex2f(-0.75, 0.75 );
// 2q
glColor3f(.25,.5,0);
glVertex2f(-0.5, 0.5 );
glVertex2f(-0.5, 0.75 );
//3q
glColor3f(0.8,0.8,0.4);
glVertex2f(-0.25, 0.5 );
glVertex2f(-0.25, 0.75 );
// 4th QUAD
glColor3f(0.7,0.3,0.6);
glVertex2f(0, 0.5);
glVertex2f(0, 0.75 );
// 5th QUAD
glColor3f(0, 0.7,0.3);
glVertex2f( 0.25, 0.5);
glVertex2f(0.25, 0.75 );
glEnd();
glBegin(GL_QUAD_STRIP);
//1 q
glColor3f(.5,0.5,.5);
glVertex2f(-1, 0.25 );
glVertex2f(-1, 0.5 );
glVertex2f(-0.75, 0.25 );
glVertex2f(-0.75, 0.5 );
// 2q
glColor3f(.5,0.1,0.1);
glVertex2f(-0.5, 0.25 );
glVertex2f(-0.5, 0.5 );
//3q
glColor3f(0.1,0.1,0.4);
glVertex2f(-0.25, 0.25 );
glVertex2f(-0.25, 0.5 );
// 4th QUAD
glColor3f(1,1,0.5);
glVertex2f(0, 0.25);
glVertex2f(0, 0.5 );
// 5th QUAD
glColor3f(0.76, 0.3,0);
glVertex2f( 0.25, 0.25);
glVertex2f(0.25, 0.5 );
glEnd();
glBegin(GL_QUAD_STRIP);
//1 q
glColor3f(.5,0,.5);
glVertex2f(-1, 0 );
glVertex2f(-1, 0.25 );
glVertex2f(-0.75, 0 );
glVertex2f(-0.75, 0.25 );
// 2q
glColor3f(.5,.3,0.3);
glVertex2f(-0.5, 0 );
glVertex2f(-0.5, 0.25 );
//3q
glColor3f(0.2,0.3,0);
glVertex2f(-0.25, 0 );
glVertex2f(-0.25, 0.25 );
// 4th QUAD
glColor3f(0,0,0);
glVertex2f(0, 0);
glVertex2f(0, 0.25 );
// 5th QUAD
glColor3f(1.0, 0,.5);
glVertex2f( 0.25, 0);
glVertex2f(0.25, 0.25 );
glEnd();
glBegin(GL_QUAD_STRIP);
glColor3f(1,0.5,0);
glVertex2f(-1, -0.25 );
glVertex2f(-1, 0.0 );
glVertex2f(-0.75, -0.25 );
glVertex2f(-0.75, 0.0 );
glColor3f(1.0,1.0,0.0);
glVertex2f(-0.5, -0.25 );
glVertex2f(-0.5, 0.0 );
glColor3f(0.0,0.0,1.0);
glVertex2f(-0.25, -0.25 );
glVertex2f(-0.25, 0.0 );
// 2nd QUAD
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.25, -0.25 );
glVertex2f(-0.25, 0.0 );
glVertex2f( 0, -0.25 );
glVertex2f(0, 0.0 );
// 3rd QUAD
glColor3f(0.0, 1.0, 0.0);
glVertex2f( 0.25, -0.25 );
glVertex2f(0.25, 0.0 );
glEnd();
glutSwapBuffers();
}
void moveBall(int key, int x, int y) {
}
void mouseMove(int x,int y) {
y = glutGet( GLUT_WINDOW_HEIGHT ) - y;
GLubyte pixel[3];
float r,g,b;
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
r = pixel[0]/255.0;
g = pixel[1]/255.0;
b = pixel[2]/255.0;
printf("red %f",r);
printf("green %f",g);
printf("blue %f",b);
glColor3f(r,g,b);
glTranslatef(0.75,0.25,0);
gluSphere(sphere,0.1,20,20);
}
int main(int argc, char** argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowPosition( 10, 10);
glutInitWindowSize(height,width);
glutCreateWindow( "ColorBlocks");
Init();
glutDisplayFunc(display);
glutSpecialFunc(moveBall);
glutMotionFunc(mouseMove);
glutMainLoop();
}
答案 0 :(得分:0)
你应该做的是将球体的绘图放在显示器中,并使用你在mouseMove中获得的变量来定位它并设置为全局:
float sphereR,sphereG,sphereB;
void display(){
//everything before the glutSwapBuffers(); call...
glPushMatrix()
glColor3f(r,g,b);
glTranslatef(0.75,0.25,0);
gluSphere(sphere,0.1,20,20);
glPopMatrix()
glutSwapBuffers();
}
void mouseMove(int x,int y) {
y = glutGet( GLUT_WINDOW_HEIGHT ) - y;
GLubyte pixel[3];
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
sphereR = pixel[0]/255.0;
sphereG = pixel[1]/255.0;
sphereB = pixel[2]/255.0;
printf("red %f",r);
printf("green %f",g);
printf("blue %f",b);
}