Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6861145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:29:54+00:00 2026-05-27T02:29:54+00:00

I am trying to load and draw a 2d texture using OpenGL with GLFW

  • 0

I am trying to load and draw a 2d texture using OpenGL with GLFW and SOIL. I have this code, but I only get one solid color (which seems to come from the texture).

I have tested whether the .png loads with an example that came with SOIL, and it worked fine so there has to be some issue in my code.

This is my code:

#include <cstdio>

#include "GL/glfw.h"
#include "SOIL.h"

// function declarations
void drawscene();
void idlefunc();
void updatedisplay();

// global data
GLuint texture; // our example texture

int main(int argc, char **argv) {
    if (!glfwInit()) {
        fprintf(stderr, "Failed to initialize GLFW\n");
        return 1;
    }

    if (!glfwOpenWindow(640, 480, 0, 0, 0, 0, 16, 0, GLFW_WINDOW)) {
        fprintf(stderr, "Failed to open GLFW window\n");
        return 1;
    }

    // enable vsync (if available)
    glfwSwapInterval(1);

    // load textures
    texture = SOIL_load_OGL_texture(
        "tex.png",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_DDS_LOAD_DIRECT
    );

    // check for an error during the texture loading
    if (!texture) {
        printf("SOIL loading error: '%s'\n", SOIL_last_result());
    }

    while (glfwGetWindowParam(GLFW_OPENED)) {
        idlefunc();
    }

    // if we get here something went wrong
    return 0;
}

// this function gets called every frame
void idlefunc() {
    updatedisplay();
    drawscene();
}

// set up te display
void updatedisplay() {
    int screen_width, screen_height;
    glfwGetWindowSize(&screen_width, &screen_height);

    if (screen_height <= 0) screen_height = 1;
    if (screen_width <= 0) screen_width = 1;

    glViewport(0, 0, screen_width, screen_height);

    glClearColor(0.02f, 0.02f, 0.02f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glEnable(GL_TEXTURE_2D);
    glDisable(GL_DEPTH_TEST);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, screen_width, screen_height, 0.0, 0.0, 1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // displacement trick for exact pixelization
    glTranslatef(0.375f, 0.375f, 0.0f);
}

// draw the scene in this function
void drawscene() {
    glBindTexture(GL_TEXTURE_2D, texture);
    glPushMatrix();
        glTranslatef(10.0f, 10.0f, 0);
        glBegin(GL_QUADS);
            glTexCoord2f(0.0f, 0.0f);
            glVertex2f(0.0f, 0.0f);
            glTexCoord2f(0.0f, 128.0f);
            glVertex2f(0.0f, 128.0f);
            glTexCoord2f(128.0f, 128.0f);
            glVertex2f(128.0f, 128.0f);
            glTexCoord2f(128.0f, 0.0f);
            glVertex2f(128.0f, 0.0f);
        glEnd();
    glPopMatrix();

    glfwSwapBuffers();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:29:54+00:00Added an answer on May 27, 2026 at 2:29 am

    Found the issue (thanks to user786653). No matter the vertex coords, the tex coords are between 0.0 and 1.0. This is the fixed code:

    // draw the scene in this function
    void drawscene() {
        glBindTexture(GL_TEXTURE_2D, texture);
        glPushMatrix();
            glTranslatef(10.0f, 10.0f, 0);
            glBegin(GL_QUADS);
                glTexCoord2f(0.0f, 0.0f);
                glVertex2f(0.0f, 0.0f);
                glTexCoord2f(0.0f, 1.0f);
                glVertex2f(0.0f, 128.0f);
                glTexCoord2f(1.0f, 1.0f);
                glVertex2f(128.0f, 128.0f);
                glTexCoord2f(1.0f, 0.0f);
                glVertex2f(128.0f, 0.0f);
            glEnd();
        glPopMatrix();
    
        glfwSwapBuffers();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to draw grayscale image in color as texture in OpenGL using two
I'm trying to load fragments of XHTML markup using jQuery's $.fn.load function, but it
I'm trying to draw a polygon using c# and directx All I get is
In this code I'm trying to: get a bitmap from an ImageView save it
I'm trying to understand how to load a texture in OpenGL and I wrote
I've been trying to get a WebBrowser to draw to a texture in XNA
I have a problem with out of memory when I'm trying load a few
I am trying to load UIImage object from NSData , and the sample code
I'm trying to load spring beans using XmlWebApplicationContext setConfigLocations method. However, I keep getting
I have some data in mysql that I load up in php and draw

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.