I am new to OpenGL and i want to draw a simple triangle on screen with my shader. I set up buffers and wrote a shaders but it doesn't seem to work. It gives white screen, i tried to change glCreateBuffer with glGenBuffer and screen turned to black screen. I want to create&compile shaders in one function called createAndCompileShader(...), buffers are defined in main function.
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <string>
#include <iostream>
static unsigned int createAndCompileShader(const char* vertexShader, const char* fragmentShader) {
unsigned int fragmentShaderr = glCreateShader(GL_FRAGMENT_SHADER);
unsigned int vertexShaderr = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShaderr, 1, &vertexShader, NULL);
glShaderSource(fragmentShaderr, 1, &fragmentShader, NULL);
glCompileShader(vertexShaderr);
glCompileShader(fragmentShaderr);
unsigned int program = glCreateProgram();
glAttachShader(program, vertexShaderr);
glAttachShader(program, fragmentShaderr);
glLinkProgram(program);
return program;
}
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
float vertexPositions[6] = {
-0.5, -0.5,
0.0, 0.5,
0.5, -0.5
};
const char* vertexShader = "#version 330 core\n"
"layout(location = 0) in vec4 position;\n"
"void main()\n"
"{\n"
"gl_Position = position;\n"
"}\n";
const char* fragementShader = "#version 330 core\n"
"layout(location = 0) in vec4 position;\n"
"void main()\n"
"{\n"
"color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";
unsigned int program = createAndCompileShader(vertexShader, fragementShader);
glUseProgram(program);
unsigned int buffer;
glGenBuffers(1, &buffer);
//glCreateBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), vertexPositions, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*2 , 0);
//std::string vertexShader;
//std::string fragmentShader;
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0 ,3);
/*glBegin(GL_TRIANGLES);
glVertex2f(-0.5, -0.5);
glVertex2f( 0.0, 0.5);
glVertex2f( 0.5, -0.5);
glEnd();*/
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glDeleteProgram(program);
glfwTerminate();
return 0;
}
createAndCompileShader