在opengl 3

时间:2015-10-19 20:54:42

标签: opengl rotation

我的代码有问题。我现在渲染两个物体。他们是地板和人物。楼层位于VAO [0]并且位于VAO 1。我的问题是我看不到对象楼层。我知道我必须改变物体地板的视图才能使其可见。我希望通过轮换来做到这一点。我只想换地板。这是我的漏洞代码。有人可以帮帮我吗我只想在这个内容opengl 3.3中使用现代opengl。我希望得到这样的观点:My scene红色区域是地板,绿色是数字。 我怎样才能实现我的目标。供您参考。这是一个测试场景。我的远程目标是像3D游戏中的洞场景。因此,这意味着更多的多边形用于地板和其他场景,如墙壁,天花板,房间内的物体和nps。所有都是用精灵制作的,我认为如果不使用3d程序来创建3d模型,这将是最好的方法。因此,如果您有我的对象的提示,请欣赏它。 谢谢您的帮助。

#define GLFW_NO_GLU
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include <gtc/type_ptr.hpp>

int main()
{
    if(glfwInit()==GL_FALSE)
    {
        std::cerr<<"GLFW FAIL";
        return 1;
    }
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow* window = glfwCreateWindow(1024, 748, "My Game", NULL, NULL);
    if (window==GL_FALSE)
    {
        std::cerr<<"Open Window FAIL";
        return 1;
    }
    glfwMakeContextCurrent(window);
    glewExperimental = true;

    if(glewInit()!=GLEW_OK)
    {
        std::cerr<<"GLEW FAIL";
        return 1;
    }

    glViewport(0, 0, 1024, 748);

    const GLchar* vertexshadersrc="#version 330 core\n"
                            "layout (location = 0) in vec4 position;\n"
                            "uniform mat4 matrix;\n"
                            "void main()\n"
                            "{\n"
                            "gl_Position = gl_Position=matrix * position;\n"
                            "}\0";
    const GLchar* fragmentshadersrc="#version 330 core\n"
                              "out vec4 color;\n"
                              "void main()\n"
                              "{\n"
                              "color=vec4(1.0f,0.0f,0.0f,1.0f);\n"
                              "}\0";

    GLuint vertexshader=glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexshader,1,&vertexshadersrc,NULL);
    glCompileShader(vertexshader);

    GLint compile_ok;
    GLint errlength;
    GLchar* errmsg;
    glGetShaderiv(vertexshader,GL_COMPILE_STATUS, &compile_ok);
    if(compile_ok==GL_FALSE)
    {
      glGetShaderiv(vertexshader, GL_INFO_LOG_LENGTH,&errlength);
      errmsg=new GLchar[errlength];
      glGetShaderInfoLog(vertexshader,errlength,&errlength,errmsg);
      std::cerr<<"Vertexshader";
      std::cerr<<errmsg;
      return 1;
    }


    GLuint fragmentshader=glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentshader,1,&fragmentshadersrc,NULL);
    glCompileShader(fragmentshader);

    glGetShaderiv(fragmentshader,GL_COMPILE_STATUS, &compile_ok);
    if(compile_ok==GL_FALSE)
    {
      glGetShaderiv(fragmentshader, GL_INFO_LOG_LENGTH,&errlength);
      errmsg=new GLchar[errlength];
      glGetShaderInfoLog(fragmentshader,errlength,&errlength,errmsg);
      std::cerr<<"Fragmentshader";
      std::cerr<<errmsg;
      return 1;
    }

    GLuint programm=glCreateProgram();
    glAttachShader(programm,vertexshader);
    glAttachShader(programm,fragmentshader);
    glLinkProgram(programm);
    glDeleteShader(vertexshader);
    glDeleteShader(fragmentshader);

    GLfloat floor[]=
    {
        -1.0f, 0.0f, 1.0f,   
        -1.0f, 0.0f, -1.0f,
        1.0f, 0.0f, 1.0f,
        1.0f, 0.0f, -1.0f,
    };

    GLfloat floorc[]=
    {

    1.0f,0.0f,0.0f,1.0f
    };

    GLfloat figure[] = {
        -0.2f, 0.4f, -0.1f,
        -0.2f, 0.0f, -0.1f,
        0.2f, 0.4f, -0.1f,
        0.2f, 0.0f, -0.1f 
    };

    GLfloat figurec[]=
    {
        0.0f,1.0f,0.0f,1.0f
    };

    GLfloat tree[] = {
        -0.7f, 0.4f, -0.1f,
        -0.7f, 0.0f, -0.1f,
        -0.3f, 0.4f, -0.1f,
        -0.3f, 0.0f, -0.1f
    };

    GLuint VAO[3], VBO[6];
    glGenVertexArrays(3, VAO);
    glGenBuffers(6, VBO);

    glBindVertexArray(VAO[0]);
    glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
    glBufferData(GL_ARRAY_BUFFER,sizeof(floor),floor,GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);


    glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
    glBufferData(GL_ARRAY_BUFFER,sizeof(floorc),floorc,GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);

    glBindVertexArray(VAO[1]);
    glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(figure), figure, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);


    glBindBuffer(GL_ARRAY_BUFFER, VBO[3]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(figurec), figurec, GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);

    glBindVertexArray(VAO[2]);
    glBindBuffer(GL_ARRAY_BUFFER, VBO[4]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(tree), tree, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);

    glm::mat4 projection;
    projection=glm::perspective(45.0f,4.0f/3.0f,0.1f,100.0f);
    glm::mat4 modelview;
    modelview=glm::translate(glm::mat4(1.0f),glm::vec3(0.0f, 0.0f, -1.0f));
    glm::mat4 mpmatrix=projection*modelview;

    //GLuint position=glGetAttribLocation(programm,"position");
    GLuint matrixuni=glGetUniformLocation(programm,"matrix");
    //glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(GLvoid*)0);
    glUseProgram(programm);

    glUniformMatrix4fv(matrixuni,1,GL_FALSE,&mpmatrix[0][0]);


    while (!glfwWindowShouldClose(window))
    {
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glBindVertexArray(VAO[0]);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

        glBindVertexArray(VAO[1]);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

        /*glBindVertexArray(VAO[2]);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);*/

        glfwPollEvents();
        glfwSwapBuffers(window);
    }
    glfwTerminate();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

从3个矩阵生成的屏幕上的图像:投影矩阵,视图矩阵和模型矩阵。似乎你忘记了视图矩阵。我用这段代码替换了你的矩阵部分:

glm::mat4 projection;
projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);

glm::mat4 view;
view = glm::lookAt(glm::vec3(0, 0.25, 0.75), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));

glm::mat4 modelview;
modelview = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -1.0f));

glm::mat4 mpmatrix = projection*view*modelview;

我得到了这张照片: enter image description here

它看起来像your picture,但两个对象都有红色,因为它们使用相同的着色器,其中颜色设置为红色。

<强> UPD: 也许因为你会减少一点楼层高度:

    GLfloat floor[] = {
    -1.0f, -0.05f, 1.0f,
    -1.0f, -0.05f, -1.0f,
    1.0f, -0.05f, 1.0f,
    1.0f, -0.05f, -1.0f,
};