OpenGL: No triangle, only background (shader problem?)

Hello. I recently tried to execute a simple OGL code that should generate a red triangle over a green/blue background. The issue is that only the background is being rendered, no triangle, so I believe that the problem is shader related. I re read the code several times and was incapable of seeing anything unusual.
I focused my attention on things the filename of both shader files, the filename inside the .cpp file (and see they match with the actual filenames) and if the second argument name of glGetAttribLocation is equal to the name of the attribute inside de vertexshader file

I found some names that were not identical, correct everything I found and yet no triangle. Bellow the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include "GL/glew.h"
#include "GL/glfw.h"

using namespace std;

GLuint loadShaders (const char* vertex_file_path, const char* fragment_file_path){
   cout << "loadShaders" << endl;
	string vs_instructions;
   cout << "vertex shader filename: " << vertex_file_path << endl;
   cout << "fragment_file_path filename: " << fragment_file_path << endl;
	ifstream fm (vertex_file_path);
   if (fm.is_open()) {
      std::string Line = "";
		while (fm.good()) {
         getline (fm, Line);
			vs_instructions += "\n" + Line;
		}
	}
	else {
		cout << "Could not open the file" << endl;
		return 0;
	}
	fm.close ();

	string fs_instructions;
	fm.open (fragment_file_path);
	if (fm.is_open()) {
		std::string Line = "";
		while (getline(fm, Line))
			fs_instructions += "\n" + Line;
	}
	else {
		cout << "Could not open the file" << endl;
		return 0;
	}

	GLint result = GL_FALSE;
	int log_length;

	GLuint fs_id = glCreateShader(GL_FRAGMENT_SHADER);
   GLuint vs_id = glCreateShader(GL_VERTEX_SHADER);

	char const * vs_ptr = vs_instructions.c_str();
	glShaderSource (vs_id, 1, &vs_ptr, NULL);
	glCompileShader(vs_id);

	glGetShaderiv (vs_id, GL_COMPILE_STATUS, &result);
	glGetShaderiv (vs_id, GL_INFO_LOG_LENGTH, &log_length);
	if (log_length > 0){
		vector<char> vs_er_ms (log_length+1);
		glGetShaderInfoLog(vs_id, log_length, NULL, &vs_er_ms[0]);
		cout << vs_er_ms[0] << endl;;
	}

	char const * fs_ptr = fs_instructions.c_str();
	glShaderSource (fs_id, 1, &fs_ptr, NULL);
	glCompileShader(fs_id);

	glGetShaderiv(fs_id, GL_COMPILE_STATUS, &result);
	glGetShaderiv(fs_id, GL_INFO_LOG_LENGTH, &log_length);
	if (log_length > 0){
		std::vector<char> fs_er_ms (log_length+1);
		glGetShaderInfoLog(fs_id, log_length, NULL, &fs_er_ms[0]);
		cout << &fs_er_ms[0] << endl;
	}

	GLuint program_id = glCreateProgram();
	glAttachShader (program_id, vs_id);
	glAttachShader (program_id, fs_id);
	glLinkProgram  (program_id);


	glGetProgramiv (program_id, GL_LINK_STATUS, &result);
	glGetProgramiv (program_id, GL_INFO_LOG_LENGTH, &log_length);
	if (log_length > 0){
		std::vector<char> pg_er_ms(log_length+1);
		glGetProgramInfoLog(program_id, log_length, NULL, &pg_er_ms[0]);
		cout << pg_er_ms[0] << endl;
	}

	glDetachShader(program_id, vs_id);
	glDetachShader(program_id, fs_id);

	glDeleteShader(vs_id);
	glDeleteShader(fs_id);

	return program_id;
}

