PDA

View Full Version : Nothing appearing on screen



ED_RockStarGuy
07-03-2016, 04:10 AM
I am following the book 'OpenGL SuperBible' and am trying to run one of the first examples of drawing a point to the screen, bit instead of using there lib, im using GLFW. I'm not sure where I am going wrong.

Using GLFW 3.0 and Opengl 4.5

Creating shader


static const GLchar *vertex_shader_source[] = {
"#version 450 core \n",
" \n",
"void main(void){ \n",
"gl_Position = vec4(0.0,0.0,0.5,1.0); \n",
"} \n",
" \n",
};
static const GLchar *fragment_shader_source[] = {
"#version 450 core \n",
"out vec4 color; \n",
"void main(void){ \n",
"color = vec4(0.0,0.8,1.0,1.0); \n",
"} \n",
" \n",
};

vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, vertex_shader_source, NULL);
glCompileShader(vertex_shader);
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, fragment_shader_source, NULL);
glCompileShader(fragment_shader);

program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);

glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
Setting up the VAO


GLuint vertex_array_object;
glGenVertexArrays(1, &vertex_array_object);
glBindVertexArray(vertex_array_object);
Rendering


glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
glUseProgram(program);
glPointSize(40.0f);
glDrawArrays(GL_POINT, 0, 1);
I'm still quite new to game development, so i'm sorry if its something silly i missed

john_connor
07-03-2016, 12:46 PM
it seems to me that you dont setup any buffers for your vertex array
does your program crash ? what doesnt work as intended ?

you can also check if everything went right compiling / linking the shaders


int status;
glGetProgramiv(program, GL_LINK_STATUS, &status);

// successfully linked program
if (status != GL_TRUE)
{
// show link errors
int logsize;
char infolog[1024] = { 0 };
glGetProgramInfoLog(program, 1024, &logsize, infolog);

std::cout << "ERROR: shader linking failed" << std::endl << infolog << std::endl;
}





here is a simple triangle example:
on initialization:


float vertices[] = {
// x y z
0, 0, 0,// 1st point
1, 0, 0,// 2nd point
0, 1, 0,// 3rd point
};

glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);

glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

// describe how the points are structured in your buffer:
// 0 = attribute location
// 3 component count (each point has 3 "components")
// GL_FLOAT = component type
// false = not normalized data
// sizeof(float) * 3 = the stride between 2 points
// (void*)(sizeof(float) * 0) = beginning of that attribute, relative to each point in your buffer
glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(float) * 3, (void*)(sizeof(float) * 0));

glBindBuffer(GL_ARRAY_BUFFER, 0);

glEnableVertexAttribArray(0);

glBindVertexArray(0);


on render (every frame)


glUseProgram(program);

glBindVertexArray(VAO);

unsigned int vertexcount = 3;// you have 3 points in your buffer
glDrawArrays(GL_TRIANGLES, 0, vertexcount);

glBindVertexArray(0);

glUseProgram(0);



on cleanup (application terminated)


glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);


EDIT:

glDrawArrays(GL_POINT, 0, 1);
--> try GL_POINTS instead of GL_POINT

ED_RockStarGuy
07-07-2016, 03:20 AM
The solution was I did not detach the shaders before deleting them, after changing this it rendered fine! :)