java - Vertex Locations are off when using Mesa Core Profile -


i using mesa 10.1.3 able use opengl 3.3 on linux computer. request core profile when create window since core profile has opengl 3.3. when tried write simple program display triangle on screen, got nothing. thought screwed somewhere in code rechecked , correct. test this, tried running program in windows , working should. experimented little bit more code; multiplied vertex location in vertex shader 0.001 , able see triangle, then, not working should. triangle see right angle triangle whereas intended equilateral 1 (in windows see equilateral triangle). guess vertex location somehow different when using opengl core profile, don't quite know how fix this. doing wrong , should doing?

by way, 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: 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 array of vertices i'm creating 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 class holds vector3f position, currently, size set 3 glvertexattribpointer(0, 3, gl_float, false, vertex.size * 4, 0); gldrawarrays(gl_triangles, 0, size); gldisablevertexattribarray(0); } 

and result i'm getting: enter image description here

your glbindattriblocation() call comes late:

glbindattriblocation(program, 0, "position"); gluseprogram(program); 

glbindattriblocation() needs called before gllinkprogram() have effect. can move it, or use glgetattriblocation() after linking location linker assigned attribute. or easier, since use glsl 3.30, can specify location in shader code:

layout(location = 0) in vec3 position; 

when using core profile, need use vertex array objects (vao). if don't have calls glgenvertexarrays() , glbindvertexarray() in code, need those. there should plenty of examples here on or on rest of internet if search "opengl vao" or "opengl vertex array object", won't repeat of it. roughly, have before start initializing vertex state:

gluint vaoid = 0; glgenvertexarrays(1, &vao); glbindvertexarray(vao); // calls glvertexattribpointer, glenablevertexattribarray 

then when ready draw:

glbindvertexarray(vao); 

your vertex data definition looks 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 code filling vbo not shown, data passed glbufferdata() needs flat, contiguous array/buffer of floats, while array of vector object references.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -