NeHe OpenGL lession 5, neheopengllession

Source: Internet
Author: User

NeHe OpenGL lession 5, neheopengllession

Lession 5 3D

// lession 5 3D#include <OpenGL/OpenGL.h>#include <GLUT/GLUT.h>#include <stdio.h>#include <stdlib.h>#include <stdio.h>float rtri; // angle for the triangelfloat rquad; // angle for the quadvoid initGL(void)  // Create some everyday functions{glShadeModel(GL_SMOOTH); // Enable smooth shadingglClearColor(0.0f, 0.0f, 0.0f, 0.5f); // black backgroundglClearDepth(1.0f);// depth buffer setupglEnable(GL_DEPTH_TEST); // Depth buffer setupglDepthFunc(GL_LEQUAL); // the type of depth testing to do.glEnable(GL_COLOR_MATERIAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);}void display(void) // create the siplay functions{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen and depth bufferglLoadIdentity(); // reset the current modelview matrixglPushMatrix();{glTranslatef(-1.5f, 0.0f, -6.0f); // Move left 1.5 units and into the screen 6.0glRotatef(rtri, 0.0f, 1.0f, 0.0f);glBegin(GL_TRIANGLES);glColor3f(1.0f, 0.0f, 0.0f); // redglVertex3f(0.0f, 1.0f, 0.0f); // top of triangle (Front);glColor3f(0.0f, 1.0f, 0.0f); // greenglVertex3f(-1.0f, -1.0f, 1.0f); // Left of triangle (Front)glColor3f(0.0, 0.0, 1.0); // blueglVertex3f(1.0f, -1.0f, 1.0f); // right of triangle (Front)glColor3f(1.0, 0.0, 0.0); // redglVertex3f(0.0f, 1.0f, 0.0f);   // top of triangle (right)glColor3f(0.0f, 0.0f, 1.0f); // blueglVertex3f(1.0f, -1.0f, 1.0f); // left of triangle (right)glColor3f(0.0, 1.0, 0.0); // greenglVertex3f(1.0f, -1.0f, -1.0f); // right of triangle (right);glColor3f(1.0f, 0.0f, 0.0f); // redglVertex3f(0.0f, 1.0f, 0.0f);   // top of triangle (back);glColor3f(0.0f, 1.0f, 0.0f); // greenglVertex3f(1.0f, -1.0f, -1.0f); // left of triangle (back)glColor3f(0.0f, 0.0f, 1.0f); // blueglVertex3f(-1.0f, -1.0f, -1.0f); // right of triangle (back)glColor3f(1.0f, 0.0f, 0.0f); // redglVertex3f(0.0f, 1.0f, 0.0f);// top of triangle (left)glColor3f(0.0f, 0.0f, 1.0f); // blueglVertex3f(-1.0f, -1.0f, -1.0f); // left of triangle (left)glColor3f(0.0f, 1.0f, 0.0f); // greenglVertex3f(-1.0f, -1.0f, 1.0f); // right of triangle (left);glEnd();glLoadIdentity();glTranslatef(1.5f, 0.0f, -6.0f);glRotatef(rquad, 1.0f, 0.0f, 0.0f);  // Rotate the quad on the x axisglColor3f(0.5f, 0.5f, 1.0f); // Set teh color to blue on time onlyglBegin(GL_QUADS);glColor3f(0.0f, 1.0f, 0.0f); // Set the color to blueglVertex3f( 1.0f,  1.0f, -1.0f); // top right of the quad (top)glVertex3f(-1.0f,  1.0f, -1.0f);  // top left of the quad (top)glVertex3f(-1.0f,  1.0f,  1.0f); // bottom left of the quad (top)glVertex3f( 1.0f,  1.0f,  1.0f); // bottom right of the quad (top)glColor3f(1.0f, 0.5f, 0.0f); // Set the color to OrangeglVertex3f( 1.0f, -1.0f, -1.0f);  // top right of the quad (bottom)glVertex3f(-1.0f, -1.0f, -1.0f); // top left of the quad (bottom)glVertex3f(-1.0f, -1.0f,  1.0f); // bottom left of the quad (bottom)glVertex3f( 1.0f, -1.0f,  1.0f); // bottom right of the quad (bottom)glColor3f(1.0f, 0.0f, 0.0f); // Set the color to redglVertex3f( 1.0f,  1.0f,  1.0f); // top right of the quad (front)glVertex3f(-1.0f,  1.0f,  1.0f);// top left of the quad (front)glVertex3f(-1.0f, -1.0f,  1.0f);// bottom left of the quad (front)glVertex3f( 1.0f, -1.0f,  1.0f); // bottom right of the quad (front)glColor3f(1.0f, 1.0f, 0.0f);// Set the color to YellowglVertex3f( 1.0f,  1.0f, -1.0f);// top right of the quad (back)glVertex3f(-1.0f,  1.0f, -1.0f);// top left of the quad (back)glVertex3f(-1.0f, -1.0f, -1.0f); // bottom left of the quad (back)glVertex3f( 1.0f, -1.0f, -1.0f); // bottom right of the quad (back)glColor3f(0.0f, 0.0f, 1.0f); // Set the color to blueglVertex3f(-1.0f,  1.0f,  1.0f); // top right of the quad (left)glVertex3f(-1.0f,  1.0f, -1.0f); // top left of the quad (left)glVertex3f(-1.0f, -1.0f, -1.0f); // bottom left of the quad (left)glVertex3f(-1.0f, -1.0f,  1.0f); // bottom right of the quad (left)glColor3f(1.0f, 0.0f, 1.0f); // Set the color to VioletglVertex3f( 1.0f,  1.0f, -1.0f); // top right of the quad (right)glVertex3f( 1.0f,  1.0f,  1.0f); // top left of the quad (right)glVertex3f( 1.0f, -1.0f,  1.0f); // bottom left of the quad (right)glVertex3f( 1.0f, -1.0f, -1.0f); // bottom right of the quad (right)glEnd();}glPopMatrix();rtri += 0.2f; // increase the rotation variable for the triangle (NEW)rquad -= 0.2f; // decrease the rotation variable for the quad (NEW)glutSwapBuffers();  // Swap the buffers to not be left with a clear screen.}// Create the reshape function (the viewport)void reshape(int width, int height){if (height == 0)height = 1;glViewport(0, 0, width, height); // Reset the current viewportglMatrixMode(GL_PROJECTION);// Select the projection matrixglLoadIdentity();// Rest the projection matrix// Calculate the aspect ratio of the windowgluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f);glMatrixMode(GL_MODELVIEW);// Select the Modelview MatrixglLoadIdentity();// Reset the Modelview Matrix}// Create keyboard functionvoid keyboard(unsigned char key, int x, int y) {switch (key) {case 27:exit(0);break;default:break;}}// Create Special Function (required for arrow keys)void arrow_keys(int a_keys, int x, int y){switch (a_keys) {case GLUT_KEY_UP:  // When Up Arrow is pressed...glutFullScreen(); // Go into full screen modebreak;case GLUT_KEY_DOWN: // When Down arrow is pressed....glutReshapeWindow(500, 500); // go into a 500 by 500 windowbreak;default:break;}}// create main function for bringing it all togetherint main(int argc, char **argv){glutInit(&argc, argv);glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutInitWindowSize(500, 500);glutCreateWindow("NeHe's OpenGL Framework");// glutFullScreen();initGL();glutDisplayFunc(display);glutReshapeFunc(reshape);glutKeyboardFunc(keyboard);glutSpecialFunc(arrow_keys);glutIdleFunc(display);glutMainLoop();return 0;}


Mac terminal running:

$: Clang-o lession lession5.c-Wno-deprecated-framework OpenGL-framework GLUT

$:./Lession




OpenGL settings under n cards

How can this problem be solved? I also compiled the NeHe tutorial during my vacation. At that time, I used the SIS graphics card. I also used the extended extensions compiled by the 1.5 class to support OpenGL, card. Now I am back to school. These programs run well, especially those in the CG script class. (I am using NV 9600GT ).
I suspect that you have not installed a graphics card driver. to suppress OpenGL's development of DX, windows only provides OpenGL1.0 as an excuse. Other functions can only use extensions. To use advanced OpenGL, you must install the graphics card driver. My OpenGL version is 3.5. You can also use the 28th course program to check if it is an OpenGL problem.

We recommend some materials for learning OpenGL.

First, let's take a look at the red treasure book of OpenGL, the sixth edition of the basic OpenGL programming guide. There is no better book than this book in the basic part. Then, let's take a look at the source code such as nehe's OpenGL tutorial.

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.