2017-04-02 19 views
0

私は三角形を作ろうとしており、OpenGLの拡張としてGLEW、GLFW、およびGLMを使用しています。OpenGL 3.3コアにはシェーダを使用する必要がありますか?

#include <stdlib.h> 
#include <stdio.h> 
#include <gl\glew.h> 
#include <GLFW\glfw3.h> 
#include <glm\glm.hpp> 
using namespace glm; 

int main() 
{ 
    if (!glfwInit()) 
    { 
     fprintf(stderr, "Failed to initialize GLFW\n"); 
     return -1; 
    } 
    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed 
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL 

    // Open a window and create its OpenGL context 
    GLFWwindow* window; // (In the accompanying source code, this variable is global) 
    window = glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL); 
    if (window == NULL) 
    { 
     fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n"); 
     glfwTerminate(); 
     return -1; 
    } 

    glfwMakeContextCurrent(window); // Initialize GLEW 
    glewExperimental = true; // Needed in core profile 
    if (glewInit() != GLEW_OK) 
    { 
     fprintf(stderr, "Failed to initialize GLEW\n"); 
     return -1; 
    } 
    // Ensure we can capture the escape key being pressed below 
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); 

    GLuint VertexArrayID; 
    glGenVertexArrays(1, &VertexArrayID); 
    glBindVertexArray(VertexArrayID); 

    // An array of 3 vectors which represents 3 vertices 
    static const GLfloat g_vertex_buffer_data[] = { 
     -1.0f, -1.0f, 0.0f, 
     1.0f, -1.0f, 0.0f, 
     0.0f, 1.0f, 0.0f, 
    }; 

    // This will identify our vertex buffer 
    GLuint vertexbuffer; 
    // Generate 1 buffer, put the resulting identifier in vertexbuffer 
    glGenBuffers(1, &vertexbuffer); 
    // The following commands will talk about our 'vertexbuffer' buffer 
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 
    // Give our vertices to OpenGL. 
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); 


    do { 
     // 1rst attribute buffer : vertices 
     glEnableVertexAttribArray(0); 
     glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 
     glVertexAttribPointer(
      0,     // attribute 0. No particular reason for 0, but must match the layout in the shader. 
      3,     // size 
      GL_FLOAT,   // type 
      GL_FALSE,   // normalized? 
      0,     // stride 
      (void*)0   // array buffer offset 
     ); 
     // Draw the triangle ! 
     glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle 
     glDisableVertexAttribArray(0); 

     // Swap buffers 
     glfwSwapBuffers(window); 
     glfwPollEvents(); 

    } // Check if the ESC key was pressed or the window was closed 
    while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && 
     glfwWindowShouldClose(window) == 0); 
} 

編集

コードだけでウィンドウが開き、空の黒を示し、その白い三角形が表示されていません。ここで

は、私が持っているコードです。 また、最初にコードを追加しましたが、コードの一部が欠落しています。しかし、それでも白い三角形は生成されません。

+0

ああ、それはnavpi.dll PDBファイルに関するエラーをスローしますが、私は、これは問題ではありませんことをお読みください。 – user7688554

+0

あなたは何を求めていますか?あなたが知りたいことは明確ではありません。 –

+0

ああ、私に再編集させてください – user7688554

答えて

1

OpenGL 3.3にシェーダを使用する必要がありますか?

あなたは何も表示されませんシェーダを使用する必要がありますか。何かが表示された場合は、それが不特定の動作です。個々のドライバーに大きく依存しています。


あなたは明確な色をいじってみて、画面をクリアすることができます:

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

チュートリアルでは、黒の背景に白の三角形を得ることができました。私のコンピュータでは、その反対のケースがありました。

チュートリアルも指摘:

あなたは幸運にしている場合、あなたは結果を見ることができます(そうでない場合は慌てる必要はありません)

そうの残りの部分を読みますこのチュートリアルには、シェーダも含まれています。

+0

ありがとう! :)今は意味があります。 – user7688554

+0

あなたは大歓迎です! – Vallentin

関連する問題