Jeff Molofee - NeHe's OpenGL Tutorials

Здесь есть возможность читать онлайн «Jeff Molofee - NeHe's OpenGL Tutorials» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Жанр: Программирование, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

NeHe's OpenGL Tutorials: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «NeHe's OpenGL Tutorials»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

NeHe's OpenGL Tutorials — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «NeHe's OpenGL Tutorials», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

{

return FALSE; // If Texture Didn't Load Return FALSE ( NEW )

}

glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )

glShadeModel(GL_SMOOTH); // Enable Smooth Shading

glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background

glClearDepth(1.0f); // Depth Buffer Setup

glEnable(GL_DEPTH_TEST); // Enables Depth Testing

glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

return TRUE; // Initialization Went OK

}

Now we draw the textured cube. You can replace the DrawGLScene code with the code below, or you can add the new code to the original lesson one code. This section will be heavily commented so it's easy to understand. The first two lines of code glClear() and glLoadIdentity() are in the original lesson one code. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) will clear the screen to the color we selected in InitGL(). In this case, the screen will be cleared to black. The depth buffer will also be cleared. The view will then be reset with glLoadIdentity().

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer

glLoadIdentity(); // Reset The Current Matrix

glTranslatef(0.0f,0.0f,-5.0f); // Move Into The Screen 5 Units

The following three lines of code will rotate the cube on the x axis, then the y axis, and finally the z axis. How much it rotates on each axis will depend on the value stored in xrot, yrot and zrot.

glRotatef(xrot,1.0f,0.0f,0.0f); // Rotate On The X Axis

glRotatef(yrot,0.0f,1.0f,0.0f); // Rotate On The Y Axis

glRotatef(zrot,0.0f,0.0f,1.0f); // Rotate On The Z Axis

The next line of code selects which texture we want to use. If there was more than one texture you wanted to use in your scene, you would select the texture using glBindTexture(GL_TEXTURE_2D, texture[number of texture to use]). If you wanted to change textures, you would bind to the new texture. One thing to note is that you can NOT bind a texture inside glBegin() and glEnd(), you have to do it before or after glBegin(). Notice how we use glBindTextures to specify which texture to create and to select a specific texture.

glBindTexture(GL_TEXTURE_2D, texture[0]); // Select Our Texture

To properly map a texture onto a quad, you have to make sure the top right of the texture is mapped to the top right of the quad. The top left of the texture is mapped to the top left of the quad, the bottom right of the texture is mapped to the bottom right of the quad, and finally, the bottom left of the texture is mapped to the bottom left of the quad. If the corners of the texture do not match the same corners of the quad, the image may appear upside down, sideways, or not at all.

The first value of glTexCoord2f is the X coordinate. 0.0f is the left side of the texture. 0.5f is the middle of the texture, and 1.0f is the right side of the texture. The second value of glTexCoord2f is the Y coordinate. 0.0f is the bottom of the texture. 0.5f is the middle of the texture, and 1.0f is the top of the texture.

So now we know the top left coordinate of a texture is 0.0f on X and 1.0f on Y, and the top left vertex of a quad is –1.0f on X, and 1.0f on Y. Now all you have to do is match the other three texture coordinates up with the remaining three corners of the quad.

Try playing around with the x and y values of glTexCoord2f. Changing 1.0f to 0.5f will only draw the left half of a texture from 0.0f (left) to 0.5f (middle of the texture). Changing 0.0f to 0.5f will only draw the right half of a texture from 0.5f (middle) to 1.0f (right).

glBegin(GL_QUADS);

// Front Face

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, –1.0f, 1.0f); // Bottom Left Of The Texture and Quad

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, –1.0f, 1.0f); // Bottom Right Of The Texture and Quad

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad

// Back Face

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, –1.0f, –1.0f); // Bottom Right Of The Texture and Quad

glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, –1.0f); // Top Right Of The Texture and Quad

glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, –1.0f); // Top Left Of The Texture and Quad

glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, –1.0f, –1.0f); // Bottom Left Of The Texture and Quad

// Top Face

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, –1.0f); // Top Left Of The Texture and Quad

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, –1.0f); // Top Right Of The Texture and Quad

// Bottom Face

glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, –1.0f, –1.0f); // Top Right Of The Texture and Quad

glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, –1.0f, –1.0f); // Top Left Of The Texture and Quad

glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, –1.0f, 1.0f); // Bottom Left Of The Texture and Quad

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, –1.0f, 1.0f); // Bottom Right Of The Texture and Quad

// Right face

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, –1.0f, –1.0f); // Bottom Right Of The Texture and Quad

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, –1.0f); // Top Right Of The Texture and Quad

glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad

glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, –1.0f, 1.0f); // Bottom Left Of The Texture and Quad

// Left Face

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, –1.0f, –1.0f); // Bottom Left Of The Texture and Quad

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, –1.0f, 1.0f); // Bottom Right Of The Texture and Quad

glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, –1.0f); // Top Left Of The Texture and Quad

glEnd();

Now we increase the value of xrot, yrot and zrot. Try changing the number each variable increases by to make the cube spin faster or slower, or try changing a + to a – to make the cube spin the other direction.

xrot+=0.3f; // X Axis Rotation

yrot+=0.2f; // Y Axis Rotation

zrot+=0.4f; // Z Axis Rotation

return true; // Keep Going

}

You should now have a better understanding of texture mapping. You should be able to texture map the surface of any quad with an image of your choice. Once you feel confident with your understanding of 2D texture mapping, try adding six different textures to the cube.

Texture mapping isn't to difficult to understand once you understand texture coordinates. If you're having problems understanding any part of this tutorial, let me know. Either I'll rewrite that section of the tutorial, or I'll reply back to you in email. Have fun creating texture mapped scenes of your own :)

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «NeHe's OpenGL Tutorials»

Представляем Вашему вниманию похожие книги на «NeHe's OpenGL Tutorials» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Jeff Jacobson - Sleep Tight
Jeff Jacobson
Jeff Salyards - Veil of the Deserters
Jeff Salyards
Jeff LaSala - The Darkwood Mask
Jeff LaSala
Jeff Lindsay - Dexter's Final Cut
Jeff Lindsay
libcat.ru: книга без обложки
Неизвестный Автор
libcat.ru: книга без обложки
Неизвестный Автор
libcat.ru: книга без обложки
Неизвестный Автор
libcat.ru: книга без обложки
Неизвестный Автор
Отзывы о книге «NeHe's OpenGL Tutorials»

Обсуждение, отзывы о книге «NeHe's OpenGL Tutorials» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

x