Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGLWidget draws in the bottom left corner[Solved]
Forum Updated to NodeBB v4.3 + New Features

QGLWidget draws in the bottom left corner[Solved]

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yoxter
    wrote on last edited by
    #1

    kind regards i am learning OpenGL with QT but it seems not draw properly

    !https://lh4.googleusercontent.com/-ivR5vY-y-94/UOD8GPbzWxI/AAAAAAAAAfI/-Xgu1RGqCjU/s405/3.png()(image)!

    There is the coords than i am using

    @float positionData[] = {
    -0.8f, -0.8f, 0.0f,
    0.8f, -0.8f, 0.0f,
    0.0f, 0.8f, 0.0f };@

    the full code

    @#include "glwidget.h"
    #include <fstream>
    #include <QDebug>
    #include "shader.hpp"

    const char* loadShaderAsString(const char* file){
    std::ifstream shader_file(file, std::ifstream::in);
    std::string str((std::istreambuf_iterator<char>(shader_file)), std::istreambuf_iterator<char>());

    return str.c_str();
    }

    GLWidget::GLWidget(QWidget *parent) :
    QGLWidget(parent)
    {
    }

    void GLWidget::initializeGL()
    {

    glClearColor(1.0f,0.0,1.0f,0.0f);
    
    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK) { qDebug() << "Error al enalzar extenciones de la GPU"; }
    
    
    
    
    GLuint ProgramHandle = LoadShaders("vertex.vert", "fragme.frag");
    glUseProgram(ProgramHandle);
    

    // Bind index 0 to the shader input variable "VertexPosition"
    glBindAttribLocation(ProgramHandle, 0, "VertexPosition");

    // Bind index 1 to the shader input variable "VertexColor"
    glBindAttribLocation(ProgramHandle, 1, " VertexColor");

    float positionData[] = {
        -0.8f, -0.8f, 0.0f,
         0.8f, -0.8f, 0.0f,
         0.0f,  0.8f, 0.0f };
    
    float colorData[] = {
         -1.0f, 0.0f, 0.0f,
          0.0f, 1.0f, 0.0f,
          0.0f, 0.0f, 1.0f };
    
    
    GLuint vboHandles[2];
    glGenBuffers(2, vboHandles);
    GLuint positionBufferHandle = vboHandles[0];
    GLuint colorBufferHandle = vboHandles[1];
    
    
    glBindBuffer(GL_ARRAY_BUFFER, positionBufferHandle);
    glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), positionData, GL_STATIC_DRAW);
    
    
    glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);
    glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), colorData, GL_STATIC_DRAW);
    
    
    
    glGenVertexArrays(1, &vaoHandle);
    glBindVertexArray(vaoHandle);
    
    
    glEnableVertexAttribArray(0); //vertex position
    glEnableVertexAttribArray(1); //vertex color
    
    // Map index 0 to the position buffer
    glBindBuffer(GL_ARRAY_BUFFER, positionBufferHandle);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte *)NULL);
    
    
    // Map index 1 to the color buffer
    glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);
    
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);
    
    glBindVertexArray(vaoHandle);
    

    }

    void GLWidget::paintGL()
    {

    glClear( GL_COLOR_BUFFER_BIT );
    
    glDrawArrays(GL_TRIANGLES, 0, 3);
    

    }

    void GLWidget::resizeGL(int w, int h)
    {
    }@

    1 Reply Last reply
    0
    • M Offline
      M Offline
      master of disaster
      wrote on last edited by
      #2

      How should it look if it would be drawn properly?

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        ZapB
        wrote on last edited by
        #3

        Where do you call glViewport()? You need to call this to set the viewport to be the same size as your window, so adding glViewPort( 0, 0, w, h) to your resizeGL() function may well help.

        Nokia Certified Qt Specialist
        Interested in hearing about Qt related work

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          yoxter
          wrote on last edited by
          #4

          [quote author="ZapB" date="1357142425"]Where do you call glViewport()? You need to call this to set the viewport to be the same size as your window, so adding glViewPort( 0, 0, w, h) to your resizeGL() function may well help.[/quote]

          Thanks to all. use @glViewport( 0, 0, w, h);@ resolved the issue.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved