2012-04-17 6 views
0

번역 및 회전을위한 입력이있는 쉐이더를 사용하여 간단한 정점 지정 (상자 모양) 찻 주전자 모델을 그리는 데 문제가있었습니다. 나는 gl 코드와 행렬 (-z, 원점 카메라 등의 객체 위치)을 반복해서 검사했는데 왜 내가 빈 스크린을 얻는지를 보지 못했다. 코드를 짧게 유지하기 위해 필자는 내 모델의 기본 큐브 코드를 입력했습니다. (적어도 일단 괜찮 으면).OpenGL에서 정점이 그려지지 않음

namespace TeapotViewer{ 

class TeapotViewer{ 

private: 
    void intitialize(); 
    void draw(); 
    void reshape(int h, int w); 
    void keyHandle(unsigned char key, int x, int y); 
    void initCamera(); 
    void reset(); 
    void changeAxis(); 
    void rotateOnAxis(float rot); 
    int createCube(int i); 

public: 

}; 

}

#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); 
} 
+0

고정 기능 파이프 라인으로 렌더링 해 보셨습니까? –

+0

@AndreasBrinck : 어떻게하면 셰이더 대구가 작동하는 데 도움이됩니까? –

+0

렌더링 루프에서 glGetError == 0을 먼저 확인할 수 있습니까? – Tim

답변

0

확실하지, 내가 천사에서 무슨 일이 일어나고 있는지 잘 모르겠어요으로 : : 뒤에서, 출력은 정확합니까?조각 쉐이더의? 나는 천사가 이것을 완화시키기 위해 특별한 것을하지 않는 한 glsl 150에서 특별한 변수 gl_FragColor을 기대한다고 생각했다.

나는 이것을 잠시 보았다. 그러나 나는 문제를 일으킬만한 다른 것을 본 적이 없다. 불행히도 저는 여러분이 정말로 갇혀 있다면 간단한 예제 (함수가 없으며, 삼각형의 렌더링을 통한 초기화의 단순한 요약)를 작성해야 할 수도 있다고 생각합니다.

또한 draw() 루프 당 적어도 한번 glGetError가 호출되어 누락되지 않았는지 확인하십시오. Angel이 링크/컴파일 오류에 대한 예외를 throw합니까?

+0

Angel의 네임 스페이스에있는 것은 모두 교과서의 리소스 참조에서 다운로드 한 정점 및 프래그먼트 쉐이더 프로그램을로드하는 함수입니다 InitShader.cpp를 .hpp로 변경했습니다. 사용하지 않으려는 다른 소스 파일없이 원래 헤더가 컴파일되지 않습니다.

정말 당신이 gl_FragColor ..에 대해 옳았기를 바랄뿐입니다.

매주 내 프로그램 인쇄 glGetError()를 실행하고 매회 0을 반환했습니다. – Darksai

+0

슬프게도 작동하지 않았습니다. 나는 glsl 1.5 스펙을 보았고, gl_fragColor는 이미 deprecated 였고 웹의 1.5의 다른 예제는 같은 방식으로 사용자 정의 변수로 색상을 출력했다. – Darksai

+0

@Darksai 실수, 죄송합니다 – Tim

0

흠, 어떻게 이것에 대해 :

viewMatrix = lookAt(cameraDirection, cameraPosition, cameraUp);

이하는 gluLookAt 같은 바라인가? 이 함수의 프로토 타입은 (position, lookAtPoint, cameraUp)입니다. 이는 여러분이 가지고있는 것의 뒤로입니다.