2016-10-23 2 views
-1

학생으로서 OpenGL을 배우려고합니다. 이 간단한 응용 프로그램이 작동하지 않는 이유를 알아 내려고 많은 시간을 보냈습니다. 그것은 우리 교수가 제시 한 예입니다. (우리는 Visual Studio를 사용하여 Windows에서 작업하고 있지만 집에서 Linux를 사용하는 것 외에는 선택의 여지가 없습니다. 다음은 내가 작성한 프로그램입니다 (Windows에서 완벽하게 작동합니다). 검은 색 창을 표시합니다 (삼각형을 표시해야합니다). 내가 (프로그램을 컴파일하는 데 사용되는 명령 여기간단한 OpenGL 응용 프로그램

#version 300 es 

layout (location = 0) in vec3 vertex_position; 

out vec3 colour; 

void main() { 
    colour = vec3(1.0, 0.0, 0.0); 
    gl_Position = vec4(vertex_position, 1.0); 
} 

된다 : shader.frag :

#version 300 es 
precision mediump float; 

in vec3 colour; 
out vec4 frag_colour; 

void main() { 
    frag_colour = vec4 (colour, 1.0); 
} 

및 shader.vert 여기

// 
// main.cpp 
// OpenGL_Shader_Example_step1 
// 
// Created by CGIS on 30/11/15. 
// Copyright © 2015 CGIS. All rights reserved. 
// 

#define GLEW_STATIC 

#include <iostream> 
#include "GL/glew.h" 
#include "GLFW/glfw3.h" 

#include <fstream> 
#include <sstream> 
#include <iostream> 
#include <string> 
#include <stdio.h> 
#include <stdlib.h> 
#include <vector> 

int glWindowWidth = 640; 
int glWindowHeight = 480; 
int retina_width, retina_height; 
GLFWwindow* glWindow = NULL; 

GLuint shaderProgram; 

GLfloat vertexCoordinates[] = { 
           0.0f, 0.5f, 0.0f, 
           0.5f, -0.5f, 0.0f, 
           -0.5f, -0.5f, 0.0f 
           }; 
GLuint verticesVBO; 
GLuint triangleVAO; 

void windowResizeCallback(GLFWwindow* window, int width, int height) 
{ 
    fprintf(stdout, "window resized to width: %d , and height: %d\n", width, height); 
    //TODO 
} 

void initObjects() 
{ 
    //generate a unique ID corresponding to the verticesVBO 
    glGenBuffers(1, &verticesVBO); 
    //bind the verticesVBO buffer to the GL_ARRAY_BUFFER target, 
    //any further buffer call made to GL_ARRAY_BUFFER will configure the 
    //currently bound buffer, which is verticesVBO 
    glBindBuffer(GL_ARRAY_BUFFER, verticesVBO); 
    //copy data into the currently bound buffer, the first argument specify 
    //the type of the buffer, the second argument specify the size (in bytes) of data, 
    //the third argument is the actual data we want to send, 
    //the last argument specify how should the graphic card manage the data 
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexCoordinates), vertexCoordinates, GL_STATIC_DRAW); 

    //generate a unique ID corresponding to the triangleVAO 
    glGenVertexArrays(1, &triangleVAO); 
    glBindVertexArray(triangleVAO); 
    glBindBuffer(GL_ARRAY_BUFFER, verticesVBO); 
    //set the vertex attributes pointers 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); 
    glEnableVertexAttribArray(0); 
    //unbind the triangleVAO 
    glBindVertexArray(0); 
} 

bool initOpenGLWindow() 
{ 
    if (!glfwInit()) { 
     fprintf(stderr, "ERROR: could not start GLFW3\n"); 
     return false; 
    } 

    //for Mac OS X 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); 
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 

    glWindow = glfwCreateWindow(glWindowWidth, glWindowHeight, "OpenGL Shader Example", NULL, NULL); 
    if (!glWindow) { 
     fprintf(stderr, "ERROR: could not open window with GLFW3\n"); 
     glfwTerminate(); 
     return false; 
    } 

    glfwSetWindowSizeCallback(glWindow, windowResizeCallback); 
    glfwMakeContextCurrent(glWindow); 

    glfwWindowHint(GLFW_SAMPLES, 4); 

    // start GLEW extension handler 
    glewExperimental = GL_TRUE; 
    glewInit(); 

    // get version info 
    const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string 
    const GLubyte* version = glGetString(GL_VERSION); // version as a string 
    printf("Renderer: %s\n", renderer); 
    printf("OpenGL version supported %s\n", version); 

    //for RETINA display 
    glfwGetFramebufferSize(glWindow, &retina_width, &retina_height); 

    return true;  
} 

