Results 1 to 2 of 2

Thread: Texture position problem

  1. #1
    Newbie Newbie
    Join Date
    Feb 2017
    Posts
    1

    Texture position problem

    Hello,

    I am getting strange results when I get quad texture on screen, if I use code

    below

    Code cpp:
    void GL_Init(){
     
        glClearColor(0.0,0.0,0.0,1.0);
        glViewport(0,0,1200,550);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        //glOrtho(0, 1200, 550, 0.0f, 1.0f, 1.0f);
        //glOrtho(0.0f, 1200.0f, 0.0f, 550.0f, 0.1f, 100.0f);
        //glOrtho(1200, 550, 0, 1.0f, 1.0f, 0.0f); 
        //glOrtho(0,1200,550,0,-1.0f,0);
        //gluOrtho2D(0,1200,0,550);
        //gluPerspective(90,500.0/250.0,1.0,-50.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glEnable(GL_TEXTURE_2D);
        glEnable(GL_BLEND);
       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        //glFrontFace(GL_CW);
     
    }
     
    void texture(){
     
     if(glfwInit()==false){ printf("GLFW could not be inialized.");}//Initializing Open GL FW
          window = glfwCreateWindow(1200,550,"GL_Scope",NULL,NULL);
          glfwSetWindowPos(window,50,100);
          if(!window){printf("GLFW could not create window.");}
          glfwMakeContextCurrent(window);
          glewInit();
          GL_Init();
          coordinate_system=SOIL_load_image("Coordinate.bmp",&width,&height,0,SOIL_LOAD_RGBA);
          glGenTextures(1, &tex_coordinate_system); // allocating a texture name
          glBindTexture(GL_TEXTURE_2D, tex_coordinate_system); // selecting current texture
          glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // selecting modulate to mix texture with color for shading
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_DECAL); 
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_DECAL); 
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); // when texture area is small, bilinear filter the closest mipmap 
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // when texture area is large, bilinear filter the first mipmap
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); // tiling texture
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
          glGenerateMipmap(GL_TEXTURE_2D);
          gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, GL_RGBA, GL_UNSIGNED_BYTE,coordinate_system ); // building texture mipmaps
     
          while(1){
     
     
          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
          glBegin(GL_QUADS);
          glVertex2f(-1.00000f, -1.00000f);
          glTexCoord2f(1.0f, 0.0f); 
          glVertex2f( 0.66666f, -1.00000f);
          glTexCoord2f(1.0f, 1.0f); 
          glVertex2f( 0.66666f, 0.81818f);
          glTexCoord2f(0.0f, 1.0f); 
          glVertex2f(-1.00000f, 0.81818f);
          glTexCoord2f(0.0f, 0.0f);
          glEnd();
          }
    }

    I am getting an image(1000/500) that almost covers all the field on the window(1200/550) and it starts bottom left and goes all the way positive x,y axis towards right and top. But when I change my vertex and coordinates to below

    Code cpp:
            glTexCoord2i(0, 550);
            //glColor3f(1.0,1.0,1.0);
            glVertex2i(0,500); // Top Left Vertex
            glTexCoord2i(1200, 550);
            //glColor3f(1.0,1.0,1.0); 
            glVertex2i(1000,500); // Top Right Vertex
            glTexCoord2i(1200, 0);
            //glColor3f(1.0,1.0,1.0); 
            glVertex2i(1000,0); // Bottom Right Vertex
            glTexCoord2i(0, 0);
            //glColor3f(1.0,1.0,1.0); glVertex2i(0,0); // Bottom Left Vertex

    I am getting an image starting from (0,0) (exact middle of screen) and goes along the positive x axis and positive y axis and stops on where y is 500 and x is 1000 but size gets 1/4 of screen height and width. I mean image gets smaller but is able to fit in positive x,y axis of screen, does not exceed screen borders even gets short.
    when I am using second code, I expect to see that my image will start exactly bottom left and go all the way top right and top and will not change its size.
    why when I use x and y coordinate which matches to screen height and width, my image gets smaller and changes its starting point on screen.
    what am I doing wrong?

    thank you
    regards

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,655
    Quote Originally Posted by bariswinston@gmail.com View Post
    why when I use x and y coordinate which matches to screen height and width, my image gets smaller and changes its starting point on screen.
    what am I doing wrong?
    You've set an identify transform for your MODELVIEW and PROJECTION transforms, so the results you get are exactly what I would expect.

    You need to pick up a copy of the OpenGL Programming Guide and read the "Viewing" chapter. What you need to learn is how modeling, viewing, and projection transforms work, as well as what object-space, world-space, eye-space, clip-space, and NDC-space are.

    I'll give you a hint: When you set up an identity transform for both MODELVIEW and PROJECTION, the vertex positions you're providing are basically being treated as clip-space positions. Assuming their w coordinate is 1, then they're NDC-space positions as well. Take a look at the definition of NDC space. Now compare to what you're seeing.

    Until your copy of the book comes in, check out these links:
    * OpenGL Transformation (Song Ho Ahn)
    * OpenGL Projection Matrix (Song Ho Ahn)

Similar Threads

  1. Problem with light position
    By TheBraXi in forum OpenGL: GLSL
    Replies: 0
    Last Post: 12-08-2012, 08:48 AM
  2. Viewpoint position problem !
    By imported_dirhem in forum OpenGL: Basic Coding
    Replies: 8
    Last Post: 02-18-2011, 03:02 PM
  3. Deferred light position problem
    By Landsdown in forum OpenGL: GLSL
    Replies: 4
    Last Post: 03-03-2010, 06:18 PM
  4. Reconstructing position from Z buffer problem
    By welford in forum OpenGL: GLSL
    Replies: 1
    Last Post: 06-18-2009, 12:03 AM
  5. light position problem
    By lerwen in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-23-2006, 01:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean