c++ - Opengl 3D cube won't render correctly on Windows -
i following tutorial on udemy - modern opengl 3.0. have managed basic tutorial part 5 "projections , coordinate systems". works on mac, same code, can't work on windows. restarted tutorial make sure didn't miss , previous tutorials worked, same result. won't render correctly. did on visual studio 2015 on windows. on mac got work using vim , makefile... think easier ide compiles you, apparently not.
this displayed on screen, flashes fast , moves lot, instead of rotating cube should getting.
here code:
shader.h:
#ifndef shader_h #define shader_h #include <string> #include <fstream> #include <sstream> #include <iostream> #include <gl/glew.h> class shader { public: gluint program; // constructor generates shader on fly shader(const glchar *vertexpath, const glchar *fragmentpath) { // 1. retrieve vertex/fragment source code filepath std::string vertexcode; std::string fragmentcode; std::ifstream vshaderfile; std::ifstream fshaderfile; // ensures ifstream objects can throw exceptions: vshaderfile.exceptions(std::ifstream::badbit); fshaderfile.exceptions(std::ifstream::badbit); try { // open files vshaderfile.open(vertexpath); fshaderfile.open(fragmentpath); std::stringstream vshaderstream, fshaderstream; // read file's buffer contents streams vshaderstream << vshaderfile.rdbuf(); fshaderstream << fshaderfile.rdbuf(); // close file handlers vshaderfile.close(); fshaderfile.close(); // convert stream string vertexcode = vshaderstream.str(); fragmentcode = fshaderstream.str(); } catch (std::ifstream::failure e) { std::cout << "error::shader::file_not_succesfully_read" << std::endl; } const glchar *vshadercode = vertexcode.c_str(); const glchar *fshadercode = fragmentcode.c_str(); // 2. compile shaders gluint vertex, fragment; glint success; glchar infolog[512]; // vertex shader vertex = glcreateshader(gl_vertex_shader); glshadersource(vertex, 1, &vshadercode, null); glcompileshader(vertex); // print compile errors if glgetshaderiv(vertex, gl_compile_status, &success); if (!success) { glgetshaderinfolog(vertex, 512, null, infolog); std::cout << "error::shader::vertex::compilation_failed\n" << infolog << std::endl; } // fragment shader fragment = glcreateshader(gl_fragment_shader); glshadersource(fragment, 1, &fshadercode, null); glcompileshader(fragment); // print compile errors if glgetshaderiv(fragment, gl_compile_status, &success); if (!success) { glgetshaderinfolog(fragment, 512, null, infolog); std::cout << "error::shader::fragment::compilation_failed\n" << infolog << std::endl; } // shader program this->program = glcreateprogram(); glattachshader(this->program, vertex); glattachshader(this->program, fragment); gllinkprogram(this->program); // print linking errors if glgetprogramiv(this->program, gl_link_status, &success); if (!success) { glgetprograminfolog(this->program, 512, null, infolog); std::cout << "error::shader::program::linking_failed\n" << infolog << std::endl; } // delete shaders they're linked our program , no longer necessery gldeleteshader(vertex); gldeleteshader(fragment); } // uses current shader void use() { gluseprogram(this->program); } }; #endif
core.frag:
#version 330 core in vec2 texcoord; out vec4 color; uniform sampler2d ourtexture1; void main() { color = texture(ourtexture1, texcoord); }
core.vs:
#version 330 core layout (location = 0) in vec3 position; layout (location = 2) in vec2 texcoord; out vec2 texcoord; uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main() { gl_position = projection * view * model * vec4(position, 1.0f); texcoord = vec2(texcoord.x, 1.0 - texcoord.y); }
main.cpp:
#include <iostream> // glew #define glew_static #include <gl/glew.h> // glfw #include <glfw/glfw3.h> // other libs #include "soil2/soil2.h" #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/type_ptr.hpp> // other includes #include "shader.h" // window dimensions const gluint width = 800, height = 600; // main function, here start application , run game loop int main() { // init glfw glfwinit(); // set required options glfw glfwwindowhint(glfw_context_version_major, 3); glfwwindowhint(glfw_context_version_minor, 3); glfwwindowhint(glfw_opengl_profile, glfw_opengl_core_profile); glfwwindowhint(glfw_opengl_forward_compat, gl_true); glfwwindowhint(glfw_resizable, gl_false); // create glfwwindow object can use glfw's functions glfwwindow *window = glfwcreatewindow(width, height, "learnopengl", nullptr, nullptr); int screenwidth, screenheight; glfwgetframebuffersize(window, &screenwidth, &screenheight); if (nullptr == window) { std::cout << "failed create glfw window" << std::endl; glfwterminate(); return exit_failure; } glfwmakecontextcurrent(window); // set true glew knows use modern approach retrieving function pointers , extensions glewexperimental = gl_true; // initialize glew setup opengl function pointers if (glew_ok != glewinit()) { std::cout << "failed initialize glew" << std::endl; return exit_failure; } // define viewport dimensions glviewport(0, 0, screenwidth, screenheight); glenable(gl_depth_test); // enable alpha support glenable(gl_blend); glblendfunc(gl_src_alpha, gl_one_minus_src_alpha);//enables jpegs , alphas // build , compile our shader program shader ourshader("res/shaders/core.vs", "res/shaders/core.frag"); // use perspective projection glfloat vertices[] = { //x y z normalized texture coordinates -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f }; gluint vbo, vao;//vertex buffer object, vertex array object glgenvertexarrays(1, &vao); glgenbuffers(1, &vbo); // bind vertex array object first, bind , set vertex buffer(s) , attribute pointer(s). glbindvertexarray(vao); glbindbuffer(gl_array_buffer, vbo); glbufferdata(gl_array_buffer, sizeof(vertices), vertices, gl_static_draw); // position attribute glvertexattribpointer(0, 3, gl_float, gl_false, 5 * sizeof(glfloat), (glvoid *)0); glenablevertexattribarray(0); // texcoord attribute glvertexattribpointer(2, 2, gl_float, gl_false, 5 * sizeof(glfloat), (glvoid *)(3 * sizeof(glfloat))); glenablevertexattribarray(2); glbindvertexarray(0); // unbind vao // load , create texture gluint texture; int width, height; glgentextures(1, &texture); glbindtexture(gl_texture_2d, texture); // set our texture parameters gltexparameteri(gl_texture_2d, gl_texture_wrap_s, gl_repeat); gltexparameteri(gl_texture_2d, gl_texture_wrap_t, gl_repeat); // set texture filtering gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear); // load, create texture , generate mipmaps unsigned char *image = soil_load_image("res/images/image1.jpg", &width, &height, 0, soil_load_rgba); glteximage2d(gl_texture_2d, 0, gl_rgba, width, height, 0, gl_rgba, gl_unsigned_byte, image); glgeneratemipmap(gl_texture_2d); soil_free_image_data(image); glbindtexture(gl_texture_2d, 0); glm::mat4 projection; projection = glm::perspective(45.0f, (glfloat)screenwidth / (glfloat)screenheight, 0.1f, 1000.0f); // game loop while (!glfwwindowshouldclose(window)) { // check if events have been activiated (key pressed, mouse moved etc.) , call corresponding response functions glfwpollevents(); // render // clear colorbuffer glclearcolor(0.9f, 0.5f, 0.3f, 1.0f); glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glactivetexture(gl_texture0); glbindtexture(gl_texture_2d, texture); gluniform1i(glgetuniformlocation(ourshader.program, "ourtexture"), 0); ourshader.use(); glm::mat4 model; glm::mat4 view; model = glm::rotate(model, (glfloat)glfwgettime() * 1.0f, glm::vec3(0.5f, 1.0f, 0.0f)); view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f)); glint modelloc = glgetuniformlocation(ourshader.program, "model"); glint viewloc = glgetuniformlocation(ourshader.program, "view"); glint projloc = glgetuniformlocation(ourshader.program, "projection"); gluniformmatrix4fv(modelloc, 1, gl_false, glm::value_ptr(model)); gluniformmatrix4fv(viewloc, 1, gl_false, glm::value_ptr(view)); gluniformmatrix4fv(projloc, 1, gl_false, glm::value_ptr(projection)); glbindvertexarray(vao); gldrawarrays(gl_triangles, 0, 36); glbindvertexarray(0); // swap screen buffers glfwswapbuffers(window); } // de-allocate resources once they've outlived purpose gldeletevertexarrays(1, &vao); gldeletebuffers(1, &vbo); // terminate glfw, clearing resources allocated glfw. glfwterminate(); return exit_success; }
in fragment shader name of texture sampler uniform "ourtexture1"
:
uniform sampler2d ourtexture1;
but when try location of texture sampler uniform use name "ourtexture"
:
glgetuniformlocation(ourshader.program, "ourtexture")
second issue is, set texture sampler uniform ourtexture1
before program part of current rendering state.
gluniform1i(glgetuniformlocation(ourshader.program, "ourtexture"), 0); ourshader.use();
the locations of uniforms, can retrieved (glgetuniformlocation
) @ time after program linked (gllinkprogram
).
gllinkprogram( ourshader.program ); glint modelloc = glgetuniformlocation( ourshader.program, "model" ); glint viewloc = glgetuniformlocation( ourshader.program, "view" ); glint projloc = glgetuniformlocation( ourshader.program, "projection" ); glint texloc = glgetuniformlocation( ourshader.program, "ourtexture1" );
but uniforms can not set (gluniform
) until program active program (gluseprogram
).
gluseprogram( ourshader.program ); gluniformmatrix4fv( modelloc, 1, gl_false, glm::value_ptr(model) ); gluniformmatrix4fv( viewloc, 1, gl_false, glm::value_ptr(view) ); gluniformmatrix4fv( projloc, 1, gl_false, glm::value_ptr(projection) ); gluniform1i( texloc, 0 );
Comments
Post a Comment