3

I started to learn GLSL yesterday and followed the first example in OpenGL 4.0 Shading Language Cookbook to draw a triangle step by step.

Here are my 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 to link shader program"); } else{ printf("OK\n"); glUseProgram(programHandle); } glutMainLoop(); return EXIT_SUCCESS; } 

I create and compile the shader in createAndCompileShader and the status of the compilation is success.

And I draw a 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 is also success. However, there's nothing drawn in the window. I'm sure the function render is right.

Is there something wrong?

6
  • Hi, Spektre. The triangle is visible when GLSL shaders are not binded. And I use nVidia. The vertex shader is just same as the one in the book. Commented Jul 14, 2015 at 8:33
  • try the new coordinates I provided with CULL_FACE off and binded GLSL Commented Jul 14, 2015 at 8:42
  • You cannot use custom shader attributes together with fixed function draw code. You either have to modify the shader to fit to you're drawing, or draw by using glDraw* commands. Commented Jul 14, 2015 at 8:43
  • or use gl_Position = ftransform(); inside your vertex shader to incorporate the transform matrices... (sorry I code in core profile so did not strike mi it at first you are missing that line) Commented Jul 14, 2015 at 8:48
  • @BDL or use compatibility profile ... or for nVidia use default layout positions but that is dirty Attribute locations Commented Jul 14, 2015 at 8:50

1 Answer 1

1

Is your triangle visible if you do not bind any GLSL shaders?

  • try glDisable(GL_CULL_FACE);
  • if it helps reorder the glVertex2f calls (different polygon winding)

What graphics card and driver you have?

  • for OpenGL+GLSL+Windows
  • Intel is almost unusable (especially for advanced things)
  • ATI/AMD are usually almost OK these days (was much worst with old ATI drivers)
  • nVidia are usually without any problems

You do not apply any projection or model view matrix In the vertex shader

  • your modelview is identity so it does not matter
  • but projection is not
  • that means you are passing non transformed coordinates to the fragment shader
  • OpenGL coordinates are usually in range <-1,+1>
  • and your untransformed triangle does not cover that range
  • so change the triangle coordinates to that range for example
  • (-0.5,-0.5),(+0.5,-0.5),(0.0,+0.5)
  • and try render (also you can temporarily rem the //gluOrtho2D(0, 100, 0, 100); just to be sure
  • if it helps then that is the reason
  • change your vertex shader to include: gl_Position = ftransform();
  • In case of core profile is that not an option anymore
  • so you should pass your transform matrices via uniform variables
  • and multiply usually inside vertex shader
  • see simple GLSL engine example
  • it have texture,normal maping,3 lights,...
  • and see Understanding homogenous 4x4 transform matrices

[edit1] you are using glVertex instead VAO so you should use compatibility profile

// vertex shader #version 400 compatibility out vec3 Color; void main() { Color = gl_Color.rgb; gl_Position = ftransform(); } 
  • that should do the trick ...
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.