First example in OpenGL 4.0.
I started learning GLSL yesterday and followed the first example in the OpenGL 4.0 Shading Language Cookbook for drawing 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 am building and compiling a shader in createAndCompileShader
and the compilation state is successful.
And I draw a triangle in render
.
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 link status is also successful. However, there was nothing in the window. I'm pretty sure the function is render
correct.
Something is wrong?
source to share
Is your triangle visible unless you link GLSL shaders?
- try it
glDisable(GL_CULL_FACE);
- if it helps to reorder glVertex2f calls (different polygonal wrapping)
What video card and driver do you have?
- for OpenGL + GLSL + Windows
- Intel is almost unusable (especially for advanced ones)
- ATI / AMD is generally pretty much ok these days (was much worse with older ATI drivers)
- nVidia usually no problem
You are not applying a projection or model view matrix In a vertex shader
- your simulation is identical, so it doesn't matter.
- but the projection is not
- This means you are passing the non-transformed coordinates to the fragment shader
- OpenGL coordinates are usually in the range
<-1,+1>
- and your untransformed triangle does not cover this range
- so change the coordinates of the triangle to this range like
-
(-0.5,-0.5),(+0.5,-0.5),(0.0,+0.5)
- and try rendering (you can also temporarily revert
//gluOrtho2D(0, 100, 0, 100);
to be sure - If it helps, then this is the reason
- change the vertex shader to include:
gl_Position = ftransform();
- In the case of the main profile, this is no longer an option.
- so you have to pass your transformation matrices using homogeneous variables
- and multiply usually inside the vertex shader
- see example of a simple GLSL engine
- it has texture, normal display, 3 lights, ...
- and see Understanding Homogeneous 4x4 Transformation Matrices
[edit1] you use 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();
}
- which should do the trick ...
source to share