OpenGL programming easy to get started pixel operation

Source: Internet
Author: User
Tags 0xc0 set background

This section explains pixel operations.

Example 14: This example draws a triangle in the window and then uses the Glcopypixel function to copy the graph five times and place the five triangles in different positions in the window.

#include <stdlib.h>
#include <GL/glut.h>
/* Set Background color * *
void Myinit (void)
{
Glclearcolor (1.0,1.0,1.0,1.0);
}

/* Draw Color triangle * *
void triangle (void)
{
Glbegin (Gl_triangles);
glcolor3f (1.0,1.0,0.0);
GLVERTEX2F (10.0,10.0);
glcolor3f (0.0,0.3,1.0);
GLVERTEX2F (20.0,30.0);
glcolor3f (1.0,0.0,0.0);
GLVERTEX2F (30.0,10.0);
Glend ();
}

void Mydisplay ()
{
int i;

Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit);

Glpushmatrix ();
Gltranslatef (10.0,100.0,0.0);
Triangle ();
Glpopmatrix ();

Glpushmatrix ();
for (i = 0;i < 5;i++)
{
Glrasterpos2i (20+i*30,10+i*5);//Specify raster position for pixel operation
Glcopypixels (50,200,500,500,gl_color);//copy pixels in cache
}
Glpopmatrix ();
Glflush ();
}

void Myreshape (int w,int h)
{
Glviewport (0,0, (Glsizei) W, (Glsizei) h);
Glmatrixmode (gl_projection);
Glloadidentity ();

if (w <= h)
Gluortho2d (0.0,150,0.0,150.0* (glfloat) h/(glfloat) w);
Else
Gluortho2d (0.0,150* (glfloat) w/(glfloat) h,0.0,150.0);
Glmatrixmode (Gl_modelview);
Glloadidentity ();
}

int main (int argc,char * * argv)
{
Glutinit (&AMP;ARGC,ARGV);
Glutinitdisplaymode (glut_single| glut_rgb| Glut_depth);
Glutinitwindowsize (500,400);
Glutinitwindowposition (100,100);

Glutcreatewindow ("copy");

Myinit ();
Glutreshapefunc (Myreshape);
Glutdisplayfunc (Mydisplay);

Glutmainloop ();
return 0;
}


Glrasterpos Specifies the raster position in pixel operations. The same number that follows represents the coordinate dimension, with 2 representing the coordinates x, y,3, Y, Z. The letter following the number represents the parameter type. The last v indicates that the parameter is a pointer.

void Glcopypixels (Glint x, glint y, glsizei width, glsizei height, glenum type) function copies pixels to the cache.

X,Y specifies the lower-right corner coordinate of the copied pixel.

WIDTH,HEIGTH Specifies the size of the rectangular area of the copied pixel.

Type specifies the types of the copied values. The value is a color, depth, or film plate value. In this case, the color.

Example 15: This example writes the word file in the lower-left corner of the window.

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

/* Letter f*/
Glubyte f_rasters[12] = {0XC0,0XC0,0XC0,0XC0,0XC0,0XFC,
0XFC,0XC0,0XC0,0XC0,0XFF,0XFF};

/* Letter i*/
Glubyte i_rasters[12] = {0xff,0xff,0x18,0x18,0x18,0x18,
0X18,0X18,0X18,0X18,0XFF,0XFF};

/* Letter l*/
Glubyte l_rasters[12] = {0xff,0xff,0xc0,0xc0,0xc0,0xc0,
0XC0,0XC0,0XC0,0XC0,0XC0,0XC0};

/* Letter e*/
Glubyte e_rasters[12] = {0xff,0xff,0xc0,0xc0,0xc0,0xff,
0XFF,0XC0,0XC0,0XC0,0XFF,0XFF};

void Myinit (void)
{
Glpixelstorei (gl_unpack_alignment,1);//set pixel storage mode
Glclearcolor (1.0,1.0,1.0,1.0);/Set Background to White
}

void Mydisplay ()
{
Glclear (Gl_color_buffer_bit);

glcolor3f (0.0,0.0,0.0);//set pixel color to black
Glrasterpos2i (20,20);//Specify Position for pixel

/* Draw Bitmap * *
Glbitmap (8,12,0.0,0.0,14.0,0.0,f_rasters);
Glbitmap (8,12,0.0,0.0,14.0,0.0,i_rasters);
Glbitmap (8,12,0.0,0.0,14.0,0.0,l_rasters);
Glbitmap (8,12,0.0,0.0,14.0,0.0,e_rasters);
Glflush ();
}

void Myreshape (int w,int h)
{
Glviewport (0,0, (Glsizei) W, (Glsizei) h);
Glmatrixmode (gl_projection);
Glloadidentity ();
Glortho (0,w,0,h,-1.0,1.0);

Glmatrixmode (Gl_modelview);
Glloadidentity ();
}

int main (int argc,char * * argv)
{
/* Initialize * *
Glutinit (&AMP;ARGC,ARGV);
Glutinitdisplaymode (glut_single| GLUT_RGB);
Glutinitwindowsize (500,400);
Glutinitwindowposition (100,100);

/* Create window * *
Glutcreatewindow ("BitMap");

/* Draw Graphics * *
Myinit ();
Glutreshapefunc (Myreshape);
Glutdisplayfunc (Mydisplay);

Glutmainloop ()//Enter GLUT event handling loop
return 0;
}


Glbitmap (Glsizei width,glsizei height,glfloat xorig,glfloat yorig,glfloat xmove,glfloat ymove,const GLubyte * Bitmap) function to draw a bitmap.

Width,height refers to the width and height of the location graph image respectively.

The origin position of the Xorig,yorig bitmap image. The origin is the lower-left corner of the bitmap. To the right and up to the forward of the axis.

Xmove,ymove the displacement of the x,y relative to the current grating after the bitmap has been plotted.

The address of the bitmap bitmap image.

You can change the parameters of this function to see the effects of different sizes and locations.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.