void renderScene() 
{ 
    //clear the color and depth buffer before rendering the current frame 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    //specify the background color 
    glClearColor(0.8, 0.8, 0.8, 1.0); 
    //specify the viewport location and dimension 
    glViewport (0, 0, retina_width, retina_height); 

    //process the keyboard inputs 
    if (glfwGetKey(glWindow, GLFW_KEY_A)) { 
    //TODO 
    } 

    if (glfwGetKey(glWindow, GLFW_KEY_D)) { 
    //TODO 
    } 

    //bind the shader program, any further rendering call 
    //will use this shader program 
    glUseProgram(shaderProgram); 

    //bind the VAO 
    glBindVertexArray(triangleVAO); 
    //specify the type of primitive, the starting index and 
    //the number of indices to be rendered 
    glDrawArrays(GL_TRIANGLES, 0, 3); 
} 

std::string readShaderFile(std::string fileName) 
{ 
    std::ifstream shaderFile; 
    std::string shaderString; 

    //open shader file 
    shaderFile.open(fileName.c_str()); 

    std::stringstream shaderStringStream; 

    //read shader content into stream 
    shaderStringStream << shaderFile.rdbuf(); 

    //close shader file 
    shaderFile.close(); 

    //convert stream into GLchar array 
    shaderString = shaderStringStream.str(); 
    return shaderString; 
} 

void shaderCompileLog(GLuint shaderId) 
{ 
    GLint success; 
    GLchar infoLog[512]; 

    //check compilation info 
    glGetShaderiv(shaderId, GL_COMPILE_STATUS, &success); 
    if (!success) 
    { 
     glGetShaderInfoLog(shaderId, 512, NULL, infoLog); 
     std::cout << "Shader compilation error\n" << infoLog << std::endl; 
    } 
} 

void shaderLinkLog(GLuint shaderProgramId) 
{ 
    GLint success; 
    GLchar infoLog[512]; 

    //check linking info 
    glGetProgramiv(shaderProgramId, GL_LINK_STATUS, &success); 
    if (!success) { 
     glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); 
     std::cout << "Shader linking error\n" << infoLog << std::endl; 
    } 
} 

GLuint initBasicShader(std::string vertexShaderFileName, std::string fragmentShaderFileName) 
{ 
    //read, parse and compile the vertex shader 
std::string v = readShaderFile(vertexShaderFileName); 
const GLchar* vertexShaderString = v.c_str(); 
GLuint vertexShader; 
vertexShader = glCreateShader(GL_VERTEX_SHADER); 
glShaderSource(vertexShader, 1, &vertexShaderString, NULL); 
glCompileShader(vertexShader); 
//check compilation status 
shaderCompileLog(vertexShader); 

//read, parse and compile the vertex shader 
std::string f = readShaderFile(fragmentShaderFileName); 
const GLchar* fragmentShaderString = f.c_str(); 
GLuint fragmentShader; 
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); 
glShaderSource(fragmentShader, 1, &fragmentShaderString, NULL); 
glCompileShader(fragmentShader); 
//check compilation status 
shaderCompileLog(fragmentShader); 

//attach and link the shader programs 
shaderProgram = glCreateProgram(); 
glAttachShader(shaderProgram, vertexShader); 
glAttachShader(shaderProgram, fragmentShader); 
glLinkProgram(shaderProgram); 
glDeleteShader(vertexShader); 
glDeleteShader(fragmentShader); 
//check linking info 
shaderLinkLog(shaderProgram); 

