Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Newbie shading question.

  1. #1
    Newbie Newbie
    Join Date
    Jan 2014
    Posts
    2

    Newbie shading question.

    Hello all! I am new to OpenGL and I'm having a small problem that I can't seem to find the answer to. I can actually live with it if there is no fix but it would be nice to have it display correctly. I hope it it something simple that I'm doing wrong. I'm using SDL with OpenGL and am writing a 2d tile engine. My init code is:

    Code :
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
     
    SDL_WM_SetCaption("build1", NULL);
    SDL_SetVideoMode(winwidth,winheight,32, SDL_OPENGL);
     
     glViewport(0,0,winwidth,winheight);
     glShadeModel(GL_SMOOTH);
     glMatrixMode(GL_MODELVIEW);
     glEnable (GL_BLEND); //enable blending
     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     glDisable(GL_DEPTH_TEST);

    My sight area function is working correctly, but I can't seem to get the shading worked out. Use the picture below for reference.

    (unable to post an image "Post denied. New posts are limited by number of URLs it may contain and checked if it doesn't contain forbidden words.") image at: draksplace dot com/openglimage.png

    The blue squares are actually textures, (I just made blue images instead of the actual image to make it easier to see). They would be considered a wall. The black areas are either the floor, (which I turned off for now) or areas that can not be seen. The green and red dots are the corners of each quad. Each dot is accessed by four different quads and carries the rgba value for that corner of the quad. The quads are drawn using this similar code:
    Code :
    1:      glEnable(GL_TEXTURE_2D);
             glBindTexture(GL_TEXTURE_2D, mytexture);
             glBegin(GL_QUADS);
     
     
    2:        glColor4ub(dot[loc].cr , dot[loc].cg , dot[loc].cb , dot[loc].ca);
               glTexCoord2d(my atlas lookup equation x,y);
               glVertex2f(clockwise position of the quad cords from dot[].x, dot[].y);
     
               glColor4ub(dot[loc].cr , dot[loc].cg , dot[loc].cb , dot[loc].ca);
               glTexCoord2d(my atlas lookup equation x,y);
               glVertex2f(clockwise position of the quad cords from dot[].x+ts, dot[].y);
     
               glColor4ub(dot[loc].cr , dot[loc].cg , dot[loc].cb , dot[loc].ca);
               glTexCoord2d(my atlas lookup equation x,y);
               glVertex2f(clockwise position of the quad cords from dot[].x+ts, dot[].y+ts);
     
               glColor4ub(dot[loc].cr , dot[loc].cg , dot[loc].cb , dot[loc].ca);
               glTexCoord2d(my atlas lookup equation x,y);
               glVertex2f(clockwise position of the quad cords from dot[].x, dot[].y+ts);
     
    3:        glEnd();
     
    do 1
    then repeat 2 for all quads on the screen
    then 3
    The green dots have an rgba value of 255,255,255,255. The red dots have a rgba value of 0,0,0,255. My problem is what I have circled in yellow. The shading is different. It looks like everything is shading diagonally. I made the engine generate the dots to tell me the rgba value of the dots for debugging. I've checked my code over and over and I am at a loss. Does anyone have any ideas of what is going on? OpenGL is drawing the lighting correctly because at every green dot that corner of the tile is bright. Where as every red dot that corner of the tile is dark. But the shading looks weird.

    I would appreciate any help, thanks in advance!

  2. #2
    Intern Contributor
    Join Date
    Sep 2013
    Posts
    70
    The problem is, that there are actually no quads anymore in openGL. When you are trying to draw a quad what actually happens is, that the driver will split it up into 2 triangles.
    The quads seem to be split from bottom-left to top-right by your drivers.

  3. #3
    Newbie Newbie
    Join Date
    Jan 2014
    Posts
    2
    Quote Originally Posted by Cornix View Post
    The problem is, that there are actually no quads anymore in openGL. When you are trying to draw a quad what actually happens is, that the driver will split it up into 2 triangles.
    The quads seem to be split from bottom-left to top-right by your drivers.
    Ok, that makes sense. So a quick fix would be to draw all the quads with rotation based on above, below, to the right or left of the player and rotate the texture opposite. Ok, thank you very much.

  4. #4
    Newbie Newbie
    Join Date
    Jan 2014
    Posts
    3
    A better fix - which would not take much longer - would be to not draw GL_QUADs at all anymore, but 2 triangles instead.

    See e.g. stackoverflow dot com/questions/6644099/what-is-so-bad-about-gl-quads

    (Sorry, I cannot post URLs (yet)).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •