codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <GL/glew.h> #include <GL/glut.h> #include <windows.h> #include <stdio.h> #include <stdlib.h> struct XYZ { float x,y,z; }; struct rot { rot() : angle_x(0), angle_y(0) {}; float angle_x; float angle_y; }; static rot rotate; static float zoom = 16.0f; //----------------------------- static const size_t width = 800; static const size_t height = 600; // количество вершин куба static const size_t cubeVerticesCount = 24; //---------------------------- static float frameCount; static float currentTime; static float previousTime; static float fps; //---------------------------- static GLuint vId; static GLuint cId; static GLuint tId; static GLuint vao; GLuint LoadTexture( const char * filename, int width, int height ); // координаты вершин куба static const float s = 10.0f; // половина размера куба static const float cubePositions[cubeVerticesCount][3] = { {-s, s, s}, { s, s, s}, { s,-s, s}, {-s,-s, s}, // front { s, s,-s}, {-s, s,-s}, {-s,-s,-s}, { s,-s,-s}, // back {-s, s,-s}, { s, s,-s}, { s, s, s}, {-s, s, s}, // top { s,-s,-s}, {-s,-s,-s}, {-s,-s, s}, { s,-s, s}, // bottom {-s, s,-s}, {-s, s, s}, {-s,-s, s}, {-s,-s,-s}, // left { s, s, s}, { s, s,-s}, { s,-s,-s}, { s,-s, s} // right }; // координаты вершин куба static const unsigned char cubeColors[cubeVerticesCount][3] = { { 255, 255, 255}, { 255, 255, 255}, { 255, 255, 255}, { 255, 255, 255}, // front { 255, 255, 255}, { 255, 255, 255}, { 255, 255, 255}, { 255, 255, 255}, // back { 255, 255, 255}, { 255, 255, 255}, { 255, 255, 255}, { 255, 255, 255}, // top { 255, 255, 255}, { 255, 255, 255}, { 255, 255, 255}, { 255, 255, 255}, // bottom { 255, 255, 255}, { 255, 255, 255}, { 255, 255, 255}, { 255, 255, 255}, // left { 255, 255, 255}, { 255, 255, 255}, { 255, 255, 255}, { 255, 255, 255} // right }; // текстурные координаты куба static const float cubeTexcoords[cubeVerticesCount][2] = { {0.0f,1.0f}, {1.0f,1.0f}, {1.0f,0.0f}, {0.0f,0.0f}, // front {0.0f,1.0f}, {1.0f,1.0f}, {1.0f,0.0f}, {0.0f,0.0f}, // back {0.0f,1.0f}, {1.0f,1.0f}, {1.0f,0.0f}, {0.0f,0.0f}, // top {0.0f,1.0f}, {1.0f,1.0f}, {1.0f,0.0f}, {0.0f,0.0f}, // bottom {0.0f,1.0f}, {1.0f,1.0f}, {1.0f,0.0f}, {0.0f,0.0f}, // left {0.0f,1.0f}, {1.0f,1.0f}, {1.0f,0.0f}, {0.0f,0.0f} // right }; void createVAO(GLuint *_vId, GLuint *_cId, GLuint *_tId, GLuint *_vao) { glGenVertexArrays(1, _vao); glBindVertexArray(*_vao); glEnableClientState( GL_VERTEX_ARRAY ); glBindBuffer(GL_ARRAY_BUFFER, *_vId ); glVertexPointer( 3, GL_FLOAT, 0, 0 ); // glEnableClientState( GL_COLOR_ARRAY ); // glBindBuffer(GL_ARRAY_BUFFER, *_cId ); // glColorPointer( 3, GL_UNSIGNED_BYTE, 0, 0 ); // // glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // glBindBuffer(GL_ARRAY_BUFFER, *_tId ); // glTexCoordPointer(2, GL_FLOAT, 0, 0 ); //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iId); glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } void createVBO(GLuint *_vId, GLuint *_cId, GLuint *_tId) { glGenBuffers(1, _vId); glBindBuffer(GL_ARRAY_BUFFER, *_vId); glBufferData(GL_ARRAY_BUFFER, sizeof(XYZ) * cubeVerticesCount, cubePositions, GL_STATIC_DRAW ); glBindBuffer(GL_ARRAY_BUFFER, 0); // glGenBuffers(1, _cId); // glBindBuffer(GL_VERTEX_ARRAY, *_cId); // glBufferData(GL_VERTEX_ARRAY, sizeof(unsigned char) * cubeVerticesCount * 3, cubeColors, GL_STATIC_DRAW ); // glBindBuffer(GL_ARRAY_BUFFER, 0); // glGenBuffers(1, _tId); // glBindBuffer(GL_VERTEX_ARRAY, *_tId); // glBufferData(GL_VERTEX_ARRAY, sizeof(float) * cubeVerticesCount * 2, cubeTexcoords, GL_STATIC_DRAW ); // glBindBuffer(GL_ARRAY_BUFFER, 0); } void deleteVBO(GLuint *_vId, GLuint *_cId, GLuint *_tId) { glBindBuffer( GL_ARRAY_BUFFER, 0 ); glDeleteBuffers(1,_vId); glDeleteBuffers(1,_cId); glDeleteBuffers(1,_tId); } void calculateFPS() { // Increase frame count frameCount++; // Get the number of milliseconds since glutInit called // (or first call to glutGet(GLUT ELAPSED TIME)). currentTime = glutGet(GLUT_ELAPSED_TIME); // Calculate time passed int timeInterval = currentTime - previousTime; if(timeInterval > 1000) { // calculate the number of frames per second fps = frameCount / (timeInterval / 1000.0f); // Set time previousTime = currentTime; // Reset frame count frameCount = 0; } char winstr[256]; char buffer[256]; strcpy( winstr, " fps = " ); gcvt(fps, 4, buffer); strcat(winstr, buffer); strcat(winstr, "s x = "); gcvt(rotate.angle_x,4,buffer); strcat(winstr, buffer); strcat(winstr, " y = "); gcvt(rotate.angle_y,4,buffer); strcat(winstr, buffer); strcat(winstr, " zoom = "); gcvt(zoom,4,buffer); strcat(winstr, buffer); glutSetWindowTitle( winstr ); } void key(unsigned char c, int x, int y) { switch (c) { // + case 43: zoom*=2.0f; break; // - case 45: zoom*=0.5f; break; // 2 case 50: rotate.angle_x += 2.0f; break; // 8 case 56: rotate.angle_x -= 2.0f; break; // 4 case 52: rotate.angle_y -= 2.0f; break; // 6 case 54: rotate.angle_y += 2.0f; break; case 27: exit(3); } } void display() { glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0, 0, -800); glRotatef(rotate.angle_x, 1, 0, 0); glRotatef(rotate.angle_y, 0, 1, 0); glScalef(zoom,zoom,zoom); // VAO - dont work glColor3f(1.0f,1.0f,1.0f); glBindVertexArray(vao); glDrawElements(GL_POINTS, 0, GL_UNSIGNED_INT, NULL); glBindVertexArray(0); // VBO - work // glEnableClientState( GL_VERTEX_ARRAY ); // glBindBuffer( GL_ARRAY_BUFFER, vId ); // glVertexPointer( 3, GL_FLOAT, 0, 0 ); // glDrawArrays(GL_POINTS, 0, cubeVerticesCount); // glDisableClientState(GL_VERTEX_ARRAY); // default - work // glEnable(GL_TEXTURE_2D); // glBindTexture(GL_TEXTURE_2D, tId); // // glBegin(GL_QUADS); // glColor3f(1.0f,1.0f,1.0f); // for(size_t i=0;i<cubeVerticesCount;i++) // { // glTexCoord2f( cubeTexcoords[i][0], cubeTexcoords[i][1] ); // glVertex3f( cubePositions[i][0], cubePositions[i][1], cubePositions[i][2] ); // } // glEnd(); calculateFPS(); glutSwapBuffers(); } void initGlut(int argc, char **argv) { // Initialize freeglut glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize(width, height); glutInitWindowPosition(100,100); glutCreateWindow( "Test OGL" ); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-200, 200, -200, 200, 100, 2000); glutKeyboardFunc(key); glutDisplayFunc(display); glutIdleFunc(display); glewInit(); } int main(int argc, char **argv) { initGlut(argc, argv); createVBO(&vId,&cId,&tId); createVAO(&vId,&cId,&tId,&vao); //tId = LoadTexture("lena.raw", 512, 512); glutMainLoop(); deleteVBO(&vId,&cId,&tId); return 0; } //function to load the RAW file GLuint LoadTexture( const char * filename, int width, int height ) { GLuint texture; unsigned char * data; FILE * file; //The following code will read in our RAW file file = fopen( filename, "rb" ); if ( file == NULL ) return 0; data = (unsigned char *)malloc( width * height * 3 ); fread( data, width * height * 3, 1, file ); fclose( file ); glGenTextures( 1, &texture ); //generate the texture with glBindTexture( GL_TEXTURE_2D, texture ); //bind the texture glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); //set texture environment parameters glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); //Here we are setting the parameter to repeat the texture glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); //Generate the texture glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); free( data ); //free the texture return texture; //return whether it was successfull }
Private
[
?
]
Run code
Submit