2013-01-17 2 views
0

나는 모든 것을 시도했다. OpenGL 튜토리얼을 따라 가면 http://www.opengl-tutorial.org/beginners-tutorials/입니다. 튜토리얼 4에서 두 모델을 사용하라는 메시지가 표시됩니다. 하지만 삼각형과 큐브를 동시에 렌더링 할 수는 없습니다 (삼각형이 표시되지 않음). 삼각형과 관련된 변수의 이름을 변경하면서 모든 것을 동일하게 수행했습니다. 나는 다른 사이트에서 OPENGL 3.3 문법에 대한 답을 찾지 못했습니다. 완전히 다른 문법으로 발견되었습니다. (나는 오류가 발견 될 때마다 그 방법을 바꾸고 싶지 않습니다. 오류. 나를 위해 어떤 이해가되지 않습니다).삼각형이 화면 (OpenGL 3.3)에 표시되지 않는 이유는 무엇입니까?

코드는 조금 큰, 그러나 많은 일들이 창을 열고 메인 루프 실행 단지이다 (그리고 큐브의 좌표를 그들은 내가 나중에 배울거야 VBO 없습니다..) :

// Include standard headers 
#include <stdio.h> 
#include <stdlib.h> 
// Include GLEW. Always include it before gl.h and glfw.h, since it's a bit magic. 
#include <GL/glew.h> 
// Include GLFW 
#include <GL/glfw.h> 
// Include GLM 
#include <glm/glm.hpp> 
#include "common/shader.hpp" 
#include <iostream> 
#include <glm/gtc/matrix_transform.hpp> 
using namespace std; 
using namespace glm; 


// Include standard headers 
#include <stdio.h> 
#include <stdlib.h> 

// Include GLEW 
#include <GL/glew.h> 

// Include GLFW 
#include <GL/glfw.h> 

// Include GLM 
#include <glm/glm.hpp> 
#include <glm/gtc/matrix_transform.hpp> 
using namespace glm; 

#include <common/shader.hpp> 

