QGLWidget draws in the bottom left corner[Solved]
-
kind regards i am learning OpenGL with QT but it seems not draw properly
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)
{
}@ -
How should it look if it would be drawn properly?
-
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 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.