Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 未在opengl中绘制顶点_C++_Opengl_Vertex Shader_Perspectivecamera - Fatal编程技术网

C++ 未在opengl中绘制顶点

C++ 未在opengl中绘制顶点,c++,opengl,vertex-shader,perspectivecamera,C++,Opengl,Vertex Shader,Perspectivecamera,我在使用着色器绘制一个简单的顶点指定(长方体)茶壶模型时遇到了问题,该模型带有一些平移和旋转输入。我一遍又一遍地检查我的gl代码和矩阵(对象在-z上的位置,相机在原点等),不明白为什么我仍然得到一个空白屏幕。为了保持代码简短,我刚刚为我的模型的基本多维数据集输入了代码(一旦我至少得到了,我就没事了) } 片段着色器 #version 150 out vec4 fColor; void main() { fColor = vec4(1.0, 1.0, 0.0, 1.0); }

我在使用着色器绘制一个简单的顶点指定(长方体)茶壶模型时遇到了问题,该模型带有一些平移和旋转输入。我一遍又一遍地检查我的gl代码和矩阵(对象在-z上的位置,相机在原点等),不明白为什么我仍然得到一个空白屏幕。为了保持代码简短,我刚刚为我的模型的基本多维数据集输入了代码(一旦我至少得到了,我就没事了)

}

片段着色器

     #version 150
out vec4 fColor;

void main()
{
    fColor = vec4(1.0, 1.0, 0.0, 1.0);
}

对此不确定,因为我不确定Angel::幕后发生了什么,但从片段着色器输出
fColor
是否正确?我认为在glsl 150中,它需要特殊变量
gl_FragColor
,除非Angel做了一些具体的事情来缓解这种情况

我看了一会儿,但我没有看到任何其他会引起问题的东西。不幸的是,我认为如果你真的被卡住了,你可能不得不开始编写一个更简单的例子(没有函数,只是通过渲染三角形直接完成初始化)

还要确保每个draw()循环至少调用一次glGetError,以确保它没有丢失任何内容。Angel是否会在链接/编译失败时引发异常?

嗯,这样如何:

viewMatrix=lookAt(CameradDirection、cameraPosition、cameraUp)


这是和gluLookAt一样的表情吗?在该功能中,原型是
(位置、注视点、摄像机)
,这与您得到的相反。

您是否尝试过使用固定的函数管道渲染它?@AndreasBrinck:这将如何帮助他使着色器cod工作?您能否首先确认渲染循环中的glGetError==0?我添加了代码以打印出glGetError(),它将返回0,但只返回一次(除非有一个键事件,在这种情况下会调用glutPostRedisplay(),但屏幕仍然是黑色的)所以draw()由于某些原因没有被调用添加了一个缺少的空闲回调,所以这是固定的,但仍然得到一个普通屏幕Angel命名空间中的所有内容都是一个加载顶点和片段着色器程序的函数,我从教科书的资源参考中下载了该程序,并将InitShader.cpp修改为.hpp作为原始头如果没有我不想使用的其他源文件,我将无法编译。

我真的希望你对gl\u FragColor的看法是正确的。这很有意义,我现在就试试。

我运行了程序打印glGetError()everyframe每次都返回0。令人遗憾的是,这不起作用。我查看了glsl 1.5规范,gl_fragColor已经被弃用,web上的其他1.5示例使用用户定义的变量以相同的方式输出颜色。好的,我做了很久以前就应该做的事情。我刚刚添加了GLDrawArray(gl_三角形,0,sizeof(顶点));对于draw方法,所有的对象变换都按“预期”工作,当然,它看起来与它应该做的完全不同。我必须找出GL_TRIANGLE_STRIP的错误,或者强制它并重写GL_TRIANGLE的顶点代码(叹气)。
#include "TeapotViewer.h"

using namespace glm;

const int S_WIDTH = 800;
const int S_HEIGHT = 600;
const float FOV = 100;
const float P_NEAR = 0.2;
const float P_FAR = 20.0;
const float SPOUT_WIDTH = 0.025;
const float HANDLE_WIDTH = 0.15;
const float ZERO = 0.0;
const int numberOfVertices = 104;
const int noCubeSide = 10;
const int noCubeFace = 4;
const int noLine = 2;

mat4 modelxViewMatrix, projMatrix, viewMatrix, rotationMatrix, translationMatrix;
vec3 rotationAxis;
vec3 teapotPosition  = vec3(0.0, 0.0,-3.0);

const vec3 cameraPosition = vec3(0.0, 0.0, 0.0);
const vec3 cameraDirection = vec3(0.0, 0.0, -1.0);
const vec3 cameraUp = vec3(0.0, 1.0, 0.0);

vec4 vertices[numberOfVertices];
GLuint refVertexArray;
GLuint refVertexBuffer;
GLuint refUniformModelxView;
GLuint refUniformProjection;

const vec4 body[] = {

    vec4(-1.0,-1.0, 1.0, 1.0), vec4(-1.0, 1.0, 1.0, 1.0),
    vec4( 1.0,-1.0, 1.0, 1.0), vec4( 1.0, 1.0, 1.0, 1.0),
    vec4( 1.0,-1.0,-1.0, 1.0), vec4( 1.0, 1.0,-1.0, 1.0),
    vec4(-1.0,-1.0,-1.0, 1.0), vec4(-1.0, 1.0,-1.0, 1.0)
};

const vec3 xAxis = vec3(1.0, 0.0, 0.0);

const vec3 yAxis = vec3(0.0, 1.0, 0.0);

const vec3 zAxis = vec3(0.0, 0.0, 1.0);

// draw callback
void draw(){

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    translationMatrix = translate(mat4(), teapotPosition);

    modelxViewMatrix = viewMatrix*translationMatrix*rotationMatrix;

    glUniformMatrix4fv(refUniformModelxView, 1, &modelxViewMatrix[0][0]);
    glUniformMatrix4fv(refUniformProjection, 1, &projMatrix[0][0]);
    void drawTeapot();

    glutSwapBuffers();
}

void drawTeapot(){

    int bufferIndex = 0;
    // draw cube
    glDrawArrays(GL_TRIANGLE_STRIP, bufferIndex, noCubeSide);
    bufferIndex += noCubeSide;
    glDrawArrays(GL_TRIANGLE_STRIP, bufferIndex, noCubeFace);
    bufferIndex += noCubeFace;
    glDrawArrays(GL_TRIANGLE_STRIP, bufferIndex, noCubeFace);
    bufferIndex += noCubeFace;


    // draw the axis of rotation
    if (rotationAxis == xAxis){

        glDrawArrays(GL_LINES, bufferIndex, noLine);
        bufferIndex += noLine;
    }
    if (rotationAxis == yAxis){

        bufferIndex += noLine;
        glDrawArrays(GL_LINES, bufferIndex, noLine);
        bufferIndex += noLine;
    }
    if (rotationAxis == zAxis){

        bufferIndex += noLine*2;
        glDrawArrays(GL_LINES, bufferIndex, noLine);
        bufferIndex += noLine; 

    }  
}

// reset back to the start
void reset(){

    teapotPosition = vec3(0.0, 0.0,-3.0);

    rotationMatrix = mat4();

}

void changeAxis(){

    if(rotationAxis == xAxis)
        rotationAxis = yAxis;
    else
    if(rotationAxis == yAxis)
        rotationAxis = zAxis;
    else
        rotationAxis = xAxis;
}

void rotateOnAxis(float rot){

    rotationMatrix = rotate(rotationMatrix, rot, rotationAxis);
}




// handle keypress
void keyHandle(unsigned char key, int x, int y){

    switch(key){

        case 033:
            exit(EXIT_SUCCESS);
            break;
        case '0':
            reset();
            break;
        case 'a':
            teapotPosition = teapotPosition + vec3(-0.1, 0.0, 0.0);
            break;
        case 'd':
            teapotPosition = teapotPosition + vec3(0.1, 0.0, 0.0);
            break;
        case 'w':
            teapotPosition = teapotPosition + vec3(0.0, 0.1, 0.0);
            break;
        case 's':
            teapotPosition = teapotPosition + vec3(0.0, -0.1, 0.0);
            break;
        case 'q':
            teapotPosition = teapotPosition + vec3(0.0, 0.0, -0.1);
            break;
        case 'e':
            teapotPosition = teapotPosition + vec3(0.0, 0.0, 0.1);
            break;
        case 'j':
            changeAxis();
            break;
        case 'k':
            rotateOnAxis(-5.0);
            break;
        case 'l':
            rotateOnAxis(5.0);
            break;
    }

    glutPostRedisplay();
}