int main(int argc, char *argv[]) 
{ 
    // Initialise GLFW 
    if(!glfwInit()) 
    { 
     fprintf(stderr, "Failed to initialize GLFW\n"); 
     return -1; 
    } 
    glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4); // 4x antialiasing 
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); // We want OpenGL 3.3 
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3); 
    glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL 

    // Open a window and create its OpenGL context 
    if(!glfwOpenWindow(1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW)) 
    { 
     fprintf(stderr, "Failed to open GLFW window\n"); 
     glfwTerminate(); 
     return -1; 
    } 



    // Initialize GLEW 
    if (glewInit() != GLEW_OK) { 
     fprintf(stderr, "Failed to initialize GLEW\n"); 
     return -1; 
    } 

    // Enable depth test 
    glEnable(GL_DEPTH_TEST); 
    // Accept fragment if it closer to the camera than the former one 
    glDepthFunc(GL_LESS); 

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

    // Create and compile our GLSL program from the shaders 
    GLuint programID = LoadShaders("TransformVertexShader.vertexshader", "ColorFragmentShader.fragmentshader"); 


    //Perspective Matrix 

    // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units 
    mat4 Projection = perspective(45.0f, 4.0f/3.0f, 0.1f, 100.0f); 
    // Camera matrix 
    mat4 View = lookAt(
    vec3(4,3,-3), // Camera is at (4,3,3), in World Space 
    vec3(0,0,0), // and looks at the origin 
    vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down) 
    ); 
    // Model matrix : an identity matrix (model will be at the origin) 
    mat4 Model = mat4(1.0f); // Changes for each model ! 
    // Our ModelViewProjection : multiplication of our 3 matrices 
    mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around 


    // Get a handle for our "MVP" uniform. 
    // Only at initialisation time. 
    GLuint MatrixID = glGetUniformLocation(programID, "MVP"); 

    // Send our transformation to the currently bound shader, 
    // in the "MVP" uniform 
    // For each model you render, since the MVP will be different (at least the M part) 
    glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); 


    mat4 triangle_Model = mat4(1.0f); 
    mat4 triangle_MPV = Projection * View * triangle_Model; 
    GLuint triangle_MatrixID = glGetUniformLocation(programID, "MPV"); 
    glUniformMatrix4fv(triangle_MatrixID, 1, GL_FALSE, &triangle_MPV[0][0]); 






    static const GLfloat triangle_buffer_data[] = { 
     -2.0f, -3.0f, 1.0f, 
     2.0f, -3.0f, 1.0f, 
     0.0f, 3.0f,-1.0f 
    }; 
    // This will identify our vertex buffer 
    GLuint triangle_vertexbuffer; 
    // Generate 1 buffer, put the resulting identifier in vertexbuffer 
    glGenBuffers(1, &triangle_vertexbuffer); 
    // The following commands will talk about our 'vertexbuffer' buffer 
    glBindBuffer(GL_ARRAY_BUFFER, triangle_vertexbuffer); 
    // Give our vertices to OpenGL. 
    glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_buffer_data), triangle_buffer_data, GL_STATIC_DRAW); 







    // Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle. 
    // A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices 
    static const GLfloat g_vertex_buffer_data[] = { 
     -1.0f,-1.0f,-1.0f, // triangle 1 : begin 
     -1.0f,-1.0f, 1.0f, 
     -1.0f, 1.0f, 1.0f, // triangle 1 : end 
     1.0f, 1.0f,-1.0f, // triangle 2 : begin 
     -1.0f,-1.0f,-1.0f, 
     -1.0f, 1.0f,-1.0f, // triangle 2 : end 
     1.0f,-1.0f, 1.0f, 
     -1.0f,-1.0f,-1.0f, 
     1.0f,-1.0f,-1.0f, 
     1.0f, 1.0f,-1.0f, 
     1.0f,-1.0f,-1.0f, 
     -1.0f,-1.0f,-1.0f, 
     -1.0f,-1.0f,-1.0f, 
     -1.0f, 1.0f, 1.0f, 
     -1.0f, 1.0f,-1.0f, 
     1.0f,-1.0f, 1.0f, 
     -1.0f,-1.0f, 1.0f, 
     -1.0f,-1.0f,-1.0f, 
     -1.0f, 1.0f, 1.0f, 
     -1.0f,-1.0f, 1.0f, 
     1.0f,-1.0f, 1.0f, 
     1.0f, 1.0f, 1.0f, 
     1.0f,-1.0f,-1.0f, 
     1.0f, 1.0f,-1.0f, 
     1.0f,-1.0f,-1.0f, 
     1.0f, 1.0f, 1.0f, 
     1.0f,-1.0f, 1.0f, 
     1.0f, 1.0f, 1.0f, 
     1.0f, 1.0f,-1.0f, 
     -1.0f, 1.0f,-1.0f, 
     1.0f, 1.0f, 1.0f, 
     -1.0f, 1.0f,-1.0f, 
     -1.0f, 1.0f, 1.0f, 
     1.0f, 1.0f, 1.0f, 
     -1.0f, 1.0f, 1.0f, 
     1.0f,-1.0f, 1.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); 

    // One color for each vertex. They were generated randomly. 
    static const GLfloat g_color_buffer_data[] = { 
     0.583f, 0.771f, 0.014f, 
     0.609f, 0.115f, 0.436f, 
     0.327f, 0.483f, 0.844f, 
     0.822f, 0.569f, 0.201f, 
     0.435f, 0.602f, 0.223f, 
     0.310f, 0.747f, 0.185f, 
     0.597f, 0.770f, 0.761f, 
     0.559f, 0.436f, 0.730f, 
     0.359f, 0.583f, 0.152f, 
     0.483f, 0.596f, 0.789f, 
     0.559f, 0.861f, 0.639f, 
     0.195f, 0.548f, 0.859f, 
     0.014f, 0.184f, 0.576f, 
     0.771f, 0.328f, 0.970f, 
     0.406f, 0.615f, 0.116f, 
     0.676f, 0.977f, 0.133f, 
     0.971f, 0.572f, 0.833f, 
     0.140f, 0.616f, 0.489f, 
     0.997f, 0.513f, 0.064f, 
     0.945f, 0.719f, 0.592f, 
     0.543f, 0.021f, 0.978f, 
     0.279f, 0.317f, 0.505f, 
     0.167f, 0.620f, 0.077f, 
     0.347f, 0.857f, 0.137f, 
     0.055f, 0.953f, 0.042f, 
     0.714f, 0.505f, 0.345f, 
     0.783f, 0.290f, 0.734f, 
     0.722f, 0.645f, 0.174f, 
     0.302f, 0.455f, 0.848f, 
     0.225f, 0.587f, 0.040f, 
     0.517f, 0.713f, 0.338f, 
     0.053f, 0.959f, 0.120f, 
     0.393f, 0.621f, 0.362f, 
     0.673f, 0.211f, 0.457f, 
     0.820f, 0.883f, 0.371f, 
     0.982f, 0.099f, 0.879f 
    }; 

    GLuint colorbuffer; 
    glGenBuffers(1, &colorbuffer); 
    glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW); 

    glfwSetWindowTitle("Tutorials"); 


    // Dark blue background 
    glClearColor(0.0f, 0.0f, 0.3f, 0.0f); 



    // Ensure we can capture the escape key being pressed below 
    glfwEnable(GLFW_STICKY_KEYS); 


    //main loop 
    do{ 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

     // Use our shader 
     glUseProgram(programID); 

     //First Attribute: Draw a Cube 
     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 
     ); 

     // 2nd attribute buffer : colors 
     glEnableVertexAttribArray(1); 
     glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); 
     glVertexAttribPointer(
      1,        // attribute. No particular reason for 1, 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, 12*3); // 12*3 indices starting at 0 -> 12 triangles -> 6 squares 

     glDisableVertexAttribArray(0); 


     //3rd attribute: draw a triangle 
     glEnableVertexAttribArray(2); 
     glBindBuffer(GL_ARRAY_BUFFER, triangle_vertexbuffer); 
     glVertexAttribPointer(
      2, 
      3, 
      GL_FLOAT, 
      GL_FALSE, 
      0, 
      (void*)0 
     ); 

     glDrawArrays(GL_TRIANGLES,0,3); 

     glDisableVertexAttribArray(2); 




     // Send our transformation to the currently bound shader, 
     // in the "MVP" uniform 
     // For each model you render, since the MVP will be different (at least the M part) 
     glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); 

     glUniformMatrix4fv(triangle_MatrixID, 1 ,GL_FALSE, &triangle_MPV[0][0]); 


     // Swap buffers 
     glfwSwapBuffers(); 

    } // Check if the ESC key was pressed or the window was closed 
    while(glfwGetKey(GLFW_KEY_ESC) != GLFW_PRESS && 
    glfwGetWindowParam(GLFW_OPENED)); 

    glfwTerminate(); 

    // Cleanup VBO 
    glDeleteBuffers(1, &vertexbuffer); 
    glDeleteVertexArrays(1, &VertexArrayID); 
    glDeleteBuffers(1, &triangle_vertexbuffer); 
    glDeleteBuffers(1, &colorbuffer); 


