Computer Graphics: Drawing a 3D Interactive Scene (1)

OpenGL is an interface between graphics and hardware. Compared with other graphics program development tools, it provides numerous graphics functions. The intuitive programming environment simplifies the drawing process of three-dimensional graphics. Using OpenGL to build a three-dimensional scene, you can interact with it through input devices. Object interaction within the scene.
Insert image description here
Deluxe single room

Configuration environment: vs22+freeglut library

1. Draw the wall to make it a closed space, and complete the layout of the scene in the closed space

//空间基本架构
void drawWall()
{
    
    
    glColor3f(0.47, 0.54, 0.60);//rgb颜色可调
    GLfloat matWall[] = {
    
     0.37, 0.83, 0.98};
	GLfloat matBlack[] = {
    
     0, 0, 0, 1 };
	GLfloat matWhite[] = {
    
     1, 1, 1, 1 };
	GLfloat matShininess[] = {
    
     40 };
	glMaterialfv(GL_FRONT, GL_AMBIENT, matWall);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, matWall);
	glMaterialfv(GL_FRONT, GL_SPECULAR, matWhite);
	glMaterialfv(GL_FRONT, GL_SHININESS, matShininess);
	glMaterialfv(GL_FRONT, GL_EMISSION, matBlack);
    // 左边
    drawQuads(-10, 10, -20,
        -10, 10, 0,
        -10, -10, 0,
        -10, -10, -20);
    // 右边
    drawQuads(10, -10, -20,
        10, -10, 0,
        10, 10, 0,
        10, 10, -20);
    // back
    drawQuads(10, 10, -20,
        -10, 10, -20,
        -10, -10, -20,
        10, -10, -20);
    // 天花板
    drawQuads(10, 10, -20,
        10, 10, 0,
        -10, 10, 0,
        -10, 10, -20);
    // 地板
    glColor3f(0.75, 0.75, 0.75);
    drawQuads(-10, -10, -20,
        -10, -10, 0,
        10, -10, 0,
        10, -10, -20);
}

The glMaterialfv () function specifies the material parameters of the lighting model.
Insert image description here
2. Draw a bed and place it in the corner (left)

void drawBed()
{
    
    
    glColor3f(1, 0.8, 0.5);
    glMaterialfv(GL_FRONT, GL_AMBIENT, matBrown);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrown);
    glMaterialfv(GL_FRONT, GL_SPECULAR, matWhite);
    glMaterialfv(GL_FRONT, GL_SHININESS, matShininess);
    glMaterialfv(GL_FRONT, GL_EMISSION, matBlack);
    // 床头
    glPushMatrix();
    glTranslatef(-9.5, -6, -16);
    glScalef(1, 8, 8);
    glutSolidCube(1);
    glPopMatrix();
    // 床尾
    glPushMatrix();
    glTranslatef(4.5, -8.5, -16);
    glScalef(1, 3, 8);
    glutSolidCube(1);
    glPopMatrix();
    //床板
    glPushMatrix();
    glTranslatef(-2, -7, -16);
    glScalef(14, 1, 8);
    glutSolidCube(1);
    glPopMatrix();
    //床垫
    glColor3f(0.6, 0.97, 0.6);
    glMaterialfv(GL_FRONT, GL_AMBIENT, matGreen);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, matGreen);
    glMaterialfv(GL_FRONT, GL_SPECULAR, matWhite);
    glMaterialfv(GL_FRONT, GL_SHININESS, matShininess);
    glMaterialfv(GL_FRONT, GL_EMISSION, matBlack);
    glPushMatrix();
    glTranslatef(-2, -6, -16);
    glScalef(14, 1, 8);
    glutSolidCube(1);
    glPopMatrix();
}

The renderings are as follows:
Insert image description here
3. Some basic configurations of the room

void drawDesk()
{
    
    
    glColor3f(1, 0.8, 0.5);
    glMaterialfv(GL_FRONT, GL_AMBIENT, matBrown);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrown);
    glMaterialfv(GL_FRONT, GL_SPECULAR, matWhite);
    glMaterialfv(GL_FRONT, GL_SHININESS, matShininess);
    glMaterialfv(GL_FRONT, GL_EMISSION, matBlack);
	//书桌
    glPushMatrix();
    glTranslatef(9, -4, -10);
    glScalef(4, 0.5, 20);
    glutSolidCube(1);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(9.75, -7, -10);
    glScalef(0.5, 6, 20);
    glutSolidCube(1);
    glPopMatrix();
	//床头柜
	glPushMatrix();
    glTranslatef(-8, -8, -10);
    glScalef(4, 4, 4);
    glutSolidCube(1);
    glPopMatrix();
}

It is recommended that this table be converted into a physical bushi
Insert image description hereand a teapot bushi be purchased.

void drawTeapot()
{
    
    
    glColor3f(0.80,0.71,0.34);
    glMaterialfv(GL_FRONT, GL_AMBIENT, matBrown);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrown);
    glMaterialfv(GL_FRONT, GL_SPECULAR, matWhite);
    glMaterialfv(GL_FRONT, GL_SHININESS, matShininess);
    glMaterialfv(GL_FRONT, GL_EMISSION, matBlack);

    glPushMatrix();
    glTranslatef(-8, -5.5, -10);
    glRotatef(-100, 0, 1, 0);
    glutSolidTeapot(0.8);
    glPopMatrix();
}

Insert image description here
Let’s write the scene here first. You can add other items as you like. The next article will be about drawing an Android elf. Please follow me and
leave your email in the comment area for the complete source code.

Guess you like

Origin blog.csdn.net/weixin_52049271/article/details/128315860