PDA

View Full Version : glDrawPixels



Cesar Ghali
05-13-2008, 02:22 AM
Dear friends,

I want to ask you how to use the glDrawPixels function, I know that we must specify the width height format type and an array that represent the pixels .
The question is what are the coordinates of the pixels? Or where will these pixels be drawn?

Thanks in advance for your help.

Zengar
05-13-2008, 03:00 AM
The origin for this operation is the raster position that can be set with glRasterPos

Cesar Ghali
05-13-2008, 03:25 AM
Dear Zengar,

Thanks for your quick reply. The problem is that I am implementing a color cube using opengl. I am using the code in the attached file, but it is too slow. I tought that the glDrawPixels function can be fater, but it is slower.

Please can you tell me is there any way that is faster than the one I am using.

Thanks in advance for your help.




#include <GL/glut.h>
#include <stdio.h>

void redraw();
void mouseClicked(int, int, int, int);
void mouseMoved(int,int);
void reshape(int, int);

float spinX = 0.0f;
float spinZ = 0.0f;
int mousePosX = 0;
int mousePosY = 0;

void main()
{
glutInitWindowSize(700, 700);
glutInitWindowPosition(50, 50);
glutCreateWindow("Test");
glutDisplayFunc(redraw);
glutReshapeFunc(reshape);
glutMouseFunc(mouseClicked);
glutMotionFunc(mouseMoved);
glutMainLoop();
}

void redraw()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT );

glPushMatrix();
glRotatef(spinZ, 1, 0, 0);
glRotatef(spinX, 0, 0, 1);
for (int r = 0; r < 255; r++)
{
for (int g = 0; g < 255; g++)
{
for (int b = 0; b < 255; b++)
{
//using glDrawPixels
//float color[3] = {(float)r / 255, (float)g / 255, (float)b / 255};
//glRasterPos3f(r - 127.5, g - 127.5, b - 127.5);
//glDrawPixels(1, 1, GL_RGB, GL_FLOAT, color);

//using glVertex3f
glColor3f((float)r / 255, (float)g / 255, (float)b / 255);
glBegin(GL_POINTS);
glVertex3f(r - 127.5, g - 127.5, b - 127.5);
glEnd();
}
}
}

glPopMatrix();

//glFlush();

glutSwapBuffers();
}

void mouseClicked(int button, int state, int xMouse, int yMouse)
{
if(state == GLUT_DOWN)
{
mousePosX = xMouse;
mousePosY = yMouse;
}
glutPostRedisplay();
}

void mouseMoved(int xMouse,int yMouse)
{
spinX += (float)(xMouse - mousePosX) * 1.0f;
spinZ += (float)(yMouse - mousePosY) * 1.0f;
mousePosX = xMouse;
mousePosY = yMouse;

glutPostRedisplay();
}

void reshape(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho((GLdouble)-w / 2, (GLdouble)w / 2, (GLdouble)-h / 2, (GLdouble)h / 2, 350.0, -350.0);

//glPointSize(10.0);
//glLineWidth(1.5);

glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

S.Seegel
05-13-2008, 05:06 AM
You seem to approach 3d graphics in a 2d-ish way: discrete points in space aka pixels (voxels).

But 3d graphics is about surface geometry. If you want to draw a color cube, you need to specify the surface of a cube with all associated attributes (color in your case), instead of your triple loop drawing discrete pixel/points in space.

A cubes surface consists of 8 vertice (corners) forming 6 faces (sides). The code to draw a cube (centered at the origin with an edge-length of 1) can look like this:


float vertice[8][3] = { { -0.5f, -0.5f, 0.5f},
{ 0.5f, -0.5f, 0.5f},
{ 0.5f, 0.5f, 0.5f},
{ -0.5f, 0.5f, 0.5f},
{ -0.5f, -0.5f, -0.5f},
{ 0.5f, -0.5f, -0.5f},
{ 0.5f, 0.5f, -0.5f},
{ -0.5f, 0.5f, -0.5f} };

glBegin(GL_QUADS);
// front side
glVertex3fv(vertice[0]);
glVertex3fv(vertice[1]);
glVertex3fv(vertice[2]);
glVertex3fv(vertice[3]);
// back side
glVertex3fv(vertice[5]);
glVertex3fv(vertice[4]);
glVertex3fv(vertice[7]);
glVertex3fv(vertice[6]);
// left side
glVertex3fv(vertice[4]);
glVertex3fv(vertice[0]);
glVertex3fv(vertice[3]);
glVertex3fv(vertice[7]);
// right side
glVertex3fv(vertice[1]);
glVertex3fv(vertice[5]);
glVertex3fv(vertice[6]);
glVertex3fv(vertice[2]);
// top side
glVertex3fv(vertice[3]);
glVertex3fv(vertice[2]);
glVertex3fv(vertice[6]);
glVertex3fv(vertice[7]);
// bottom side
glVertex3fv(vertice[1]);
glVertex3fv(vertice[0]);
glVertex3fv(vertice[4]);
glVertex3fv(vertice[5]);
glEnd();


For your color-cube you need to define the color for each of the eight vertice and put corresponding glColor3fv()-calls before each glVertex3fv()-call. opengl will do the interpolation (computation of in-between colors) across the surface for you.

I recommend the Red Book for an in-depth introduction to opengl, available online (http://opengl.org/documentation/red_book) (not the up-to-date version, but sufficient for the basics).