0

I've been having trouble drawing a simple vertex-specified (box-like) teapot model using shaders with some input for translation and rotation. I've checked my gl code and matrices (object position on -z, camera at origin, etc) over and over and don't see why I'm still just getting a blank screen. to keep the code short, I've just put in the code for the basic cube of my model (once I at least get that I'll be fine).

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;
}

vertex shader

#version 150
uniform mat4 ModelxView;
uniform mat4 Projection;
in vec4 Position;


void main()
{
    gl_Position = Projection*ModelxView*Position;
}

fragment shader

     #version 150
out vec4 fColor;

void main()
{
    fColor = vec4(1.0, 1.0, 0.0, 1.0);
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Darksai
  • 11
  • Have you tried rendering it with the fixed function pipeline? – Andreas Brinck Apr 17 '12 at 11:55
  • @AndreasBrinck: How would that help him get his shader cod working? – Nicol Bolas Apr 17 '12 at 16:03
  • Can you first confirm that glGetError == 0 in your rendering loop? – Tim Apr 17 '12 at 16:25
  • i added code to print out glGetError() and it is returning 0, but only once (unless there is a key event, in which case glutPostRedisplay() is called, but screen is still black) so draw() is not being recalled as it should for some reason – Darksai Apr 17 '12 at 17:02
  • added a missing idle callback, so that's fixed, but still getting a plain screen – Darksai Apr 17 '12 at 17:08

2 Answers2

0

Not sure about this, as I'm not sure what's going on in Angel:: behind the scenes, but is it correct to output fColor from your fragment shader? I thought in glsl 150 it expects the special variable gl_FragColor, unless Angel does something specific to mitigate this.

I looked at this for a while but I don't see anything else that would cause a problem. Unfortunately I think if you're really stuck you might have to start writing a simpler example (no functions, just a straight rundown of initialization through rendering of a triangle).

Also make sure glGetError is called at least once per draw() loop to make sure it's not missing anything. Does Angel throw an exception on link/compilation failure?

Tim
  • 35,413
  • 11
  • 95
  • 121
  • All that's in the Angel namespace is a function to load the vertex and fragment shader program, which I downloaded from my textbook's resource reference and adapted the InitShader.cpp into a .hpp as the original header wouldn't compile without other source files I didn't want to use. I really hope you're right about gl_FragColor.. that would make a lot of sense, will try that out now. I ran my program printing glGetError() everyframe and it returned 0 everytime. – Darksai Apr 17 '12 at 21:13
  • Didn't work sadly. I looked at the glsl 1.5 spec, gl_fragColor was already deprecated and other examples of 1.5 on the web output the color with a user-defined variable in the same way. – Darksai Apr 17 '12 at 21:38
  • OK, I did something I should've done a long time ago.. I just added glDrawArrays(GL_TRIANGLES, 0, sizeof(vertices)); to the draw method and all the object transformations work as "expected" tho of course it looks nothing like what it should. I'll either have to figure out what's wrong with GL_TRIANGLE_STRIP or brute force it and rewrite the vertex code for GL_TRIANGLES (sigh). – Darksai Apr 17 '12 at 22:18
0

Hmm, how about this:

viewMatrix = lookAt(cameraDirection, cameraPosition, cameraUp);

Is this the same lookAt as gluLookAt? In that function the prototype is (position, lookAtPoint, cameraUp), which is backwards of what you've got.

Tim
  • 35,413
  • 11
  • 95
  • 121