为什么没有想要识别我的3D形状的颜色?

时间:2016-10-21 23:33:41

标签: c++ visual-studio opengl graphics 3d

我的第一个机器人机器人();平整,颜色完美。对于我的第二个机器人RobotBodyMotion();围绕y轴旋转的那个不想识别我的颜色命令。我唯一能做的就是改变氛围,漫反射和镜面反射。这会改变整个颜色,我想分别为每个形状着色。我可以改变或移动什么来获得形状的单独颜色?

#include<Windows.h>
#include <GL\glew.h>
#include <math.h>
#include <iostream>
#include <GL\GL.h>
#include <GL\GLU.h>
#include <GLUT/glut.h>
#include <glm\glm.hpp>
#include <glm\gtc\matrix_transform.hpp>
#include <glm\gtx\transform.hpp>
#include "Canvas_freeglut.h"
#include<vector>

static int window;
static int menu_id;
static int submenu_id;
static int submenu_id2;
static int value = 0;




//part one
//robot alone with the missing features which is his body
//rotating hands and feet on off
//non flat shading model
//menu to toggle shading smooth or flat


static GLfloat theta[] = { 0.0, 0.0, 0.0 };
static GLint axis = 2.0;

void drawRectangle()
{
    glutSolidCube(.15);
}

void drawSphere()
{
    glutSolidSphere(.1, 16, 16);
}

void spinsphere()
{
theta[axis]+=.05;

if (theta[axis] > 360.0)
    theta[axis] -= 360.0;
    glutPostRedisplay();


////stop and reposition
//else
//{
//  glutPostRedisplay();
//}

}

void drawRightSideAppendages()
{

    glColor3f(1.000, 1.000, 0.000);
    glPushMatrix(); // right arm
    glTranslatef(0.139f, 0.12f, 0.0f);
    glRotatef(150,0, 0, 1);
    glScalef(0.35f, 1.20f, 0.1f);
    drawSphere();
    glPopMatrix();


    glColor3f(0.000, 1.000, 0.000);
    glPushMatrix(); // right hand
    glTranslatef(0.22f, .25f, 0.0f);
    glRotatef(theta[2], 0.0, 0.0, 1.0);
    glScalef(0.44f, 0.50f, 0.50f);
    drawSphere();
    glPopMatrix();

    glPushMatrix(); // right thigh
    glColor3f(1.000, 0.000, 1.000);
    glTranslatef(0.10f, -0.40f, 0.0f);
    glRotatef(20, 0, 0, 1);
    glScalef(0.40f, 1.40f, 0.35f);
    drawSphere();
    glPopMatrix();

    glPushMatrix(); // right shin
    glTranslatef(0.14f, -0.66f, 0.0f);
    glScalef(0.35f, 1.60f, 0.35f);
    drawSphere();
    glPopMatrix();


    glPushMatrix(); // right foot
    glColor3f(0.000, 1.000, 0.000);
    glTranslatef(0.16f, -.85f, 0.0f);
    glRotatef(theta[2], 0.0, 0.0, 1.0);
    glScalef(0.45f, 0.51f, 1.0f);
    drawSphere();
    glPopMatrix();
}

void drawRightSideAppendagesNoMotion()
{
    glPushMatrix(); // right arm
    glColor3f(1.000, 1.000, 0.000);
    glTranslatef(0.139f, 0.12f, 0.0f);
    glRotatef(150, 0, 0, 1);
    glScalef(0.35f, 1.20f, .30f);
    drawSphere();
    glPopMatrix();

    glPushMatrix(); // right hand
    glColor3f(0.000, 1.000, 0.000);
    glTranslatef(0.22f, .25f, 0.0f);
    glScalef(0.44f, 0.50f, 0.50f);
    drawSphere();
    glPopMatrix();

    glPushMatrix(); // right thigh
    glColor3f(1.000, 0.000, 1.000);
    glTranslatef(0.10f, -0.40f, 0.0f);
    glRotatef(20, 0, 0, 1);
    glScalef(0.40f, 1.40f, 0.35f);
    drawSphere();
    glPopMatrix();

    glPushMatrix(); // right shin
    glTranslatef(0.14f, -0.66f, 0.0f);
    glScalef(0.35f, 1.60f, 0.35f);
    drawSphere();
    glPopMatrix();


    glPushMatrix(); // right foot
    glColor3f(0.000, 1.000, 0.000);
    glTranslatef(0.16f, -.85f, 0.0f);
    glScalef(0.45f, 0.51f, 1.0f);
    drawSphere();
    glPopMatrix();
}

