OpenGL Programming Guide Sample program 2-15 complete code

Source: Internet
Author: User
#include "stdafx.h"
#include <windows.h>
#include <GL/glew.h>
#include <GL/glut.h>
#define Freeglut_static
#include <GL/freeglut.h>
#pragma COMMENT (linker, "/nodefaultlib:libc.lib")
#define BUFFER_OFFSET (offset) ((const glubyte *) NULL + (offset))

#define XStart-0.8
#define XEND 0.8
#define YStart-0.8
#define YEND 0.8
#define Numxpoints 11
#define Numypoints 11
#define NUMPOINTS (numxpoints * numypoints)
#define NUMPOINTSPERSTRIP (2 * numxpoints)
#define NUMSTRIPS (NUMYPOINTS-1)
#define RESTARTINDEX 0xFFFF
Glfloat color[6][3]={
{1.0,1.0,1.0},{1.0,0.0,0.0},{1.0,1.0,0.0},
{0.0,1.0,0.0},{0.0,1.0,1.0},{0.0,0.0,1.0}
};
void Init () {
Gluint Vbo, Ebo;
glfloat* vertices = 0;
glushort* indices = 0;
Glgenbuffers (1, &AMP;VBO);
Glbindbuffer (Gl_array_buffer, VBO);
Glbufferdata (Gl_array_buffer, 6 * numpoints * sizeof (glfloat), NULL, Gl_static_draw);
vertices = (glfloat*) glmapbuffer (Gl_array_buffer, gl_write_only);
if (vertices = = NULL) {
Exit (Exit_failure);
}
else{
int i;
Int J;
Glfloat dx = (glfloat) (Xend-xstart)/(NUMXPOINTS-1);
Glfloat dy = (glfloat) (Yend-ystart)/(NUMYPOINTS-1);
glfloat* tmp = vertices;
int n = 0;
for (j = 0; j < numypoints; ++j) {
Glfloat y = (glfloat) (Ystart + j * dy);
for (i = 0; i < numxpoints; ++i) {
Glfloat x = (glfloat) (Xstart + i * dx);
*tmp++ = color[(i+j)%6][0];
*tmp++ = color[(i+j)%6][1];
*tmp++ = color[(i+j)%6][2];
*tmp++ = x;
*tmp++ = y;
*tmp++ = 0;
}
}
Glunmapbuffer (Gl_array_buffer);
Glvertexpointer (3, gl_float, 6 * sizeof (glfloat), Buffer_offset (3 * sizeof (glfloat)));
Glcolorpointer (3, gl_float, 6 * sizeof (glfloat), Buffer_offset (0));
Glenableclientstate (Gl_vertex_array);
Glenableclientstate (Gl_color_array);
Glinterleavedarrays (gl_c3f_v3f, 0, 0);
}

Glgenbuffers (1, &ebo);
Glbindbuffer (Gl_element_array_buffer, Ebo);
Glbufferdata (Gl_element_array_buffer, Numstrips * (Numpointsperstrip + 1) * sizeof (Glushort), NULL, Gl_static_draw);
indices = (glushort*) glmapbuffer (Gl_element_array_buffer, gl_write_only);
if (indices = = NULL) {
Exit (Exit_failure);
}
else{
int i;
Int J;
glushort* index = indices;
for (j = 0; j < numstrips; ++j) {
Glushort Bottomrow = j * numypoints;
Glushort Toprow = Bottomrow + numypoints;
for (i = 0; i < numxpoints; ++i) {
*index++ = Toprow + i;
*index++ = Bottomrow + i;
}
*index++ = Restartindex;
}
Glunmapbuffer (Gl_element_array_buffer);
}
Glprimitiverestartindex (Restartindex);
Glenable (Gl_primitive_restart);
}

void display () {
Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit);
Glpolygonmode (Gl_front_and_back, gl_line);
glcolor3f (1, 1, 1);
Gldrawelements (Gl_triangle_strip, Numstrips * (Numpointsperstrip + 1), Gl_unsigned_short, Buffer_offset (0));
Glutswapbuffers ();
}
void reshape (int w, int h) {
Glviewport (0, 0, W, h);
Glmatrixmode (gl_projection);
Glloadidentity ();
gluortho2d (0.0, W, 0.0, h);
}
void Keyboardfunc (unsigned char key, int x, int y) {
Std::cout << key << x << y << Std::endl;
}
void Mouse (int button, int state, int x, int y) {
Switch (Button)
{
Case Glut_left_button:
if (state = = Glut_down) {
Glutidlefunc (NULL);
}
Break
Case Glut_right_button:
if (state = = Glut_down) {
Glutidlefunc (NULL);
}
Break
Default
Break
}
}
void Move (int x, int y) {
Std::cout << x << y << Std::endl;
}
int main (int argc, char** argv) {
Glutinit (&AMP;ARGC, argv);
Glutinitdisplaymode (glut_double | GLUT_RGB);
Glutinitwindowsize (250, 250);
Glutinitwindowposition (100, 100);
Glutinitcontextversion (3, 0);
Glutcreatewindow ("Hello");
Glewinit ();
Init ();
Glutdisplayfunc (display);
Glutreshapefunc (reshape);
Glutmousefunc (mouse);
Glutmotionfunc (move);
Glutkeyboardfunc (Keyboardfunc);
Glutmainloop ();
return 0;
}

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.