Open Gl (Games)

  • Malik Salah
    Likes 0


  • Sonar Systems admin
    Likes 0

    Do you have a quesiton ?

  • mrstanlez
    Likes 0

    Another code that works fine in Codeblocks, but can not show any textures. No errors.

     

    #include <GL/glew.h>
    #include <GLFW/glfw3.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <SOIL.h>
    GLuint texture;

    static void error_callback(int error, const char* description)
    {
        fputs(description, stderr);
    }
    static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
    {
        if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
            glfwSetWindowShouldClose(window, GL_TRUE);
    }

    GLuint LoadTexture(std::string filename)
    {
       glGenTextures(1, &texture);
       glBindTexture(GL_TEXTURE_2D, texture);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

       int w=0, h=0;
       unsigned char *textura; // buffer for data file texture, JPEG is only RGB, not RGBA
       textura = SOIL_load_image(filename.c_str(), &w, &h, 0, SOIL_LOAD_RGB);
       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h,  0, GL_RGB, GL_UNSIGNED_BYTE, textura);
       printf("Texture parametres: w= %d, h= %d\n", w, h);

       SOIL_free_image_data(textura);
       glBindTexture(GL_TEXTURE_2D, 0);
       return texture;
    }

    int main(void)
    {
        glewInit();
        glewExperimental = GL_TRUE;
        LoadTexture("cs.jpg"); // is a JPEG image 128x128 in same directory as project.

        GLFWwindow* window;
        glfwSetErrorCallback(error_callback);
        if (!glfwInit())
            exit(EXIT_FAILURE);
        window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
        if (!window)
        {
            glfwTerminate();
            exit(EXIT_FAILURE);
        }
        glfwMakeContextCurrent(window);
        glfwSwapInterval(1);
        glfwSetKeyCallback(window, key_callback);
        while (!glfwWindowShouldClose(window))
        {
            float ratio;
            int width, height;
            glfwGetFramebufferSize(window, &width, &height);
            ratio = width / (float) height;
            glViewport(0, 0, width, height);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
            glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f);

            glEnable(GL_TEXTURE_2D);
            glBegin(GL_TRIANGLES);
              glTexCoord2f(0.0f, 0.0f);
              glColor3f(1.f, 0.f, 0.f);
              glVertex3f(-0.6f, -0.4f, 0.f);

              glTexCoord2f(1.0f, 0.0f);
              glColor3f(0.f, 1.f, 0.f);
              glVertex3f(0.6f, -0.4f, 0.f);

              glTexCoord2f(1.0, 1.0f);
              glColor3f(0.f, 0.f, 1.f);
              glVertex3f(0.f, 0.6f, 0.f);
            glEnd();

            glfwSwapBuffers(window);
            glfwPollEvents();
        }

        glfwDestroyWindow(window);
        glfwTerminate();
        exit(EXIT_SUCCESS);
    }

     

Login to reply