void LoadTexture(){ glGenTextures( 1, &texture); glBindTexture( GL_TEXTURE_2D, texture); imgdata image_data = loadJpeg("wooden-crate.jpg"); 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); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_data.width, image_data.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data.data); //free( imgdata ); GLuint utex= glGetUniformLocation(shader3dprogram, "tex"); if(utex == -1){ throw std::runtime_error(std::string("program uniform not found: tex")); } glUniform1i(utex, texture); }
int main(int argc, char *argv[]){ if(!glfwInit()){ throw std::runtime_error("glfwInit failed"); } glfwOpenWindowHint(0, GLFW_OPENGL_CORE_PROFILE); glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 1); glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE); if(!glfwOpenWindow(800, 600, 8, 8, 8, 8, 0, 0, GLFW_WINDOW)){ throw std::runtime_error("glfwOpenWindow failed. does this hardware work with 3.1"); } glewExperimental = GL_TRUE; if(glewInit() != GLEW_OK){ throw std::runtime_error("glewInit failed"); } if(!GLEW_VERSION_3_1){ throw std::runtime_error("OpenGL 3.1 API is not available"); } printf("loading vertex shader\n"); handleShader("vertex.shader", GL_VERTEX_SHADER); printf("loading fragment shader\n"); handleShader("fragment.shader", GL_FRAGMENT_SHADER); printf("Shaders loaded\n"); printf("Building shader program\n"); shaderprogram = glCreateProgram(); if(shaderprogram == 0){ throw std::runtime_error("glCreateProgram failed"); } glAttachShader(shaderprogram, vertshader); glAttachShader(shaderprogram, fragshader); glLinkProgram(shaderprogram); GLint status; glGetProgramiv(shaderprogram, GL_LINK_STATUS, &status); if(status == GL_FALSE){ std::string msg("Program linking failure: "); GLint infologlen; glGetProgramiv(shaderprogram, GL_INFO_LOG_LENGTH, &infologlen); char* strinfolog = new char[infologlen + 1]; glGetProgramInfoLog(shaderprogram, infologlen, NULL, strinfolog); msg += strinfolog; delete[] strinfolog; glDeleteProgram(shaderprogram); throw std::runtime_error(msg); } GLint vertprog = glGetAttribLocation(shaderprogram,"vert"); GLint verttexprog = glGetAttribLocation(shaderprogram,"vertTexCoord"); //bind VAO glGenVertexArrays(1, &cubeVAO); glBindVertexArray(cubeVAO); //bind VBO glGenBuffers(1, &cubeVBO); glBindBuffer(GL_ARRAY_BUFFER, cubeVBO); glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW); glEnableVertexAttribArray(vertprog); glVertexAttribPointer(vertprog, 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), NULL); glEnableVertexAttribArray(verttexprog); glVertexAttribPointer(verttexprog, 2, GL_FLOAT, GL_TRUE, 5*sizeof(GLfloat), (const GLvoid*)(3*sizeof(GLfloat))); uniform = glGetUniformLocation(shaderprogram, "tex"); glBindVertexArray(0); glGenTextures( 1, &texture); glBindTexture( GL_TEXTURE_2D, texture); imgdata image_data = loadJpeg("new.jpg"); //imgdata image_data = loadRaw("texture.bmp", 256, 256); 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); //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, image_data.data); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_data.width, image_data.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data.data); //free( imgdata ); while(glfwGetWindowParam(GLFW_OPENED)){ display(); } glfwTerminate(); return 0; }
bool UrlValue::loadImg(UrlValue_Img &img) const { return loadPng(img) || loadJpeg(img); }