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

  • Home
  • SEARCH
  • 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 8185679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:56:01+00:00 2026-06-07T01:56:01+00:00

I have been playing around with trying to draw a 320 by 240 full

  • 0

I have been playing around with trying to draw a 320 by 240 full screen resolution image in opengl using java and lwjgl. I set the resolution to 640 by 480 and doubled the size of the pixels to fill in the space. After a lot of google searching I found some information about using the glDrawPixels function to speed up drawing to the screen. I wanted to test it by assigning random colors to all the pixels on the screen, but it wouldn’t fill the screen. I divided the width into 4 sections of 80 pixels each and colored them red, green, blue, and white. I saw that I was interleaving the colors but I can’t figure out how.

Here is an image of the output:

img

Here is where I run the openGL code:

// init OpenGL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 640, 0, 480, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);

while (!Display.isCloseRequested()) {
    pollInput();

    // Clear the screen and depth buffer
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);  



    randomizePixels();
    GL11.glRasterPos2i(0, 0);

    GL11.glDrawPixels(320, 240,GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE,buff);

    GL11.glPixelZoom(2, 2);


    Display.update();   
}
Display.destroy();
}

and here is where I create the pixel color data:

public void randomizePixels(){
    for(int y = 0; y < 240; y++){
        for(int x = 0; x < 320; x+=4){
            /*
            pixels[x * 320 + y] = (byte)(-128 + ran.nextInt(256));
            pixels[x * 320 + y + 1] = (byte)(-128 + ran.nextInt(256));
            pixels[x * 320 + y + 2] = (byte)(-128 + ran.nextInt(256));
            pixels[x * 320 + y + 3] = (byte)(-128 + ran.nextInt(256));
            */

            if(x >= 0 && x < 80){
                pixels[y * 240 + x] = (byte)128;
                pixels[y * 240 + x + 1] = (byte)0;
                pixels[y * 240 + x + 2] = (byte)0;
                pixels[y * 240 + x + 3] = (byte)128;
            }else if(x >= 80 && x < 160){
                pixels[y * 240 + x] = (byte)0;
                pixels[y * 240 + x + 1] = (byte)128;
                pixels[y * 240 + x + 2] = (byte)0;
                pixels[y * 240 + x + 3] = (byte)128;
            }else if(x >= 160 && x < 240){
                pixels[y * 240 + x] = (byte)0;
                pixels[y * 240 + x + 1] = (byte)0;
                pixels[y * 240 + x + 2] = (byte)128;
                pixels[y * 240 + x + 3] = (byte)128;
            }else if(x >= 240 && x < 320){
                pixels[y * 240 + x] = (byte)128;
                pixels[y * 240 + x + 1] = (byte)128;
                pixels[y * 240 + x + 2] = (byte)128;
                pixels[y * 240 + x + 3] = (byte)128;
            }

        }
    }
    buff.put(pixels).flip();
}

If you can figure out why I can’t get the pixels to line up to the x and y coordinates I want them to go to that would be great. I have read that glDrawPixels probably isn’t the best or fastest way to draw pixels to the screen, but I want to understand why I’m having this particular issue before I have to move on to some other method.

  • 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-06-07T01:56:02+00:00Added an answer on June 7, 2026 at 1:56 am

    I spot 2 issues in your randomizePixels().

    1. Indexing Pixel Buffer

    The total size of pixel buffer is 320x240x4 bytes because the pixel type is GL_RGBA. So, indexing each pixel with subscript operator, [], it would be;

    for(int y = 0; y < 240; y++)
    {
        for(int x = 0; x < 320; x++)
        {
            pixels[y * 320 * 4 + x * 4 + 0] = ... // R
            pixels[y * 320 * 4 + x * 4 + 1] = ... // G
            pixels[y * 320 * 4 + x * 4 + 2] = ... // B
            pixels[y * 320 * 4 + x * 4 + 3] = ... // A
        }
    }
    

    2. Colour Value

    The max intensity of 8bit colour is 255, for example, an opaque red pixel would be (255, 0, 0, 255).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been playing around with Google Apps Script today and I am trying
I have been playing around with NSURLConnection. Now I'm trying to grab some data
I have been playing around with the MVP pattern using winforms for the last
I have been playing around with using graphs to analyze big data. Its been
I am playing around with Audacity and have been trying to generate tones with
I've been playing around with OpenGL for a full week or equivalent. After 2D
So, I have been playing around with Amazon Cloud (AWS) and I am trying
I've been playing around with boost::spirit::qi lately and have been trying to write my
I have been playing around with tableviews for the past few days trying to
I have been playing around with my phonegap app in eclipse using android sdk

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.