0

I haven't seen OpenGl for few years, and now i'm trying to code anything in new style, but I have problems to draw simple triangle. First of all i can't find any tutorial with good examples and without use of 'supporting libraries', but that's not the point, code below should (as i think) draw red triangle, but instead of this it's drawing white triangle, what am I doing wrong ?

dpy = XOpenDisplay( NULL );
glxWin = generateXWindow(dpy);

glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glDepthFunc(GL_LESS);

float points[] = {
     0.0f,  0.5f,  0.0f,
     0.5f, -0.5f,  0.0f,
    -0.5f, -0.5f,  0.0f
};

unsigned int vbo = 0;
glGenBuffers (1, &vbo);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), points, GL_STATIC_DRAW);

unsigned int vao = 0;
glGenVertexArrays (1, &vao);
glBindVertexArray (vao);
glEnableVertexAttribArray (0);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);

const char* fragmet_shader = "out vec4 frag_colour; void main() { frag_colour = vec4 (0.7, 0.0, 0.7, 1.0); }";
const char* vertex_shader  = "in vec3 vp; void main() { gl_Position = vec4 (vp, 1.0); }";

unsigned int vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource( vs, 1, &vertex_shader, NULL);
glCompileShader(vs);
unsigned int fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource( fs, 1, &fragmet_shader, NULL );
glCompileShader(fs);

unsigned int shader_program = glCreateProgram();
glAttachShader(shader_program, fs);
glAttachShader(shader_program, vs);
glLinkProgram(shader_program);

glVertexPointer(3, GL_FLOAT, 0, NULL);
glEnableClientState(GL_VERTEX_ARRAY);

glClearColor( 0.2, 0.2, 0.2, 1.0 );
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram (shader_program);
glBindVertexArray (vao);
glDrawArrays (GL_TRIANGLES, 0, 3);

glFlush();
glXSwapBuffers( dpy, glxWin );
mucka
  • 1,159
  • 2
  • 20
  • 28

2 Answers2

0

One thing that comes to my mind: you are using the more modern shader syntax with in and out but are not declaring any shader version, so it will be back at default 1.10 which does not support that syntax. Use a preprocessor directive like #version 130 as the very first line of your shader sources (don't forget the newline character here, it is important for preprocessor directives).

You should also check for GL errors and especially the compile and link status for your shaders/programs and query the info log.

derhass
  • 38,787
  • 2
  • 42
  • 61
0

I hereby just refer you to my example program that does all "advanced" stuff in one: FBConfig instead of visual, shaders and glXCreateContextAttrib to obtain a OpenGL-3 context (and falls back to legacy OpenGL if that's not available).

https://github.com/datenwolf/codesamples/blob/master/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c

datenwolf
  • 149,702
  • 12
  • 167
  • 273