return EXIT_SUCCESS; 
} 

그리고, 나는이 문제가 (튜토리얼은 1 개 쉐이더를 필요로하고 큐브가 완벽하게 작동했다)이라고 생각하지 않는다,하지만 난 너무 여기 쉐이더 코드를 게시합니다 :

Collor Fragment shader: 

#version 330 core 

// Interpolated values from the vertex shaders 
in vec3 fragmentColor; 

// Ouput data 
out vec3 color; 


void main(){ 

// Output color = color specified in the vertex shader, 
// interpolated between all 3 surrounding vertices 
color = fragmentColor; 

} 




TransformVertexShadder: 

#version 330 core 

// Input vertex data, different for all executions of this shader. 
layout(location = 0) in vec3 vertexPosition_modelspace; 
layout(location = 1) in vec3 vertexColor; 

// Output data ; will be interpolated for each fragment. 
out vec3 fragmentColor; 
// Values that stay constant for the whole mesh. 
uniform mat4 MVP; 

void main(){  

// Output position of the vertex, in clip space : MVP * position 
gl_Position = MVP * vec4(vertexPosition_modelspace,1); 

// The color of each vertex will be interpolated 
// to produce the color of each fragment 
fragmentColor = vertexColor; 

}

도움이 필요하십니까?

답변

0

조각 색상을 vec3으로 출력 할 수 없다고 생각합니다. 대부분 vec4 (color, 1.0)를 사용합니다.

+0

사실, 쉐이더는 컴파일 할 수 없습니다. 물론 오류 코드를 확인하지 않으면 알 수 없습니다. – datenwolf

+0

그건 이상합니다. 큐브에서 vec3와 함께 작동합니다. 튜토리얼에서 그것은 다른 객체를 그리는 것이 동일한 쉐이더로 사용될 수 있다고 말했습니다. –

관련 문제