bool CGUIManager::CreateDeviceObjects() { Window->MakeContextCurrent(); // Backup GL state GLint last_texture, last_array_buffer, last_vertex_array; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer); glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array); const GLchar *vertex_shader = "#version 330\n" "uniform mat4 ProjMtx;\n" "in vec2 Position;\n" "in vec2 UV;\n" "in vec4 Color;\n" "out vec2 Frag_UV;\n" "out vec4 Frag_Color;\n" "void main()\n" "{\n" " Frag_UV = UV;\n" " Frag_Color = Color;\n" " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" "}\n"; const GLchar* fragment_shader = "#version 330\n" "uniform sampler2D Texture;\n" "in vec2 Frag_UV;\n" "in vec4 Frag_Color;\n" "out vec4 Out_Color;\n" "void main()\n" "{\n" " Out_Color = Frag_Color * texture( Texture, Frag_UV.st);\n" "}\n"; ShaderHandle = glCreateProgram(); VertHandle = glCreateShader(GL_VERTEX_SHADER); FragHandle = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(VertHandle, 1, &vertex_shader, 0); glShaderSource(FragHandle, 1, &fragment_shader, 0); glCompileShader(VertHandle); glCompileShader(FragHandle); glAttachShader(ShaderHandle, VertHandle); glAttachShader(ShaderHandle, FragHandle); glLinkProgram(ShaderHandle); AttribLocationTex = glGetUniformLocation(ShaderHandle, "Texture"); AttribLocationProjMtx = glGetUniformLocation(ShaderHandle, "ProjMtx"); AttribLocationPosition = glGetAttribLocation(ShaderHandle, "Position"); AttribLocationUV = glGetAttribLocation(ShaderHandle, "UV"); AttribLocationColor = glGetAttribLocation(ShaderHandle, "Color"); glGenBuffers(1, &VboHandle); glGenBuffers(1, &ElementsHandle); glGenVertexArrays(1, &VaoHandle); glBindVertexArray(VaoHandle); glBindBuffer(GL_ARRAY_BUFFER, VboHandle); glEnableVertexAttribArray(AttribLocationPosition); glEnableVertexAttribArray(AttribLocationUV); glEnableVertexAttribArray(AttribLocationColor); #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT)) glVertexAttribPointer(AttribLocationPosition, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*) OFFSETOF(ImDrawVert, pos)); glVertexAttribPointer(AttribLocationUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*) OFFSETOF(ImDrawVert, uv)); glVertexAttribPointer(AttribLocationColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*) OFFSETOF(ImDrawVert, col)); #undef OFFSETOF CreateFontsTexture(); // Restore modified GL state glBindTexture(GL_TEXTURE_2D, last_texture); glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); glBindVertexArray(last_vertex_array); return true; }
///////////////////////////////// // Initialize function ///////////////////////////////// void init() { /*select clearing (background) color*/ glClearColor(0.0, 0.0, 0.0, 1.0); rotateYEarth = 1; rotateZEarth = 1; //populate our arrays //spherevertcount = generateSphere(2, 30); vec4 sphereverts[2592]; vec2 spheretexcoords[2592]; // 2592 vec3 spherenormals[2592]; vec4 spheretangents[2592]; vec4 cloudverts[2592]; vec2 cloudtexcoords[2592]; // 2592 vec3 cloudnormals[2592]; vec4 cloudtangents[2592]; CreateSphere(sphereverts, spheretexcoords, spherenormals, spheretangents, 2, 0,0,0); // create cloud CreateSphere(cloudverts, cloudtexcoords, cloudnormals, cloudtangents, 2, 0,0,0); ///////////////////////////////////////// // Create a vertex array object spherevao = new GLuint[1]; spherevbo = new GLuint[3]; cloudvao = new GLuint[1]; cloudvbo = new GLuint[3]; ////////////////////////// // earth buffer ////////////////////////// glGenVertexArrays( 1, &spherevao[0] ); // Create and initialize any buffer objects glBindVertexArray( spherevao[0] ); glGenBuffers( 2, &spherevbo[0] ); glBindBuffer( GL_ARRAY_BUFFER, spherevbo[0] ); glBufferData( GL_ARRAY_BUFFER, VertexCount*sizeof(vec4), sphereverts, GL_STATIC_DRAW); glBindBuffer( GL_ARRAY_BUFFER, spherevbo[1] ); glBufferData( GL_ARRAY_BUFFER, VertexCount*sizeof(vec2), spheretexcoords, GL_STATIC_DRAW); glBindBuffer( GL_ARRAY_BUFFER, spherevbo[2] ); glBufferData( GL_ARRAY_BUFFER, VertexCount*sizeof(vec3), spherenormals, GL_STATIC_DRAW); ////////////////////// // Cloud buffer ////////////////////// glGenVertexArrays( 1, &cloudvao[0] ); // Create and initialize any buffer objects glBindVertexArray( cloudvao[0] ); glGenBuffers( 2, &cloudvbo[0] ); glBindBuffer( GL_ARRAY_BUFFER, cloudvbo[0] ); glBufferData( GL_ARRAY_BUFFER, VertexCount*sizeof(vec4), cloudverts, GL_STATIC_DRAW); glBindBuffer( GL_ARRAY_BUFFER, cloudvbo[1] ); glBufferData( GL_ARRAY_BUFFER, VertexCount*sizeof(vec2), cloudtexcoords, GL_STATIC_DRAW); glBindBuffer( GL_ARRAY_BUFFER, cloudvbo[2] ); glBufferData( GL_ARRAY_BUFFER, VertexCount*sizeof(vec3), cloudnormals, GL_STATIC_DRAW); // Load shaders and use the resulting shader program programAllFeatures = InitShader( "vshader-phongshading.glsl", "fshader-phongshading.glsl" ); programColor = InitShader( "vshader-color.glsl", "fshader-color.glsl" ); programCloud = InitShader( "vshader-cloud.glsl", "fshader-cloud.glsl" ); glUseProgram(programAllFeatures); // Create a vertex array object // glGenVertexArrays( 1, &vao[0] ); // // Create and initialize any buffer objects //glBindVertexArray( vao[0] ); //glGenBuffers( 2, &vbo[0] ); // glBindBuffer( GL_ARRAY_BUFFER, vbo[0] ); // glBufferData( GL_ARRAY_BUFFER, spherevertcount*sizeof(vec4), sphere_verts, GL_STATIC_DRAW); // ////and now our colors for each vertex //glBindBuffer( GL_ARRAY_BUFFER, vbo[1] ); //glBufferData( GL_ARRAY_BUFFER, spherevertcount*sizeof(vec3), sphere_normals, GL_STATIC_DRAW ); // setupShader(programAllFeatures); model_view = glGetUniformLocation(programAllFeatures, "model_view"); projection = glGetUniformLocation(programAllFeatures, "projection"); setupEarthShader(programAllFeatures, spherevao, spherevbo); // set up cloud shader // This is where I have error that whenever I use setupearthshader for cloud // I have bad images rendered. // I don't understand and keep looking errors why but give up cloud // setupEarthShader(programCloud, cloudvao, cloudvbo); glUseProgram(programAllFeatures); ILuint ilTexID[5]; /* ILuint is a 32bit unsigned integer. //Variable texid will be used to store image name. */ ilInit(); /* Initialization of OpenIL */ ilGenImages(5, ilTexID); /* Generation of three image names for OpenIL image loading */ glGenTextures(5, texName); //and we eventually want the data in an OpenGL texture ///////////////////////////////////////// // EARTH BUFFERS //////////////////////////////////////////////////// // load color map if ( true ) { ilBindImage(ilTexID[0]); /* Binding of IL image name */ loadTexFile("images/Earth.png"); glBindTexture(GL_TEXTURE_2D, texName[0]); //bind OpenGL texture name glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); //GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); //GL_NEAREST); //Note how we depend on OpenIL to supply information about the file we just loaded in glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT),0, ilGetInteger(IL_IMAGE_FORMAT), ilGetInteger(IL_IMAGE_TYPE), ilGetData()); glGenerateMipmap(GL_TEXTURE_2D); } // load cloud if (true) { glUseProgram(programCloud); ilBindImage(ilTexID[1]); /* Binding of IL image name */ glBindTexture(GL_TEXTURE_2D, texName[1]); //bind OpenGL texture name loadTexFile("images/earthcloudmap.png"); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); //GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); //GL_NEAREST); //Note how we depend on OpenIL to supply information about the file we just loaded in glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT),0, ilGetInteger(IL_IMAGE_FORMAT), ilGetInteger(IL_IMAGE_TYPE), ilGetData()); glGenerateMipmap(GL_TEXTURE_2D); } // Spec map if (true) { glUseProgram(programAllFeatures); ilBindImage(ilTexID[2]); /* Binding of IL image name */ glBindTexture(GL_TEXTURE_2D, texName[2]); //bind OpenGL texture name loadTexFile("images/EarthSpec.png"); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); //GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); //GL_NEAREST); //Note how we depend on OpenIL to supply information about the file we just loaded in glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT),0, ilGetInteger(IL_IMAGE_FORMAT), ilGetInteger(IL_IMAGE_TYPE), ilGetData()); glGenerateMipmap(GL_TEXTURE_2D); } // Normal map if (true) { ilBindImage(ilTexID[3]); /* Binding of IL image name */ glBindTexture(GL_TEXTURE_2D, texName[3]); //bind OpenGL texture name loadTexFile("images/EarthNormal.png"); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); //GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); //GL_NEAREST); //Note how we depend on OpenIL to supply information about the file we just loaded in glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT),0, ilGetInteger(IL_IMAGE_FORMAT), ilGetInteger(IL_IMAGE_TYPE), ilGetData()); glGenerateMipmap(GL_TEXTURE_2D); } // Night map if (true) { ilBindImage(ilTexID[4]); /* Binding of IL image name */ glBindTexture(GL_TEXTURE_2D, texName[4]); //bind OpenGL texture name loadTexFile("images/EarthNight.png"); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); //GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); //GL_NEAREST); //Note how we depend on OpenIL to supply information about the file we just loaded in glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT),0, ilGetInteger(IL_IMAGE_FORMAT), ilGetInteger(IL_IMAGE_TYPE), ilGetData()); glGenerateMipmap(GL_TEXTURE_2D); } //////////////////////////////////////////////// ilDeleteImages(5, ilTexID); //we're done with OpenIL, so free up the memory //////////////////////////////////////////////////// glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); texMap = glGetUniformLocation(programAllFeatures, "texture"); glUniform1i(texMap, 0);//assign this one to texture unit 0 cloudMap = glGetUniformLocation(programCloud, "cloudtexture"); glUniform1i(cloudMap, 1);//assign cloud map to texture unit 1 specMap = glGetUniformLocation(programAllFeatures, "spectexture"); glUniform1i(specMap, 2);//assign spec map to 2 texture unit normalMap = glGetUniformLocation(programAllFeatures, "normalMap"); glUniform1i(normalMap, 3);//assign normal map to 3 texture unit nightMap = glGetUniformLocation(programAllFeatures, "nighttexture"); glUniform1i(nightMap, 4);//assign spec map to 4 texture unit // setup lightning vAmbientDiffuseColor = glGetAttribLocation(programAllFeatures, "vAmbientDiffuseColor"); vSpecularColor = glGetAttribLocation(programAllFeatures, "vSpecularColor"); vSpecularExponent = glGetAttribLocation(programAllFeatures, "vSpecularExponent"); light_position = glGetUniformLocation(programAllFeatures, "light_position"); light_color = glGetUniformLocation(programAllFeatures, "light_color"); ambient_light = glGetUniformLocation(programAllFeatures, "ambient_light"); //Only draw the things in the front layer glEnable(GL_DEPTH_TEST); }
void raytracer_app::startup() { int i; load_shaders(); glGenBuffers(1, &uniforms_buffer); glBindBuffer(GL_UNIFORM_BUFFER, uniforms_buffer); glBufferData(GL_UNIFORM_BUFFER, sizeof(uniforms_block), NULL, GL_DYNAMIC_DRAW); glGenBuffers(1, &sphere_buffer); glBindBuffer(GL_UNIFORM_BUFFER, sphere_buffer); glBufferData(GL_UNIFORM_BUFFER, 128 * sizeof(sphere), NULL, GL_DYNAMIC_DRAW); glGenBuffers(1, &plane_buffer); glBindBuffer(GL_UNIFORM_BUFFER, plane_buffer); glBufferData(GL_UNIFORM_BUFFER, 128 * sizeof(plane), NULL, GL_DYNAMIC_DRAW); glGenBuffers(1, &light_buffer); glBindBuffer(GL_UNIFORM_BUFFER, light_buffer); glBufferData(GL_UNIFORM_BUFFER, 128 * sizeof(sphere), NULL, GL_DYNAMIC_DRAW); glGenVertexArrays(1, &vao); glBindVertexArray(vao); glGenFramebuffers(MAX_RECURSION_DEPTH, ray_fbo); glGenTextures(1, &tex_composite); glGenTextures(MAX_RECURSION_DEPTH, tex_position); glGenTextures(MAX_RECURSION_DEPTH, tex_reflected); glGenTextures(MAX_RECURSION_DEPTH, tex_refracted); glGenTextures(MAX_RECURSION_DEPTH, tex_reflection_intensity); glGenTextures(MAX_RECURSION_DEPTH, tex_refraction_intensity); glBindTexture(GL_TEXTURE_2D, tex_composite); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB16F, MAX_FB_WIDTH, MAX_FB_HEIGHT); for (i = 0; i < MAX_RECURSION_DEPTH; i++) { glBindFramebuffer(GL_FRAMEBUFFER, ray_fbo[i]); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex_composite, 0); glBindTexture(GL_TEXTURE_2D, tex_position[i]); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB32F, MAX_FB_WIDTH, MAX_FB_HEIGHT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, tex_position[i], 0); glBindTexture(GL_TEXTURE_2D, tex_reflected[i]); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB16F, MAX_FB_WIDTH, MAX_FB_HEIGHT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, tex_reflected[i], 0); glBindTexture(GL_TEXTURE_2D, tex_refracted[i]); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB16F, MAX_FB_WIDTH, MAX_FB_HEIGHT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, tex_refracted[i], 0); glBindTexture(GL_TEXTURE_2D, tex_reflection_intensity[i]); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB16F, MAX_FB_WIDTH, MAX_FB_HEIGHT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT4, tex_reflection_intensity[i], 0); glBindTexture(GL_TEXTURE_2D, tex_refraction_intensity[i]); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB16F, MAX_FB_WIDTH, MAX_FB_HEIGHT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT5, tex_refraction_intensity[i], 0); } glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindTexture(GL_TEXTURE_2D, 0); }
void GDALTerrain::init() { dataset = (GDALDataset*) GDALOpen(gdalFile.c_str(), GA_ReadOnly); if(dataset == nullptr) { std::cerr << "Unable to open GDAL File: " << gdalFile << std::endl; exit(1); } auto raster = dataset->GetRasterBand(1); width = raster->GetXSize(); height = raster->GetYSize(); int gotMin, gotMax; float min = (float) raster->GetMinimum(&gotMin); float max = (float) raster->GetMaximum(&gotMax); double minMax[2] = {min, max}; if(!(gotMin && gotMax)) { GDALComputeRasterMinMax((GDALRasterBandH) raster, TRUE, minMax); min = (float) minMax[0]; max = (float) minMax[1]; } if(Engine::getEngine()->getOptions().verbose) { std::cout << "terrain: " << gdalFile << " x: " << width << " y: " << height << " min: " << min << " max: " << max << std::endl; } Vertex vert; vert.normal[0] = 0; vert.normal[1] = 1; vert.normal[2] = 0; float scale = Engine::getEngine()->getOptions().map_scalar; heightScale = 100.0f; gdal_data = std::vector<std::vector<float>>(height, std::vector<float>(width, 0.0f)); float *lineData1 = new float[width]; float *lineData2 = new float[width]; float range = max - min; for(int z = 0; z < height-1; z++) { raster->RasterIO(GF_Read, 0, z, width, 1, lineData1, width, 1, GDT_Float32, 0, 0); raster->RasterIO(GF_Read, 0, z+1, width, 1, lineData2, width, 1, GDT_Float32, 0, 0); gdal_data[z].assign(lineData1, lineData1+width); gdal_data[z+1].assign(lineData2, lineData2+width); for(int x = 0; x < width-1; x++) { vert.pos[0] = x * scale; vert.pos[1] = heightScale * (lineData1[x]-min)/range; vert.pos[2] = z * scale; vert.texture[0] = x;// / float(width); vert.texture[1] = z;// / float(height); calcNormal(x, z, vert, min, range); geometry.push_back(vert); vert.pos[0] = (x+1) * scale; vert.pos[1] = heightScale * (lineData1[x+1]-min)/range; vert.pos[2] = z * scale; vert.texture[0] = (x+1);// / float(width); vert.texture[1] = z;// / float(height); calcNormal(x, z, vert, min, range); geometry.push_back(vert); vert.pos[0] = x * scale; vert.pos[1] = heightScale * (lineData2[x]-min)/range; vert.pos[2] = (z+1) * scale; vert.texture[0] = x;// / float(width); vert.texture[1] = (z+1);// / float(height); calcNormal(x, z, vert, min, range); geometry.push_back(vert); vert.pos[0] = x * scale; vert.pos[1] = heightScale * (lineData2[x]-min)/range; vert.pos[2] = (z+1) * scale; vert.texture[0] = x;// / float(width); vert.texture[1] = (z+1);// / float(height); calcNormal(x, z, vert, min, range); geometry.push_back(vert); vert.pos[0] = (x+1) * scale; vert.pos[1] = heightScale * (lineData2[x+1]-min)/range; vert.pos[2] = (z+1) * scale; vert.texture[0] = (x+1);// / float(width); vert.texture[1] = (z+1);// / float(height); calcNormal(x, z, vert, min, range); geometry.push_back(vert); vert.pos[0] = (x+1) * scale; vert.pos[1] = heightScale * (lineData1[x+1]-min)/range; vert.pos[2] = z * scale; vert.texture[0] = (x+1);// / float(width); vert.texture[1] = z;// / float(height); calcNormal(x, z, vert, min, range); geometry.push_back(vert); } } // for(int i = 0; i < geometry.size(); i++) { // if(i % 6 == 0) // std::cout << i << ": " << geometry[i].pos[1] << std::endl; //} std::cout << "size: " << geometry.size() << std::endl; delete[] lineData1; delete[] lineData2; Vertex *geo = geometry.data(); glGenVertexArrays(1, &vao); glBindVertexArray(vao); glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * geometry.size(), geo, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(program->getLocation("vs_pos"), 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex,pos)); glEnableVertexAttribArray(1); glVertexAttribPointer(program->getLocation("vs_norm"), 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex,normal)); glEnableVertexAttribArray(2); glVertexAttribPointer(program->getLocation("vs_uv"), 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex,texture)); texture = new Texture("../assets/desert.jpg", GL_TEXTURE_2D); //model = glm::translate(model, glm::vec3(width/2, 0, height/2)); }
void Assignment1::Init() { // Init VBO here // Set background color to dark blue glClearColor(0.3f, 0.0f, 0.0f, 0.0f); // Generate a default VAO for now glGenVertexArrays(1, &m_vertexArrayID); glBindVertexArray(m_vertexArrayID); glGenBuffers(NUM_GEOMETRY, &m_vertexBuffer[0]); glGenBuffers(NUM_GEOMETRY, &m_colorBuffer[0]); static const GLfloat vertex_buffer_data[] = { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.1f, 0.0f, }; glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer[GEO_TRIANGLE_1]); glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer_data), vertex_buffer_data, GL_STATIC_DRAW); static const GLfloat vertex_buffer_data1[] = { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, }; glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer[GEO_TRIANGLE_2]); glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer_data1), vertex_buffer_data1, GL_STATIC_DRAW); static const GLfloat vertex_buffer_data2[] = { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, }; glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer[GEO_TRIANGLE_3]); glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer_data2), vertex_buffer_data2, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer[GEO_DOOR]); glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer_data2), vertex_buffer_data2, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer[GEO_CLOUD]); glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer_data2), vertex_buffer_data2, GL_STATIC_DRAW); static const GLfloat vertex_buffer_data3[] = { -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, }; glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer[GEO_BIRD]); glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer_data3), vertex_buffer_data3, GL_STATIC_DRAW); static const GLfloat color_buffer_data[] = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, }; glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer[GEO_TRIANGLE_1]); glBufferData(GL_ARRAY_BUFFER, sizeof(color_buffer_data), color_buffer_data, GL_STATIC_DRAW); static const GLfloat color_buffer_data1[] = { 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, }; glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer[GEO_TRIANGLE_2]); glBufferData(GL_ARRAY_BUFFER, sizeof(color_buffer_data1), color_buffer_data1, GL_STATIC_DRAW); static const GLfloat color_buffer_data2[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, }; glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer[GEO_TRIANGLE_3]); glBufferData(GL_ARRAY_BUFFER, sizeof(color_buffer_data2), color_buffer_data2, GL_STATIC_DRAW); static const GLfloat color_buffer_data3[] = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, }; glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer[GEO_DOOR]); glBufferData(GL_ARRAY_BUFFER, sizeof(color_buffer_data3), color_buffer_data3, GL_STATIC_DRAW); static const GLfloat color_buffer_data4[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, }; glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer[GEO_BIRD]); glBufferData(GL_ARRAY_BUFFER, sizeof(color_buffer_data4), color_buffer_data4, GL_STATIC_DRAW); static const GLfloat color_buffer_data5[] = { 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, }; glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer[GEO_CLOUD]); glBufferData(GL_ARRAY_BUFFER, sizeof(color_buffer_data5), color_buffer_data5, GL_STATIC_DRAW); for (int i = 0; i < 100; i++) { rainpositionx[i] = rand() % 80 - 40; rainpositiony[i] = rand() % 60 - 30; } //Load vertex and fragment shaders m_programID = LoadShaders( "Shader//TransformVertexShader.vertexshader", "Shader//SimpleFragmentShader.fragmentshader"); m_parameters[U_MVP] = glGetUniformLocation(m_programID, "MVP"); glUseProgram(m_programID); glEnable(GL_DEPTH_TEST); rotateAngle = 0.0f; translateX = 1; translateX2 = 1; translateY = 30; scaleAll = 10; }
// loads a cube into the VAO and VBO globals: gVAO and gVBO static void LoadCube() { // make and bind the VAO glGenVertexArrays(1, &gVAO); glBindVertexArray(gVAO); // make and bind the VBO glGenBuffers(1, &gVBO); glBindBuffer(GL_ARRAY_BUFFER, gVBO); // Make a cube out of triangles (two triangles per side) GLfloat vertexData[] = { // X Y Z U V // bottom -1.0f,-1.0f,-1.0f, 0.0f, 0.0f, 1.0f,-1.0f,-1.0f, 1.0f, 0.0f, -1.0f,-1.0f, 1.0f, 0.0f, 1.0f, 1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 1.0f,-1.0f, 1.0f, 1.0f, 1.0f, -1.0f,-1.0f, 1.0f, 0.0f, 1.0f, // top -1.0f, 1.0f,-1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,-1.0f, 1.0f, 0.0f, 1.0f, 1.0f,-1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // front -1.0f,-1.0f, 1.0f, 1.0f, 0.0f, 1.0f,-1.0f, 1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,-1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // back -1.0f,-1.0f,-1.0f, 0.0f, 0.0f, -1.0f, 1.0f,-1.0f, 0.0f, 1.0f, 1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 1.0f,-1.0f,-1.0f, 1.0f, 0.0f, -1.0f, 1.0f,-1.0f, 0.0f, 1.0f, 1.0f, 1.0f,-1.0f, 1.0f, 1.0f, // left -1.0f,-1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f,-1.0f, 1.0f, 0.0f, -1.0f,-1.0f,-1.0f, 0.0f, 0.0f, -1.0f,-1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f,-1.0f, 1.0f, 0.0f, // right 1.0f,-1.0f, 1.0f, 1.0f, 1.0f, 1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 1.0f, 1.0f,-1.0f, 0.0f, 0.0f, 1.0f,-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,-1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f }; glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW); // connect the xyz to the "vert" attribute of the vertex shader glEnableVertexAttribArray(gProgram->attrib("vert")); glVertexAttribPointer(gProgram->attrib("vert"), 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), NULL); // connect the uv coords to the "vertTexCoord" attribute of the vertex shader glEnableVertexAttribArray(gProgram->attrib("vertTexCoord")); glVertexAttribPointer(gProgram->attrib("vertTexCoord"), 2, GL_FLOAT, GL_TRUE, 5*sizeof(GLfloat), (const GLvoid*)(3 * sizeof(GLfloat))); // unbind the VAO glBindVertexArray(0); }
int main() { int width = 640; int height = 480; sf::Window window(sf::VideoMode(width, height), "sfml", sf::Style::Default, sf::ContextSettings(0,0,0,3,3)); // using glew since sfml doesn create a core context if(glewInit()) { std::cerr << "failed to init GLEW" << std::endl; return 1; } // shader source code std::string vertex_source = "#version 330\n" "layout(location = 0) in vec4 vposition;\n" "layout(location = 1) in vec4 vcolor;\n" "out vec4 fcolor;\n" "void main() {\n" " fcolor = vcolor;\n" " gl_Position = vposition;\n" "}\n"; std::string fragment_source = "#version 330\n" "in vec4 fcolor;\n" "layout(location = 0) out vec4 FragColor;\n" "void main() {\n" " FragColor = fcolor;\n" "}\n"; // program and shader handles GLuint shader_program, vertex_shader, fragment_shader; // we need these to properly pass the strings const char *source; int length; // create and compiler vertex shader vertex_shader = glCreateShader(GL_VERTEX_SHADER); source = vertex_source.c_str(); length = vertex_source.size(); glShaderSource(vertex_shader, 1, &source, &length); glCompileShader(vertex_shader); if(!check_shader_compile_status(vertex_shader)) { return 1; } // create and compiler fragment shader fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); source = fragment_source.c_str(); length = fragment_source.size(); glShaderSource(fragment_shader, 1, &source, &length); glCompileShader(fragment_shader); if(!check_shader_compile_status(fragment_shader)) { return 1; } // create program shader_program = glCreateProgram(); // attach shaders glAttachShader(shader_program, vertex_shader); glAttachShader(shader_program, fragment_shader); // link the program and check for errors glLinkProgram(shader_program); if(!check_program_link_status(shader_program)) { return 1; } // vao and vbo handle GLuint vao, vbo; // generate and bind the vao glGenVertexArrays(1, &vao); glBindVertexArray(vao); // generate and bind the buffer object glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); // data for a fullscreen quad GLfloat vertexData[] = { // X Y Z R G B 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, // vertex 0 -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // vertex 1 1.0f,-1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // vertex 2 1.0f,-1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // vertex 3 -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // vertex 4 -1.0f,-1.0f, 0.0f, 1.0f, 0.0f, 0.0f, // vertex 5 }; // 6 vertices with 6 components (floats) each // fill with data glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*6*6, vertexData, GL_STATIC_DRAW); // set up generic attrib pointers glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(GLfloat), (char*)0 + 0*sizeof(GLfloat)); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(GLfloat), (char*)0 + 3*sizeof(GLfloat)); bool closed = false; while(!closed) { sf::Event event; while(window.pollEvent(event)) { if(event.type == sf::Event::Closed) { closed = true; } } // clear first glClear(GL_COLOR_BUFFER_BIT); // use the shader program glUseProgram(shader_program); // bind the vao glBindVertexArray(vao); // draw glDrawArrays(GL_TRIANGLES, 0, 6); // check for errors GLenum error = glGetError(); if(error != GL_NO_ERROR) { std::cerr << error << std::endl; break; } // finally swap buffers window.display(); } // delete the created objects glDeleteVertexArrays(1, &vao); glDeleteBuffers(1, &vbo); glDetachShader(shader_program, vertex_shader); glDetachShader(shader_program, fragment_shader); glDeleteShader(vertex_shader); glDeleteShader(fragment_shader); glDeleteProgram(shader_program); return 0; }
void OBJObject::setup() { local = glm::mat4(1.0f); direction = glm::vec3(0, 0, -1); pointSize = 1.0f; scale = 1.0f; this->angle = 0.0f; // Create buffers/arrays glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); glGenBuffers(1, &NBO); // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s). glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); if(b_cube) { glBufferData(GL_ARRAY_BUFFER, sizeof(c_vertices), c_vertices, GL_STATIC_DRAW); } else { glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW); } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); if(b_cube) { glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(c_indices), c_indices, GL_STATIC_DRAW); } else { glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW); } glVertexAttribPointer(0,// This first parameter x should be the same as the number passed into the line "layout (location = x)" in the vertex shader. In this case, it's 0. Valid values are 0 to GL_MAX_UNIFORM_LOCATIONS. 3, // This second line tells us how any components there are per vertex. In this case, it's 3 (we have an x, y, and z component) GL_FLOAT, // What type these components are GL_FALSE, // GL_TRUE means the values should be normalized. GL_FALSE means they shouldn't sizeof(glm::vec3), // Offset between consecutive vertex attributes. Since each of our vertices have 3 floats, they should have the size of 3 floats in between (GLvoid*)0); // Offset of the first vertex's component. In our case it's 0 since we don't pad the vertices array with anything. glEnableVertexAttribArray(0); //normals glBindBuffer(GL_ARRAY_BUFFER, NBO); glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_TRUE, sizeof(glm::vec3), (GLvoid*)0); // glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs), remember: do NOT unbind the EBO, keep it bound to this VAO }
void SW::GLViewer::init() { setGL(); initGLSL(); #if BUFFER_ //glClearColor(0.0, 0.0, 0.0, 0.0); //glDisable(GL_DITHER); glShadeModel(GL_FLAT); //glEnable(GL_DEPTH_TEST); enum{Vertices, Color, Elements, NumVBOs}; GLuint buffers[NumVBOs]; // glew init is very important or errors occur glewInit(); // generate vertex arrays, and each array is corresponding to a object to be render glGenVertexArrays(1, &arrayId); #ifdef QT_BUFFER QGLFunctions qtgl; qtgl.initializeGLFunctions(0); #endif // 3D world coordinate of points #ifdef QT_BUFFER QVector<QVector3D> Verts; Verts.append(QVector3D(-1.0, -1.0, -1.0)); Verts.append(QVector3D(-1.0, -1.0, 1.0)); Verts.append(QVector3D(-1.0, 1.0, -1.0)); Verts.append(QVector3D(-1.0, 1.0, 1.0)); Verts.append(QVector3D(1.0, -1.0, -1.0)); Verts.append(QVector3D(1.0, -1.0, 1.0)); Verts.append(QVector3D(1.0, 1.0, -1.0)); Verts.append(QVector3D(1.0, 1.0, 1.0)); #else GLfloat Verts[][3] = { {-1.0, -1.0, -1.0}, {-1.0, -1.0, 1.0}, {-1.0, 1.0, -1.0}, {-1.0, 1.0, 1.0}, {1.0, -1.0, -1.0}, {1.0, -1.0, 1.0}, {1.0, 1.0, -1.0}, {1.0, 1.0, 1.0}, }; #endif // colors of points #ifdef QT_BUFFER QVector<QVector3D> Colors; Colors.append(QVector3D(0, 0.0, 0.0)); Colors.append(QVector3D(0, 0.0, 1.0)); Colors.append(QVector3D(0, 1.0, 0.0)); Colors.append(QVector3D(0, 1.0, 1.0)); Colors.append(QVector3D(1.0, 0.0, 0.0)); Colors.append(QVector3D(1.0, 0.0, 1.0)); Colors.append(QVector3D(1.0, 1.0, 0.0)); Colors.append(QVector3D(1.0, 1.0, 1.0)); #else GLfloat Colors[][3] = { {0.0, 0.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 1.0, 0.0}, {0.0, 1.0, 1.0}, {1.0, 0.0, 0.0}, {1.0, 0.0, 1.0}, {1.0, 1.0, 0.0}, {1.0, 1.0, 1.0}, }; #endif // indices of points #ifdef QT_BUFFER QVector<uint> Indices; Indices.append(0);Indices.append(1);Indices.append(3);Indices.append(2); Indices.append(4);Indices.append(6);Indices.append(7);Indices.append(5); Indices.append(2);Indices.append(3);Indices.append(7);Indices.append(6); Indices.append(0);Indices.append(4);Indices.append(5);Indices.append(1); Indices.append(0);Indices.append(2);Indices.append(6);Indices.append(4); Indices.append(1);Indices.append(5);Indices.append(7);Indices.append(3); #else GLubyte Indices[]={ 0, 1, 3, 2, 4, 6, 7, 5, 2, 3, 7, 6, 0, 4, 5, 1, 0, 2, 6, 4, 1, 5, 7, 3, }; #endif // active a vertex array glBindVertexArray(arrayId); // generate buffer objects, and each attribute(vertices, color, and normal..) is corresponding to one buffer #ifdef QT_BUFFER qtgl.glGenBuffers(NumVBOs, buffers); #else glGenBuffers(NumVBOs, buffers); #endif //---------------------------------------buffer for vertices----------------------------------// // active a buffer object #ifdef QT_BUFFER qtgl.glBindBuffer(GL_ARRAY_BUFFER, buffers[Vertices]); #else glBindBuffer(GL_ARRAY_BUFFER, buffers[Vertices]); #endif // alloc a space for buffer #ifdef QT_BUFFER qtgl.glBufferData(GL_ARRAY_BUFFER, Verts.size()*sizeof(GLfloat), Verts.data(), GL_STATIC_DRAW); #else glBufferData(GL_ARRAY_BUFFER, sizeof(Verts), Verts, GL_STATIC_DRAW); #endif // put the data into the corresponding buffer glVertexPointer(3, GL_FLOAT, 0,BUFFER_OFFSET(0)); glEnableClientState(GL_VERTEX_ARRAY); //---------------------------------------buffer for colors----------------------------------// // buffer for colors #ifdef QT_BUFFER qtgl.glBindBuffer(GL_ARRAY_BUFFER, buffers[Color]); #else glBindBuffer(GL_ARRAY_BUFFER, buffers[Color]); #endif #ifdef QT_BUFFER qtgl.glBufferData(GL_ARRAY_BUFFER, Colors.size()*sizeof(GLfloat), Colors.data(), GL_STATIC_DRAW); #else glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW); #endif glColorPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)); glEnableClientState(GL_COLOR_ARRAY); //---------------------------------------buffer for elements----------------------------------// // buffer for elements #ifdef QT_BUFFER numElement = Indices.size(); #else numElement = sizeof(Indices)/sizeof(Indices[0]); #endif #ifdef QT_BUFFER qtgl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[Elements]); #else glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[Elements]); #endif #ifdef QT_BUFFER qtgl.glBufferData(GL_ELEMENT_ARRAY_BUFFER, Indices.size()*sizeof(uint), Indices.data(), GL_STATIC_DRAW); #else glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW); #endif #endif }
// The MAIN function, from here we start the application and run the game loop int main() { // Init GLFW glfwInit(); // Set all the required options for GLFW glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Create a GLFWwindow object that we can use for GLFW's functions GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr); glfwMakeContextCurrent(window); // Set the required callback functions glfwSetKeyCallback(window, key_callback); // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions glewExperimental = GL_TRUE; // Initialize GLEW to setup the OpenGL Function pointers glewInit(); // Define the viewport dimensions glViewport(0, 0, WIDTH, HEIGHT); // Build and compile our shader program Shader ourShader("basic.vs", "basic.frag"); // Set up vertex data (and buffer(s)) and attribute pointers GLfloat vertices[] = { // Positions // Colors 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // Bottom Right -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // Bottom Left 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // Top }; GLuint VBO, VAO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and 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, 6 * sizeof(GLfloat), (GLvoid*)0); glEnableVertexAttribArray(0); // Color attribute glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); glEnableVertexAttribArray(1); glBindVertexArray(0); // Unbind VAO // Game loop while (!glfwWindowShouldClose(window)) { // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions glfwPollEvents(); // Render // Clear the colorbuffer glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // Draw the triangle ourShader.Use(); glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 3); glBindVertexArray(0); // Swap the screen buffers glfwSwapBuffers(window); } // Properly de-allocate all resources once they've outlived their purpose glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); // Terminate GLFW, clearing any resources allocated by GLFW. glfwTerminate(); return 0; }
/* * Setup OpenGL * * Set up GLFW and GLEW, opens a window. */ void SetupOpenGL() { if (!glfwInit()) { std::cerr << "Failed to initialize GLFW." << std::endl; exit(1); } glfwWindowHint(GLFW_SAMPLES, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); s_window = glfwCreateWindow(1024, 768, "Bullet + OpenGL", nullptr, nullptr); if (s_window == nullptr) { std::cerr << "Failed to open GLFW window." << std::endl; glfwTerminate(); exit(1); } glfwMakeContextCurrent(s_window); glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { std::cerr << "Failed to initialize GLEW." << std::endl; glfwTerminate(); exit(1); } glfwSetInputMode(s_window, GLFW_STICKY_KEYS, GL_TRUE); glfwSetCursorPos(s_window, 1024/2, 768/2); glClearColor(0.0f, 0.0f, 0.4f, 0.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_CULL_FACE); glGenVertexArrays(1, &s_vertexArrayId); glBindVertexArray(s_vertexArrayId); s_model = LoadModelFromObjFile("../res/textures/cube.obj"); glGenBuffers(1, &s_vertexBufferId); glBindBuffer(GL_ARRAY_BUFFER, s_vertexBufferId); glBufferData(GL_ARRAY_BUFFER, s_model.vertices().size() * sizeof(glm::vec3), &s_model.vertices()[0], GL_STATIC_DRAW); glGenBuffers(1, &s_uvCoordBufferId); glBindBuffer(GL_ARRAY_BUFFER, s_uvCoordBufferId); glBufferData(GL_ARRAY_BUFFER, s_model.uvCoords().size() * sizeof(glm::vec2), &s_model.uvCoords()[0], GL_STATIC_DRAW); glGenBuffers(1, &s_normalBufferId); glBindBuffer(GL_ARRAY_BUFFER, s_normalBufferId); glBufferData(GL_ARRAY_BUFFER, s_model.normals().size() * sizeof(glm::vec3), &s_model.normals()[0], GL_STATIC_DRAW); LoadShaders("../res/shaders/vertex.glsl", "../res/shaders/fragment.glsl"); s_mvpMatrixId = glGetUniformLocation(s_programId, "mvpMatrix"); s_modelMatrixId = glGetUniformLocation(s_programId, "modelMatris"); s_viewMatrixId = glGetUniformLocation(s_programId, "modelMatrix"); s_textureId = LoadDDS("../res/textures/uvmap.DDS"); s_uniformTextureId = glGetUniformLocation(s_programId, "myTextureSampler"); glUseProgram(s_programId); s_lightPositionId = glGetUniformLocation(s_programId, "lightPosition_worldspace"); s_lightColorId = glGetUniformLocation(s_programId, "lightColor"); s_lightPowerId = glGetUniformLocation(s_programId, "lightPower"); }
/* Load object from '*.g3d' file function. * ARGUMENTS: * - object structure pointer: * vg4OBJ *Obj; * - file name: * CHAR *FileName; * RETURNS: * (BOOL) TRUE is success, FALSE otherwise. */ BOOL VG4_RndObjLoad( vg4OBJ *Obj, CHAR *FileName ) { FILE *F; DWORD Sign; INT NumOfPrimitives; CHAR MtlFile[300]; INT NumOfV; INT NumOfI; CHAR Mtl[300]; INT p; vg4VERTEX *V; INT *I; memset(Obj, 0, sizeof(vg4OBJ)); F = fopen(FileName, "rb"); if (F == NULL) return FALSE; /* File structure: * 4b Signature: "G3D\0" CHAR Sign[4]; * 4b NumOfPrimitives INT NumOfPrimitives; * 300b material file name: CHAR MtlFile[300]; * repeated NumOfPrimitives times: * 4b INT NumOfV; - vertex count * 4b INT NumOfI; - index (triangles * 3) count * 300b material name: CHAR Mtl[300]; * repeat NumOfV times - vertices: * !!! float point -> FLT * typedef struct * { * VEC P; - Vertex position * VEC2 T; - Vertex texture coordinates * VEC N; - Normal at vertex * VEC4 C; - Vertex color * } VERTEX; * repeat (NumOfF / 3) times - facets (triangles): * INT N0, N1, N2; - for every triangle (N* - vertex number) */ fread(&Sign, 4, 1, F); if (Sign != *(DWORD *)"G3D") { fclose(F); return FALSE; } fread(&NumOfPrimitives, 4, 1, F); fread(MtlFile, 1, 300, F); VG4_RndLoadMaterials(MtlFile); /* Allocate mnemory for primitives */ if ((Obj->Prims = malloc(sizeof(vg4PRIM) * NumOfPrimitives)) == NULL) { fclose(F); return FALSE; } Obj->NumOfPrims = NumOfPrimitives; for (p = 0; p < NumOfPrimitives; p++) { /* Read primitive info */ fread(&NumOfV, 4, 1, F); fread(&NumOfI, 4, 1, F); fread(Mtl, 1, 300, F); /* Allocate memory for primitive */ if ((V = malloc(sizeof(vg4VERTEX) * NumOfV + sizeof(INT) * NumOfI)) == NULL) { while (p-- > 0) { glBindVertexArray(Obj->Prims[p].VA); glBindBuffer(GL_ARRAY_BUFFER, 0); glDeleteBuffers(1, &Obj->Prims[p].VBuf); glBindVertexArray(0); glDeleteVertexArrays(1, &Obj->Prims[p].VA); glDeleteBuffers(1, &Obj->Prims[p].IBuf); } free(Obj->Prims); memset(Obj, 0, sizeof(vg4OBJ)); fclose(F); return FALSE; } I = (INT *)(V + NumOfV); Obj->Prims[p].NumOfI = NumOfI; Obj->Prims[p].M = MatrIdentity(); Obj->Prims[p].MtlNo = VG4_RndFindMaterial(Mtl); fread(V, sizeof(vg4VERTEX), NumOfV, F); fread(I, sizeof(INT), NumOfI, F); /* Create OpenGL buffers */ glGenVertexArrays(1, &Obj->Prims[p].VA); glGenBuffers(1, &Obj->Prims[p].VBuf); glGenBuffers(1, &Obj->Prims[p].IBuf); /* Activate vertex array */ glBindVertexArray(Obj->Prims[p].VA); /* Activate vertex buffer */ glBindBuffer(GL_ARRAY_BUFFER, Obj->Prims[p].VBuf); /* Store vertex data */ glBufferData(GL_ARRAY_BUFFER, sizeof(vg4VERTEX) * NumOfV, V, GL_STATIC_DRAW); /* Setup data order */ /* layout, * components count, * type * should be normalize, * vertex structure size in bytes (stride), * offset in bytes to field start */ glVertexAttribPointer(0, 3, GL_FLOAT, FALSE, sizeof(vg4VERTEX), (VOID *)0); /* position */ glVertexAttribPointer(1, 2, GL_FLOAT, FALSE, sizeof(vg4VERTEX), (VOID *)sizeof(VEC)); /* texture coordinates */ glVertexAttribPointer(2, 3, GL_FLOAT, FALSE, sizeof(vg4VERTEX), (VOID *)(sizeof(VEC) + sizeof(VEC2))); /* normal */ glVertexAttribPointer(3, 4, GL_FLOAT, FALSE, sizeof(vg4VERTEX), (VOID *)(sizeof(VEC) * 2 + sizeof(VEC2))); /* color */ /* Enable used attributes */ glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); glEnableVertexAttribArray(3); /* Indices */ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Obj->Prims[p].IBuf); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(INT) * NumOfI, I, GL_STATIC_DRAW); /* Disable vertex array */ glBindVertexArray(0); free(V); } fclose(F); return TRUE; } /* End of 'VG4_RndObjLoad' function */
DrawRectShader::DrawRectShader() : OpenGLShaderProgram("drawrect") { GetLocations(); glGenBuffers(1, &_vbo); glGenBuffers(1, &_vboInstances); glGenVertexArrays(1, &_vao); glBindBuffer(GL_ARRAY_BUFFER, _vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(VertexData), VertexData, GL_STATIC_DRAW); glBindVertexArray(_vao); glVertexAttribPointer(vVertMat + 0, 2, GL_FLOAT, GL_FALSE, sizeof(VDStruct), (void*)offsetof(VDStruct, mat[0])); glVertexAttribPointer(vVertMat + 1, 2, GL_FLOAT, GL_FALSE, sizeof(VDStruct), (void*)offsetof(VDStruct, mat[1])); glVertexAttribPointer(vVertMat + 2, 2, GL_FLOAT, GL_FALSE, sizeof(VDStruct), (void*)offsetof(VDStruct, mat[2])); glVertexAttribPointer(vVertMat + 3, 2, GL_FLOAT, GL_FALSE, sizeof(VDStruct), (void*)offsetof(VDStruct, mat[3])); glVertexAttribPointer(vVertVec, 2, GL_FLOAT, GL_FALSE, sizeof(VDStruct), (void*)offsetof(VDStruct, vec)); glBindBuffer(GL_ARRAY_BUFFER, _vboInstances); glVertexAttribIPointer(vClip, 4, GL_INT, sizeof(DrawRectCommand), (void*)offsetof(DrawRectCommand, clip)); glVertexAttribIPointer( vTexColourAtlas, 1, GL_INT, sizeof(DrawRectCommand), (void*)offsetof(DrawRectCommand, texColourAtlas)); glVertexAttribPointer( vTexColourBounds, 4, GL_FLOAT, GL_FALSE, sizeof(DrawRectCommand), (void*)offsetof(DrawRectCommand, texColourBounds)); glVertexAttribIPointer(vTexMaskAtlas, 1, GL_INT, sizeof(DrawRectCommand), (void*)offsetof(DrawRectCommand, texMaskAtlas)); glVertexAttribPointer( vTexMaskBounds, 4, GL_FLOAT, GL_FALSE, sizeof(DrawRectCommand), (void*)offsetof(DrawRectCommand, texMaskBounds)); glVertexAttribIPointer(vPalettes, 3, GL_INT, sizeof(DrawRectCommand), (void*)offsetof(DrawRectCommand, palettes)); glVertexAttribIPointer(vFlags, 1, GL_INT, sizeof(DrawRectCommand), (void*)offsetof(DrawRectCommand, flags)); glVertexAttribIPointer(vColour, 1, GL_UNSIGNED_INT, sizeof(DrawRectCommand), (void*)offsetof(DrawRectCommand, colour)); glVertexAttribIPointer(vBounds, 4, GL_INT, sizeof(DrawRectCommand), (void*)offsetof(DrawRectCommand, bounds)); glVertexAttribIPointer(vDepth, 1, GL_INT, sizeof(DrawRectCommand), (void*)offsetof(DrawRectCommand, depth)); glEnableVertexAttribArray(vVertMat + 0); glEnableVertexAttribArray(vVertMat + 1); glEnableVertexAttribArray(vVertMat + 2); glEnableVertexAttribArray(vVertMat + 3); glEnableVertexAttribArray(vVertVec); glEnableVertexAttribArray(vClip); glEnableVertexAttribArray(vTexColourAtlas); glEnableVertexAttribArray(vTexColourBounds); glEnableVertexAttribArray(vTexMaskAtlas); glEnableVertexAttribArray(vTexMaskBounds); glEnableVertexAttribArray(vPalettes); glEnableVertexAttribArray(vFlags); glEnableVertexAttribArray(vColour); glEnableVertexAttribArray(vBounds); glEnableVertexAttribArray(vDepth); glVertexAttribDivisor(vClip, 1); glVertexAttribDivisor(vTexColourAtlas, 1); glVertexAttribDivisor(vTexColourBounds, 1); glVertexAttribDivisor(vTexMaskAtlas, 1); glVertexAttribDivisor(vTexMaskBounds, 1); glVertexAttribDivisor(vPalettes, 1); glVertexAttribDivisor(vFlags, 1); glVertexAttribDivisor(vColour, 1); glVertexAttribDivisor(vBounds, 1); glVertexAttribDivisor(vDepth, 1); Use(); glUniform1i(uTexture, 0); glUniform1i(uPaletteTex, 1); glUniform1i(uPeelingTex, 2); glUniform1i(uPeeling, 0); }
void CoordinateSystemsComponent::Initialize() { std::vector<GLfloat> color{ 0.2f, 0.3f, 0.3f, 1.0f }; //Background color mVertices = { //FRONT FACE //Positions //Texture Coords. 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // Top Right = 0 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // Bottom Right = 1 -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // Bottom Left = 2 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // Top Right = 0 -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // Bottom Left = 2 -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, // Top Left = 3 //BACK FACE 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // Top Right = 0 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, // Bottom Right = 1 -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // Bottom Left = 2 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // Top Right = 0 -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // Bottom Left = 2 -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // Top Left = 3 //RIGHT FACE 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, 0.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, //LEFT FACE -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, 0.0f, 0.0f, -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, //TOP FACE 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, 0.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, //BOTTOM FACE 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, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f }; mCubePositions = { glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(2.0f, 5.0f, -15.0f), glm::vec3(-1.5f, -2.2f, -2.5f), glm::vec3(-3.8f, -2.0f, -12.3f), glm::vec3(2.4f, -0.4f, -3.5f), glm::vec3(-1.7f, 3.0f, -7.5f), glm::vec3(1.3f, -2.0f, -2.5f), glm::vec3(1.5f, 2.0f, -2.5f), glm::vec3(1.5f, 0.2f, -1.5f), glm::vec3(-1.3f, 1.0f, -1.5f) }; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, mVertices.size() * sizeof(GLfloat), &mVertices[0], GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0); glEnableVertexAttribArray(0); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); glEnableVertexAttribArray(2); glBindVertexArray(0); mShader = Shader("shaders/vertexshader.vert", "shaders/fragmentshader.frag"); //---------- TEXTURE ----------// mTextureContainer.SetProgram(mShader.Program()); mTextureContainer.BindTexture(GL_TEXTURE0); mTextureContainer.SetTextureParameter(GL_TEXTURE_WRAP_S, GL_REPEAT); mTextureContainer.SetTextureParameter(GL_TEXTURE_WRAP_T, GL_REPEAT); mTextureContainer.SetTextureParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); mTextureContainer.SetTextureParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR); mTextureContainer.LoadTexture("res\\container.jpg"); mTextureContainer.GenerateTexture(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE); mTextureContainer.GenerateMipmaps(); mTextureContainer.FreeImageData(); mTextureContainer.UnbindTexture(); mTextureAwesomeFace.SetProgram(mShader.Program()); mTextureAwesomeFace.BindTexture(GL_TEXTURE1); mTextureAwesomeFace.SetTextureParameter(GL_TEXTURE_WRAP_S, GL_REPEAT); mTextureAwesomeFace.SetTextureParameter(GL_TEXTURE_WRAP_T, GL_REPEAT); mTextureAwesomeFace.SetTextureParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); mTextureAwesomeFace.SetTextureParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR); mTextureAwesomeFace.LoadTexture("res\\awesomeface.png"); mTextureAwesomeFace.GenerateTexture(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE); mTextureAwesomeFace.GenerateMipmaps(); mTextureAwesomeFace.FreeImageData(); mTextureAwesomeFace.UnbindTexture(); }
void startup(void) { int i, j; load_shaders(); vmath::vec4 * initial_positions = new vmath::vec4 [POINTS_TOTAL]; vmath::vec3 * initial_velocities = new vmath::vec3 [POINTS_TOTAL]; vmath::ivec4 * connection_vectors = new vmath::ivec4 [POINTS_TOTAL]; int n = 0; for (j = 0; j < POINTS_Y; j++) { float fj = (float)j / (float)POINTS_Y; for (i = 0; i < POINTS_X; i++) { float fi = (float)i / (float)POINTS_X; initial_positions[n] = vmath::vec4((fi - 0.5f) * (float)POINTS_X, (fj - 0.5f) * (float)POINTS_Y, 0.6f * sinf(fi) * cosf(fj), 1.0f); initial_velocities[n] = vmath::vec3(0.0f); connection_vectors[n] = vmath::ivec4(-1); if (j != (POINTS_Y - 1)) { if (i != 0) connection_vectors[n][0] = n - 1; if (j != 0) connection_vectors[n][1] = n - POINTS_X; if (i != (POINTS_X - 1)) connection_vectors[n][2] = n + 1; if (j != (POINTS_Y - 1)) connection_vectors[n][3] = n + POINTS_X; } n++; } } glGenVertexArrays(2, m_vao); glGenBuffers(5, m_vbo); for (i = 0; i < 2; i++) { glBindVertexArray(m_vao[i]); glBindBuffer(GL_ARRAY_BUFFER, m_vbo[POSITION_A + i]); glBufferData(GL_ARRAY_BUFFER, POINTS_TOTAL * sizeof(vmath::vec4), initial_positions, GL_DYNAMIC_COPY); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, m_vbo[VELOCITY_A + i]); glBufferData(GL_ARRAY_BUFFER, POINTS_TOTAL * sizeof(vmath::vec3), initial_velocities, GL_DYNAMIC_COPY); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, m_vbo[CONNECTION]); glBufferData(GL_ARRAY_BUFFER, POINTS_TOTAL * sizeof(vmath::ivec4), connection_vectors, GL_STATIC_DRAW); glVertexAttribIPointer(2, 4, GL_INT, 0, NULL); glEnableVertexAttribArray(2); } delete [] connection_vectors; delete [] initial_velocities; delete [] initial_positions; glGenTextures(2, m_pos_tbo); glBindTexture(GL_TEXTURE_BUFFER, m_pos_tbo[0]); glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, m_vbo[POSITION_A]); glBindTexture(GL_TEXTURE_BUFFER, m_pos_tbo[1]); glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, m_vbo[POSITION_B]); int lines = (POINTS_X - 1) * POINTS_Y + (POINTS_Y - 1) * POINTS_X; glGenBuffers(1, &m_index_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_index_buffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, lines * 2 * sizeof(int), NULL, GL_STATIC_DRAW); int * e = (int *)glMapBufferRange(GL_ELEMENT_ARRAY_BUFFER, 0, lines * 2 * sizeof(int), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT); for (j = 0; j < POINTS_Y; j++) { for (i = 0; i < POINTS_X - 1; i++) { *e++ = i + j * POINTS_X; *e++ = 1 + i + j * POINTS_X; } } for (i = 0; i < POINTS_X; i++) { for (j = 0; j < POINTS_Y - 1; j++) { *e++ = i + j * POINTS_X; *e++ = POINTS_X + i + j * POINTS_X; } } glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); }
// Here is where the real meat of the OSD setup happens. The mesh topology is // created and stored for later use. Actual subdivision happens in updateGeom // which gets called at the end of this function and on frame change. // void createOsdMesh(int level, int kernel) { // // Setup an OsdHbr mesh based on the desired subdivision scheme // static OpenSubdiv::HbrCatmarkSubdivision<OpenSubdiv::OsdVertex> _catmark; OpenSubdiv::OsdHbrMesh * hmesh(new OpenSubdiv::OsdHbrMesh(&_catmark)); // // Now that we have a mesh, we need to add verticies and define the topology. // Here, we've declared the raw vertex data in-line, for simplicity // float verts[] = { 0.000000f, -1.414214f, 1.000000f, 1.414214f, 0.000000f, 1.000000f, -1.414214f, 0.000000f, 1.000000f, 0.000000f, 1.414214f, 1.000000f, -1.414214f, 0.000000f, -1.000000f, 0.000000f, 1.414214f, -1.000000f, 0.000000f, -1.414214f, -1.000000f, 1.414214f, 0.000000f, -1.000000f }; // // The cube faces are also in-lined, here they are specified as quads // int faces[] = { 0,1,3,2, 2,3,5,4, 4,5,7,6, 6,7,1,0, 1,7,5,3, 6,0,2,4 }; // // Record the original vertex positions and add verts to the mesh. // // OsdVertex is really just a place holder, it doesn't care what the // position of the vertex is, it's just being used here as a means of // defining the mesh topology. // for (unsigned i = 0; i < sizeof(verts)/sizeof(float); i += 3) { g_orgPositions.push_back(verts[i+0]); g_orgPositions.push_back(verts[i+1]); g_orgPositions.push_back(verts[i+2]); OpenSubdiv::OsdVertex vert; hmesh->NewVertex(i/3, vert); } // // Now specify the actual mesh topology by processing the faces array // const unsigned VERTS_PER_FACE = 4; for (unsigned i = 0; i < sizeof(faces)/sizeof(int); i += VERTS_PER_FACE) { // // Do some sanity checking. It is a good idea to keep this in your // code for your personal sanity as well. // // Note that this loop is not changing the HbrMesh, it's purely validating // the topology that is about to be created below. // for (unsigned j = 0; j < VERTS_PER_FACE; j++) { OpenSubdiv::OsdHbrVertex * origin = hmesh->GetVertex(faces[i+j]); OpenSubdiv::OsdHbrVertex * destination = hmesh->GetVertex(faces[i+((j+1)%VERTS_PER_FACE)]); OpenSubdiv::OsdHbrHalfedge * opposite = destination->GetEdge(origin); if(origin==NULL || destination==NULL) { std::cerr << " An edge was specified that connected a nonexistent vertex" << std::endl; exit(1); } if(origin == destination) { std::cerr << " An edge was specified that connected a vertex to itself" << std::endl; exit(1); } if(opposite && opposite->GetOpposite() ) { std::cerr << " A non-manifold edge incident to more than 2 faces was found" << std::endl; exit(1); } if(origin->GetEdge(destination)) { std::cerr << " An edge connecting two vertices was specified more than once." " It's likely that an incident face was flipped" << std::endl; exit(1); } } // // Now, create current face given the number of verts per face and the // face index data. // OpenSubdiv::OsdHbrFace * face = hmesh->NewFace(VERTS_PER_FACE, faces+i, 0); // // If you had ptex data, you would set it here, for example // /* face->SetPtexIndex(ptexIndex) */ } // // Apply some tags to drive the subdivision algorithm. Here we set the // default boundary interpolation mode along with a corner sharpness. See // the API and the renderman spec for the full list of available operations. // hmesh->SetInterpolateBoundaryMethod( OpenSubdiv::OsdHbrMesh::k_InterpolateBoundaryEdgeOnly ); OpenSubdiv::OsdHbrVertex * v = hmesh->GetVertex(0); v->SetSharpness(2.7f); // // Finalize the mesh object. The Finish() call is a signal to the internals // that optimizations can be made on the mesh data. // hmesh->Finish(); // // Setup some raw vectors of data. Remember that the actual point values were // not stored in the OsdVertex, so we keep track of them here instead // g_normals.resize(g_orgPositions.size(),0.0f); calcNormals( hmesh, g_orgPositions, g_normals ); // // At this point, we no longer need the topological structure of the mesh, // so we bake it down into subdivision tables by converting the HBR mesh // into an OSD mesh. Note that this is just storing the initial subdivision // tables, which will be used later during the actual subdivision process. // // Again, no vertex positions are being stored here, the point data will be // sent to the mesh in updateGeom(). // g_osdmesh = new OpenSubdiv::OsdMesh(); g_osdmesh->Create(hmesh, level, kernel); delete hmesh; // // Initialize the index and vertex buffers // g_elementArrayBuffer = g_osdmesh->CreateElementArrayBuffer(level); g_vertexBuffer = g_osdmesh->InitializeVertexBuffer(6 /* 3 floats for position, + 3 floats for normal*/ ); // // Setup camera positioning based on object bounds. This really has nothing // to do with OSD. // computeCenterAndSize(g_orgPositions, g_center, &g_size); // // Finally, make an explicit call to updateGeom() to force creation of the // initial buffer objects for the first draw call. // updateGeom(); // // The OsdVertexBuffer provides GL identifiers which can be bound in the // standard way. Here we setup a single VAO and enable points and normals // as attributes on the vertex buffer and set the index buffer. // GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, g_vertexBuffer->GetGpuBuffer()); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof (GLfloat) * 6, 0); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof (GLfloat) * 6, (float*)12); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_elementArrayBuffer->GetGlBuffer()); glBindBuffer(GL_ARRAY_BUFFER, 0); }
static void LoadWoodenCrateAsset() { gWoodenCrate.shaders = LoadShaders("vertex-shader.txt", "fragment-shader.txt"); gWoodenCrate.drawType = GL_TRIANGLES; gWoodenCrate.drawStart = 0; gWoodenCrate.drawCount = 6 * 2 * 3; gWoodenCrate.texture = LoadTexture("wooden-crate.jpg"); gWoodenCrate.shininess = 80.0; gWoodenCrate.specularColor = glm::vec3(1.0f, 1.0f, 1.0f); glGenBuffers(1, &gWoodenCrate.vbo); glBindBuffer(GL_ARRAY_BUFFER, gWoodenCrate.vbo); glGenVertexArrays(1, &gWoodenCrate.vao); glBindVertexArray(gWoodenCrate.vao); // Make a cube out of triangles (two triangles per side) GLfloat vertexData[] = { // X Y Z U V Normal // bottom -1.0f,-1.0f,-1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, -1.0f,-1.0f, 1.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,-1.0f, 1.0f, 1.0f, 1.0f, 0.0f, -1.0f, 0.0f, -1.0f,-1.0f, 1.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, // top -1.0f, 1.0f,-1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,-1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,-1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, // front -1.0f,-1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,-1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,-1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, // back -1.0f,-1.0f,-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, -1.0f, 1.0f,-1.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, -1.0f, 1.0f,-1.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,-1.0f, 1.0f, 1.0f, 0.0f, 0.0f, -1.0f, // left -1.0f,-1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f,-1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f,-1.0f,-1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f,-1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f,-1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, // right 1.0f,-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,-1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,-1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f }; glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW); // connect the xyz to the "vert" attribute of the vertex shader glEnableVertexAttribArray(gWoodenCrate.shaders->attrib("vert")); glVertexAttribPointer(gWoodenCrate.shaders->attrib("vert"), 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), NULL); // connect the uv coords to the "vertTexCoord" attribute of the vertex shader glEnableVertexAttribArray(gWoodenCrate.shaders->attrib("vertTexCoord")); glVertexAttribPointer(gWoodenCrate.shaders->attrib("vertTexCoord"), 2, GL_FLOAT, GL_TRUE, 8 * sizeof(GLfloat), (const GLvoid*)(3 * sizeof(GLfloat))); glEnableVertexAttribArray(gWoodenCrate.shaders->attrib("vertNormal")); glVertexAttribPointer(gWoodenCrate.shaders->attrib("vertNormal"), 3, GL_FLOAT, GL_TRUE, 8 * sizeof(GLfloat), (const GLvoid*)(5 * sizeof(GLfloat))); // unbind the VAO glBindVertexArray(0); }
void Mandlebrot::init(void) { // Create and compile our GLSL program from the shaders init_prog = LoadShaders("/home/steve/fractals/fractals/second_mandlebrot/passthrough.vert", "/home/steve/fractals/fractals/second_mandlebrot/init.frag"); if (!init_prog) exit(-1); kern_prog = LoadShaders("/home/steve/fractals/fractals/second_mandlebrot/passthrough.vert", "/home/steve/fractals/fractals/second_mandlebrot/mand_kern.frag"); if (!kern_prog) exit(-1); show_prog = LoadShaders("/home/steve/fractals/fractals/second_mandlebrot/passthrough.vert", "/home/steve/fractals/fractals/second_mandlebrot/mand_show.frag"); if (!show_prog) exit(-1); // Make a square. glGenVertexArrays(1, &handles["init_VAO"]); glBindVertexArray(handles["init_VAO"]); glGenBuffers(1, &handles["init_VBO"]); glBindBuffer(GL_ARRAY_BUFFER, handles["init_VBO"]); glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(0); glGenBuffers(1, &handles["init_uv_VBO"]); glBindBuffer(GL_ARRAY_BUFFER, handles["init_uv_VBO"]); glBufferData(GL_ARRAY_BUFFER, sizeof(g_textcoords), g_textcoords, GL_DYNAMIC_DRAW); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(1); glBindVertexArray(0); // glDeleteBuffers(1, &handles["init_VBO"]); // glDeleteBuffers(1, &handles["init_uv_VBO"]); glGenVertexArrays(1, &handles["kern_VAO"]); glBindVertexArray(handles["kern_VAO"]); glGenBuffers(1, &handles["kern_VBO"]); glBindBuffer(GL_ARRAY_BUFFER, handles["kern_VBO"]); glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(0); glGenBuffers(1, &handles["kern_uv_VBO"]); glBindBuffer(GL_ARRAY_BUFFER, handles["kern_uv_VBO"]); glBufferData(GL_ARRAY_BUFFER, sizeof(g_textcoords), g_textcoords, GL_DYNAMIC_DRAW); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(1); glBindVertexArray(0); // glDeleteBuffers(1, &handles["kern_VBO"]); // glDeleteBuffers(1, &handles["kern_uv_VBO"]); // Create a 1-d texture to use as a color palette. const GLubyte g_colors[][4] = {{0xff, 0x88, 0, 0xff}, {0x88, 0xcc, 0x22, 0xff}, {0x00, 0xff, 0x44, 0xff}, {0x88, 0xcc, 0x66, 0xff}, {0xff, 0x88, 0x88, 0xff}, {0x88, 0x44, 0xaa, 0xff}, {0xff, 0x00, 0xcc, 0xff}, {0x88, 0x44, 0xff, 0xff}, {0x00, 0x88, 0xcc, 0xff}, {0x88, 0xcc, 0xaa, 0xff}, {0xff, 0xff, 0x88, 0xff}, {0x88, 0xcc, 0x66, 0xff}, {0x00, 0x88, 0x44, 0xff}}; glGenTextures(1, &handles["colors"]); glBindTexture(GL_TEXTURE_1D, handles["colors"]); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 12, 0, GL_RGBA, GL_UNSIGNED_BYTE, g_colors); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); handles["aspect_ratio"] = glGetUniformLocationARB(init_prog, "aspect"); handles["scale"] = glGetUniformLocation(init_prog, "scale"); handles["center"] = glGetUniformLocationARB(init_prog, "center"); handles["in_c_loc"] = glGetUniformLocation(kern_prog, "in_c"); handles["in_z_loc"] = glGetUniformLocation(kern_prog, "in_z"); handles["iters2do_loc"] = glGetUniformLocation(kern_prog, "iters_to_do"); handles["colors_loc"] = glGetUniformLocation(show_prog, "colors"); handles["iter_loc"] = glGetUniformLocationARB(show_prog, "iter"); handles["loc_loc"] = glGetUniformLocation(show_prog, "location"); glBindBuffer(GL_ARRAY_BUFFER, 0); // generate a framebuffer to render to glGenFramebuffers(1, &handles["fbo_init"]); glBindFramebuffer(GL_FRAMEBUFFER, handles["fbo_init"]); glGenTextures(1, &handles["tex_init"]); glBindTexture(GL_TEXTURE_2D, handles["tex_init"]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RG32F, 1024, 768, 0, GL_RG, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, handles["tex_init"], 0); // Always check that our framebuffer is ok if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { std::cout << "framebuffer was not properly initialized. \n"; exit(1); } // Set the list of draw buffers. glGenFramebuffers(1, &handles["fbo_kern"]); glBindFramebuffer(GL_FRAMEBUFFER, handles["fbo_kern"]); glGenTextures(1, &handles["tex_kern1"]); glBindTexture(GL_TEXTURE_2D, handles["tex_kern1"]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 1024, 768, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, handles["tex_kern1"], 0); glBindFramebuffer(GL_FRAMEBUFFER, 0); print_handles(handles); }
void FaceCullingComponent::Initialize() { // Vertices defined in counter-clockwise winding order. mVertices = { // Back face -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // Bottom-left 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, // bottom-right 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // bottom-left -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left // Front face -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // bottom-right 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // top-right 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // top-right -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, // top-left -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left // Left face -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-right -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-left -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-left -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-left -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-right -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-right // Right face 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-left 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-right 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-right 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-left 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left // Bottom face -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // top-right 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, // top-left 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // bottom-left 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // bottom-left -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-right -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // top-right // Top face -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // bottom-right 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // bottom-right -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left -0.5f, 0.5f, 0.5f, 0.0f, 0.0f // bottom-left }; mCubePositions = { glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(2.0f, 5.0f, -15.0f), glm::vec3(-1.5f, -2.2f, -2.5f), glm::vec3(-3.8f, -2.0f, -12.3f), glm::vec3(2.4f, -0.4f, -3.5f), glm::vec3(-1.7f, 3.0f, -7.5f), glm::vec3(1.3f, -2.0f, -2.5f), glm::vec3(1.5f, 2.0f, -2.5f), glm::vec3(1.5f, 0.2f, -1.5f), glm::vec3(-1.3f, 1.0f, -1.5f) }; mPointLightPositions = { glm::vec3(0.7f, 0.2f, 2.0f), glm::vec3(2.3f, -3.3f, -4.0f), glm::vec3(-4.0f, 2.0f, -12.0f), glm::vec3(0.0f, 0.0f, -3.0f) }; //Generate VBOs and VAO. glGenVertexArrays(1, &mVAO); glGenBuffers(1, &mVBO); //Bind VAO and buffers, and fill in buffer data glBindVertexArray(mVAO); glBindBuffer(GL_ARRAY_BUFFER, mVBO); glBufferData(GL_ARRAY_BUFFER, mVertices.size() * sizeof(GLfloat), &mVertices[0], GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); int texWidth, texHeight; glActiveTexture(GL_TEXTURE0); glGenTextures(1, &mTextureDiffuse); glBindTexture(GL_TEXTURE_2D, mTextureDiffuse); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); unsigned char* diffuseMap = SOIL_load_image("res/diffuseMap.bmp", &texWidth, &texHeight, nullptr, SOIL_LOAD_AUTO); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, diffuseMap); glGenerateMipmap(GL_TEXTURE_2D); SOIL_free_image_data(diffuseMap); glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE1); glGenTextures(1, &mTextureSpecular); glBindTexture(GL_TEXTURE_2D, mTextureSpecular); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); unsigned char* specularMap = SOIL_load_image("res/specularMap.bmp", &texWidth, &texHeight, nullptr, SOIL_LOAD_AUTO); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, specularMap); glGenerateMipmap(GL_TEXTURE_2D); SOIL_free_image_data(specularMap); glBindTexture(GL_TEXTURE_2D, 0); //Unbind glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); mShaderContainer = Shader("shaders/container.vert", "shaders/container.frag"); mShaderLight = Shader("shaders/light.vert", "shaders/light.frag"); }
int main(int argc, char** argv) { if (!glfwInit()) // 初始化glfw库 { std::cout << "Error::GLFW could not initialize GLFW!" << std::endl; return -1; } // 开启OpenGL 3.3 core profile std::cout << "Start OpenGL core profile version 3.3" << std::endl; glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); glfwWindowHint(GLFW_SAMPLES, 4); // 设置采样点个数 注意这里设置GLFW选项 不要写成了GL_SAMPLES // 创建窗口 GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo of anti-aliasing(press O on, F off)", NULL, NULL); if (!window) { std::cout << "Error::GLFW could not create winddow!" << std::endl; glfwTerminate(); std::system("pause"); return -1; } // 创建的窗口的context指定为当前context glfwMakeContextCurrent(window); // 注册窗口键盘事件回调函数 glfwSetKeyCallback(window, key_callback); // 注册鼠标事件回调函数 glfwSetCursorPosCallback(window, mouse_move_callback); // 注册鼠标滚轮事件回调函数 glfwSetScrollCallback(window, mouse_scroll_callback); // 鼠标捕获 停留在程序内 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); // 初始化GLEW 获取OpenGL函数 glewExperimental = GL_TRUE; // 让glew获取所有拓展函数 GLenum status = glewInit(); if (status != GLEW_OK) { std::cout << "Error::GLEW glew version:" << glewGetString(GLEW_VERSION) << " error string:" << glewGetErrorString(status) << std::endl; glfwTerminate(); std::system("pause"); return -1; } // 设置视口参数 glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); //Section1 顶点属性数据 // 指定立方体顶点属性数据 顶点位置 纹理 GLfloat cubeVertices[] = { -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // A 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // B 0.5f, 0.5f, 0.5f,1.0f, 1.0f, // C 0.5f, 0.5f, 0.5f,1.0f, 1.0f, // C -0.5f, 0.5f, 0.5f,0.0f, 1.0f, // D -0.5f, -0.5f, 0.5f,0.0f, 0.0f, // A -0.5f, -0.5f, -0.5f,0.0f, 0.0f, // E -0.5f, 0.5f, -0.5f,0.0, 1.0f, // H 0.5f, 0.5f, -0.5f,1.0f, 1.0f, // G 0.5f, 0.5f, -0.5f,1.0f, 1.0f, // G 0.5f, -0.5f, -0.5f,1.0f, 0.0f, // F -0.5f, -0.5f, -0.5f,0.0f, 0.0f, // E -0.5f, 0.5f, 0.5f,0.0f, 1.0f, // D -0.5f, 0.5f, -0.5f,1.0, 1.0f, // H -0.5f, -0.5f, -0.5f,1.0f, 0.0f, // E -0.5f, -0.5f, -0.5f,1.0f, 0.0f, // E -0.5f, -0.5f, 0.5f,0.0f, 0.0f, // A -0.5f, 0.5f, 0.5f,0.0f, 1.0f, // D 0.5f, -0.5f, -0.5f,1.0f, 0.0f, // F 0.5f, 0.5f, -0.5f,1.0f, 1.0f, // G 0.5f, 0.5f, 0.5f,0.0f, 1.0f, // C 0.5f, 0.5f, 0.5f,0.0f, 1.0f, // C 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // B 0.5f, -0.5f, -0.5f,1.0f, 0.0f, // F 0.5f, 0.5f, -0.5f,1.0f, 1.0f, // G -0.5f, 0.5f, -0.5f,0.0, 1.0f, // H -0.5f, 0.5f, 0.5f,0.0f, 0.0f, // D -0.5f, 0.5f, 0.5f,0.0f, 0.0f, // D 0.5f, 0.5f, 0.5f,1.0f, 0.0f, // C 0.5f, 0.5f, -0.5f,1.0f, 1.0f, // G -0.5f, -0.5f, 0.5f,0.0f, 0.0f, // A -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,// E 0.5f, -0.5f, -0.5f,1.0f, 1.0f, // F 0.5f, -0.5f, -0.5f,1.0f, 1.0f, // F 0.5f, -0.5f, 0.5f,1.0f, 0.0f, // B -0.5f, -0.5f, 0.5f,0.0f, 0.0f, // A }; // Section2 准备缓存对象 GLuint cubeVAOId, cubeVBOId; glGenVertexArrays(1, &cubeVAOId); glGenBuffers(1, &cubeVBOId); glBindVertexArray(cubeVAOId); glBindBuffer(GL_ARRAY_BUFFER, cubeVBOId); glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW); // 顶点位置数据 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GL_FLOAT), (GLvoid*)0); glEnableVertexAttribArray(0); // 顶点纹理数据 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GL_FLOAT), (GLvoid*)(3 * sizeof(GL_FLOAT))); glEnableVertexAttribArray(1); glBindVertexArray(0); // Section3 准备着色器程序 Shader shader("scene.vertex", "scene.frag"); glEnable(GL_MULTISAMPLE); // 开启multisample glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_CULL_FACE); // 开始游戏主循环 while (!glfwWindowShouldClose(window)) { GLfloat currentFrame = (GLfloat)glfwGetTime(); deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; glfwPollEvents(); // 处理例如鼠标 键盘等事件 do_movement(); // 根据用户操作情况 更新相机属性 glClearColor(0.18f, 0.04f, 0.14f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shader.use(); glm::mat4 projection = glm::perspective(camera.mouse_zoom, (GLfloat)(WINDOW_WIDTH) / WINDOW_HEIGHT, 1.0f, 100.0f); // 投影矩阵 glm::mat4 view = camera.getViewMatrix(); // 视变换矩阵 glUniformMatrix4fv(glGetUniformLocation(shader.programId, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(glGetUniformLocation(shader.programId, "view"), 1, GL_FALSE, glm::value_ptr(view)); glm::mat4 model; model = glm::mat4(); model = glm::rotate(model, glm::radians(45.0f), glm::vec3(0.0f, 1.0f, 0.0f)); glUniformMatrix4fv(glGetUniformLocation(shader.programId, "model"), 1, GL_FALSE, glm::value_ptr(model)); // 这里填写场景绘制代码 glBindVertexArray(cubeVAOId); glDrawArrays(GL_TRIANGLES, 0, 36); glBindVertexArray(0); glBindVertexArray(0); glUseProgram(0); glfwSwapBuffers(window); // 交换缓存 } // 释放资源 glDeleteVertexArrays(1, &cubeVAOId); glDeleteBuffers(1, &cubeVBOId); glfwTerminate(); return 0; }