0

I am trying to load a .obj file using a .obj file loader I built and it seems to be working correctly (in terms of loading).

With that being said, when I go to draw the 3d object to the screen it creates a large red square.

GLenum err = glewInit();
if (err != GLEW_OK)
{
    std::cout << "NOT WORKING" << std::endl;
}
const char* vertexShaderSource = "#version 330\n\
    in vec4 position;\
    void main(void) {\
        gl_Position = position;\
    }";

// compile fragment shader source
const GLchar* fragmentShaderSource = "#version 330\n\
    out vec4 fragColor;\
    void main(void) {\
        fragColor = vec4(1.0,0.3,0.3,0.15);\
    }";

/* Creating Shader */
this->programId = glCreateProgram();
this->vId = glCreateShader(GL_VERTEX_SHADER);
this->fId = glCreateShader(GL_FRAGMENT_SHADER);

/* Get Shader Size */
int vertexShaderLength = strlen(vertexShaderSource);
int fragmentShaderLength = strlen(fragmentShaderSource);

/* Loading and binding shader */
glShaderSource(this->vId, 1, &vertexShaderSource, NULL);
glShaderSource(this->fId, 1, &fragmentShaderSource, NULL);

/* Compile Shaders */
glCompileShader(vId);
int iIsOk = 0;
glGetShaderiv(this->vId, GL_COMPILE_STATUS, &iIsOk);
if (!iIsOk)
{
    return;
}
int iIsOk2;
glCompileShader(fId);
glGetShaderiv(this->fId, GL_COMPILE_STATUS, &iIsOk2);
if (!iIsOk2)
{
    std::cout << "U SUCK!" << std::endl;
    std::cout << glGetError() << std::endl;
}

/* Attach Shaders */
glAttachShader(this->programId, this->vId);
glAttachShader(this->programId, this->fId);

/* Bind Attributes from Shader */
glBindAttribLocation(this->programId, 0, "position");
glBindFragDataLocation(this->programId, 0, "fragcolor");

/* Link gprogram */
glLinkProgram(this->programId);

/* Use and bind attribute */
glUseProgram(this->programId);
this->positionId = glGetAttribLocation(this->programId, "position");
glUseProgram(0);

/* VAO Time */
glGenVertexArrays(1, &this->vaoId);
glBindVertexArray(this->vaoId);

/* VBO Time assigning to VAO */
glGenBuffers(1, &this->vboId);
glBindBuffer(GL_ARRAY_BUFFER, this->vboId);
glBufferData(GL_ARRAY_BUFFER, vertcies.size() * sizeof(sf::Vector3f), &vertcies[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(this->positionId);
glVertexAttribPointer(this->positionId, 3, GL_FLOAT, GL_FALSE, sizeof(sf::Vector3f), 0);

/* Close out bindings */
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

DRAW:

glUseProgram(this->programId);
glBindVertexArray(this->vaoId);
glDrawArrays(GL_TRIANGLES, 0, this->vertcies.size());
glBindVertexArray(0);
glUseProgram(0);

I uploaded an image of what the red box displayed.

I am curious if I am missing something in the openGL code or what I am doing wrong?

I am able to display a triangle correctly, but when I go to load my .obj files this is the stuff I get displayed.

The above is all the openGL code I have in the project, is there a simple error in my code...or forgetting something?

ScreenShot Result

genpfault
  • 51,148
  • 11
  • 85
  • 139
user5600884
  • 119
  • 1
  • 8
  • 1
    Why are you specifying 2 components for the positions in the `glVertexAttribPointer()` call? Aren't you using 3D coordinates? – Reto Koradi Jan 04 '16 at 06:24
  • @Reto Koradi I am guess extremely confused on how the whole deal works. I changed that to 3 but still I get the same issue, however I did update code to show you the exact change I made. – user5600884 Jan 04 '16 at 06:26
  • 1
    There is no view matrix defined and the shading is a flat color. What result are you expecting to see? Also, are you sure your vertices are laid out properly for rendering without defining elements? I recommend starting with a comprehensive opengl tutorial, e.g. http://open.gl – Rotem Jan 04 '16 at 06:34

1 Answers1

0

You need to use a model matrix and a view matrix...

Without these matrices, your object will fill the screen, because you are viewing it in local space and the screen's pixel coordinates in that state have their value between 0 and 1.

I guess your object has its vertex coordinates go beyond 0 or 1. That's why you cannot view it entirely.

Then, if you use a hard coded color in your shader, your object will not be properly viewed.

Even if your object is a cube, you will still see a quad. So either use lighting or texture - or try to change the color of each vertex.

Hope it helps.

zx485
  • 28,498
  • 28
  • 50
  • 59
Amine Bensalem
  • 362
  • 3
  • 15