使用Mesa Core Profile时,顶点位置关闭 [英] Vertex Locations are off when using Mesa Core Profile

查看:241
本文介绍了使用Mesa Core Profile时,顶点位置关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Mesa 10.1.3在我的Linux计算机上使用OpenGL 3.3。我创建窗口时请求一个核心配置文件,因为只有核心配置文件具有OpenGL 3.3。但是当我试图编写一个简单的程序在屏幕上显示一个三角形时,我什么都没有。所以我认为我在代码中的某处搞砸了,但我重新检查了它,它是正确的。为了测试这个,我尝试在Windows中运行该程序,并且它正在按照应有的方式工作。所以我对代码做了更多的尝试;我将顶点着色器中的顶点位置乘以0.001,然后才能看到我的三角形,但即使如此,它仍然不能正常工作。我看到的三角形是一个直角三角形,而我希望它是一个等边三角形(在Windows中我看到一个等边三角形)。所以我的猜测是在使用OpenGL Core profile时顶点位置有点不同,但我不知道如何解决这个问题。我在做什么错了,我应该做什么?



顺便说一下,这是我的顶点着色器看起来像:

  #version 330 

在vec3的位置;

void main()
{
gl_Position = vec4(0.001 * position,1.0);

片段着色器:

  #version 330 

out vec4 fragColor;

void main()
{
fragColor = vec4(0.0,1.0,1.0,1.0);

着色器类:

  public Shader()
{
program = glCreateProgram();
if(program == 0)
{
System.err.println(Shader creation failed:Could not find valid memory location);
System.exit(1);



public void bind()
{
glBindAttribLocation(program,0,position);
glUseProgram(program);
}

public void addVertexShader(String text)
{
addProgram(text,GL_VERTEX_SHADER);


public void addFragmentShader(String text)
{
addProgram(text,GL_FRAGMENT_SHADER);


public void addGeometryShader(String text)
{
addProgram(text,GL_GEOMETRY_SHADER);
}

public void compile()
{
glLinkProgram(program);

if(glGetProgrami(program,GL_LINK_STATUS)== 0)
{
System.err.println(glGetProgramInfoLog(program,1024));
System.exit(1);
}

glValidateProgram(program);

if(glGetProgrami(program,GL_VALIDATE_STATUS)== 0)
{
System.err.println(glGetProgramInfoLog(program,1024));
System.exit(1);



public void addProgram(String text,int type)
{
int shader = glCreateShader(type);

if(shader == 0)
{
System.err.println(Shader creation failed);
System.exit(1);
}

glShaderSource(shader,text);
glCompileShader(着色器);

if(glGetShaderi(shader,GL_COMPILE_STATUS)== 0)
{
System.err.println(glGetShaderInfoLog(shader,1024));
System.exit(1);
}

glAttachShader(program,shader);


$ / code>

我创建VBO的顶点数组有:

  Vertex [] data = new Vertex [] {
new Vertex(new Vector3f(-0.1f, -0.1f,0)),
new Vertex(new Vector3f(0,1,0)),
new Vertex(new Vector3f(1,-1,0))};

我的绘制方法:

  public void draw()
{
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
// Vertex是一个类,它只保存一个Vector3f的位置,当前,所以size被设置为3
glVertexAttribPointer(0,3,GL_FLOAT,false,Vertex.SIZE * 4,0);
glDrawArrays(GL_TRIANGLES,0,size);
glDisableVertexAttribArray(0);
}

这就是我得到的结果:
< img src =https://i.stack.imgur.com/g29Wv.pngalt =在这里输入图片描述>

解决方案
$ b

   

glBindAttribLocation(程序,0,位置);
glUseProgram(program);

需要调用glBindAttribLocation() 之前 glLinkProgram()有任何作用。您可以移动它,或在链接后使用 glGetAttribLocation()来获取链接器分配给该属性的位置。或者更容易,因为您使用GLSL 3.30,您可以在着色器代码中指定位置:

  layout(location = 0)在vec3的位置; 

使用Core Profile时,您还需要使用顶点数组对象(VAO)。如果您的代码中没有像 glGenVertexArrays() glBindVertexArray()这样的调用,则需要这些调用。如果你搜索OpenGL VAO或OpenGL顶点数组对象,那么在SO或互联网的其他部分应该有大量的例子,所以我不会重复太多。粗略地说,在你开始初始化你的顶点状态之前,你会有这样的东西:

  GLuint vaoId = 0; 
glGenVertexArrays(1,& vao);
glBindVertexArray(vao);
//调用glVertexAttribPointer,glEnableVertexAttribArray

然后当您准备绘制时:

  glBindVertexArray(vao); 

您的顶点数据定义看起来也可能是一个麻烦来源:

  Vertex [] data = new Vertex [] {
new Vertex(new Vector3f(-0.1f,-0.1f,0)),
new Vertex(new Vector3f(0,1,0)),
new Vertex(new Vector3f(1,-1,0))};

虽然没有显示填入VBO的代码,但是将数据传递给 glBufferData()需要是一个平坦的,连续的浮点数组/缓冲区,而这是一个矢量对象引用数组。


I am using Mesa 10.1.3 to be able to use OpenGL 3.3 on my Linux computer. I request a core profile when I create the window since only the core profile has OpenGL 3.3. But when I tried to write a simple program to display a triangle on the screen, I got nothing. So I thought I screwed up somewhere in the code but I rechecked it and it was correct. To test this, I tried running the program in Windows and it was working as it should. So I experimented a little bit more with the code; I multiplied the vertex location in the vertex shader by 0.001 and only then I was able to see my triangle, but even then, it was not working as it should. The triangle I see was a right angle triangle whereas I intended it to be an equilateral one (in Windows I see an equilateral triangle). So my guess is vertex location is somehow different when using OpenGL Core profile, but I don't quite know how to fix this. What am I doing wrong and what should I be doing?

By the way, this is my vertex shader looks like:

#version 330

in vec3 position;

void main()
{
  gl_Position = vec4(0.001*position, 1.0);
}

Fragment shader:

#version 330

out vec4 fragColor;

void main()
{
  fragColor = vec4(0.0, 1.0, 1.0, 1.0);
}

Shader class:

public Shader()
    {
    program = glCreateProgram();
    if(program == 0)
    {
        System.err.println("Shader creation failed: Could not find valid memory location");
        System.exit(1);
    }
    }

    public void bind()
    {
    glBindAttribLocation(program, 0, "position");
    glUseProgram(program);
    }

    public void addVertexShader(String text)
    {
    addProgram(text, GL_VERTEX_SHADER);
    }

    public void addFragmentShader(String text)
    {
    addProgram(text, GL_FRAGMENT_SHADER);
    }

    public void addGeometryShader(String text)
    {
    addProgram(text, GL_GEOMETRY_SHADER);
    }

    public void compile()
    {
    glLinkProgram(program);

    if(glGetProgrami(program, GL_LINK_STATUS) == 0)
    {
        System.err.println(glGetProgramInfoLog(program, 1024));
        System.exit(1);
    }

    glValidateProgram(program);

    if(glGetProgrami(program, GL_VALIDATE_STATUS) == 0)
    {
        System.err.println(glGetProgramInfoLog(program, 1024));
        System.exit(1);
    }
    }

    public void addProgram(String text, int type)
    {
    int shader = glCreateShader(type);

    if(shader == 0)
    {
        System.err.println("Shader creation failed");
        System.exit(1);
    }

    glShaderSource(shader, text);
    glCompileShader(shader);

    if(glGetShaderi(shader, GL_COMPILE_STATUS) == 0)
    {
        System.err.println(glGetShaderInfoLog(shader, 1024));
        System.exit(1);
    }

    glAttachShader(program, shader);

    }

And my array of vertices which I'm creating the VBO with:

Vertex[] data = new Vertex[] {            
new Vertex(new Vector3f(-0.1f, -0.1f, 0)),
new Vertex(new Vector3f(0,  1, 0)),
new Vertex(new Vector3f( 1,  -1, 0))};

My draw method:

public void draw()
{
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
//Vertex is a class which only holds a Vector3f for position, currently, so size is set to 3
glVertexAttribPointer(0, 3, GL_FLOAT, false, Vertex.SIZE * 4, 0);
glDrawArrays(GL_TRIANGLES, 0, size);
glDisableVertexAttribArray(0);
}

And this is the result that I'm getting:

解决方案

Your glBindAttribLocation() call comes too late:

glBindAttribLocation(program, 0, "position");
glUseProgram(program);

glBindAttribLocation() needs to be called before the glLinkProgram() to have any effect. You can move it, or use glGetAttribLocation() after linking to get the location that the linker assigned to the attribute. Or even easier, since you use GLSL 3.30, you can specify the location in the shader code:

layout(location = 0) in vec3 position;

When using the Core Profile, you will also need to use Vertex Array Objects (VAO). If you don't have calls like glGenVertexArrays() and glBindVertexArray() in your code, you will need those. There should be plenty of examples here on SO or on the rest of the internet if you search for "OpenGL VAO" or "OpenGL Vertex Array Object", so I won't repeat too much of it. Roughly, you will have something like this before you start initializing your vertex state:

GLuint vaoId = 0;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// calls to glVertexAttribPointer, glEnableVertexAttribArray

Then when you get ready to draw:

glBindVertexArray(vao);

Your vertex data definition also looks like it could be a source of trouble:

Vertex[] data = new Vertex[] {            
new Vertex(new Vector3f(-0.1f, -0.1f, 0)),
new Vertex(new Vector3f(0,  1, 0)),
new Vertex(new Vector3f( 1,  -1, 0))};

While the code for filling this into the VBO is not shown, the data passed to glBufferData() needs to be a flat, contiguous array/buffer of floats, while this is an array of vector object references.

这篇关于使用Mesa Core Profile时,顶点位置关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