return shaderProgram; 
} 

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

    initObjects(); 

    shaderProgram = initBasicShader("shaders/shader.vert", "shaders/shader.frag"); 

    while (!glfwWindowShouldClose(glWindow)) { 
     renderScene(); 

     glfwPollEvents(); 
     glfwSwapBuffers(glWindow); 
    } 

    //close GL context and any other GLFW resources 
    glfwTerminate(); 

    return 0; 
} 

는 쉐이더 프로그램입니다 비주얼 스튜디오 코드 사용) :

{ 
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format 
    "version": "0.1.0", 
    "command": "g++", 
    "isShellCommand": true, 
    "args": ["-g", 
      "-Wall", 
      "-o", "lab2.exe", 
      "main.cpp", 
      "-I/usr/include/c++/4.8.5", 
      "-I/usr/include/GL", 
      "-I/usr/include/glm", 
      "-I/usr/local/include/GLFW", 
      "-L/usr/local/lib", "-lGLEW", "-lGLU", "-lglfw3", "-lGL", "-lm", "-ldl", "-lXrender", "-ldrm", 
      "-lXdamage", "-lX11-xcb", "-lxcb-glx", "-lxcb-dri2", "-lxcb-dri3", "-lxcb-present", "-lxcb-sync", "-lxshmfence", "-lXxf86vm", 
      "-lXfixes", "-lXext", "-lX11", "-lpthread", "-lxcb", "-lXau", "-lXdmcp", 
      "-lXrandr", "-lXi", "-lXxf86vm", "-lXcursor"], 
    "showOutput": "always" 
} 

args 매개 변수는 g ++에 제공됩니다. 오류가 발생하면 시작해야 할 시점이 있지만 이렇게해야 할 일을 모릅니다.

OpenGL vendor string: Intel Open Source Technology Center 
OpenGL renderer string: Mesa DRI Intel(R) Haswell Mobile 
OpenGL core profile version string: 3.3 (Core Profile) Mesa 11.2.0 
OpenGL core profile shading language version string: 3.30 
OpenGL core profile context flags: (none) 
OpenGL core profile profile mask: core profile 
OpenGL core profile extensions: 
OpenGL version string: 3.0 Mesa 11.2.0 
OpenGL shading language version string: 1.30 
OpenGL context flags: (none) 
OpenGL extensions: 
+1

도움말을 디버깅하려면 [도움말]에 따라 [mcve]가 필요합니다. 귀하의 코드가 최소가 아닙니다. 질문을 편집하여 (질문에서 문제를 재현하는 데 필요한 코드 만), 완료 (사용자가 문제를 재현 할 다른 방법은 필요 없음) 및 확인 가능한 코드를 작성하십시오 (제공된 코드는 당신이 직면 한 정확한 문제). 그것은 귀하의 질문은 스택 오버플로에 대한 주제입니다. 이것은 또한 일반적인 [downvote 이유] (http://idownvotedyoubecause.com/so/TooMuchCode)입니다. –

+0

프로그래밍에 관한 실제적인 질문이있는 것 같지 않습니다. [작은 프로그램을 디버깅하는 방법] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)을 읽는 것만으로도 더 많은 이점을 얻을 수 있습니다. – StoryTeller

+2

OpenGL 4.1을 요구하지만 드라이버는 3.3 만 지원합니다. 나는 당신이 실제로 새로운 버전을 요구하는 것을 사용하고 있다면 신중하게 점검하지 않았다. 그러나 무엇이 잘못되었는지 확인하려면 gl 디버깅이나 추적을 사용하여 opengl/glx 호출이 실패했는지 확인하고 싶습니다. 그것은 실제로 문제가 무엇인지 알아내는 데 도움이 될 것입니다. –

답변

0

내가 무슨 일이 일어나고 있는지보기 위해 OpenGL을/GLSL 디버거를 사용하는 것이 좋습니다 것 : 여기

glxinfo | grep OpenGL 명령의 결과입니다. 오래 전부터 glslDevil을 사용하여 많은 성공을 거두었습니다. Linux 호환이며 OpenGL 오류 (특히 하드웨어/플랫폼에서 지원되지 않는 기능)를 발견하는 데 도움이 될 수 있습니다.