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 1 of 1

Thread: Picture is displayed to big on the screen

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2017
    Posts
    3

    Picture is displayed to big on the screen

    Hello together,
    I have a problem with drawing textures in openGL. In order to draw the texture I am using the library pikoPNG.

    Now my problem is that the texture apears to big on the screen, so you can't see the whole texture. It gets even worse when I try to display two textures. For example I want to display the first texture at the left side of the screen and the second one at the right side of the screen. (I am drawing the textures on quods.)

    What happens is follows: The pictures are cutten in halfs, so you can see just the right half of the first picture (left side of the screen) and the left half of the second picture (right at the screen). The rest is propably drawn but you can't see it. I will post some parts of my code here. Posting the whole program would be by far to much code, but if you need more parts of the program please let me know.

    C++-Code:

    Code cpp:
    // The gameEngine, where displaying is happening:
     
    #pragma once
    #include "gameEngine.h"
    #include <iostream>
    #include "imageLoader.h"
     
    GameEngine::GameEngine()
    {
      window = nullptr;
      screenWidth = 512;
      screenHight = 512;
      currentGameState = GameState::PLAY;
      time = 0.0f;
    }
     
    GameEngine::~GameEngine()
    {
     
    }
     
    void GameEngine::run()
    {
      init();
      sprites.push_back(new Sprite());
      sprites.back()->init("textures/CharacterRight_standing.png", -1.0f, -1.0f, 1.0f, 2.0f);
     
      sprites.push_back(new Sprite());
      sprites.back()->init("textures/CharacterRight_standing.png", 0.0f, -1.0f, 1.0f, 2.0f);
     
      gameLoop();
    }
     
    void GameEngine::init()
    {
      SDL_Init(SDL_INIT_EVERYTHING);
      window = SDL_CreateWindow("OpenGL Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screenWidth, screenHight, SDL_WINDOW_OPENGL);
     
     
      SDL_GLContext glContext = SDL_GL_CreateContext(window);
      glewInit();
      SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
      glClearColor(0.0f, 0.75f, 0.0f, 1.0f);
      glViewport(0, 0, screenWidth, screenHight);
      initShaders();
    }
     
    void GameEngine::initShaders()
    {
      colorShader.compileShaders("shaders/colorshader.vert", "shaders/colorshader.frag");
      colorShader.addAttribute("vertexPosition");
      colorShader.addAttribute("vertexColor");
      colorShader.addAttribute("vertexUvCoordinate");
      colorShader.linkShaders();
    }
     
    void GameEngine::gameLoop()
    {
      while(currentGameState != GameState::EXIT) {
        getInput();
        drawGame();
        time += 0.01f;
      }
    }
     
    void GameEngine::getInput()
    {
      SDL_Event evnt;
      while (SDL_PollEvent(&evnt) == 1) {
     
        switch (evnt.type) {
        case SDL_QUIT:
          currentGameState = GameState::EXIT;
          break;
     
        }
      }
    }
     
    void GameEngine::drawGame()
    {
      glClearDepth(1.0);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glActiveTexture(GL_TEXTURE0);
     
      colorShader.use();
     
     
      GLint textureLocation = colorShader.getUniformLocation("mySampler");
      glUniform1i(textureLocation, 0);
     
      //GLint timeLocation = colorShader.getUniformLocation("time");
      //glUniform1f(timeLocation, time);
     
     
      for (int i = 0; i < sprites.size(); i++) {
        sprites[i]->draw();
      }
     
      //glBindTexture(GL_TEXTURE_2D, 0);
     
      colorShader.unuse();
     
      SDL_GL_SwapWindow(window);
    }
     
    //Here is the Image loader
     
    #pragma once
    #include "imageLoader.h"
    #include "decodePNG.h"
    #include "ioManager.h"
    #include <iostream>
     
    GLTexture ImageLoader::loadPNG(std::string filePath)
    {
      GLTexture texture = {};
      std::vector<unsigned char> out;
      unsigned long width, height;
      std::vector<unsigned char> in;
      IOManager::readFileToBuffer(filePath, in);
     
      int error = decodePNG(out, width, height, &(in[0]), in.size());
      if (error != 0) {
        std::cout << "Failed to decode PNG-File!" << error << std::endl;
      }
      glGenTextures(1, &(texture.textureID));
      glBindTexture(GL_TEXTURE_2D, texture.textureID);
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &(out[0]));
      glEnable(GL_TEXTURE_2D);
      glGenerateMipmap(GL_TEXTURE_2D);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
      glBindTexture(GL_TEXTURE_2D, 0);
      texture.width = width;
      texture.height = height;
     
      std::cout << "Texturewidth = " << texture.width << " " << "Texturehight = " << texture.height << std::endl; 
        return texture;
    }
     
    // Sprite class
     
    #pragma once
    #include "sprite.h"
    #include "vertex.h"
    #include <cstddef>
    #include "resourceManager.h"
     
    Sprite::Sprite()
    {
     
    }
     
    void Sprite::init(std::string texturePath, float x, float y, float width, float height)
    {
      this->x = x;
      this->y = y;
      this->width = width;
      this->height = height;
     
      texture = ResourceManager::getTexture(texturePath);
     
      //Vertexbuffer erstellen
      glGenBuffers(1, &vboID);
     
      //Vertexes erstellen
      Vertex vertexData[6];
     
      //initialisieren der Vertexes
      vertexData[0].setPosition(x + width, y + height);
      vertexData[0].setUvCoordinate(1.0f, 1.0f);
      vertexData[1].setPosition(x, y + height);
      vertexData[1].setUvCoordinate(0.0f, 1.0f);
      vertexData[2].setPosition(x, y);
      vertexData[2].setUvCoordinate(0.0f, 0.0f);
     
      vertexData[3].setPosition(x, y);
      vertexData[3].setUvCoordinate(0.0f, 0.0f);
      vertexData[4].setPosition(x + width, y);
      vertexData[4].setUvCoordinate(1.0f, 0.0f);
      vertexData[5].setPosition(x + width, y + height);
      vertexData[5].setUvCoordinate(1.0f, 1.0f);
     
     
      //Initialisieren der Farben
      for (int i = 0; i < 6; i++) {
        vertexData[i].setColor(255, 0, 255, 255);
      } 
     
      vertexData[0].setColor(155, 155, 155, 255);
     
      vertexData[1].setColor(0, 0, 255, 255);
     
      vertexData[4].setColor(0, 255, 0, 255);
     
      vertexData[5].setColor(255, 255, 255, 255);
     
      glBindBuffer(GL_ARRAY_BUFFER, vboID);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
     
    }
     
    Sprite::~Sprite()
    {
      if (vboID != 0) {
        glDeleteBuffers(1, &vboID);
      }
    }
     
    void Sprite::draw() 
    {
      glBindTexture(GL_TEXTURE_2D, texture.textureID);
      glBindBuffer(GL_ARRAY_BUFFER, vboID);
     
      glEnableVertexAttribArray(0);
     
      //position pointer
      glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
     
      //color pointer
      glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, color));
     
      // UV Pointer
      glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, uvCoordinate));
      glDrawArrays(GL_TRIANGLES, 0, 6); 
     
      glDisableVertexAttribArray(0);
     
      glBindBuffer(GL_ARRAY_BUFFER, 0);
    }


    I am sorry that most of the comments are in german, but if you need them in english, please let me know and I will translate them.

    Thanks for the help in Advance!
    Last edited by Dark Photon; 10-14-2017 at 06:34 PM.

Tags for this Thread

Posting Permissions

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