void reshape(int h, int w){

    glViewport(0, 0, h, w);

}

void initCamera(){

    viewMatrix = lookAt(cameraDirection, cameraPosition, cameraUp);
    projMatrix = perspective(FOV, (float)S_WIDTH/(float)S_HEIGHT, P_NEAR, P_FAR);
    reset();
}




int createCube(int i){

    // sides of the cube
    vertices[i++] = body[0];
    vertices[i++] = body[1];
    vertices[i++] = body[2];
    vertices[i++] = body[3];
    vertices[i++] = body[4];
    vertices[i++] = body[5];
    vertices[i++] = body[6];
    vertices[i++] = body[7];
    vertices[i++] = body[0];
    vertices[i++] = body[1];

    // top
    vertices[i++] = body[0];
    vertices[i++] = body[2];
    vertices[i++] = body[4];
    vertices[i++] = body[6];

    //bottom
    vertices[i++] = body[1];
    vertices[i++] = body[3];
    vertices[i++] = body[5];
    vertices[i++] = body[7];

    std::cout << i << '\n';

    return i;

}

int createAxes(int i){

    // X axis
    vertices[i++] = vec4( 2.0, 0.0, 0.0, 1.0);
    vertices[i++] = vec4(-2.0, 0.0, 0.0, 1.0);

    // Y axis
    vertices[i++] = vec4( 0.0, 2.0, 0.0, 1.0);
    vertices[i++] = vec4( 0.0,-2.0, 0.0, 1.0);

    // Z axis
    vertices[i++] = vec4( 0.0, 0.0, 2.0, 1.0);
    vertices[i++] = vec4( 0.0, 0.0,-2.0, 1.0);

    std::cout << i << '\n';

    return i;
}

// Initialize 
void initialize(){

    // generate vertex data
    int i = 0; 
    i = createCube(i);
    i = createAxes(i);

    if(i != numberOfVertices){

        std::cout << "Error creating vertex data: check vertex count\n";
        std::exit(0);
    }

    // set 
    initCamera();

    // load shader and activate shader
    GLuint refVertexShader = Angel::InitShader("Vertex_Shader.glsl", "Fragment_Shader.glsl");
    glUseProgram(refVertexShader);

    // create and activate a new vertex array object (vao)
    glGenVertexArrays(1, &refVertexArray);
    glBindVertexArray(refVertexArray);

    // create and activate a new buffer array object in the vao
    glGenBuffers(1, &refVertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, refVertexBuffer);

    // load vertex data into the buffer array
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    // load postion pipeline variable
    GLuint refVec4Position = glGetAttribLocation(refVertexShader, "Position");
    glEnableVertexAttribArray(refVec4Position);
    glVertexAttribPointer(refVec4Position, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));

    // get pointers for uniform variables in shader program
    refUniformModelxView = glGetUniformLocation(refVertexShader, "ModelxView");
    refUniformProjection = glGetUniformLocation(refVertexShader, "Projection");

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS); 

    glClearColor(0.0, 0.0, 0.0, 1.0);
}

int main(int argc, char* argv[]){

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(S_WIDTH, S_HEIGHT );
    glutCreateWindow("TeapotViewer");

    glewInit();

    initialize();

    glutDisplayFunc(draw);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyHandle);

    glutMainLoop();
    return 0;
}
#version 150
uniform mat4 ModelxView;
uniform mat4 Projection;
in vec4 Position;


void main()
{
    gl_Position = Projection*ModelxView*Position;
}
     #version 150
out vec4 fColor;

void main()
{
    fColor = vec4(1.0, 1.0, 0.0, 1.0);
}