NeHe OpenGL lession 4, neheopengllession

Source: Internet
Author: User

NeHe OpenGL lession 4, neheopengllession

// lession4.c#include <OpenGL/OpenGL.h>#include <GLUT/GLUT.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>/* ASCII code for teh escape key. */#define ESCAPE 27/* The number of our GLUT window */int window;/* rotation angle for the triangle. */float rtri = 0.0f;/* rotation angle for the quadrilateral. */float rquad = 0.0f;/* A general OpenGL initialization function. Sets all of the initial parameters. */// We call this right after our OpoenGL window is created.void initGL(int width, int height) { // This will clear The background color to black.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);glClearDepth(1.0); // Enables clearing of the depth bufferglDepthFunc(GL_LESS); // The type of test to do.glEnable(GL_DEPTH_TEST); // Enables depth testing.glShadeModel(GL_SMOOTH);    // Enalbes smooth color shading.glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Reset the projection matrix.// calculate the aspect ratio of the window.gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);glMatrixMode(GL_MODELVIEW);}/* The function called when our window is resized (which shouldn't happend,    because we're fullscreen) */void resizeGLScene(int width, int height) {if (height == 0)  // Prevent a divide by zero if the window is too small. height = 1;glViewport(0, 0, width, height); // Reset the current viewport and perspective transformationglMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);glMatrixMode(GL_MODELVIEW);}/* The main drawing function. */void drawGLScene() {glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and the depth bufferglLoadIdentity(); // Reset the view.glTranslatef(-1.5f, 0.0f, -6.0f); // Move left 1.5 units and into the screen 6.0.glRotatef(rtri, 0.0f, 1.0f, 0.0f); // Rotate the triangle on the y axis// Draw a triangel (in smooth coloring mode)glBegin(GL_POLYGON);// Start drawing a polygonglColor3f(1.0f, 0.0f, 0.0f); // set the color to redglVertex3f(0.0f, 1.0f, 0.0f); // topglColor3f(0.0f, 1.0f, 0.0f); // set the color to green.glVertex3f(1.0f, -1.0f, 0.0f); // bottom rightglColor3f(0.0f, 0.0f, 1.0f); // set the color to blue.glVertex3f(-1.0f, -1.0f, 0.0f); // bottom left.glEnd(); // we're done with the polygon (smooth color interpolation)glLoadIdentity(); // make sure we're no longer rotated.glTranslatef(1.5f, 0.0f, -6.0f);   // Move Right 3 Units, and back into the screen 6.0glRotatef(rquad, 1.0f, 0.0f, 0.0f); // Roate the quad on the x axis// Draw a square (quadrilateral)glColor3f(0.5f, 0.5f, 1.0f);// set color to a blue shade.glBegin(GL_QUADS); // Start drawing a polygon (4 sides);glVertex3f(-1.0f,  1.0f, 0.0f);// top left.glVertex3f( 1.0f,  1.0f, 0.0f); // top right.glVertex3f( 1.0f, -1.0f, 0.0f);     // bottom right.glVertex3f(-1.0f, -1.0f, 0.0f); // bottom left.glEnd(); // done with the polygon.rtri += 15.0f;// Increase the rotation variable for the trianglerquad -= 15.0f; // decrease the rotation variable for the quad.// swap the buffers to display, since double buffering in used.glutSwapBuffers();}/* The function called whenever a key is pressed. */void keyPressed(unsigned char key, int x, int y) {/* sleep to avoid thrashing this procedure */usleep(100);/* If escape is pressed, kill everything. */if (key == ESCAPE) {/*shut down our window */glutDestroyWindow(window);/* exit the program...normal termination. */exit(0);}}int main(int argc, char **argv) {glutInit(&argc, argv);/* Select type of display mode:   Double buffer   RGBA color   Alpha components supported   Depth buffer */ glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH); /* Get a 640 x 480 window */ glutInitWindowSize(640, 480); /* the window starts at teh upper left corner of the screen */ glutInitWindowSize(0, 0); /* Openg a window */ window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99"); /* Register the function to do all our OpenGL drawing. */ glutDisplayFunc(&drawGLScene); /* Go full screen. This is as soon as possible. */// glutFullScreen(); /* Even if there are not events, redarw our gl scene. */ glutIdleFunc(&drawGLScene); /* register the function called when our window is resized. */ glutReshapeFunc(&resizeGLScene);  /* Register the function claled when teh keyboard is pressed. */ glutKeyboardFunc(&keyPressed); /* Initialize our window. */ initGL(640, 480); /* start event processing engine. */ glutMainLoop(); return 1;}

This article mainly introduces the Rotation



Ceshi


Qt Nehe OpenGL 25th class no image?

WinMain, you have defined two entities ~~~~ In addition, do not enter spaces when enabling the Chinese input method ......
There is a Chinese Punctuation space in each row 314 and 316.

Creating a blank WIN32 APPLICATION is correct ~~~ Of course, you can also change it ~~

========================
The number of lines reported by the compiler is not necessarily correct. You need to check carefully. In addition, it is best to download the project.
You can upload it here
Not bad. I hope you will adopt it.

OpenGL, DirectX

Learning is done in parallel. You cannot do it in this way. If you want to learn a game, you can:
C, c ++ syntax level, 1-2 months
Data structure, 1-2 weeks; algorithm design more than 4 years
More than one year of Design Model

---------
After a good c ++ background (at least understand its memory model) and master several common design patterns,
Learning windows programming, or you will never understand the various mfc frameworks !!! There is no reason to say that, because some design of the windows Programming Mechanism knows what it means. If you don't know it, you will always think about it. Isn't that a waste of time?
After learning some details about win32 programming, it is easy to understand the mfc. At this time, <in-depth analysis of mfc> combined with multiple tracking and debugging, we should be able to master the introduction of mfc to a framework. The more time you spend, the faster your software architecture capabilities will naturally increase.

During the learning period of mfc, you can start opengl, and then start with the red book, and then use other example books. Later I read the second version of 3d game engine, and a developer named ogre wrote it as an engine book.

It may take a year. However, if you come in this order, your foundation will be quite reliable and it will be easy to do anything.

If you want to learn dx, you need to win the essence of com.
----------------
A skillful book is useless in the early stage. It will take effect only after you have accumulated some experience. Learning is actually a parallel process. The effect is much better. Like algorithm design, you can spare some time every day.

Related Article

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.