示例#1
0
int main() {
    std::string dataRoot;
#ifdef DATA_ROOT
    dataRoot = DATA_ROOT;
#else
    std::cerr << "No DATA_ROOT path found" << std::endl;
    return -1;
#endif

    GLFWwindow *window = nullptr;

    if (initWindow(window)) {
        return -1;
    }

    Shader shader(dataRoot + "/data/shaders/shader.vert", dataRoot + "/data/shaders/shader.frag");

    Texture textures(dataRoot + "/data/assets/textures.png");
    Texture normals(dataRoot + "/data/assets/normals.png");

    std::vector<GLfloat> vertices;
    std::vector<GLuint> indices;

    std::vector<Quad> quads;

    Map map(10, 10);
    for (int x = 0; x < map.getWidth(); ++x) {
        for (int y = 0; y < map.getHeight(); ++y) {
            if (map.isPassable(x, y)) {
                quads.push_back(Quad(glm::vec3(x, 0, -y), glm::vec3(0, 0, -1), glm::vec3(1, 0, 0), 0.5f, 0.0f));
                quads.push_back(Quad(glm::vec3(x, 1, -y), glm::vec3(1, 0, 0), glm::vec3(0, 0, -1), 0.0f, 0.5f));
            }
            for (int direction = 0; direction < 4; ++direction) {
                int cx = x + dx[direction];
                int cy = y + dy[direction];
                if (map.isPassable(x, y) && !map.isPassable(cx, cy)) {
                    quads.push_back(Quad(
                            glm::vec3(x, 0, -y) + WALL_SHIFT[direction],
                            glm::vec3(0, 1, 0),
                            WALL_DIRECTION[direction],
                            0.0f,
                            0.0f
                    ));
                }
            }
        }
    }

    for (int index = 0; index < quads.size(); ++index) {
        Quad quad = quads[index];
        quad.update(vertices);

        indices.push_back(GLuint(4 * index + 0));
        indices.push_back(GLuint(4 * index + 1));
        indices.push_back(GLuint(4 * index + 2));

        indices.push_back(GLuint(4 * index + 2));
        indices.push_back(GLuint(4 * index + 3));
        indices.push_back(GLuint(4 * index + 0));
    }

    GLuint VBO, VAO, EBO;
    createBuffers(VBO, VAO, EBO, vertices, indices);

    glm::mat4 projection = glm::perspective(45.0f, WIDTH / (float) HEIGHT, 0.1f, 100.0f);
    glm::vec3 lamp(map.getWidth() / 2, 0.9f, -map.getHeight() / 2);

    float time = (float) glfwGetTime();

    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();

        float cTime = (float) glfwGetTime();
        float delta = cTime - time;
        time = cTime;

        update(map, delta);

        glClearColor(117 / 255.0f, 187 / 255.0f, 253 / 255.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        shader.use();

        GLint textureLocation = glGetUniformLocation(shader.get(), "textureSampler");
        glUniform1i(textureLocation, 0);

        GLint normalLocation = glGetUniformLocation(shader.get(), "normalSampler");
        glUniform1i(normalLocation, 1);

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, textures.get());

        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, normals.get());

        GLfloat timeValue = (GLfloat) glfwGetTime();

        GLfloat xValue = (GLfloat) (sin(timeValue) / 4) + 0.25f;
        GLint xValueLocation = glGetUniformLocation(shader.get(), "xValue");
        glUniform1f(xValueLocation, xValue);

        GLfloat yValue = (GLfloat) (cos(timeValue + 0.2) / 4) + 0.25f;
        GLint yValueLocation = glGetUniformLocation(shader.get(), "yValue");
        glUniform1f(yValueLocation, yValue);

        glm::mat4 model;
        model = glm::rotate(model, rotation, glm::vec3(0.0f, 1.0f, 0.0f));
        model = glm::translate(model, -cameraPos);

        GLint modelLocation = glGetUniformLocation(shader.get(), "model");
        glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(model));

        glm::mat4 view;
        glm::vec3 eye(0.0f, 0.5f, 0.0f);
        view = glm::lookAt(eye, eye + cameraFront, cameraUp);

        GLint viewLocation = glGetUniformLocation(shader.get(), "view");
        glUniformMatrix4fv(viewLocation, 1, GL_FALSE, glm::value_ptr(view));

        GLint projectionLocation = glGetUniformLocation(shader.get(), "projection");
        glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, glm::value_ptr(projection));

        GLint lightSourceLocation = glGetUniformLocation(shader.get(), "lightSource");
        if (spotlight) {
            glUniform3f(lightSourceLocation, cameraPos.x, cameraPos.y + 0.5f, cameraPos.z);
        } else {
            glUniform3f(lightSourceLocation, lamp.x, lamp.y, lamp.z);
        }

        glm::vec3 lookDirection = glm::normalize(glm::rotate(cameraFront, -rotation, cameraUp));
        GLint lookDirectionLocation = glGetUniformLocation(shader.get(), "lookDirection");
        glUniform3f(lookDirectionLocation, lookDirection.x, lookDirection.y, lookDirection.z);

        GLint spotlightLocation = glGetUniformLocation(shader.get(), "spotlight");
        glUniform1i(spotlightLocation, spotlight);

        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, (GLuint) indices.size(), GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);

        glfwSwapBuffers(window);
    }

    disposeBuffers(VBO, VAO, EBO);

    glfwTerminate();
    return 0;
}
示例#2
0
文件: lab3.cpp 项目: kuanslove/CGprj
void myDisplay(void){
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(60.0, 1.0 * screenWidth / screenHeight, 0.1, 100.0);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(cameraX, cameraY, cameraZ, coiX, coiY, coiZ, 0.0, 1.0, 0.0);

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

  //set the light source properties
  glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient0);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse0);
  glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular0);
  glLightfv(GL_LIGHT0, GL_POSITION, light_position0);

  glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient1);
  glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse1);
  glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular1);
  glLightfv(GL_LIGHT1, GL_POSITION, light_position1);


  //set the remaining light sources	- write your code here
  glLightfv(GL_LIGHT2, GL_AMBIENT, light_ambient2);
  glLightfv(GL_LIGHT2, GL_DIFFUSE, light_diffuse2);
  glLightfv(GL_LIGHT2, GL_SPECULAR, light_specular2);
  glLightfv(GL_LIGHT2, GL_POSITION, light_position2);

  glLightfv(GL_LIGHT3, GL_AMBIENT, light_ambient3);
  glLightfv(GL_LIGHT3, GL_DIFFUSE, light_diffuse3);
  glLightfv(GL_LIGHT3, GL_SPECULAR, light_specular3);
  glLightfv(GL_LIGHT3, GL_POSITION, light_position3);

  glLightfv(GL_LIGHT4, GL_AMBIENT, light_ambient4);
  glLightfv(GL_LIGHT4, GL_DIFFUSE, light_diffuse4);
  glLightfv(GL_LIGHT4, GL_SPECULAR, light_specular4);
  glLightfv(GL_LIGHT4, GL_POSITION, light_position4);
  //turn on or off the fog - write your code here
