Sign in to follow this  
Followers 0
KaiserJohan

OpenGL
Applying a perspective matrix

7 posts in this topic

Hi,
I'm having problems applying a perspective matrix to my 3d cube in OpenGL (3.3)

I'm uploading the perspective matrix (along with the vertex color) through uniform buffers. I can confirm this uniform buffer process works if I upload the identity matrix, but when I try the perspective variant it does not work (nothing gets drawn),

So when I set the perspective matrix like this:

[CODE]
uint16_t h = mEngine->GetRenderer().GetScreenMode().ScreenHeight;
uint16_t w = mEngine->GetRenderer().GetScreenMode().ScreenWidth;
shaderData.mPerspMatrix = CreatePerspectiveMatrix(45.0f, w/(float) h, 0.5f, 3.0f);
[/CODE]

it dosn't work, but when I set it to the identity matrix it draws, but of course with no perspective

[CODE]
shaderData.mPerspMatrix = Mat4(1.0f);
[/CODE]

Here's the cube data:

[CODE]
const float vertexPositions[] =
{
// front vertices
0.0f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
// back vertices
0.0f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.0f, 0.5f,
0.0f, 0.0f, 0.5f
};
const GLshort indexData[] =
{
// back
5, 4, 7,
5, 7, 6,
// right
1, 5, 6,
1, 6, 2,
// left
0, 4, 7,
0, 7, 3,
// top
5, 4, 0,
5, 0, 1,
// bottom
6, 7, 3,
6, 3, 2,
// front
1, 0, 3,
1, 3, 2
};
[/CODE]

Buffer and draw it like this:

[CODE]
// buffer data
glBindBuffer(GL_ARRAY_BUFFER, mVBO_VertexShader);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexData), indexData, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

// setup VAO
glBindVertexArray(mVAO);
glBindBuffer(GL_ARRAY_BUFFER, mVBO_VertexShader);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);


//.... and drawing
glBindVertexArray(mVAO);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
glBindVertexArray(0);
[/CODE]

Here's my vertex + fragment shader:

[CODE]
const std::string gVertexShader = "#version 330 \n \
\n \
layout(std140) uniform Uni \n \
{ \n \
vec4 mColor; \n \
mat4 mPerspMatrix; \n \
}; \n \
\n \
in vec3 in_position; \n \
out vec3 ex_color; \n \
void main() \n \
{ \n \
vec4 camPos = vec4(in_position, 1.0f); \n \
ex_color = mColor.xyz; \n \
gl_Position = mPerspMatrix * camPos; \n \
} \n";

const std::string gFragmentShader = "#version 330 \n \
in vec3 ex_color; \n \
out vec4 outColor; \n \
void main() \n \
{ \n \
outColor = vec4(ex_color, 1.0f); \n \
} \n";
[/CODE]

What am I missing to apply a perspective?

EDIT: Sorry I hope its readable, dosn't seem copy-paste works too well? Edited by KaiserJohan
0

Share this post


Link to post
Share on other sites
The cube extends from 0 to 0.5 along all axes, including the Z-axis, and the near clip plane of the projection matrix is 0.5. As expected, the cube is clipped entirely because it is outside the view volume.
2

Share this post


Link to post
Share on other sites
Sounds reasonable; but I set the nearest Z-values of the cube to 0.2f and change to CreatePerspectiveMatrix(45.0f, w/(float) h, 0.1f, 3.0f); for example and the problem still persists Edited by KaiserJohan
0

Share this post


Link to post
Share on other sites
I don't know, why don't you try and see?

I'm not saying that is the only problem, just one I saw. I could see another potential problem in that only the far side of the cube remains, and with back face culling, you won't see the back of the cube (which would be viewed with the inside towards the viewpoint) assuming the winding order of all faces are consistent. A better idea than adjusting the near clip plane is to simply move the cube inside the view volume instead. Move it 1 or 2 units into the scene so that the Z-range becomes, say, 1 to 1.5 instead.
2

Share this post


Link to post
Share on other sites
Perhaps the perspective matrix is similar to the one in gluPerspective where the Z-range is actually negative. A near and far clip plane parameter pair of 0.5 and 3.0 corresponds to a visible Z-range of -0.5 to -3.0.
1

Share this post


