1

glDrawArrays は画面に何も描画していませんが、glVertex2d を使用した描画は機能しているようです。

私はOpenGL 3.0を使用しています。私の推測では、シェーダーの問題だと思いますが、どちらも正常にコンパイルされるため、何が問題なのかわかりません。何が問題を引き起こしているのか誰にも分かりますか?

   // OpenGL.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"

    #define GLEW_STATIC
    #include <GL\glew.h>
    #include <fstream>
    #include <iostream>
    #include <GLFW\glfw3.h>

    using namespace std;

    const char* vertex_shader_data = 
        "#version 150\n"
        "in vec2 position;"
        "void main(){"
        "gl_Position = vec4(position, 0.0, 1.0);"
        "}";

    const char* fragment_shader_data = 
        "#version 150\n"
        "out vec4 outColor;"
        "void main(){"
        "outColor = vec4(1.0, 1.0, 1.0, 1.0);"
        "}";


    int _tmain(int argc, _TCHAR* argv[])

{

    glewExperimental = GL_TRUE;
    glfwInit();

    GLFWwindow *window = glfwCreateWindow(640, 480, "OpenGL Fun", NULL, NULL);

    glfwMakeContextCurrent(window);

    glewInit();

    GLuint buffer;

    float vertices[] = { 0.0f, 0.5f, -0.5f, -0.5f,  0.5f, -0.5f };

    glEnable(GL_VERTEX_ARRAY);

    glGenBuffers(1, &buffer);

    GLuint vao;
    glGenVertexArrays(1, &vao);


    glBindVertexArray(vao);

    glBindBuffer(GL_ARRAY_BUFFER, buffer);

    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);


    GLuint vertexshader = glCreateShader(GL_VERTEX_SHADER);
    GLuint fragmentshader = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(vertexshader, 1, &vertex_shader_data, NULL);
    glShaderSource(fragmentshader, 1, &fragment_shader_data, NULL);

    glCompileShader(vertexshader);
    glCompileShader(fragmentshader);

    GLint vstatus;
    GLint fstatus;

    glGetShaderiv(vertexshader, GL_COMPILE_STATUS, &vstatus);
    glGetShaderiv(fragmentshader, GL_COMPILE_STATUS, &fstatus);

#pragma region Shader Compilation Test
    if(!((vstatus == GL_TRUE) && (fstatus == GL_TRUE))) {

        char info[512];

        glGetShaderInfoLog(vertexshader, 512, NULL, info);

        for(int i=0; i<512; i++) {

            cout << info[i];

        }

        glGetShaderInfoLog(fragmentshader, 512, NULL, info);

        for(int i=0; i<512; i++) {

            cout << info[i];

        }

    }  
#pragma endregion


    GLuint shaderprogram = glCreateProgram();
    glAttachShader(shaderprogram, vertexshader);
    glAttachShader(shaderprogram, fragmentshader);

    glBindFragDataLocation(shaderprogram, 0, "outColor");

    glLinkProgram(shaderprogram);

    glUseProgram(shaderprogram);

    GLuint pos_attribute = glGetAttribLocation(vertexshader, "position");
    glEnableVertexAttribArray(pos_attribute);

    glVertexAttribPointer(pos_attribute, 2, GL_FLOAT, GL_FALSE, 0, 0);


    while(!glfwWindowShouldClose(window)) {

        glClearColor( 1.0f, 0.0f, 0.0f, 1.0f );
        glClear( GL_COLOR_BUFFER_BIT );

        glDrawArrays(GL_TRIANGLES, 0, 3);

        /*
        glBegin(GL_TRIANGLES);
        glVertex2d(0.0f, 0.5f);
        glVertex2d(-0.5f, -0.5f);
        glVertex2d(0.5f, -0.5f);
        glEnd();*/

        glfwSwapBuffers(window);

        glfwPollEvents();

    }


    glfwTerminate();


    return 0;
}
4

0 に答える 0