void init(void) { makeCheckImage(); generateCylinder(&cylinder); generatePerFaceNormals(&cylinder); generatePerVertexNormals(true, &cylinder, 30); }
Cylinder::Cylinder(const D3DXVECTOR3& position, float height, float radius, int stacks, int slices, uint32_t color, bool randomColor, IDirect3DDevice9 *d3dDevice, IDirect3DVertexShader9 *shader): Object(d3dDevice), m_vertexBuffer(0), m_indexBuffer(0), m_shader(shader), m_bones(0), m_time(0), m_height(height) { uint16_t *indices; Vertex *vertices; generateCylinder(stacks, slices, radius, height, color, randomColor, &indices, &vertices, m_indexCount, m_vertexCount); if(0 != m_d3dDevice) { if(!FAILED(m_d3dDevice->CreateVertexBuffer(m_vertexCount * sizeof(Vertex), D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &m_vertexBuffer, 0))) { void *vertexPointer; if(!FAILED(m_vertexBuffer->Lock(0, m_vertexCount * sizeof(Vertex), (void**)&vertexPointer, 0))) { memcpy(vertexPointer, vertices, m_vertexCount * sizeof(Vertex)); } if(FAILED(m_vertexBuffer->Unlock())) { m_vertexBuffer = 0; } } if(!FAILED(m_d3dDevice->CreateIndexBuffer(m_indexCount * sizeof(uint16_t), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_indexBuffer, 0))) { void *indexPointer; if(!FAILED(m_indexBuffer->Lock(0, m_indexCount * sizeof(uint16_t), (void**)&indexPointer, 0))) { memcpy(indexPointer, indices, m_indexCount * sizeof(uint16_t)); } if(FAILED(m_indexBuffer->Unlock())) { m_indexBuffer = 0; } } } setColor(color); delete[] indices; delete[] vertices; m_bones = new D3DXMATRIX[2]; D3DXMatrixIdentity(&m_bones[0]); D3DXMatrixIdentity(&m_bones[1]); D3DXMatrixTranspose(&m_bones[0], &m_bones[0]); D3DXMatrixTranspose(&m_bones[1], &m_bones[1]); m_d3dDevice->SetVertexShader(m_shader); setPosition(position); }
af::Mesh<float>::Ptr af::generateCylinder (Eigen::Vector3f &orig, Eigen::Vector3f &tip, const int num_circle_samples, const int num_length_samples, const double radius) { double length = (orig - tip).norm (); af::Mesh<float>::Ptr mesh = generateCylinder (radius, length, num_circle_samples, num_length_samples); /// Rotate the cylinder float angle = acos (Eigen::Vector3f::UnitZ ().dot ((tip - orig).normalized ())); Eigen::Vector3f axis = (Eigen::Vector3f::UnitZ ().cross ((tip - orig).normalized ())).normalized (); Eigen::Matrix3f rot = Eigen::AngleAxisf (angle, axis).matrix (); std::cerr << "rotation matrix:\n" << rot << std::endl; for (size_t v_i = 0; v_i < mesh->vertices_.cols (); ++v_i) mesh->vertices_.col (v_i) = rot * mesh->vertices_.col (v_i) + orig; return (mesh); }
// Performs most of the OpenGL intialization -- change these with care, if you must. void myinit(void) { #ifndef EMSCRIPTEN GLuint program = InitShader( "../my code/vshader.glsl", "../my code/fshader.glsl" ); // Load shaders and use the resulting shader program #else GLuint program = InitShader( "vshader.glsl", "fshader.glsl" ); // Load shaders and use the resulting shader program #endif glUseProgram(program); // Generate vertex arrays for geometric shapes generateCube(program, &cubeData); generateSphere(program, &sphereData); generateCone(program, &coneData); generateCylinder(program, &cylData); uModelView = glGetUniformLocation( program, "ModelView" ); uProjection = glGetUniformLocation( program, "Projection" ); uView = glGetUniformLocation( program, "View" ); glClearColor( 0.1, 0.1, 0.2, 1.0 ); // dark blue background uAmbient = glGetUniformLocation( program, "AmbientProduct" ); uDiffuse = glGetUniformLocation( program, "DiffuseProduct" ); uSpecular = glGetUniformLocation( program, "SpecularProduct" ); uLightPos = glGetUniformLocation( program, "LightPosition" ); uShininess = glGetUniformLocation( program, "Shininess" ); uTex = glGetUniformLocation( program, "Tex" ); uEnableTex = glGetUniformLocation( program, "EnableTex" ); glUniform4f(uAmbient, 0.2f, 0.2f, 0.2f, 1.0f); glUniform4f(uDiffuse, 0.6f, 0.6f, 0.6f, 1.0f); glUniform4f(uSpecular, 0.2f, 0.2f, 0.2f, 1.0f); glUniform4f(uLightPos, 15.0f, 15.0f, 30.0f, 0.0f); glUniform1f(uShininess, 100.0f); glEnable(GL_DEPTH_TEST); Arcball = new BallData; Ball_Init(Arcball); Ball_Place(Arcball,qOne,0.75); }
/************************************************************** * * add the static menu bars * *************************************************************/ void MainWindow::addStaticMenuBarContent() { qDebug("Beginning addStaticMenuBarContent"); //File QPopupMenu* fileMenu = _menus.find(FILE_KEY); if(!fileMenu) { std::cout << "PROUT" << std::endl; QPopupMenu * file = new QPopupMenu( this ); _menus.insert(FILE_KEY, file); menuBar()->insertItem( FILE_KEY, _menus[FILE_KEY]); } _menus[FILE_KEY]->insertSeparator(); //Cylinder Menu QPopupMenu* cylinderMenu = _menus.find(CYLINDER_KEY); if (!cylinderMenu) { QPopupMenu * help = new QPopupMenu( this ); _menus.insert(CYLINDER_KEY, help); menuBar()->insertItem( CYLINDER_KEY, _menus[CYLINDER_KEY]); } QAction* generateCylinderAction = new QAction("generate", QIconSet( QPixmap(IMAGES_DIR + CYLINDER_IMAGE) ), "&Generate", CTRL+Key_G, this); connect(generateCylinderAction, SIGNAL( activated() ), this, SLOT( generateCylinder() ) ); generateCylinderAction->addTo(_menus[CYLINDER_KEY]); _toolBar->addSeparator(); generateCylinderAction->addTo(_toolBar); QAction* quitAction = new QAction("quit", QIconSet( QPixmap(IMAGES_DIR + QUIT_IMAGE) ), "&Quit", CTRL+Key_Q, this); connect(quitAction, SIGNAL( activated() ), qApp, SLOT( quit() ) ); quitAction->addTo(_menus[FILE_KEY]); _toolBar->addSeparator(); quitAction->addTo(_toolBar); //Tools Menu QPopupMenu* toolsMenu = _menus.find(TOOLS_KEY); if (!toolsMenu) { QPopupMenu * tools = new QPopupMenu( this ); _menus.insert(TOOLS_KEY, tools); menuBar()->insertItem( TOOLS_KEY, _menus[TOOLS_KEY]); } //_menus[TOOLS_KEY]->insertItem("Validate Solid", this, SLOT( validation() ) ); //Help Menu with about QPopupMenu* helpMenu = _menus.find(HELP_KEY); if (!helpMenu) { QPopupMenu * help = new QPopupMenu( this ); _menus.insert(HELP_KEY, help); menuBar()->insertItem( HELP_KEY, _menus[HELP_KEY]); } _menus[HELP_KEY]->insertItem( "Getting &Started", this, SLOT(manual())); _menus[HELP_KEY]->insertItem( "&About", this, SLOT(about())); }
void myinit(void) { // Load shaders and use the resulting shader program GLuint program = InitShader( "vshader.glsl", "fshader.glsl" ); glUseProgram(program); // Generate vertex arrays for geometric shapes generateCube(program, &cubeData); generateSphere(program, &sphereData); generateCone(program, &coneData); generateCylinder(program, &cylData); generatePyramid(program, &pyramidData); uModelView = glGetUniformLocation( program, "ModelView" ); uProjection = glGetUniformLocation( program, "Projection" ); uView = glGetUniformLocation( program, "View" ); glClearColor( .1, .1, .6, 1.0 ); // dark blue background uAmbient = glGetUniformLocation( program, "AmbientProduct" ); uDiffuse = glGetUniformLocation( program, "DiffuseProduct" ); uSpecular = glGetUniformLocation( program, "SpecularProduct" ); uLightPos = glGetUniformLocation( program, "LightPosition" ); uShininess = glGetUniformLocation( program, "Shininess" ); uTex = glGetUniformLocation( program, "Tex" ); uEnableTex = glGetUniformLocation( program, "EnableTex" ); glUniform4f(uAmbient, 0.2f, 0.2f, 0.2f, 1.0f); glUniform4f(uDiffuse, 0.6f, 0.6f, 0.6f, 1.0f); glUniform4f(uSpecular, 0.2f, 0.2f, 0.2f, 1.0f); glUniform4f(uLightPos, 15.0f, 15.0f, 30.0f, 0.0f); glUniform1f(uShininess, 100.0f); glEnable(GL_DEPTH_TEST); TgaImage coolImage; if (!coolImage.loadTGA("dirt.tga")) { printf("Error loading image file\n"); exit(1); } TgaImage earthImage; if (!earthImage.loadTGA("earth.tga")) { printf("Error loading image file\n"); exit(1); } TgaImage roofImage; if (!roofImage.loadTGA("roof.tga")) { printf("Error loading image file\n"); exit(1); } TgaImage treeImage; if (!treeImage.loadTGA("tree.tga")) { printf("Error loading image file\n"); exit(1); } TgaImage skyImage; if (!skyImage.loadTGA("sky.tga")) { printf("Error loading image file\n"); exit(1); } TgaImage stoneImage; if (!stoneImage.loadTGA("stone.tga")) { printf("Error loading image file\n"); exit(1); } TgaImage leafImage; if (!leafImage.loadTGA("leaf.tga")) { printf("Error loading image file\n"); exit(1); } glGenTextures( 1, &texture_cube ); glBindTexture( GL_TEXTURE_2D, texture_cube ); glTexImage2D(GL_TEXTURE_2D, 0, 4, coolImage.width, coolImage.height, 0, (coolImage.byteCount == 3) ? GL_BGR : GL_BGRA, GL_UNSIGNED_BYTE, coolImage.data ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glGenTextures( 1, &texture_earth ); glBindTexture( GL_TEXTURE_2D, texture_earth ); glTexImage2D(GL_TEXTURE_2D, 0, 4, earthImage.width, earthImage.height, 0, (earthImage.byteCount == 3) ? GL_BGR : GL_BGRA, GL_UNSIGNED_BYTE, earthImage.data ); //roof glGenTextures( 1, &texture_roof ); glBindTexture( GL_TEXTURE_2D, texture_roof ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexImage2D(GL_TEXTURE_2D, 0, 4, roofImage.width, roofImage.height, 0, (roofImage.byteCount == 3) ? GL_BGR : GL_BGRA, GL_UNSIGNED_BYTE, roofImage.data ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glGenTextures( 1, &texture_trees ); glBindTexture( GL_TEXTURE_2D, texture_trees ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexImage2D(GL_TEXTURE_2D, 0, 4, treeImage.width, treeImage.height, 0, (treeImage.byteCount == 3) ? GL_BGR : GL_BGRA, GL_UNSIGNED_BYTE, treeImage.data ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glGenTextures( 1, &texture_leaf ); glBindTexture( GL_TEXTURE_2D, texture_leaf ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexImage2D(GL_TEXTURE_2D, 0, 4, leafImage.width, leafImage.height, 0, (leafImage.byteCount == 3) ? GL_BGR : GL_BGRA, GL_UNSIGNED_BYTE, leafImage.data ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glGenTextures( 1, &texture_stone ); glBindTexture( GL_TEXTURE_2D, texture_stone ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexImage2D(GL_TEXTURE_2D, 0, 4, stoneImage.width, stoneImage.height, 0, (stoneImage.byteCount == 3) ? GL_BGR : GL_BGRA, GL_UNSIGNED_BYTE, stoneImage.data ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); // Set texture sampler variable to texture unit 0 // (set in glActiveTexture(GL_TEXTURE0)) glUniform1i( uTex, 0); Arcball = new BallData; Ball_Init(Arcball); Ball_Place(Arcball,qOne,0.75); }
bool Engine::init(int width, int height, HINSTANCE hInstance) { WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, ::WindowProc, 0, 0, hInstance, 0, 0, 0, 0, "CG Task3", 0}; if (0 == RegisterClassEx(&wc)) { return false; } m_window = CreateWindow("CG Task3", "CG Task3: Cylinder", (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX), 0, 0, width, height, GetDesktopWindow(), 0, hInstance, 0); if(NULL == m_window) { return false; } m_d3d = Direct3DCreate9(D3D_SDK_VERSION); if(!m_d3d) { return false; } D3DPRESENT_PARAMETERS d3dpp = {0}; d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; HRESULT hr = m_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_window, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &m_d3dDevice); if(FAILED(hr) || !m_d3dDevice) { return false; } m_d3dDevice->SetRenderState(D3DRS_AMBIENT, AMBIENT_COLOR); m_d3dDevice->SetRenderState(D3DRS_LIGHTING, false); m_d3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); m_d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); m_d3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); m_d3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); //m_d3dDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); D3DVERTEXELEMENT9 vertexDeclaration[] = { {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {0, 3 * sizeof(float), D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0}, D3DDECL_END()}; IDirect3DVertexDeclaration9 *decl; if(FAILED(m_d3dDevice->CreateVertexDeclaration(vertexDeclaration, &decl))) { return false; } if(FAILED(m_d3dDevice->SetVertexDeclaration(decl))) { return false; } decl->Release(); Vertex *vertices; uint16_t *indices; generateCylinder(CYLINDER_STACKS, CYLINDER_SLICES, CYLINDER_RADIUS, CYLINDER_HEIGHT, 0x80000000, true, &indices, &vertices, m_indexCount, m_vertexCount); if((0 == vertices) || (0 == indices)) { return false; } if(FAILED(m_d3dDevice->CreateVertexBuffer(m_vertexCount * sizeof(Vertex), D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &m_vertexBuffer, 0))) { return false; } void *vertexPointer; if(FAILED(m_vertexBuffer->Lock(0, m_vertexCount * sizeof(Vertex), (void**)&vertexPointer, 0))) { return false; } memcpy(vertexPointer, vertices, m_vertexCount * sizeof(Vertex)); if(FAILED(m_vertexBuffer->Unlock())) { return false; } if(FAILED(m_d3dDevice->CreateIndexBuffer(m_indexCount * sizeof(uint16_t), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_indexBuffer, 0))) { return false; } void *indexPointer; if(FAILED(m_indexBuffer->Lock(0, m_indexCount * sizeof(uint16_t), (void**)&indexPointer, 0))) { return false; } memcpy(indexPointer, indices, m_indexCount * sizeof(uint16_t)); if(FAILED(m_indexBuffer->Unlock())) { return false; } #include "shader.h" if (FAILED(m_d3dDevice->CreateVertexShader(g_vs11_main, &m_shader))) { return false; } m_mainCamera.setDirection(DEFAULT_CAMERA_LOOK, DEFAULT_CAMERA_UP); D3DXVECTOR3 look = m_mainCamera.look(); m_mainCamera.setPosition(*D3DXVec3Scale(&look, &look, -m_radius)); D3DXMatrixIdentity(&m_worldMatrix); resize(width, height); m_bones = new D3DXMATRIX[2]; D3DXMatrixIdentity(&m_bones[0]); D3DXMatrixIdentity(&m_bones[1]); D3DXMatrixTranspose(&m_bones[0], &m_bones[0]); D3DXMatrixTranspose(&m_bones[1], &m_bones[1]); m_d3dDevice->SetVertexShader(m_shader); if(FAILED(m_d3dDevice->SetVertexShaderConstantF(4, &(m_bones[0].m[0][0]), 4))) { return false; } if(FAILED(m_d3dDevice->SetVertexShaderConstantF(8, &(m_bones[1].m[0][0]), 4))) { return false; } float sizeConst[4]; sizeConst[0] = 1 / CYLINDER_HEIGHT; if(FAILED(m_d3dDevice->SetVertexShaderConstantF(12, sizeConst, 1))) { return false; } updateMatrices(); return true; }
void Cylinder::init(const char *textureFile) { m_textureOn = (textureFile[0] != '\0')? true: false; // Allocate space for vertices memAlloc(); m_circlePoints = new vec2[m_numDivisions]; generateCylinder(); // generate cylinder points // Create a vertex array object #ifdef __APPLE__ glGenVertexArraysAPPLE(1, &m_vertexArrayObject); glBindVertexArrayAPPLE(m_vertexArrayObject); #else glGenVertexArrays(1, &m_vertexArrayObject); glBindVertexArray(m_vertexArrayObject); #endif // Create a buffer object glGenBuffers(1, &m_vertexBufferObject); glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferObject); // Initialize buffer glBufferData(GL_ARRAY_BUFFER, (sizeof(m_vertices[0]) + sizeof(m_normals[0]) + sizeof(m_textureUV[0]))*m_numVertices + sizeof(m_edges[0])*m_numEdges, NULL, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(m_vertices[0])*m_numVertices, m_vertices); glBufferSubData(GL_ARRAY_BUFFER, sizeof(m_vertices[0])*m_numVertices, sizeof(m_edges[0])*m_numEdges, m_edges); glBufferSubData(GL_ARRAY_BUFFER, sizeof(m_vertices[0])*m_numVertices + sizeof(m_edges[0])*m_numEdges, sizeof(m_normals[0])*m_numVertices, m_normals); glBufferSubData(GL_ARRAY_BUFFER, (sizeof(m_vertices[0]) + sizeof(m_normals[0]))*m_numVertices + sizeof(m_edges[0])*m_numEdges, sizeof(m_textureUV[0])*m_numVertices, m_textureUV); // Load shaders and use the resulting shader program m_program = InitShader("vshader.glsl", "fshader.glsl"); glUseProgram(m_program); // Specify vertex positions as an input to the shader program GLuint position = glGetAttribLocation(m_program, "vPosition"); glEnableVertexAttribArray(position); glVertexAttribPointer(position, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); // Specify normal vectors as an input to the shader program GLuint normal = glGetAttribLocation(m_program, "vNormal"); glEnableVertexAttribArray(normal); glVertexAttribPointer(normal, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(m_vertices[0])*m_numVertices + sizeof(m_edges[0])*m_numEdges)); // Specify texture positions as an input to the shader program GLuint texture = glGetAttribLocation(m_program, "vTexture"); glEnableVertexAttribArray(texture); glVertexAttribPointer(texture, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET((sizeof(m_vertices[0]) + sizeof(m_normals[0]))*m_numVertices + sizeof(m_edges[0])*m_numEdges)); // Load image TgaImage image; if(m_textureOn && !image.loadTGA(textureFile)) { printf("Error loading image file\n"); exit(1); } // Bind texture glGenTextures(1, &m_textureBufferObject); glBindTexture(GL_TEXTURE_2D, m_textureBufferObject); glTexImage2D(GL_TEXTURE_2D, 0, 4, image.width, image.height, 0, (image.byteCount == 3) ? GL_BGR : GL_BGRA, GL_UNSIGNED_BYTE, image.data); glGenerateMipmap(GL_TEXTURE_2D); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glUniform1i(glGetUniformLocation(m_program, "Tex"), 0); // Clean up delete [] m_vertices; delete [] m_edges; delete [] m_textureUV; delete [] m_normals; delete [] m_circlePoints; }
void initPrimitives(GLuint program) { generateCube ( program, &cubeData ); generateSphere ( program, &sphereData ); generateCone ( program, &coneData ); generateCylinder( program, &cylData ); }
void myinit(void) { // Load shaders and use the resulting shader program GLuint program = InitShader( "vshader.glsl", "fshader.glsl" ); glUseProgram(program); // Generate vertex arrays for geometric shapes generateGrass(program, &grassData); generateHeart(program, &heartData); generateCube(program, &cubeData); generateSphere(program, &sphereData); generateCone(program, &coneData); generateCylinder(program, &cylData); uModelView = glGetUniformLocation( program, "ModelView" ); uProjection = glGetUniformLocation( program, "Projection" ); uView = glGetUniformLocation( program, "View" ); glClearColor( 0.5, 0.5, 1.0, 1.0 ); // dark blue background uAmbient = glGetUniformLocation( program, "AmbientProduct" ); uDiffuse = glGetUniformLocation( program, "DiffuseProduct" ); uSpecular = glGetUniformLocation( program, "SpecularProduct" ); uLightPos = glGetUniformLocation( program, "LightPosition" ); uShininess = glGetUniformLocation( program, "Shininess" ); uTex = glGetUniformLocation( program, "Tex" ); uEnableTex = glGetUniformLocation( program, "EnableTex" ); glUniform4f(uAmbient, 0.2f, 0.2f, 0.2f, 1.0f); glUniform4f(uDiffuse, 0.6f, 0.6f, 0.6f, 1.0f); glUniform4f(uSpecular, 0.2f, 0.2f, 0.2f, 1.0f); glUniform4f(uLightPos, 15.0f, 15.0f, 30.0f, 0.0f); glUniform1f(uShininess, 100.0f); glEnable(GL_DEPTH_TEST); TgaImage coolImage; if (!coolImage.loadTGA("challenge.tga")) { printf("Error loading image file\n"); exit(1); } GLuint texture; glGenTextures( 1, &texture ); glBindTexture( GL_TEXTURE_2D, texture ); glTexImage2D(GL_TEXTURE_2D, 0, 4, coolImage.width, coolImage.height, 0, (coolImage.byteCount == 3) ? GL_BGR : GL_BGRA, GL_UNSIGNED_BYTE, coolImage.data ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, texture ); // Set texture sampler variable to texture unit 0 // (set in glActiveTexture(GL_TEXTURE0)) glUniform1i( uTex, 0 ); Arcball = new BallData ; Ball_Init(Arcball); Ball_Place(Arcball,qOne,0.75); }