Link to post
Share on other sites
Yes, that nailed it, I had to supply negative values. Thanks.

On a sidenote, what is the reason behind it? Edited by KaiserJohan
0

Share this post


Link to post
Share on other sites
That's the result of making the fairly arbitrary decision of using a bottom-left origin and a right-handed-based coordinate system; the negative Z-axis points into the screen.
2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • By recp
      Hi,
      I'm working on new asset importer (https://github.com/recp/assetkit) based on COLLADA specs, the question is not about COLLADA directly
      also I'm working on a new renderer to render (https://github.com/recp/libgk) imported document.
      In the future I'll spend more time on this renderer of course, currently rendering imported (implemented parts) is enough for me
      assetkit imports COLLADA document (it will support glTF too),
      importing scene, geometries, effects/materials, 2d textures and rendering them seems working
      My actual confusion is about shaders. COLLADA has COMMON profile and GLSL... profiles,
      GLSL profile provides shaders for effects so I don't need to wory about them just compile, link, group them before render

      The problem occours in COMMON profile because I need to write shaders,
      Actually I wrote them for basic matrials and another version for 2d texture
      I would like to create multiple program but I am not sure how to split this this shader into smaller ones,

      Basic material version (only colors):
      https://github.com/recp/libgk/blob/master/src/default/shader/gk_default.frag
      Texture version:
      https://gist.github.com/recp/b0368c74c35d9d6912f524624bfbf5a3
      I used subroutines to bind materials, actually I liked it,
      In scene graph every node can have different program, and it switches between them if parentNode->program != node->program
      (I'll do scene graph optimizations e.g.  view frustum culling, grouping shaders... later)

      I'm going to implement transparency but I'm considering to create separate shaders,
      because default shader is going to be branching hell
      I can't generate shader for every node because I don't know how many node can be exist, there is no limit.
      I don't know how to write a good uber-shader for different cases:

      Here material struct:
      struct Material { ColorOrTexture emission; ColorOrTexture ambient; ColorOrTexture specular; ColorOrTexture reflective; ColorOrTexture transparent; ColorOrTexture diffuse; float shininess; float reflectivEyety; float transparency; float indexOfRefraction; }; ColorOrTexture could be color or 2d texture, if there would be single colorOrTex then I could split into two programs,
      Also I'm going to implement transparency, I am not sure how many program that I needed

      I'm considering to maintain a few default shaders for COMMON profile,
      1-no-texture, 2-one of colorOrTexture contains texture, 3-........

      Any advices in general or about how to optimize/split (if I need) these shaders which I provied as link?
      What do you think the shaders I wrote, I would like to write them without branching if posible,
      I hope I don't need to write 50+ or 100+ shaders, and 100+ default programs

      PS: These default shaders should render any document, they are not specific, they are general purpose...
             I'm compiling and linking default shaders when app launched

      Thanks
    • By CircleOfLight97
      Hi guys,
      I would like to contribute to a game project as a developer (open source possibly). I have some experiences in C/C++ in game development (perso projects). I don't know either unreal or unity but I have some knowledges in opengl, glsl and shading theory as I had some courses at university regarding to that. I have some knowledges in maths and basic in physics. I know a little how to use blender to do modelling, texturing and simple game assets (no characters, no animation no skinning/rigging). I have no game preferences but I like aventure game, dungeon crawler, platformers, randomly generated things. I know these kind of projects involve a lot of time and I'd be really to work on it but if there are no cleary defined specific design goals/stories/gameplay mechanics I would like to not be part of it x) and I would rather prefer a smaller but well defined project to work on that a huge and not 'finishable' one.
      CircleOfLight97
    • By gamesthatcouldbeworse
      Hi, I finally released KILL COMMANDO on gamejolt for free. It is a retro-funsplatter-shooter with C64 style. Give it a try.
    • By phil67rpg

      void TimerFunction(int value) {  glutPostRedisplay();  glutTimerFunc(1000, TimerFunction, 1); } void drawScene() {  glClear(GL_COLOR_BUFFER_BIT);      drawScene_bug();  TimerFunction(1);  eraseScene_bug(); // drawScene_bug_two(); // eraseScene_bug_two(); drawScene_ship(); drawScene_bullet();  glutSwapBuffers(); }
  • Popular Now