0

我有以下工作代码(我从一个例子中得到它)。

它产生一个立方体,其侧面颜色不同。有什么方法可以根据它们的位置或某些模拟为侧面的每个(一组)像素分配颜色。像立方体表面上标量场的颜色图之类的东西......?有人可以给我一个在任意表面上这样做的例子吗?

#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/freeglut_ext.h>

void init(void)
{
glClearColor(0,0,0,0);
glShadeModel(GL_FLAT);
}

void DrawCube(void)
{
glLoadIdentity();
gluLookAt(0, 10, 10, 0, 1, 0, 0, -2, -35.7);
glBegin(GL_QUADS);

//face in xy plane
glColor3f(0.12, 0.91, 0.02);//this the color with which complete cube is drawn. 
glVertex3f(0,0 ,0 );
glVertex3f(5, 0, 0);
glVertex3f(5, 5, 0);
glVertex3f(0, 5, 0);

//face in yz plane
glColor3f(1, 0.23, 0.56);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 5);
glVertex3f(0, 5, 0);
glVertex3f(0, 5, 5);

//face in zx plance
glColor3f(0.45, .81, 1);
glVertex3f(0, 0, 0  );
glVertex3f(0, 0, 5);
glVertex3f(5, 0, 5);
glVertex3f(5, 0, 0);

//|| to xy plane.
glColor3f(0.23, 0.78,0.13);
glVertex3f(0, 0, 5);
glVertex3f(5, 0, 5);
glVertex3f(5, 5, 5);
glVertex3f(0, 5, 5);

//|| to yz plane
glColor3f(0.73, 0.58, 0.58);
glVertex3f(0,0 ,5 );
glVertex3f(5, 0, 5);
glVertex3f(5, 5, 5);
glVertex3f(0, 5, 5);

//|| to zx plane
glVertex3f(0.58, 0, 0.82);
glVertex3f(0, 5, 0  );
glVertex3f(0, 5, 5);
glVertex3f(5, 5, 5);
glVertex3f(5, 5, 0);
glEnd();
glFlush();
}


void reshape(int w,int h){

glViewport(0, 0, (GLsizei)w, (GLsizei)h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1, 1, -1, 1, 1.5, 20);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv){

glutInit(&argc, argv);//we initizlilze the glut. functions
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(800, 800);
glutCreateWindow(argv[1]);
init();
glutDisplayFunc(DrawCube);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
4

1 回答 1

1

观看此视频,您还可以下载代码。 http://www.videotutorialsrock.com/opengl_tutorial/cube/home.php

在此视频中,您可以了解您需要的立方体表面上的纹理映射。

于 2013-03-02T10:19:30.930 回答