void drawMiddleAppendages()
{
    glPushMatrix();//antena
    glColor3f(0.000, 0.000, 0.000);
    glTranslatef(0.0f, 0.72f, 0.0f);
    glScalef(0.1f, 0.75f, 0.1f);
    drawRectangle();
    glEnd;
    glPopMatrix();

    glPushMatrix(); // part of antena(circle)
    glColor3f(1.000, 0.000, 0.000);
    glTranslatef(0.0f, 0.79f, 0.0f);
    glScalef(.30f, .30f, .30f);
    drawSphere();
    glPopMatrix();

    glPushMatrix(); // head
    glColor3f(0.000, 0.000, 1.000);
    glTranslatef(0.0f, 0.50f, 0.0f);
    glScalef(1.75f, 1.75f, 1.75f);
    drawSphere();
    glPopMatrix();

    glPushMatrix(); // eyes (left)

    glColor3f(0.627, 0.322, 0.176);
    glTranslatef(-0.07f, 0.56f, -.11f);
    glScalef(.35f,.35f, .35f);
    drawSphere();
    glPopMatrix(); 

    glPushMatrix(); // eyes (right)
    glColor3f(0.627, 0.322, 0.176);
    glTranslatef(0.07f, 0.56f, -.11f);
    glScalef(.35f, .35f, .35f);
    drawSphere();
    glPopMatrix();

    glPushMatrix();//mouth
    glColor3f(0.000, 0.000,.000);
    glTranslatef(0.0f, 0.45f, -0.16f);
    glRotatef(-10, 1, 0,0);
    glScalef(1.0f, 0.20f, 0.20f);
    drawRectangle();
    glPopMatrix();


    glPushMatrix(); // body
    glColor3f(0.000, 0.000, 1.000);
    glTranslatef(0.0f, 0.0f, 0.0f);
    glScalef(.85f, 3.5f, .50f);
    drawSphere();
    glPopMatrix();

}

void drawLeftSideAppendages()
{
    glPushMatrix(); // left arm
    glColor3f(1.000, 1.000, 0.000);
    glTranslatef(-0.139f, 0.12f, 0.0f);
    glRotatef(-150, 0, 0, 1);
    glScalef(0.35f, 1.20f, 0.1f);
    drawSphere();
    glPopMatrix();

    glPushMatrix(); // left hand
    glColor3f(0.000, 1.000, 0.000);
    glTranslatef(-0.22f, .25f, 0.0f);
    glRotatef(theta[2], 0.0, 0.0, 1.0);
    glScalef(0.44f, 0.50f, 0.50f);
    drawSphere();
    glPopMatrix();

    glPushMatrix(); // left thigh
    glColor3f(1.000, 0.000, 1.000);
    glTranslatef(-0.10f, -0.40f, 0.0f);
    glRotatef(-20, 0, 0, 1);
    glScalef(0.40f, 1.40f, 0.35f);
    drawSphere();
    glPopMatrix();

    glPushMatrix(); // left shin
    glTranslatef(-0.14f, -0.66f, 0.0f);
    glScalef(0.35f, 1.60f, 0.35f);
    drawSphere();
    glPopMatrix();

    glPushMatrix(); // left foot
    glColor3f(0.000, 1.000, 0.000);
    glTranslatef(-0.16f, -.85f, 0.0f);
    glRotatef(theta[2], 0.0, 0.0, 1.0);
    glScalef(0.45f, 0.51f, 1.0f);
    drawSphere();
    glPopMatrix();
} 

void drawLeftSideAppendagesNoMotion()
{
    glPushMatrix(); // left arm
    glColor3f(1.000, 1.000, 0.000);
    glTranslatef(-0.139f, 0.12f, 0.0f);
    glRotatef(-150, 0, 0, 1);
    glScalef(0.35f, 1.20f, 0.30f);
    drawSphere();
    glPopMatrix();

    glPushMatrix(); // left hand
    glColor3f(0.000, 1.000, 0.000);
    glTranslatef(-0.22f, .25f, 0.0f);
    glScalef(0.44f, 0.50f, 0.50f);
    drawSphere();
    glPopMatrix();

    glPushMatrix(); // left thigh
    glColor3f(1.000, 0.000, 1.000);
    glTranslatef(-0.10f, -0.40f, 0.0f);
    glRotatef(-20, 0, 0, 1);
    glScalef(0.40f, 1.40f, 0.35f);
    drawSphere();
    glPopMatrix();

    glPushMatrix(); // left shin
    glTranslatef(-0.14f, -0.66f, 0.0f);
    glScalef(0.35f, 1.60f, 0.35f);
    drawSphere();
    glPopMatrix();

    glPushMatrix(); // left foot
    glColor3f(0.000, 1.000, 0.000);
    glTranslatef(-0.16f, -.85f, 0.0f);
    glScalef(0.45f, 0.51f, 1.0f);
    drawSphere();
    glPopMatrix();
}

