Jump to content
  • Advertisement
Sign in to follow this  
nextman

glDrawPixels help needed

This topic is 3815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone. I'm a beginner in opengl and c++, and i wanted to create an example for using gldrawpixels. My source compiles but runs into exception. I wanted to draw a 600x600 window, with each pixel set to 0.5 in red,greenn, and blue. Here is the source, can you tell me, what is the problem, and how should i do it correctly?: Thanks, Peter #include <math.h> #include <stdlib.h> #ifdef WIN32 #include <windows.h> #endif #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> void Init( ) { glViewport(0, 0, 600, 600); glMatrixMode(GL_MODELVIEW); glLoadIdentity( ); glMatrixMode(GL_PROJECTION); glLoadIdentity( ); gluOrtho2D(0., 600., 0., 600.); } void onDisplay( ) { glClearColor(1, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); // // struct RGBType {float r; float g; float b;}; RGBType pixels[600][600]; for (int i=0;i<599;i++) for(int j=0;j<599;j++) { pixels[j].r=0.5; pixels[j].g=0.5; pixels[j].b=0.5; } //RGBType * pix;pix=&pixels; glDrawPixels(600,600,GL_RGB,GL_FLOAT,pixels); glutSwapBuffers(); } void onMousePress(int button, int state, int x, int y) { } void onIdle( ) { } void onKeyboard(unsigned char key, int x, int y) { } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ void main(int argc, char * argv[]) { glutInit(&argc, argv); glutInitWindowSize(600, 600); glutInitWindowPosition(100, 100); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("Grafika hazi feladat"); Init(); glutDisplayFunc(onDisplay); glutMouseFunc(onMousePress); glutIdleFunc(onIdle); glutKeyboardFunc(onKeyboard); glutMainLoop(); }

Share this post


Link to post
Share on other sites
Advertisement
Hi nextman.

If you're getting an unhandled exception, you need to tell us where in your code the exception it was thrown and what the exception was.

Also, shouldn't this:


RGBType pixels[600][600];
for (int i=0;i<599;i++)
for(int j=0;j<599;j++) {
pixels[j].r=0.5;
pixels[j].g=0.5;
pixels[j].b=0.5;
}


be this?


RGBType pixels[600][600];
for (int i=0;i<600;i++)
for(int j=0;j<600;j++) {
pixels[j].r=0.5;
pixels[j].g=0.5;
pixels[j].b=0.5;
}

Share this post


Link to post
Share on other sites
Hi nextman,

there were a couple of small problems with your code.

1. You created a two-dimensional array "RGBType pixels[600][600]", but you treated it like it was just a normal one-dimensional array. You can't do that :). A 2D array is actually an array of arrays.

2. The program still crashed because of a stack overflow. That happens when you create something that takes a large amount of memory, but don't do it properly. In C++, you should use "new" and "delete" for big things like your 600x600 pixel array. If you want a technical explanation, google for the terms "heap" and "stack".

Below is the fixed code. It should compile, and show a beautiful grey screen without problems :). Hope this helps!

-juuso


#include <math.h>
#include <stdlib.h>
#ifdef WIN32
#include <windows.h>
#endif
#include <GL/glut.h>

struct RGBType {
float r;
float g;
float b;
};

void Init( ) {

glViewport(0, 0, 600, 600);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity( );
glMatrixMode(GL_PROJECTION);
glLoadIdentity( );
gluOrtho2D(0., 600., 0., 600.);

}

void onDisplay( ) {

glClearColor(1, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);

// Dynamically create a big one-dimensional array for the pixels
RGBType *pixels = new RGBType[600*600];

for (int i=0; i< 600*600; i++) {

pixels.r=0.5;
pixels.g=0.5;
pixels.b=0.5;

}

glDrawPixels(600,600,GL_RGB,GL_FLOAT,pixels);
glutSwapBuffers();

// Remember to delete any dynamically created stuff!
delete[] pixels;

}

void onMousePress(int button, int state, int x, int y) {
}

void onIdle( ) {
}

void onKeyboard(unsigned char key, int x, int y) {
}

void main(int argc, char * argv[]) {
glutInit(&argc, argv);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Grafika hazi feladat");
Init();
glutDisplayFunc(onDisplay);
glutMouseFunc(onMousePress);
glutIdleFunc(onIdle);
glutKeyboardFunc(onKeyboard);
glutMainLoop();
}

Share this post


Link to post
Share on other sites
Thanks you guys, yes , that was really helpful.
I wonder though, if i want to change the color of the pixel in the 234. row and 45. column, which one is it now?
600*234+45??

Share this post


Link to post
Share on other sites
Quote:
Original post by nextman
I wonder though, if i want to change the color of the pixel in the 234. row and 45. column, which one is it now?
600*234+45??


Basically yes. It depends a little on how you count your rows and columns. You might want to use 600*(y-1)+(x-1), giving 600*233+44.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!