if(fogSetting){
    glEnable(GL_FOG);
}
else {
    glDisable(GL_FOG);

}





  glColor3d(0.0, 0.0, 0.0);

/////////////////////////////////////////floor////////////////////////////////////////
  glMaterialfv(GL_FRONT, GL_AMBIENT, floor_ambient);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, floor_diffuse);
  glMaterialfv(GL_FRONT, GL_SPECULAR, floor_specular);
  glMaterialfv(GL_FRONT, GL_SHININESS, floor_shininess);

glRotatef(theta,0,1,0);
  //draw the bottom floor
  glPushMatrix();
  glTranslatef(0,floorHeight/2.0,0);
  glScaled(floorWidth, floorHeight, floorWidth);

  glutSolidCube(1.0);
  glPopMatrix();

  //draw the left wall
  glPushMatrix();
  glTranslated(-floorWidth / 2.0, floorWidth / 2.0, 0.0);
  glRotated(90.0, 0.0, 0.0, 1.0);
  glScaled(floorWidth, floorHeight, floorWidth);
  glutSolidCube(1.0);
  glPopMatrix();

  //draw the right wall
  glPushMatrix();
  glTranslated(0.0, floorWidth / 2.0, -floorWidth / 2.0);
  glRotated(90.0, 1.0, 0.0, 0.0);
  glScaled(floorWidth, floorHeight, floorWidth);
  glutSolidCube(1.0);
  glPopMatrix();

  if (textureSetting == ON) glEnable(GL_TEXTURE_2D);

  //map an image texture on the left wall
  glBindTexture(GL_TEXTURE_2D, texImage1Name);
  glBegin(GL_QUADS);
    glNormal3f(1.0, 0.0, 0.0);
    glTexCoord2f(0.0, 0.0); glVertex3d(-floorWidth / 2.0 + floorHeight, 1.5, floorWidth / 2.0 - 1.25);
    glTexCoord2f(1.0, 0.0); glVertex3d(-floorWidth / 2.0 + floorHeight, 1.5, -floorWidth / 2.0 + 1.25);
    glTexCoord2f(1.0, 1.0); glVertex3d(-floorWidth / 2.0 + floorHeight, floorWidth - 0.5, -floorWidth / 2.0 + 1.25);
    glTexCoord2f(0.0, 1.0); glVertex3d(-floorWidth / 2.0 + floorHeight, floorWidth - 0.5, floorWidth / 2.0 - 1.25);
  glEnd();

  //map another image texture on the right wall - write your code here
  glBindTexture(GL_TEXTURE_2D, texImage2Name);
  glBegin(GL_QUADS);
    glNormal3f(0.0, 0.0, 1.0);
    glTexCoord2f(0.0, 0.0); glVertex3d(-floorWidth / 2.0 + 0.5, 1.5, -floorWidth / 2.0 + floorHeight);
    glTexCoord2f(1.0, 0.0); glVertex3d( floorWidth / 2.0 - 0.5, 1.5, -floorWidth / 2.0 + floorHeight);
    glTexCoord2f(1.0, 1.0); glVertex3d( floorWidth / 2.0 - 0.5, floorWidth - 0.5,-floorWidth / 2.0 + floorHeight);
    glTexCoord2f(0.0, 1.0); glVertex3d(-floorWidth / 2.0 + 0.5, floorWidth - 0.5,-floorWidth / 2.0 + floorHeight);
  glEnd();
  //map the checkerboard texture on the bottom floor - write your code here






  glBindTexture(GL_TEXTURE_2D, texCheckerBoardName);
  glBegin(GL_QUADS);
    glNormal3f(0.0, 1.0, 0.0);
    glTexCoord2f(0.0, 0.0); glVertex3d(-floorWidth / 2.0 , floorHeight+0.001, floorWidth / 2.0 );
    glTexCoord2f(1.0, 0.0); glVertex3d( floorWidth / 2.0 , floorHeight+0.001, floorWidth / 2.0 );
    glTexCoord2f(1.0, 1.0); glVertex3d( floorWidth / 2.0 , floorHeight+0.001, -floorWidth / 2.0 );
    glTexCoord2f(0.0, 1.0); glVertex3d(-floorWidth / 2.0 , floorHeight+0.001, -floorWidth / 2.0 );
  glEnd();






  if (textureSetting == ON) glDisable(GL_TEXTURE_2D);