void Robot()
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    drawRightSideAppendages();
    drawMiddleAppendages();
    drawLeftSideAppendages();
}

void RobotNoMotion()
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    drawRightSideAppendagesNoMotion();
    drawMiddleAppendages();
    drawLeftSideAppendagesNoMotion();
}

void RobotBodyMotion()
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glRotatef(theta[2], 0.0, 1.0,0.0);
    drawRightSideAppendagesNoMotion();
    drawMiddleAppendages();
    drawLeftSideAppendagesNoMotion();

}

void MyInit(void)
{
    GLfloat mat_ambient[] = { 0.25, 0.25, 0.25, 1.0f };//shape ambience
    GLfloat mat_diffuse[] = { .4 , .4, .4, 0.2775, 1.0f };//where light fades to shade
    GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0f };//light reflect on shape specular
    GLfloat mat_shininess[] = { 50.8f };//specular exponent
    GLfloat light_position[] = { 1.0, 1.0, -1.0, 0.0 };
    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glClearColor(0.663, 0.663, 0.663, 1.0);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_NORMALIZE);
    // set white background color   
    // set the drawing color (black)  
    glOrtho(-1, 1, -1, 1, -1, 1);
}

void Display()
{
    if (value != 0 && value!=1)
    {
        spinsphere();
    }

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    if (value == 1)
    {
        glClearColor(0.663, 0.663, 0.663, 0.0);//clear
    }
    else if (value == 2){

        glDisable(GL_LIGHTING);
        Robot();
    }
    else if (value == 3){
        glDisable(GL_LIGHTING);
        RobotNoMotion();
    }
    else if (value == 4){

        glEnable(GL_LIGHTING);
        RobotBodyMotion();
    }
    else if (value == 5){

        glEnable(GL_LIGHTING);
        RobotNoMotion();
    }
    glFlush();
}

void menu(int num)
{
    if (num == 0)
    {
        glutDestroyWindow(window);
        exit(0);
    }
    else
    {
        value = num;
    }
    glutPostRedisplay();
}

void createMenu(void)
{

    submenu_id = glutCreateMenu(menu);
    glutAddMenuEntry("Flat Rotation ON", 2);
    glutAddMenuEntry("Flat Rotaion OFF",3);

    submenu_id2 = glutCreateMenu(menu);
    glutAddMenuEntry("Smooth Rotation ON", 4);
    glutAddMenuEntry("Smooth Rotation OFF", 5);

    menu_id = glutCreateMenu(menu);
    glutAddMenuEntry("Clear", 1);
    glutAddSubMenu("Flat Robot", submenu_id);
    glutAddSubMenu("Smooth Robot", submenu_id2);
    glutAddMenuEntry("Quit", 0);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
}

void main(int argc, char** argv)
{
    glutInit(&argc, argv);// initialize the toolkit   
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB|GLUT_DEPTH); // set display mode  
    glutInitWindowSize(1000,500);// set window size   
    glutInitWindowPosition(0, 0);
    window = glutCreateWindow("ROBOT");// open the screen window   
    MyInit();
    createMenu();
    glutDisplayFunc(Display);// register redraw function
    glutMainLoop();// go into a perpetual loop   
}

1 个答案:

答案 0 :(得分:0)

您在此处启用了照明:

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

完成此操作后,将根据光照和材质定义评估光照方程,确定几何体的颜色。使用glColor3f()设置的颜色将不再使用。

您有两种方法可以解决此问题:

  • 您目前在代码中调用glColor3f()的任何地方,都会调用修改材质属性。例如。每次要更改材质的漫反射颜色时,请调用glMaterialfv(GL_FRONT, GL_DIFFUSE, ...)
  • 在初始化期间,请致电:

    glColorMaterial(GL_FRONT, GL_DIFFUSE);
    glEnable(GL_COLOR_MATERIAL);
    

    这指定材质的漫反射颜色使用glColor3f()跟踪当前颜色集。因此,您可以在代码的其余部分中使用glColor3f()来设置材质的漫反射颜色。

更好的选择是使用更现代的OpenGL版本,您可以在其中编写着色器,为您实现它的方式着色,而不依赖于难以理解的状态值的组合。