Reputation: 130
I have a simple OpenGL C application that attempts to draw a triangle on screen. I have successfully written similar code in C++ with no problems. When I run the C program it creates the window, but nothing is displayed. The main chunk of my code is below.
void init()
{
GLfloat vertices[] = {
// Positions
0.0f, 0.5f, 0.0f, 0.0f, // Top
0.5f, -0.5f, 0.0f, 0.0f, // Bottom Right
-0.5f, -0.5f, 0.0f, 0.0f // Bottom Left
};
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//glBindBuffer(GL_ARRAY_BUFFER, 0);
GLuint program = loadShader("/shader1.vert",
"/shader1.frag");
glUseProgram(program);
GLuint vPosition = glGetAttribLocation(program, "VPosition");
glEnableVertexAttribArray(vPosition);
glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glBindVertexArray (vao);
}
int main()
{
glfwInit();
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
int w = 800;
int h = 600;
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
//glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "glfw3", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
glViewport(0, 0, WIDTH, HEIGHT);
init();
while(!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays( GL_TRIANGLES, 0, 3 );
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
With the vertex shader
attribute vec4 vPosition;
void main()
{
gl_Position = vPosition;
}
and the fragment shader
void main()
{
gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );
}
And loading the shaders
char *readShader(char *filename)
{
FILE *fp;
char *content = NULL;
int count = 0;
if(filename != NULL)
{
fp = fopen(filename, "rt");
if(fp != NULL){
fseek(fp, 0, SEEK_END);
count = ftell(fp);
rewind(fp);
if (count > 0) {
content = (char *)malloc(sizeof(char) * (count+1));
count = fread(content,sizeof(char),count,fp);
content[count] = '\0';
}
}
fclose(fp);
}
return content;
}
GLuint loadShader(char *vertex_path, char *fragement_path)
{
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
char *vertShaderStr = readShader(vertex_path);
char *fragShaderStr = readShader(fragement_path);
printf("Compiling vertex shader\n");
GLint vertLength = strlen(vertShaderStr);
glShaderSource(vertShader, 1, (const char **)&vertShaderStr, &vertLength);
glCompileShader(vertShader);
//Check vertex shader
GLint vertSuccess;
glGetShaderiv(vertShader, GL_COMPILE_STATUS, &vertSuccess);
if(vertSuccess == GL_FALSE) {
char log[1024];
glGetShaderInfoLog(vertShader, sizeof(log), 0, log);
printf("Vertex shader compile info: \n %s \n", log);
glDeleteShader(vertShader);
return 1;
}
printf("Compiling fragment shader\n");
GLint fragLength = strlen(fragShaderStr);
glShaderSource(fragShader, 1, (const char **)&fragShaderStr, &fragLength);
glCompileShader(fragShader);
//Check fragment shader
GLint fragSuccess;
glGetShaderiv(fragShader, GL_COMPILE_STATUS, &fragSuccess);
if(vertSuccess == GL_FALSE) {
char log[1024];
glGetShaderInfoLog(vertShader, sizeof(log), 0, log);
printf("Fragment shader compile info: \n %s \n", log);
glDeleteShader(fragShader);
return 1;
}
printf("Linking program\n");
GLuint program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glLinkProgram(program);
GLint programSuccess;
glGetProgramiv(program, GL_LINK_STATUS, &programSuccess);
if(programSuccess == GL_FALSE) {
char log[1024];
glGetProgramInfoLog(program, sizeof(log), 0, log);
printf("shader link info: \n %s \n", log);
glDeleteProgram(program);
return 1;
}
//printf("%s", vertShaderStr);
//printf("%s", fragShaderStr);
return program;
}
Upvotes: 1
Views: 214
Reputation: 45352
Here:
GLfloat vertices[] = {
// Positions
0.0f, 0.5f, 0.0f, 0.0f, // Top
0.5f, -0.5f, 0.0f, 0.0f, // Bottom Right
-0.5f, -0.5f, 0.0f, 0.0f // Bottom Left
}
you are specifying 0 as w coordinate for all of your vertices. You really want to put 1 here, otherwise your vertices are indefinitely far away.
Note that usually the input w`coordinates of all your vertices is always 1 and it is a waste of memory and bandwith to explicitely store this in the array. The GL will automatically extend the fourth component of vectors to 1 if it is not specified, so you could simply use
GLfloat vertices[] = {
// Positions
0.0f, 0.5f, 0.0f, // Top
0.5f, -0.5f, 0.0f, // Bottom Right
-0.5f, -0.5f, 0.0f, // Bottom Left
};
// ...
glVertexAttribPointer(vPosition, 3 GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
without changing your attribute vec4
in the shader at all.
What I also find suspicious is that you never load the GL function pointers. I have not pasted what headers you include, but the could will definitely not work on all platforms.
Upvotes: 2