//////////////////////////////////////////floor////////////////////////////////////////

  glTranslated(0.0, floorHeight, 0.0);


	//set up furniture (table, chairs) material properties
	glMaterialfv(GL_FRONT, GL_AMBIENT, furniture_ambient);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, furniture_diffuse);
	glMaterialfv(GL_FRONT, GL_SPECULAR, furniture_specular);
	glMaterialfv(GL_FRONT, GL_SHININESS, furniture_shininess);

	//draw the two chairs - write your code here
    glPushMatrix();
    glTranslatef(-floorWidth / 4.0, 0, floorWidth / 4.0);
    glRotatef(-90,0,1,0);
    chair();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(floorWidth / 4.0, 0, -floorWidth / 4.0);
    chair();
    glPopMatrix();

	//shift to the quad on the floor which the table will be placed
	glPushMatrix();
	glTranslated(-1.0, 0.0, -1.0);

	//draw the table
	drawTable();

	//raise to the table top: all the remaining objects are above the table
	glTranslated(0.0, tableTopHeight, 0.0);

	//set up teapot/teacup material properties - write your code here
	glMaterialfv(GL_FRONT, GL_AMBIENT, teapot_ambient);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, teapot_diffuse);
	glMaterialfv(GL_FRONT, GL_SPECULAR, teapot_specular);
	glMaterialfv(GL_FRONT, GL_SHININESS, teapot_shininess);
	//draw the teapot - write your code here
	glPushMatrix();
	glTranslatef(0,0.15,0);
	glRotatef(45,0,1,0);
    tpot();
    glPopMatrix();
	//draw the teacup - write your code here
    glPushMatrix();
    glTranslatef(0, 0, tableTopWidth / 4.0);
    tcup();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(tableTopWidth / 4.0, 0, 0);
    tcup();
    glPopMatrix();
	//set up lamp material properties - write your code here
	glMaterialfv(GL_FRONT, GL_AMBIENT, lamp_ambient);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, lamp_diffuse);
	glMaterialfv(GL_FRONT, GL_SPECULAR, lamp_specular);
	glMaterialfv(GL_FRONT, GL_SHININESS, lamp_shininess);
	//draw the lamp - write your code here
    glPushMatrix();
    glTranslatef(-tableTopWidth / 4.0, 0, -tableTopWidth / 4.0);
    lamp();
    glPopMatrix();

	glPopMatrix();
	//set up dustbin material properties - write your code here
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glMaterialfv(GL_FRONT, GL_AMBIENT, dustbin_ambient);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, dustbin_diffuse);
	glMaterialfv(GL_FRONT, GL_SPECULAR, dustbin_specular);
	glMaterialfv(GL_FRONT, GL_SHININESS, dustbin_shininess);
	//draw the dustbin - write your code here
  glPushMatrix();
  glTranslated(floorWidth / 4.0, dustbinHeight/2.0, floorWidth / 4.0);
  glScaled(dustbinWidth, dustbinHeight, dustbinWidth);
  glutSolidCube(1.0);
  glPopMatrix();
glDisable(GL_BLEND);
  glutSwapBuffers();
}