bool ImGui_ImplSdl_CreateDeviceObjects() { // Backup GL state GLint last_texture, last_array_buffer; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer); const GLchar *vertex_shader = "uniform mat4 ProjMtx;\n" "attribute vec2 Position;\n" "attribute vec2 UV;\n" "attribute vec4 Color;\n" "varying vec2 Frag_UV;\n" "varying 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 = // WebGL requires precision specifiers but OpenGL 2.1 disallows // them, so I define the shader without it and then add it here. "precision mediump float;\n" "uniform sampler2D Texture;\n" "varying vec2 Frag_UV;\n" "varying vec4 Frag_Color;\n" "void main()\n" "{\n" " gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV);\n" "}\n"; g_ShaderHandle = glCreateProgram(); g_VertHandle = glCreateShader(GL_VERTEX_SHADER); g_FragHandle = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(g_VertHandle, 1, &vertex_shader, 0); glShaderSource(g_FragHandle, 1, &fragment_shader, 0); glCompileShader(g_VertHandle); glCompileShader(g_FragHandle); glAttachShader(g_ShaderHandle, g_VertHandle); glAttachShader(g_ShaderHandle, g_FragHandle); glLinkProgram(g_ShaderHandle); g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture"); g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx"); g_AttribLocationPosition = glGetAttribLocation(g_ShaderHandle, "Position"); g_AttribLocationUV = glGetAttribLocation(g_ShaderHandle, "UV"); g_AttribLocationColor = glGetAttribLocation(g_ShaderHandle, "Color"); glGenBuffers(1, &g_VboHandle); glGenBuffers(1, &g_ElementsHandle); ImGuiIO& io = ImGui::GetIO(); // Build texture unsigned char* pixels; int width, height; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bits for OpenGL3 demo because it is more likely to be compatible with user's existing shader. // Create OpenGL texture glGenTextures(1, &g_FontTexture); glBindTexture(GL_TEXTURE_2D, g_FontTexture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); // Store our identifier io.Fonts->TexID = (void *)(intptr_t)g_FontTexture; // Cleanup (don't clear the input data if you want to append new fonts later) io.Fonts->ClearInputData(); io.Fonts->ClearTexData(); // Restore modified GL state glBindTexture(GL_TEXTURE_2D, last_texture); glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); return true; }
void Object3D::setShaderProgram(const char *vertexShader, const char *fragmentShader){ GLuint vsSource, fsSource; vsSource = glCreateShader(GL_VERTEX_SHADER); fsSource = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fsSource, 1, &fragmentShader, 0); glShaderSource(vsSource, 1, &vertexShader, 0); glCompileShader(vsSource); glCompileShader(fsSource); GLint success = 0; glGetShaderiv(fsSource, GL_COMPILE_STATUS, &success); if (!success){ GLint maxLength = 0; glGetShaderiv(fsSource, GL_INFO_LOG_LENGTH, &maxLength); std::vector<GLchar> infolog(maxLength); glGetShaderInfoLog(fsSource, maxLength, &maxLength, &infolog[0]); std::cout << infolog.data() << std::endl; return; } success = 0; glGetShaderiv(vsSource, GL_COMPILE_STATUS, &success); if (!success){ GLint maxLength = 0; glGetShaderiv(vsSource, GL_INFO_LOG_LENGTH, &maxLength); std::vector<GLchar> infolog(maxLength); glGetShaderInfoLog(vsSource, maxLength, &maxLength, &infolog[0]); std::cout << infolog.data() << std::endl; return; } shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vsSource); glAttachShader(shaderProgram, fsSource); glLinkProgram(shaderProgram); glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); if (success == GL_FALSE){ GLint maxLength = 0; glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &maxLength); std::vector<GLchar> infolog(maxLength); glGetProgramInfoLog(shaderProgram, maxLength, &maxLength, &infolog[0]); std::cout << infolog.data() << std::endl; return; } glDetachShader(shaderProgram, vsSource); glDetachShader(shaderProgram, fsSource); attributeUV = glGetAttribLocation(shaderProgram, "vertexUV"); attributeTex = glGetUniformLocation(shaderProgram, "textureSampler"); hasShaderProgram = true; if (attributeUV == -1){ std::cout << "UV not bound. " << std::endl; hasShaderProgram = false; } if (attributeTex == -1){ std::cout << "Texture not bound. " << std::endl; hasShaderProgram = false; } }
GLint ShaderProgram::getAttribLocation(const GLchar *name) { return glGetAttribLocation(m_program, name); }
void piglit_init(int argc, char **argv) { GLfloat *vb; GLuint *ib; GLuint vbo; GLint OffsetAttrib; GLboolean user_va = GL_FALSE; int i; for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "user_varrays")) { user_va = GL_TRUE; puts("Testing user vertex arrays."); } } piglit_require_GLSL(); if (!user_va) piglit_require_extension("GL_ARB_vertex_buffer_object"); piglit_require_extension("GL_ARB_draw_instanced"); piglit_require_extension("GL_ARB_draw_elements_base_vertex"); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); if (!user_va) { glGenBuffersARB(1, &vbo); glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo); glBufferDataARB(GL_ARRAY_BUFFER_ARB, NUM_QUADS * 8 * sizeof(GLfloat) + 2 * 4 * sizeof(GLuint), NULL, GL_DYNAMIC_DRAW); vb = glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); } else { vb = malloc(NUM_QUADS * 8 * sizeof(GLfloat) + 2 * 4 * sizeof(GLuint)); } for (i = 0; i < NUM_QUADS; i++) { float x1 = 10; float y1 = 10 + i * 20; float x2 = 20; float y2 = 20 + i * 20; vb[i * 8 + 0] = x1; vb[i * 8 + 1] = y1; vb[i * 8 + 2] = x2; vb[i * 8 + 3] = y1; vb[i * 8 + 4] = x2; vb[i * 8 + 5] = y2; vb[i * 8 + 6] = x1; vb[i * 8 + 7] = y2; } ib_offset = NUM_QUADS * 8 * sizeof(GLfloat); ib = (GLuint *)((char *)vb + ib_offset); for (i = 0; i < 8; i++) ib[i] = i; if (user_va) { ib_offset = (uintptr_t)ib; } else { glUnmapBufferARB(GL_ARRAY_BUFFER_ARB); glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vbo); } glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(2, GL_FLOAT, 0, user_va ? vb : NULL); VertShader = piglit_compile_shader_text(GL_VERTEX_SHADER, VertShaderText); assert(VertShader); FragShader = piglit_compile_shader_text(GL_FRAGMENT_SHADER, FragShaderText); assert(FragShader); Program = piglit_link_simple_program(VertShader, FragShader); glUseProgram(Program); OffsetAttrib = glGetAttribLocation(Program, "yOffsetPerInstance"); glVertexAttrib1f(OffsetAttrib, 20); }
/** * Function for initialization. */ GLUSboolean init(GLUSvoid) { // Matrix for the model GLfloat model[16]; GLUSshape cube; GLUStgaimage image; GLUStextfile vertexSource; GLUStextfile fragmentSource; // Load the source of the vertex shader. glusLoadTextFile("../Example05/Vertex.vs", &vertexSource); // Load the source of the fragment shader. glusLoadTextFile("../Example05/Fragment.fs", &fragmentSource); // Build and ... glusBuildProgram(&g_program, (const GLUSchar**)&vertexSource.text, 0, (const GLUSchar**)&fragmentSource.text); // Destroy the text resource glusDestroyTextFile(&vertexSource); // Destroy the text resource glusDestroyTextFile(&fragmentSource); // ToDo: glGenVertexArrays(1, &g_vao); // ToDo: glBindVertexArray(g_vao); glusCreateCubef(&cube, 0.5f); numberIndices = cube.numberIndices; // http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml g_projectionLocation = glGetUniformLocation(g_program.program, "projectionMatrix"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml g_modelViewLocation = glGetUniformLocation(g_program.program, "modelViewMatrix"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml g_textureLocation = glGetUniformLocation(g_program.program, "firstTexture"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetAttribLocation.xml g_vertexLocation = glGetAttribLocation(g_program.program, "vertex"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetAttribLocation.xml g_normalLocation = glGetAttribLocation(g_program.program, "normal"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetAttribLocation.xml g_texCoordLocation = glGetAttribLocation(g_program.program, "texCoord"); // ToDo: glBindFragDataLocation(g_program.program, 0, "fragColor"); // http://www.opengl.org/sdk/docs/man/xhtml/glGenBuffers.xml glGenBuffers(1, &g_vertices); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_vertices); // http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml glBufferData(GL_ARRAY_BUFFER, cube.numberVertices*4*sizeof(GLfloat), (GLfloat*)cube.vertices, GL_STATIC_DRAW); // http://www.opengl.org/sdk/docs/man/xhtml/glGenBuffers.xml glGenBuffers(1, &g_normals); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_normals); // http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml glBufferData(GL_ARRAY_BUFFER, cube.numberVertices*3*sizeof(GLfloat), (GLfloat*)cube.normals, GL_STATIC_DRAW); // http://www.opengl.org/sdk/docs/man/xhtml/glGenBuffers.xml glGenBuffers(1, &g_texCoords); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_texCoords); // http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml glBufferData(GL_ARRAY_BUFFER, cube.numberVertices*2*sizeof(GLfloat), (GLfloat*)cube.texCoords, GL_STATIC_DRAW); // http://www.opengl.org/sdk/docs/man/xhtml/glGenBuffers.xml glGenBuffers(1, &g_indices); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indices); // http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml glBufferData(GL_ELEMENT_ARRAY_BUFFER, cube.numberIndices*sizeof(GLuint), (GLuint*)cube.indices, GL_STATIC_DRAW); glusDestroyShapef(&cube); glusLoadTgaImage("crate.tga", &image); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/gentextures.html glGenTextures(1, &g_texture); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/bindtexture.html glBindTexture(GL_TEXTURE_2D, g_texture); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/teximage2d.html glTexImage2D(GL_TEXTURE_2D, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusDestroyTgaImage(&image); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/texparameter.html glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/texparameter.html glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/texparameter.html glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/texparameter.html glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // http://www.opengl.org/sdk/docs/man/xhtml/glUseProgram.xml glUseProgram(g_program.program); // Calculate the model matrix ... glusLoadIdentityf(model); glusRotateRzRyRxf(model, 30.0f, 30.0f, 0.0f); // ... and the view matrix ... glusLookAtf(g_modelView, 0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); // ... to get the final model view matrix glusMultMatrixf(g_modelView, g_modelView, model); // http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml glUniformMatrix4fv(g_modelViewLocation, 1, GL_FALSE, g_modelView); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_vertices); // http://www.opengl.org/sdk/docs/man/xhtml/glVertexAttribPointer.xml glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); // http://www.opengl.org/sdk/docs/man/xhtml/glEnableVertexAttribArray.xml glEnableVertexAttribArray(g_vertexLocation); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_normals); // http://www.opengl.org/sdk/docs/man/xhtml/glVertexAttribPointer.xml glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); // http://www.opengl.org/sdk/docs/man/xhtml/glEnableVertexAttribArray.xml glEnableVertexAttribArray(g_normalLocation); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_texCoords); // http://www.opengl.org/sdk/docs/man/xhtml/glVertexAttribPointer.xml glVertexAttribPointer(g_texCoordLocation, 2, GL_FLOAT, GL_FALSE, 0, 0); // http://www.opengl.org/sdk/docs/man/xhtml/glEnableVertexAttribArray.xml glEnableVertexAttribArray(g_texCoordLocation); // http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml glUniform1i(g_textureLocation, 0); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/clearcolor.html glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/cleardepth.html glClearDepth(1.0f); //http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/enable.html glEnable(GL_DEPTH_TEST); //http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/enable.html glEnable(GL_CULL_FACE); return GLUS_TRUE; }
GLUSboolean init(GLUSvoid) { GLfloat* points = (GLfloat*) malloc(WATER_PLANE_LENGTH * WATER_PLANE_LENGTH * 4 * sizeof(GLfloat)); GLuint* indices = (GLuint*) malloc(WATER_PLANE_LENGTH * (WATER_PLANE_LENGTH - 1) * 2 * sizeof(GLuint)); GLUStgaimage image; GLUStextfile vertexSource; GLUStextfile fragmentSource; GLuint x, z, i, k; GLuint waterTexture; for (z = 0; z < WATER_PLANE_LENGTH; z++) { for (x = 0; x < WATER_PLANE_LENGTH; x++) { points[(x + z * (WATER_PLANE_LENGTH)) * 4 + 0] = -(GLfloat) WATER_PLANE_LENGTH / 2 + 0.5f + (GLfloat) x; points[(x + z * (WATER_PLANE_LENGTH)) * 4 + 1] = 0.0f; points[(x + z * (WATER_PLANE_LENGTH)) * 4 + 2] = +(GLfloat) WATER_PLANE_LENGTH / 2 - 0.5f - (GLfloat) z; points[(x + z * (WATER_PLANE_LENGTH)) * 4 + 3] = 1.0f; } } for (k = 0; k < WATER_PLANE_LENGTH - 1; k++) { for (i = 0; i < WATER_PLANE_LENGTH; i++) { if (k % 2 == 0) { indices[(i + k * (WATER_PLANE_LENGTH)) * 2 + 0] = i + (k + 1) * WATER_PLANE_LENGTH; indices[(i + k * (WATER_PLANE_LENGTH)) * 2 + 1] = i + k * WATER_PLANE_LENGTH; } else { indices[(i + k * (WATER_PLANE_LENGTH)) * 2 + 0] = WATER_PLANE_LENGTH - 1 - i + k * WATER_PLANE_LENGTH; indices[(i + k * (WATER_PLANE_LENGTH)) * 2 + 1] = WATER_PLANE_LENGTH - 1 - i + (k + 1) * WATER_PLANE_LENGTH; } } } // glusFileLoadText("../Example15_ES/shader/Water.vert.glsl", &vertexSource); glusFileLoadText("../Example15_ES/shader/Water.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_program, (const GLUSchar**) &vertexSource.text, (const GLUSchar**) &fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&fragmentSource); // g_projectionMatrixLocation = glGetUniformLocation(g_program.program, "u_projectionMatrix"); g_viewMatrixLocation = glGetUniformLocation(g_program.program, "u_viewMatrix"); g_inverseViewNormalMatrixLocation = glGetUniformLocation(g_program.program, "u_inverseViewNormalMatrix"); g_waterPlaneLengthLocation = glGetUniformLocation(g_program.program, "u_waterPlaneLength"); g_cubemapLocation = glGetUniformLocation(g_program.program, "u_cubemap"); g_waterTextureLocation = glGetUniformLocation(g_program.program, "u_waterTexture"); g_passedTimeLocation = glGetUniformLocation(g_program.program, "u_passedTime"); g_waveParametersLocation = glGetUniformLocation(g_program.program, "u_waveParameters"); g_waveDirectionsLocation = glGetUniformLocation(g_program.program, "u_waveDirections"); g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex"); // glGenBuffers(1, &g_verticesVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glBufferData(GL_ARRAY_BUFFER, WATER_PLANE_LENGTH * WATER_PLANE_LENGTH * 4 * sizeof(GLfloat), (GLfloat*) points, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &g_indicesVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, WATER_PLANE_LENGTH * (WATER_PLANE_LENGTH - 1) * 2 * sizeof(GLuint), (GLuint*) indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // free(points); free(indices); // glGenTextures(1, &g_cubemap); glBindTexture(GL_TEXTURE_CUBE_MAP, g_cubemap); glusImageLoadTga("water_pos_x.tga", &image); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusImageDestroyTga(&image); glusImageLoadTga("water_neg_x.tga", &image); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusImageDestroyTga(&image); glusImageLoadTga("water_pos_y.tga", &image); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusImageDestroyTga(&image); glusImageLoadTga("water_neg_y.tga", &image); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusImageDestroyTga(&image); glusImageLoadTga("water_pos_z.tga", &image); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusImageDestroyTga(&image); glusImageLoadTga("water_neg_z.tga", &image); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusImageDestroyTga(&image); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_CUBE_MAP, 0); // waterTexture = initWaterTexture((GLUSfloat) WATER_PLANE_LENGTH); glUseProgram(g_program.program); glUniform1f(g_waterPlaneLengthLocation, (GLUSfloat) WATER_PLANE_LENGTH); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_CUBE_MAP, g_cubemap); glUniform1i(g_cubemapLocation, 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, waterTexture); glUniform1i(g_waterTextureLocation, 1); glGenVertexArrays(1, &g_vao); glBindVertexArray(g_vao); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); // initBackground(); // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepthf(1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); return GLUS_TRUE; }
LinkedShader::LinkedShader(Shader *vs, Shader *fs, u32 vertType, bool useHWTransform, LinkedShader *previous) : useHWTransform_(useHWTransform), program(0), dirtyUniforms(0) { program = glCreateProgram(); glAttachShader(program, vs->shader); glAttachShader(program, fs->shader); // Bind attribute locations to fixed locations so that they're // the same in all shaders. We use this later to minimize the calls to // glEnableVertexAttribArray and glDisableVertexAttribArray. glBindAttribLocation(program, ATTR_POSITION, "position"); glBindAttribLocation(program, ATTR_TEXCOORD, "texcoord"); glBindAttribLocation(program, ATTR_NORMAL, "normal"); glBindAttribLocation(program, ATTR_W1, "w1"); glBindAttribLocation(program, ATTR_W2, "w2"); glBindAttribLocation(program, ATTR_COLOR0, "color0"); glBindAttribLocation(program, ATTR_COLOR1, "color1"); #ifndef USING_GLES2 if (gl_extensions.ARB_blend_func_extended) { // Dual source alpha glBindFragDataLocationIndexed(program, 0, 0, "fragColor0"); glBindFragDataLocationIndexed(program, 0, 1, "fragColor1"); } else if (gl_extensions.VersionGEThan(3, 3, 0)) { glBindFragDataLocation(program, 0, "fragColor0"); } #endif glLinkProgram(program); // Detaching shaders is annoying when debugging with gDebugger // so let's not do that on Windows. #ifdef USING_GLES glDetachShader(program, vs->shader); glDetachShader(program, fs->shader); #endif GLint linkStatus = GL_FALSE; glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); if (linkStatus != GL_TRUE) { GLint bufLength = 0; glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength); if (bufLength) { char* buf = new char[bufLength]; glGetProgramInfoLog(program, bufLength, NULL, buf); #ifdef ANDROID ELOG("Could not link program:\n %s", buf); #endif ERROR_LOG(G3D, "Could not link program:\n %s", buf); ERROR_LOG(G3D, "VS:\n%s", vs->source().c_str()); ERROR_LOG(G3D, "FS:\n%s", fs->source().c_str()); Reporting::ReportMessage("Error in shader program link: info: %s / fs: %s / vs: %s", buf, fs->source().c_str(), vs->source().c_str()); #ifdef SHADERLOG OutputDebugStringUTF8(buf); OutputDebugStringUTF8(vs->source().c_str()); OutputDebugStringUTF8(fs->source().c_str()); #endif delete [] buf; // we're dead! } // Prevent a buffer overflow. numBones = 0; return; } INFO_LOG(G3D, "Linked shader: vs %i fs %i", (int)vs->shader, (int)fs->shader); u_tex = glGetUniformLocation(program, "tex"); u_proj = glGetUniformLocation(program, "u_proj"); u_proj_through = glGetUniformLocation(program, "u_proj_through"); u_texenv = glGetUniformLocation(program, "u_texenv"); u_fogcolor = glGetUniformLocation(program, "u_fogcolor"); u_fogcoef = glGetUniformLocation(program, "u_fogcoef"); u_alphacolorref = glGetUniformLocation(program, "u_alphacolorref"); u_colormask = glGetUniformLocation(program, "u_colormask"); u_stencilReplaceValue = glGetUniformLocation(program, "u_stencilReplaceValue"); // Transform u_view = glGetUniformLocation(program, "u_view"); u_world = glGetUniformLocation(program, "u_world"); u_texmtx = glGetUniformLocation(program, "u_texmtx"); if (vertTypeGetWeightMask(vertType) != GE_VTYPE_WEIGHT_NONE) numBones = TranslateNumBones(vertTypeGetNumBoneWeights(vertType)); else numBones = 0; #ifdef USE_BONE_ARRAY u_bone = glGetUniformLocation(program, "u_bone"); #else for (int i = 0; i < 8; i++) { char name[10]; sprintf(name, "u_bone%i", i); u_bone[i] = glGetUniformLocation(program, name); } #endif // Lighting, texturing u_ambient = glGetUniformLocation(program, "u_ambient"); u_matambientalpha = glGetUniformLocation(program, "u_matambientalpha"); u_matdiffuse = glGetUniformLocation(program, "u_matdiffuse"); u_matspecular = glGetUniformLocation(program, "u_matspecular"); u_matemissive = glGetUniformLocation(program, "u_matemissive"); u_uvscaleoffset = glGetUniformLocation(program, "u_uvscaleoffset"); for (int i = 0; i < 4; i++) { char temp[64]; sprintf(temp, "u_lightpos%i", i); u_lightpos[i] = glGetUniformLocation(program, temp); sprintf(temp, "u_lightdir%i", i); u_lightdir[i] = glGetUniformLocation(program, temp); sprintf(temp, "u_lightatt%i", i); u_lightatt[i] = glGetUniformLocation(program, temp); sprintf(temp, "u_lightangle%i", i); u_lightangle[i] = glGetUniformLocation(program, temp); sprintf(temp, "u_lightspotCoef%i", i); u_lightspotCoef[i] = glGetUniformLocation(program, temp); sprintf(temp, "u_lightambient%i", i); u_lightambient[i] = glGetUniformLocation(program, temp); sprintf(temp, "u_lightdiffuse%i", i); u_lightdiffuse[i] = glGetUniformLocation(program, temp); sprintf(temp, "u_lightspecular%i", i); u_lightspecular[i] = glGetUniformLocation(program, temp); } attrMask = 0; if (-1 != glGetAttribLocation(program, "position")) attrMask |= 1 << ATTR_POSITION; if (-1 != glGetAttribLocation(program, "texcoord")) attrMask |= 1 << ATTR_TEXCOORD; if (-1 != glGetAttribLocation(program, "normal")) attrMask |= 1 << ATTR_NORMAL; if (-1 != glGetAttribLocation(program, "w1")) attrMask |= 1 << ATTR_W1; if (-1 != glGetAttribLocation(program, "w2")) attrMask |= 1 << ATTR_W2; if (-1 != glGetAttribLocation(program, "color0")) attrMask |= 1 << ATTR_COLOR0; if (-1 != glGetAttribLocation(program, "color1")) attrMask |= 1 << ATTR_COLOR1; availableUniforms = 0; if (u_proj != -1) availableUniforms |= DIRTY_PROJMATRIX; if (u_proj_through != -1) availableUniforms |= DIRTY_PROJTHROUGHMATRIX; if (u_texenv != -1) availableUniforms |= DIRTY_TEXENV; if (u_alphacolorref != -1) availableUniforms |= DIRTY_ALPHACOLORREF; if (u_colormask != -1) availableUniforms |= DIRTY_COLORMASK; if (u_fogcolor != -1) availableUniforms |= DIRTY_FOGCOLOR; if (u_fogcoef != -1) availableUniforms |= DIRTY_FOGCOEF; if (u_texenv != -1) availableUniforms |= DIRTY_TEXENV; if (u_uvscaleoffset != -1) availableUniforms |= DIRTY_UVSCALEOFFSET; if (u_world != -1) availableUniforms |= DIRTY_WORLDMATRIX; if (u_view != -1) availableUniforms |= DIRTY_VIEWMATRIX; if (u_texmtx != -1) availableUniforms |= DIRTY_TEXMATRIX; if (u_stencilReplaceValue != -1) availableUniforms |= DIRTY_STENCILREPLACEVALUE; // Looping up to numBones lets us avoid checking u_bone[i] for (int i = 0; i < numBones; i++) { if (u_bone[i] != -1) availableUniforms |= DIRTY_BONEMATRIX0 << i; } if (u_ambient != -1) availableUniforms |= DIRTY_AMBIENT; if (u_matambientalpha != -1) availableUniforms |= DIRTY_MATAMBIENTALPHA; if (u_matdiffuse != -1) availableUniforms |= DIRTY_MATDIFFUSE; if (u_matemissive != -1) availableUniforms |= DIRTY_MATEMISSIVE; if (u_matspecular != -1) availableUniforms |= DIRTY_MATSPECULAR; for (int i = 0; i < 4; i++) { if (u_lightdir[i] != -1 || u_lightspecular[i] != -1 || u_lightpos[i] != -1) availableUniforms |= DIRTY_LIGHT0 << i; } glUseProgram(program); // Default uniform values glUniform1i(u_tex, 0); // The rest, use the "dirty" mechanism. dirtyUniforms = DIRTY_ALL; use(vertType, previous); }
JNIEXPORT void JNICALL Java_com_qualcomm_QCARSamples_VideoPlayback_VideoPlaybackRenderer_initRendering( JNIEnv* env, jobject obj) { LOG("Java_com_qualcomm_QCARSamples_VideoPlayback_VideoPlaybackRenderer_initRendering"); // Define clear color glClearColor(0.0f, 0.0f, 0.0f, QCAR::requiresAlpha() ? 0.0f : 1.0f); // Now generate the OpenGL texture objects and add settings for (int i = 0; i < textureCount; ++i) { // Here we create the textures for the keyframe // and for all the icons glGenTextures(1, &(textures[i]->mTextureID)); glBindTexture(GL_TEXTURE_2D, textures[i]->mTextureID); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textures[i]->mWidth, textures[i]->mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) textures[i]->mData); } // Now we create the texture for the video data from the movie // IMPORTANT: // Notice that the textures are not typical GL_TEXTURE_2D textures // but instead are GL_TEXTURE_EXTERNAL_OES extension textures // This is required by the Android SurfaceTexture for (int i=0; i<NUM_TARGETS; i++) { glGenTextures(1, &videoPlaybackTextureID[i]); glBindTexture(GL_TEXTURE_EXTERNAL_OES, videoPlaybackTextureID[i]); glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0); } // The first shader is the one that will display the video data of the movie // (it is aware of the GL_TEXTURE_EXTERNAL_OES extension) videoPlaybackShaderID = SampleUtils::createProgramFromBuffer( videoPlaybackVertexShader, videoPlaybackFragmentShader); videoPlaybackVertexHandle = glGetAttribLocation(videoPlaybackShaderID, "vertexPosition"); videoPlaybackNormalHandle = glGetAttribLocation(videoPlaybackShaderID, "vertexNormal"); videoPlaybackTexCoordHandle = glGetAttribLocation(videoPlaybackShaderID, "vertexTexCoord"); videoPlaybackMVPMatrixHandle = glGetUniformLocation(videoPlaybackShaderID, "modelViewProjectionMatrix"); videoPlaybackTexSamplerOESHandle = glGetUniformLocation(videoPlaybackShaderID, "texSamplerOES"); // This is a simpler shader with regular 2D textures keyframeShaderID = SampleUtils::createProgramFromBuffer( keyframeVertexShader, keyframeFragmentShader); keyframeVertexHandle = glGetAttribLocation(keyframeShaderID, "vertexPosition"); keyframeNormalHandle = glGetAttribLocation(keyframeShaderID, "vertexNormal"); keyframeTexCoordHandle = glGetAttribLocation(keyframeShaderID, "vertexTexCoord"); keyframeMVPMatrixHandle = glGetUniformLocation(keyframeShaderID, "modelViewProjectionMatrix"); keyframeTexSampler2DHandle = glGetUniformLocation(keyframeShaderID, "texSampler2D"); keyframeQuadAspectRatio[STONES] = (float)textures[0]->mHeight / (float)textures[0]->mWidth; keyframeQuadAspectRatio[CHIPS] = (float)textures[1]->mHeight / (float)textures[1]->mWidth; }
bool initialize() { // Set light values spotLight.position[0] = 10.0f; spotLight.position[1] = 10.0f; spotLight.position[2] = 10.0f; spotLight.direction[0] = -1.0f; spotLight.direction[1] = -1.0f; spotLight.direction[2] = -1.0f; spotLight.color[0] = 1.0f; spotLight.color[1] = 1.0f; spotLight.color[2] = 1.0f; spotLight.fov = 30.0/180.0*M_PI; // Initialize geometry and shaders for this example //this defines a cube, this is why a model loader is nice //you can also do this with a draw elements and indices, try to get that working std::cout << "Obj file is loading this might take a moment. Please wait." << std::endl; if(!loadObj("dragon.obj", geometry, vertexCount)) { std::cerr << "[F] The obj file did not load correctly." << std::endl; return false; } // Create a Vertex Buffer object to store this vertex info on the GPU glGenBuffers(1, &vbo_geometry); glBindBuffer(GL_ARRAY_BUFFER, vbo_geometry); glBufferData(GL_ARRAY_BUFFER, vertexCount*sizeof(Vertex), geometry, GL_STATIC_DRAW); //--Geometry done GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); //Shader Sources // Put these into files and write a loader in the future // Note the added uniform! std::string vs = loadShader("VertexShader.txt"); std::string fs = loadShader("FragShader.txt"); //compile the shaders GLint shader_status; const char* _vs = vs.c_str(); const char* _fs = fs.c_str(); // Vertex shader first glShaderSource(vertex_shader, 1, &_vs, NULL); glCompileShader(vertex_shader); //check the compile status glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &shader_status); if(!shader_status) { std::cerr << "[F] FAILED TO COMPILE VERTEX SHADER!" << std::endl; return false; } // Now the Fragment shader glShaderSource(fragment_shader, 1, &_fs, NULL); glCompileShader(fragment_shader); //check the compile status glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &shader_status); if(!shader_status) { std::cerr << "[F] FAILED TO COMPILE FRAGMENT SHADER!" << std::endl; return false; } //Now we link the 2 shader objects into a program //This program is what is run on the GPU program = glCreateProgram(); glAttachShader(program, vertex_shader); glAttachShader(program, fragment_shader); glLinkProgram(program); //check if everything linked ok glGetProgramiv(program, GL_LINK_STATUS, &shader_status); if(!shader_status) { std::cerr << "[F] THE SHADER PROGRAM FAILED TO LINK" << std::endl; return false; } //Now we set the locations of the attributes and uniforms //this allows us to access them easily while rendering loc_position = glGetAttribLocation(program, const_cast<const char*>("v_position")); if(loc_position == -1) { std::cerr << "[F] POSITION NOT FOUND" << std::endl; return false; } loc_color = glGetAttribLocation(program, const_cast<const char*>("v_color")); if(loc_color == -1) { std::cerr << "[F] V_COLOR NOT FOUND" << std::endl; return false; } loc_norm = glGetAttribLocation(program, const_cast<const char*>("v_norm")); if(loc_norm == -1) { std::cerr << "[F] V_NORM NOT FOUND" << std::endl; return false; } loc_dp = glGetUniformLocation(program, const_cast<const char*>("DP")); if(loc_dp == -1) { std::cerr << "[F] DP NOT FOUND" << std::endl; return false; } loc_sp = glGetUniformLocation(program, const_cast<const char*>("SP")); if(loc_sp == -1) { std::cerr << "[F] SP NOT FOUND" << std::endl; return false; } loc_shininess = glGetUniformLocation(program, const_cast<const char*>("shininess")); if(loc_shininess == -1) { std::cerr << "[F] SHININESS NOT FOUND" << std::endl; return false; } loc_modelView = glGetUniformLocation(program, const_cast<const char*>("ModelView")); if(loc_modelView == -1) { std::cerr << "[F] MODELVIEW NOT FOUND" << std::endl; return false; } loc_projection = glGetUniformLocation(program, const_cast<const char*>("Projection")); if(loc_projection == -1) { std::cerr << "[F] PROJECTION NOT FOUND" << std::endl; return false; } loc_slColor = glGetUniformLocation(program, const_cast<const char*>("spotLight.color")); if(loc_slColor == -1) { std::cerr << "[F] SPOTLIGHTCOLOR NOT FOUND" << std::endl; return false; } loc_slPosition = glGetUniformLocation(program, const_cast<const char*>("spotLight.position")); if(loc_slPosition == -1) { std::cerr << "[F] SPOTLIGHTPOSITION NOT FOUND" << std::endl; return false; } loc_slDirection = glGetUniformLocation(program, const_cast<const char*>("spotLight.direction")); if(loc_slDirection == -1) { std::cerr << "[F] SPOTLIGHTDIRECTION NOT FOUND" << std::endl; return false; } loc_slFOV = glGetUniformLocation(program, const_cast<const char*>("spotLight.fov")); if(loc_slFOV == -1) { std::cerr << "[F] SPOTLIGHTFOV NOT FOUND" << std::endl; return false; } //--Init the view and projection matrices // if you will be having a moving camera the view matrix will need to more dynamic // ...Like you should update it before you render more dynamic // for this project having them static will be fine view = glm::lookAt( glm::vec3(0.0, 8.0, -16.0), //Eye Position glm::vec3(0.0, 0.0, 0.0), //Focus point glm::vec3(0.0, 1.0, 0.0)); //Positive Y is up projection = glm::perspective( 45.0f, //the FoV typically 90 degrees is good which is what this is set to float(w)/float(h), //Aspect Ratio, so Circles stay Circular 0.01f, //Distance to the near plane, normally a small value like this 100.0f); //Distance to the far plane, //enable depth testing glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); //and its done 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); }
GLint ShaderHandler::get_attrib_location(string variable) { return glGetAttribLocation(m_program, variable.c_str()); }
GLint OpenGLProgram::get_attrib_location(string variable) { return glGetAttribLocation(m_program, variable.c_str()); }
void init() { // Vertices of a square float ext = 1.0; float points[NumPoints * 4] = { -ext, -ext, 0, 1.0 , //v1 ext, -ext, 0, 1.0 , //v2 -ext, ext, 0, 1.0 , //v3 -ext, ext, 0, 1.0 , //v3 ext, -ext, 0, 1.0 , //v2 ext, ext, 0, 1.0 //v4 }; // Texture coordinates float tex_coords[NumPoints * 2] = { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0 }; // Initialize texture Objects float newFrame[WIN_HEIGHT][WIN_WIDTH][3]; for(int i=0;i<WIN_HEIGHT;i++) for(int j=0;j<WIN_WIDTH;j++){ newFrame[i][j][0] = frame[i][j].x; newFrame[i][j][1] = frame[i][j].y; newFrame[i][j][2] = frame[i][j].z; } GLuint texture; glGenTextures( 1, &texture ); glBindTexture( GL_TEXTURE_2D, texture ); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, WIN_WIDTH, WIN_HEIGHT, 0, GL_RGB, GL_FLOAT, newFrame ); 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_MAG_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glActiveTexture( GL_TEXTURE0 ); // Create and initialize a buffer Object GLuint buffer; glGenBuffers( 1, &buffer ); glBindBuffer( GL_ARRAY_BUFFER, buffer ); glBufferData( GL_ARRAY_BUFFER, sizeof(points) + sizeof(tex_coords), NULL, GL_STATIC_DRAW ); GLintptr offset = 0; glBufferSubData( GL_ARRAY_BUFFER, offset, sizeof(points), points ); offset += sizeof(points); glBufferSubData( GL_ARRAY_BUFFER, offset, sizeof(tex_coords), tex_coords ); // Load shaders and use the resulting shader program Shader myShader( "vshader.glsl", "fshader.glsl" ); myShader.Use(); // set up vertex arrays offset = 0; GLuint vPosition = glGetAttribLocation( myShader.Program, "vPosition" ); glEnableVertexAttribArray( vPosition ); glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid*) (offset) ); offset += sizeof(points); GLuint vTexCoord = glGetAttribLocation( myShader.Program, "vTexCoord" ); glEnableVertexAttribArray( vTexCoord ); glVertexAttribPointer( vTexCoord, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*) (offset) ); glUniform1i( glGetUniformLocation( myShader.Program, "texture"), 0 ); glClearColor( 1.0, 1.0, 1.0, 1.0 ); }
bool ImGui_ImplSdlGL3_CreateDeviceObjects() { // 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"; g_ShaderHandle = glCreateProgram(); g_VertHandle = glCreateShader(GL_VERTEX_SHADER); g_FragHandle = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(g_VertHandle, 1, &vertex_shader, 0); glShaderSource(g_FragHandle, 1, &fragment_shader, 0); glCompileShader(g_VertHandle); glCompileShader(g_FragHandle); glAttachShader(g_ShaderHandle, g_VertHandle); glAttachShader(g_ShaderHandle, g_FragHandle); glLinkProgram(g_ShaderHandle); g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture"); g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx"); g_AttribLocationPosition = glGetAttribLocation(g_ShaderHandle, "Position"); g_AttribLocationUV = glGetAttribLocation(g_ShaderHandle, "UV"); g_AttribLocationColor = glGetAttribLocation(g_ShaderHandle, "Color"); glGenBuffers(1, &g_VboHandle); glGenBuffers(1, &g_ElementsHandle); glGenVertexArrays(1, &g_VaoHandle); glBindVertexArray(g_VaoHandle); glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle); glEnableVertexAttribArray(g_AttribLocationPosition); glEnableVertexAttribArray(g_AttribLocationUV); glEnableVertexAttribArray(g_AttribLocationColor); #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT)) glVertexAttribPointer(g_AttribLocationPosition, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, pos)); glVertexAttribPointer(g_AttribLocationUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, uv)); glVertexAttribPointer(g_AttribLocationColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, col)); #undef OFFSETOF ImGui_ImplSdlGL3_CreateFontsTexture(); // Restore modified GL state glBindTexture(GL_TEXTURE_2D, last_texture); glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); glBindVertexArray(last_vertex_array); return true; }
int main(int argc, char** argv) { // Standard stuff... glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(1080, 600); glutCreateWindow("Lighted Cube"); glutReshapeFunc(changeViewport); glutKeyboardFunc(keyboardFunc); glutDisplayFunc(render); glewInit(); initMatrices(); // New <======================================== // Make a shader char* vertexShaderSourceCode = readFile("vertexShader.vsh"); char* fragmentShaderSourceCode = readFile("fragmentShader.fsh"); GLuint vertShaderID = makeVertexShader(vertexShaderSourceCode); GLuint fragShaderID = makeFragmentShader(fragmentShaderSourceCode); shaderProgramID = makeShaderProgram(vertShaderID, fragShaderID); // Create the "remember all" glGenVertexArrays(1, &vao); glBindVertexArray(vao); glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); // Create the buffer, but don't load anything yet //glBufferData(GL_ARRAY_BUFFER, 7*NUM_VERTICES*sizeof(GLfloat), NULL, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, 6 * NUM_VERTICES*sizeof(GLfloat), NULL, GL_STATIC_DRAW); // NEW! - we're only loading vertices and normals (6 elements, not 7) // Load the vertex points glBufferSubData(GL_ARRAY_BUFFER, 0, 3 * NUM_VERTICES*sizeof(GLfloat), vertices); // Load the colors right after that //glBufferSubData(GL_ARRAY_BUFFER, 3*NUM_VERTICES*sizeof(GLfloat),4*NUM_VERTICES*sizeof(GLfloat), colors); glBufferSubData(GL_ARRAY_BUFFER, 3 * NUM_VERTICES*sizeof(GLfloat), 3 * NUM_VERTICES*sizeof(GLfloat), normals); #ifdef USING_INDEX_BUFFER glGenBuffers(1, &indexBufferID); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID); glBufferData(GL_ELEMENT_ARRAY_BUFFER, NUM_INDICES*sizeof(GLuint), indices, GL_STATIC_DRAW); #endif // Find the position of the variables in the shader positionID = glGetAttribLocation(shaderProgramID, "s_vPosition"); normalID = glGetAttribLocation(shaderProgramID, "s_vNormal"); // NEW lightID1 = glGetUniformLocation(shaderProgramID, "vLight1"); // NEW lightID2 = glGetUniformLocation(shaderProgramID, "vLight2"); // NEW // ============ New! glUniformLocation is how you pull IDs for uniform variables=============== perspectiveMatrixID = glGetUniformLocation(shaderProgramID, "mP"); viewMatrixID = glGetUniformLocation(shaderProgramID, "mV"); modelMatrixID = glGetUniformLocation(shaderProgramID, "mM"); allRotsMatrixID = glGetUniformLocation(shaderProgramID, "mRotations"); // NEW //============================================================================================= glVertexAttribPointer(positionID, 3, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(normalID, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(vertices))); glUseProgram(shaderProgramID); glEnableVertexAttribArray(positionID); glEnableVertexAttribArray(normalID); // NEW glEnable(GL_CULL_FACE); // NEW! - we're doing real 3D now glCullFace(GL_BACK); // Other options? GL_FRONT and GL_FRONT_AND_BACK glEnable(GL_DEPTH_TEST); // Make sure the depth buffer is on. As you draw a pixel, update the screen only if it's closer than previous ones glutMainLoop(); return 0; }
void init() { gProgram = glCreateProgram(); { GLuint shader = glCreateShader(GL_VERTEX_SHADER); const GLchar* source = ReadShader("vertex-shader.txt"); if (source == NULL) { return; } glShaderSource(shader, 1, &source, NULL); delete[] source; glCompileShader(shader); GLint status; glGetShaderiv(shader, GL_COMPILE_STATUS, &status); if (status == GL_FALSE) { std::string msg("Compile failure in shader:\n"); GLint infoLogLength; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength); char* strInfoLog = new char[infoLogLength + 1]; glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog); msg += strInfoLog; delete[] strInfoLog; glDeleteShader(shader); shader = 0; throw std::runtime_error(msg); } glAttachShader(gProgram, shader); } { GLuint shader = glCreateShader(GL_FRAGMENT_SHADER); const GLchar* source = ReadShader("fragment-shader.txt"); if (source == NULL) { return; } glShaderSource(shader, 1, &source, NULL); delete[] source; glCompileShader(shader); GLint status; glGetShaderiv(shader, GL_COMPILE_STATUS, &status); if (status == GL_FALSE) { std::string msg("Compile failure in shader:\n"); GLint infoLogLength; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength); char* strInfoLog = new char[infoLogLength + 1]; glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog); msg += strInfoLog; delete[] strInfoLog; glDeleteShader(shader); shader = 0; throw std::runtime_error(msg); } glAttachShader(gProgram, shader); } glLinkProgram(gProgram); //glDetachShader(program, shader); GLint linkStatus; glGetProgramiv(gProgram, GL_LINK_STATUS, &linkStatus); if (linkStatus == GL_FALSE) { std::string msg("Program linking failure: "); GLint infoLogLength; glGetProgramiv(gProgram, GL_INFO_LOG_LENGTH, &infoLogLength); char* strInfoLog = new char[infoLogLength + 1]; glGetProgramInfoLog(gProgram, infoLogLength, NULL, strInfoLog); msg += strInfoLog; delete[] strInfoLog; glDeleteProgram(gProgram); gProgram = 0; throw std::runtime_error(msg); } glUseProgram(gProgram); /* glm::mat4 projection = glm::perspective(glm::radians(50.0f), 800.0f / 600.0f, 0.1f, 10.0f); GLint uniformProjection = glGetUniformLocation(gProgram, "projection"); if (uniformProjection == -1) throw std::runtime_error(std::string("Program uniform not found: ") + "projection"); glUniformMatrix4fv(uniformProjection, 1, GL_FALSE, glm::value_ptr(projection)); glm::mat4 camera = glm::lookAt(glm::vec3(3, 3, 3), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0)); //glm::mat4 camera = glm::lookAt(glm::vec3(0.5, 0.5, 0.5), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0)); GLint uniformCamera = glGetUniformLocation(gProgram, "camera"); if (uniformCamera == -1) throw std::runtime_error(std::string("Program uniform not found: ") + "camera"); glUniformMatrix4fv(uniformCamera, 1, GL_FALSE, glm::value_ptr(camera)); */ int width, height, channels; unsigned char* pixels = stbi_load("wooden-crate.jpg", &width, &height, &channels, 0); if (!pixels) { throw std::runtime_error(stbi_failure_reason()); } unsigned long rowSize = channels *width; unsigned char* rowBuffer = new unsigned char[rowSize]; unsigned halfRows = height / 2; for (unsigned rowIdx = 0; rowIdx < halfRows; ++rowIdx) { unsigned char* row = pixels + (rowIdx * width + 0) * channels;//GetPixelOffset(0, rowIdx, _width, _height, _format); unsigned char* oppositeRow = pixels + ((height - rowIdx - 1) * width + 0) * channels;//GetPixelOffset(0, _height - rowIdx - 1, _width, _height, _format); memcpy(rowBuffer, row, rowSize); memcpy(row, oppositeRow, rowSize); memcpy(oppositeRow, rowBuffer, rowSize); } delete rowBuffer; glGenTextures(1, &gTex); glBindTexture(GL_TEXTURE_2D, gTex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); GLint internalformat; switch (channels) { case 1: internalformat = GL_LUMINANCE; break; case 2: internalformat = GL_LUMINANCE_ALPHA; break; case 3: internalformat = GL_RGB; break; case 4: internalformat = GL_RGBA; break; default: throw std::runtime_error("Unrecognised Bitmap::Format"); } glTexImage2D(GL_TEXTURE_2D, 0, internalformat, (GLsizei)width, (GLsizei)height, 0, internalformat, GL_UNSIGNED_BYTE, pixels); glBindTexture(GL_TEXTURE_2D, 0); stbi_image_free(pixels); // make and bind the VAO glGenVertexArrays(1, &gVAO); glBindVertexArray(gVAO); // make and bind the VBO glGenBuffers(1, &gVBO); glBindBuffer(GL_ARRAY_BUFFER, gVBO); 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); GLint attrib = glGetAttribLocation(gProgram, "vert"); if (attrib == -1) throw std::runtime_error(std::string("Program attribute not found: ") + "vert"); glEnableVertexAttribArray(attrib); glVertexAttribPointer(attrib, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), NULL); GLint vertTexCoord = glGetAttribLocation(gProgram, "vertTexCoord"); if (vertTexCoord == -1) throw std::runtime_error(std::string("Program attribute not found: ") + "vertTexCoord"); glEnableVertexAttribArray(vertTexCoord); glVertexAttribPointer(vertTexCoord, 2, GL_FLOAT, GL_TRUE, 5 * sizeof(GLfloat), (const GLvoid*)(3 * sizeof(GLfloat))); glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glClearColor(0.196078431372549f, 0.3137254901960784f, 0.5882352941176471f, 1); }
void RiftAppSkeleton::display_client() //const { ovrHmd hmd = m_Hmd; if (hmd == NULL) return; //ovrFrameTiming hmdFrameTiming = ovrHmd_BeginFrameTiming(hmd, 0); bindFBO(m_renderBuffer, m_fboScale); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); for (int eyeIndex = 0; eyeIndex < ovrEye_Count; eyeIndex++) { const ovrEyeType eye = hmd->EyeRenderOrder[eyeIndex]; const ovrPosef eyePose = ovrHmd_GetEyePose(hmd, eye); m_eyeOri = eyePose.Orientation; // cache this for movement direction _StoreHmdPose(eyePose); const ovrGLTexture& otex = l_EyeTexture[eye]; const ovrRecti& rvp = otex.OGL.Header.RenderViewport; const ovrRecti rsc = { static_cast<int>(m_fboScale * rvp.Pos.x), static_cast<int>(m_fboScale * rvp.Pos.y), static_cast<int>(m_fboScale * rvp.Size.w), static_cast<int>(m_fboScale * rvp.Size.h) }; glViewport(rsc.Pos.x, rsc.Pos.y, rsc.Size.w, rsc.Size.h); const OVR::Matrix4f proj = ovrMatrix4f_Projection( m_EyeRenderDesc[eye].Fov, 0.01f, 10000.0f, true); ///@todo Should we be using this variable? //m_EyeRenderDesc[eye].DistortedViewport; const OVR::Matrix4f view = _MakeModelviewMatrix( eyePose, m_EyeRenderDesc[eye].ViewAdjust, m_chassisYaw, m_chassisPos); const OVR::Matrix4f scaledView = _MakeModelviewMatrix( eyePose, m_EyeRenderDesc[eye].ViewAdjust, m_chassisYaw, m_chassisPos, m_headSize); _resetGLState(); _DrawScenes(&view.Transposed().M[0][0], &proj.Transposed().M[0][0], rsc, &scaledView.Transposed().M[0][0]); } unbindFBO(); // Set full viewport...? const int w = m_Cfg.OGL.Header.RTSize.w; const int h = m_Cfg.OGL.Header.RTSize.h; glViewport(0, 0, w, h); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); // Now draw the distortion mesh... for(int eyeNum = 0; eyeNum < 2; eyeNum++) { const ShaderWithVariables& eyeShader = eyeNum == 0 ? m_presentDistMeshL : m_presentDistMeshR; const GLuint prog = eyeShader.prog(); glUseProgram(prog); //glBindVertexArray(eyeShader.m_vao); { const ovrDistortionMesh& mesh = m_DistMeshes[eyeNum]; glBindBuffer(GL_ARRAY_BUFFER, 0); const int a_pos = glGetAttribLocation(prog, "vPosition"); glVertexAttribPointer(a_pos, 4, GL_FLOAT, GL_FALSE, sizeof(ovrDistortionVertex), &mesh.pVertexData[0].ScreenPosNDC.x); glEnableVertexAttribArray(a_pos); const int a_texR = glGetAttribLocation(prog, "vTexR"); if (a_texR > -1) { glVertexAttribPointer(a_texR, 2, GL_FLOAT, GL_FALSE, sizeof(ovrDistortionVertex), &mesh.pVertexData[0].TanEyeAnglesR); glEnableVertexAttribArray(a_texR); } const int a_texG = glGetAttribLocation(prog, "vTexG"); if (a_texG > -1) { glVertexAttribPointer(a_texG, 2, GL_FLOAT, GL_FALSE, sizeof(ovrDistortionVertex), &mesh.pVertexData[0].TanEyeAnglesG); glEnableVertexAttribArray(a_texG); } const int a_texB = glGetAttribLocation(prog, "vTexB"); if (a_texB > -1) { glVertexAttribPointer(a_texB, 2, GL_FLOAT, GL_FALSE, sizeof(ovrDistortionVertex), &mesh.pVertexData[0].TanEyeAnglesB); glEnableVertexAttribArray(a_texB); } ovrVector2f uvoff = m_uvScaleOffsetOut[2*eyeNum + 1]; //DistortionData.UVScaleOffset[eyeNum][0]; ovrVector2f uvscale = m_uvScaleOffsetOut[2*eyeNum + 0]; //DistortionData.UVScaleOffset[eyeNum][1]; glUniform2f(eyeShader.GetUniLoc("EyeToSourceUVOffset"), uvoff.x, uvoff.y); glUniform2f(eyeShader.GetUniLoc("EyeToSourceUVScale"), uvscale.x, uvscale.y); #if 0 // Setup shader constants DistortionData.Shaders->SetUniform2f( "EyeToSourceUVScale", DistortionData.UVScaleOffset[eyeNum][0].x, DistortionData.UVScaleOffset[eyeNum][0].y); DistortionData.Shaders->SetUniform2f( "EyeToSourceUVOffset", DistortionData.UVScaleOffset[eyeNum][1].x, DistortionData.UVScaleOffset[eyeNum][1].y); if (distortionCaps & ovrDistortionCap_TimeWarp) { // TIMEWARP - Additional shader constants required ovrMatrix4f timeWarpMatrices[2]; ovrHmd_GetEyeTimewarpMatrices(HMD, (ovrEyeType)eyeNum, eyeRenderPoses[eyeNum], timeWarpMatrices); //WARNING!!! These matrices are transposed in SetUniform4x4f, before being used by the shader. DistortionData.Shaders->SetUniform4x4f("EyeRotationStart", Matrix4f(timeWarpMatrices[0])); DistortionData.Shaders->SetUniform4x4f("EyeRotationEnd", Matrix4f(timeWarpMatrices[1])); } // Perform distortion pRender->Render( &distortionShaderFill, DistortionData.MeshVBs[eyeNum], DistortionData.MeshIBs[eyeNum]); #endif glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_renderBuffer.tex); glUniform1i(eyeShader.GetUniLoc("fboTex"), 0); // This is the only uniform that changes per-frame glUniform1f(eyeShader.GetUniLoc("fboScale"), m_fboScale); glDrawElements( GL_TRIANGLES, mesh.IndexCount, GL_UNSIGNED_SHORT, &mesh.pIndexData[0]); } glBindVertexArray(0); glUseProgram(0); } ovrHmd_EndFrameTiming(hmd); }