c++ - First example in OpenGL 4.0 Shading Language Cookbook -


i started learn glsl yesterday , followed first example in opengl 4.0 shading language cookbook draw triangle step step.

here codes:

1.shader.vert

#version 400  in vec3 vertexposition; in vec3 vertexcolor;  out vec3 color;  void main() {     color = vertexcolor;     gl_position = vec4(vertexposition, 1.0); } 

2.shader.frag

#version 400  in vec3 color; out vec4 fragcolor;  void main(){     fragcolor = vec4(color, 1.0); } 

3.main.cpp

int main(int argc, char *argv[]) {     glutinit(&argc, argv);     glutinitdisplaymode(glut_rgba | glut_double);     glutinitwindowsize(500, 500);     glutcreatewindow("project1");      glutdisplayfunc(render);      glmatrixmode(gl_projection);     glloadidentity();     gluortho2d(0, 100, 0, 100);     glmatrixmode(gl_modelview);     glloadidentity();     glenable(gl_point_smooth);      // init glew     glenum err = glewinit();     if (glew_ok != err){         printf("error: %s\n", glewgeterrorstring(err));     }     else{         printf("ok: glew init.\n");     }      // check gl version     const glubyte *renderer = glgetstring(gl_renderer);     const glubyte *vendor = glgetstring(gl_vendor);     const glubyte *version = glgetstring(gl_version);     const glubyte *glslversion = glgetstring(gl_shading_language_version);     glint major, minor;     glgetintegerv(gl_major_version, &major);     glgetintegerv(gl_minor_version, &minor);     printf("gl vendor   : %s\n", vendor);     printf("gl renderer : %s\n", renderer);     printf("gl version (string) : %s\n", version);     printf("gl version (integer): %d.%d\n", major, minor);     printf("glsl version: %s\n", glslversion);      // vertex shader     gluint vertshader = createandcompileshader("shader.vert", vertex);      // fragment shader     gluint fragshader = createandcompileshader("shader.frag", fragment);      // program     gluint programhandle = glcreateprogram();     if (programhandle == 0)     {         printf("error creating program object.\n");     }      glattachshader(programhandle, vertshader);     glattachshader(programhandle, fragshader);      gllinkprogram(programhandle);      glint status;     glgetprogramiv(programhandle, gl_link_status, &status);     if (gl_false == status){         printf("failed link shader program");     }     else{         printf("ok\n");         gluseprogram(programhandle);     }      glutmainloop();       return exit_success; } 

i create , compile shader in createandcompileshader , status of compilation success.

and draw triangle in render function.

void render() {     glloadidentity();     glclear(gl_color_buffer_bit | gl_depth_buffer_bit);     glpushmatrix();      glbegin(gl_triangles);      glcolor3f(1.0, 0.0, 0.0);     glvertex2f(20, 20);     glcolor3f(0.0, 1.0, 0.0);     glvertex2f(80, 20);     glcolor3f(0.0, 0.0, 1.0);     glvertex2f(50, 80);      glend();      glpopmatrix();      glutswapbuffers(); } 

the status of link success. however, there's nothing drawn in window. i'm sure function render right.

is there wrong?

is triangle visible if not bind glsl shaders?

  • try gldisable(gl_cull_face);
  • if helps reorder glvertex2f calls (different polygon winding)

what graphics card , driver have?

  • for opengl+glsl+windows
  • intel unusable (especially advanced things)
  • ati/amd ok these days (was worst old ati drivers)
  • nvidia without problems

you not apply projection or model view matrix in vertex shader

  • your modelview identity not matter
  • but projection not
  • that means passing non transformed coordinates fragment shader
  • opengl coordinates in range <-1,+1>
  • and untransformed triangle not cover range
  • so change triangle coordinates range example
  • (-0.5,-0.5),(+0.5,-0.5),(0.0,+0.5)
  • and try render (also can temporarily rem //gluortho2d(0, 100, 0, 100); sure
  • if helps reason
  • change vertex shader include: gl_position = ftransform();
  • in case of core profile not option anymore
  • so should pass transform matrices via uniform variables
  • and multiply inside vertex shader
  • see simple glsl engine example
  • it have texture,normal maping,3 lights,...
  • and see understanding homogenous 4x4 transform matrices

[edit1] using glvertex instead vao should use compatibility profile

// vertex shader #version 400 compatibility  out vec3 color;  void main()     {     color = gl_color.rgb;     gl_position = ftransform();     } 
  • that should trick ...

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -