c++ - I'm trying to draw a triangle using shaders and VBO. Why my program doesn't work? -
i've tried use code drawing triangle using vbo , shaders.
#include "gl.h" #include <stdlib.h> #include <glfw/glfw3.h> // utility libraries #include "oglshader.h" #include "oglprogram.h" #include "oglcommon.h" #include "shaders.h" #include "oglbuffer.h" using namespace opengl; glfwwindow *window; const int window_width = 800; const int window_height = 600; shader **shaders; program *mainprogram; int verticescount = 3; float triangle[] = { 0.0f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f, }; int indicescount = 3; int indices[] = { 0, 1, 2 }; buffer *verticesbuffer; void init() { glviewport(0, 0, window_width, window_height); glclearcolor(0, 0, 0, 1); shaders = new opengl::shader*[2]; // code creates , compiles shaders, links shader program shaders[0] = new opengl::shader(gl_vertex_shader, opengl::vertexshadersource); shaders[1] = new opengl::shader(gl_fragment_shader, opengl::fragmentshadersource); mainprogram = new opengl::program(2, shaders); mainprogram->use(); // use program vertices_attrib_location = 0; colors_attrib_location = 1; glbindattriblocation(mainprogram->getid(), vertices_attrib_location, "ivertex"); glbindattriblocation(mainprogram->getid(), colors_attrib_location, "icolor"); verticesbuffer = new buffer(gl_array_buffer); verticesbuffer->data(verticescount*3*sizeof(float), triangle, gl_static_draw); } void deinit() { delete verticesbuffer; delete shaders[0]; delete shaders[1]; delete []shaders; delete mainprogram; } void display() { glclear(gl_color_buffer_bit); glvertexattrib3f(colors_attrib_location, 1, 0, 1); verticesbuffer->bind(); glenablevertexattribarray(vertices_attrib_location); glvertexattribpointer(vertices_attrib_location, 3, gl_float, gl_false, 0, nullptr); gldrawarrays(gl_triangles, 0, 3); } void update(double deltatime) { } int main(int argc, char **argv) { if (!glfwinit()) { exit(1); } glfwwindowhint(glfw_samples, 1); glfwwindowhint(glfw_context_version_major, 2); glfwwindowhint(glfw_context_version_minor, 1); window = glfwcreatewindow(window_width, window_height, "test", nullptr, nullptr); if (!window) { glfwterminate(); exit(exit_failure); } glfwmakecontextcurrent(window); glfwswapinterval(1); init(); double lasttime = 0.0; while (!glfwwindowshouldclose(window)) { display(); double curtime = glfwgettime(); update(curtime - lasttime); lasttime = curtime; glfwswapbuffers(window); glfwpollevents(); } deinit(); glfwdestroywindow(window); glfwterminate(); return 0; }
here shaders:
namespace opengl { std::string vertexshadersource = "#version 120\n\ attribute vec3 ivertex;\ attribute vec3 icolor;\ varying vec3 ocolor;\ void main(void)\n\ {\n\ ocolor = icolor;\n\ \n\ gl_position = vec4(ivertex, 1);\n\ }\n\ "; std::string fragmentshadersource = "#version 120\n\ varying vec3 ocolor;\ varying vec2 otexcoord;\n\ void main(void)\n\ {\n\ gl_fragcolor = vec4(ocolor, 1);\n\ }\n\ "; }
i tried run code both on intel , ati videocards, see on screen empty black window. i'm doing wrong?
Comments
Post a Comment