void display (GLuint matrix_id, GLuint programID, GLuint vertex_pos_modelspace_id, GLuint vertex_buffer/*GLfloat * model_view_projection_mat_row1_x*/) {
   glClear (GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
   glUseProgram (programID);
   glEnableVertexAttribArray (vertex_pos_modelspace_id);
   glBindBuffer (GL_ARRAY_BUFFER, vertex_buffer);
   glVertexAttribPointer(
   vertex_pos_modelspace_id, // The attribute we want to configure
      3,                  // size
      GL_FLOAT,           // type
      GL_FALSE,           // normalized?
      0,                  // stride
      (void*)0            // array buffer offset
   );
	glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle
   glDisableVertexAttribArray(vertex_pos_modelspace_id);
   glfwSwapBuffers();
}

int main (int argc, char* argv[]) {
   bool running=false;
   /*if (!GLEW_VERSION_2_0) {
      cout << "glew not available. closing..." << endl;
      exit (-1);
   }*/
   glfwInit ();
   glEnable (GL_DEPTH_TEST);
   if (!glfwOpenWindow (1024,768,8,8,8,0,8,0,GLFW_WINDOW)) {
      glfwTerminate();
      exit (1);
   }
   glewInit ();
   glfwSetWindowTitle ("Test window:");
   glClearColor (0.0f,0.66f,1.0f,1.0f);

   GLuint programID = loadShaders ("/media/34GB/demos/Ogl/SimpleVertexShader.vs","/media/34GB/demos/Ogl/SimpleFragmentShader.fs");

   GLuint vertex_pos_modelspace_id = glGetAttribLocation(programID, "vertexPosition_modelspace");

   static const GLfloat g_vertex_buffer_data[] = {
		-1.0f, -1.0f, 0.0f,
		 1.0f, -1.0f, 0.0f,
		 0.0f,  1.0f, 0.0f,
   };
   //static const GLushort g_element_nuffer_data [] = {0, 1, 2};

   GLuint vertex_buffer;
   glGenBuffers (1, &vertex_buffer);
   glBindBuffer (GL_ARRAY_BUFFER, vertex_buffer);
   glBufferData (GL_ARRAY_BUFFER, sizeof (g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

   do {
      //display (scene,program_id,matrix_id,vertex_pos_modelspace_id,vertex_buffer,&model_view_projection_mat.row1.x);
      display (programID,0,vertex_pos_modelspace_id,vertex_buffer);
      running = glfwGetWindowParam (GLFW_OPENED);
   } while (running);

   glDeleteBuffers(1, &vertex_buffer);
	glDeleteProgram(programID);
	glfwTerminate();
}


The shader code:
1
2
3
4
5
6
7
8
9
#version 120

void main()
{

	// Output color = red 
	gl_FragColor = vec4(1,0,0,1);

}


1
2
3
4
5
6
7
8
9
10
11
#version 120

attribute vec3 vertexPosition_modelspace;

//uniform mat4 MVP;

void main(){

	gl_Position = vec4(vertex_position_modelspace, 1.0);

}


This shall be a self contained example (as long as there is glfw.h and glew.h on a GL dir inside a system wide dir) if someone wants to try it.
May I ask why you're using GLFW 2? GLFW 3 is more up to date.
But regardless I do commend you for producing a complete example, I know it can be frustrating to do that.

My headers are set up a bit different than you. I use GLAD instead of GLEW, and my GLFW header is #include <GLFW/glfw3.h>

I'll try to modify my code a bit to work with yours.

But for now, try the following,
get the pointer that is returned from glfwOpenWindow.
and call
glfwMakeContextCurrent(window); before loading GLEW.


Nevermind that might only apply to GLFW 3.
__________________________

1.
Don't call gl* anything until after you've initialized GLEW. (It's possible that OpenGL 1.0/1.1 functions might work without GLEW, but you might be playing with fire)
i.e. move "glEnable" to after glewInit.

2.
Call glGetError() when in doubt
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glGetError.xhtml

I get OpenGL Error 1282 (Invalid Operation) at the start, and OpenGL Error 1281 (Invalid Value) each iteration of the display loop.
Narrow down the issue until you find the exact GL call that generates the error.
Last edited on
Your vertex shader:
1
2
3
attribute vec3 vertexPosition_modelspace;
// ...
gl_Position = vec4(vertex_position_modelspace, 1.0);


Btw, I could be wrong, but I'm not sure if you're checking for compile errors correctly. I don't think that shader should have compiled?

In my code, I do:
1
2
3
4
    GLint status;
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
    if (status != GL_TRUE)
        std::cout << "Error compiling vertex shader\n";


You can also then do all that stuff with making the buffers to store the error messages.
But at the very least, you will know the shader didn't compile.
Last edited on
Your vertex shader:

1
2
3
attribute vec3 vertexPosition_modelspace;
// ...
gl_Position = vec4(vertex_position_modelspace, 1.0);




Unfortunately I had already fixed it and just copied the old code here.

The new version:

1
2
3
4
5
6
7
8
9
10
11
#version 120

attribute vec3 vertex_position_modelspace;

//uniform mat4 MVP;

void main(){

	gl_Position = vec4(vertex_position_modelspace, 1.0);

}


Btw, I could be wrong, but I'm not sure if you're checking for compile errors correctly. I don't think that shader should have compiled?


Why not? Some problem with:
1
2
3
4
5
6
7
glGetShaderiv (vs_id, GL_COMPILE_STATUS, &result);
	glGetShaderiv (vs_id, GL_INFO_LOG_LENGTH, &log_length);
	if (log_length > 0){
		vector<char> vs_er_ms (log_length+1);
		glGetShaderInfoLog(vs_id, log_length, NULL, &vs_er_ms[0]);
		cout << vs_er_ms[0] << endl;;
	}


About the glfw3 stuff, it does not run on my primary OS
I can take a closer look later, but when I ran your program with the incorrect shader, it did not print any compilation error for me. Perhaps you should test this by feeding it a deliberately bad shader to see if it prints an error; I could be mistaken.

Either way, what I said about checking for errors still applies. Put a bunch of glGetError() calls in your code and try to figure out exactly which GL call is causing an error, if any.
Topic archived. No new replies allowed.