#include "opengl.h" #include "globals.h" #include "fifo.h" #include "report.h" GLuint createTexture(SDL_Surface *surface) { GLuint texture; GLenum format; GLint colors = surface->format->BytesPerPixel; if (colors == 4) { // with alpha if (surface->format->Rmask == 0x000000ff) { format = GL_RGBA; } else { format = GL_BGRA; } } else if (colors == 3) { if (surface->format->Rmask == 0x000000ff) { format = GL_RGB; } else { format = GL_BGR; } } else { // non truecolor, handle error or convert } // generate texture handle glGenTextures(1, &texture); // bind the texture object glBindTexture(GL_TEXTURE_2D, texture); // set stretching properties 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_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // mipmap, I guess //glGenerateMipmap(GL_TEXTURE_2D); // get the appropriate endianness for data packing int num = 1; int pack_type = ( (*(char*)&num == 1) ? GL_UNSIGNED_BYTE : GL_UNSIGNED_INT_8_8_8_8_REV); // unsigned char vs reversed uint? Doesn't seem right... // copy it over! :) glTexImage2D(GL_TEXTURE_2D, 0, colors, surface->w, surface->h, 0, format, pack_type, surface->pixels); return texture; } GLuint create1dTexture(SDL_Surface *surface) { GLenum format; GLint colors = surface->format->BytesPerPixel; if (colors == 4) { // with alpha if (surface->format->Rmask == 0x000000ff) { format = GL_RGBA; } else { format = GL_BGRA; } } else if (colors == 3) { if (surface->format->Rmask == 0x000000ff) { format = GL_RGB; } else { format = GL_BGR; } } else { // non truecolor, handle error or convert } GLuint texture; // generate texture handle glGenTextures(1, &texture); // bind 1D texture object glBindTexture(GL_TEXTURE_1D, texture); // set stretching properties glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT); // get endianness for data packing int num = 1; int pack_type = ( (*(char*)&num == 1) ? GL_UNSIGNED_BYTE : GL_UNSIGNED_INT_8_8_8_8_REV); // unsigned char vs reversed uint? Doesn't seem right... // copy image over glTexImage1D(GL_TEXTURE_1D, 0, colors, surface->w, 0, format, pack_type, surface->pixels); return texture; } // this should not go here, but it's the most accessible int reinitDisplay() { SDL_SetWindowSize(g_window, g_v_width, g_v_height); SDL_GL_SetSwapInterval(g_v_vsync); reinitGl(); } int reinitGl() { glViewport(0, 0, g_v_width, g_v_height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0f, g_v_width * g_v_zoom, g_v_height * g_v_zoom, 1.0f, -1.0f, 1.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); return 0; } /*int loadShader(const char *filename, GLchar **shader) { int ret; if ((ret = fileToBuffer(shader, filename)) < -1) { report(ERROR, "loadShader", "fileToBuffer returned %d", ret); free(*shader); *shader = NULL; return -1; } return 0; } int unloadShader(GLchar **shader) { if (shader != NULL) { free(*shader); } *shader = NULL; return 0; } // compile fragment shader GLuint compileFShader(GLchar *shader_source) { // create shader GLuint shader = glCreateShader(GL_FRAGMENT_SHADER); // load source into shader glShaderSource(shader, 1, &shader_source, NULL); // compile shader glCompileShader(shader); // check results GLint status; glGetShaderiv(shader, GL_COMPILE_STATUS, &status); if (status == 0) { GLint length; char *info; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length); info = SDL_stack_alloc(char, length+1); glGetShaderInfoLog(shader, length, NULL, info); report(ERROR, "compileFShader", "failed to compile:\n%s", info); SDL_stack_free(info); return 0; } else { } // return shader! return shader; } int deleteShader(GLuint shader) { glDeleteShader(shader); return 0; } GLuint shaderToProgram(GLuint shader) { GLuint program = glCreateProgram(); glAttachShader(program, shader); glLinkProgram(program); GLint status; glGetProgramiv(program, GL_LINK_STATUS, &status); if (status == 0) { report(ERROR, "shaderToProgram", "failed to link program: %d", status); return 0; } return program; } int deleteProgram(GLuint program) { glDeleteProgram(program); return 0; }*/