如何在opengl中更改圆柱体的颜色?

问题描述

我想在 Visual C++ 中使用 OpenGL 绘制一个圆柱体。 我想把圆柱体的颜色变成红色,所以我在renderCylinder函数添加了以下代码,但它没有改变。

glColor3f(1.0,0.0,0.0);

你能帮我解决这个问题吗?

以下代码是制作测试用圆柱体的完整代码

#include <GL/glew.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <iostream>

int selectedobject = 1;
bool drawThatAxis = 0;
bool lightEffect = 1;

float fovy = 60.0,aspect = 1.0,zNear = 1.0,zFar = 100.0;

float depth = 8;
float phi = 0,theta = 0;
float downX,downY;
bool leftButton = false,middleButton = false;

void renderCylinder(double x1,double y1,double z1,double x2,double y2,double z2,double radius,gluquadricObj* quadric);
void displayCallback(void);

GLdouble width,height;
int wd;
int main(int argc,char* argv[])
{
    glutinit(&argc,argv);
    glutinitdisplayMode(gluT_DEPTH);
    glutinitwindowSize(800,600);

    wd = glutCreateWindow("3D Molecules");

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glCullFace(GL_BACK);
    glEnable(GL_CULL_FACE);
    glClearColor(0.0,0.0);

    gluint id;
    id = glGenLists(1);
    
    printf("hi %d\n",id);

    gluquadric* myQuad;
    myQuad = gluNewQuadric();

    glNewList(id,GL_COMPILE);

    renderCylinder(0,5,1.5,myQuad);

    glEndList();

    glutdisplayFunc(displayCallback);
    glutMainLoop();
    return 0;
}
void renderCylinder(double x1,gluquadricObj* quadric)
{
    double vx = x2 - x1;
    double vy = y2 - y1;
    double vz = z2 - z1;
    double ax,rx,ry,rz;
    double len = sqrt(vx * vx + vy * vy + vz * vz);

    glPushmatrix();
    glColor3f(1.0,0.0);
    glTranslatef(x1,y1,z1);
    if (fabs(vz) < 0.0001)
    {
        glrotatef(90,1,0);
        ax = 57.2957795 * -atan(vy / vx);
        if (vx < 0)
        {

        }
        rx = 1;
        ry = 0;
        rz = 0;
    }
    else
    {
        ax = 57.2957795 * acos(vz / len);
        if (vz < 0.0)
            ax = -ax;
        rx = -vy * vz;
        ry = vx * vz;
        rz = 0;
    }
    glrotatef(ax,rz);
    gluQuadricOrientation(quadric,glu_OUTSIDE);
    gluCylinder(quadric,radius,len,10,10);
    glPopMatrix();
}
void displayCallback(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fovy,aspect,zNear,zFar);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(0,40,0);

    glTranslatef(0.0,-depth);
    glrotatef(-theta,1.0,0.0);
    glrotatef(phi,0.0);

    if (lightEffect) {
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
    }
    else
    {
        gldisable(GL_LIGHTING);
        gldisable(GL_LIGHT0);
    }

    switch (selectedobject)
    {
    case (1):
        glCallList(1);
        break;
    default:
        break;
    }

    glFlush();
}

解决方法

您打开了照明,因此忽略 glColor 并使用材质和灯光属性...

要解决将其添加到启用灯光附近的代码中的问题:

glEnable(GL_COLOR_MATERIAL);

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...