I am trying to draw the shape shown below (in black) in OpenGL (for desktops, not mobile). I'd prefer it in 3D, but 2D would suffice as well. I will be mapping a texture to it as well. I've used triangle fans/strips for other things in the past and I imagine that's what I'd need to use here but I'm just not sure where to start on it. I've drawn the WHITE part before, but never the inverse (the black part). Any thoughts or guidance on what to use (triangle fan, triangle strip, some other odd OpenGL shape I probably didn't know existed, etc...)
Final Solution:
void draw_arch(GLfloat width, GLfloat height, GLint slices)
{
glPushMatrix();
GLfloat offset = 0.5f;
glScalef(width/2,height/(1+offset),1.0f);
glBegin(GL_QUADS);
for( unsigned int i = 0; i < slices; ++i ) {
float curAngle = ( ( i + 0 ) / (float)slices ) * 3.14159;
float nxtAngle = ( ( i + 1 ) / (float)slices ) * 3.14159;
glVertex2f( cos( curAngle ), sin( curAngle ) );
glVertex2f( cos( curAngle ), 1.0f + offset );
glVertex2f( cos( nxtAngle ), 1.0f + offset );
glVertex2f( cos( nxtAngle ), sin( nxtAngle ) );
}
glEnd();
glPopMatrix();
}
I can adjust the "offset" variable to make different looking arches, however in this application, I choose 0.5 to make it look the way I wanted it to!
Generate the top half of a circle and an offset above it and link the two with quads/triangles:
#include <GL/glut.h>
#include <cmath>
void glShape( const float height, unsigned int segs )
{
glBegin( GL_QUADS );
for( unsigned int i = 0; i < segs; ++i )
{
float curAngle = ( ( i + 0 ) / (float)segs ) * 3.14159;
float nxtAngle = ( ( i + 1 ) / (float)segs ) * 3.14159;
glVertex2f( cos( curAngle ), sin( curAngle ) );
glVertex2f( cos( curAngle ), 1 + height );
glVertex2f( cos( nxtAngle ), 1 + height );
glVertex2f( cos( nxtAngle ), sin( nxtAngle ) );
}
glEnd();
}
void display()
{
glClearColor( 0, 0, 0, 1 );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
double ar = w / h;
glOrtho( -2 * ar, 2 * ar, -2, 2, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3ub( 255, 0, 0 );
glShape( 0.1f, 20 );
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
Probably not the minimal number of quads/triangles but it's quick and easy :)
I just started with DirectX learning and decided to write a simple classic game: SpaceBattle. But now I have some performance problems mainly related to Render (I guess not to Update). As a result: I have 150-200 FPS with maximum 80-100 polygons. If I comment Render method, FPS grows up to 2500-3000. So here is my Render method:
void DX11Game::Render()
{
if( d3dContext_ == 0 )
return;
stride = sizeof( VertexPT );
offset = 0;
d3dContext_->IASetInputLayout(inputLayout);
d3dContext_->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
d3dContext_->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
d3dContext_->VSSetShader(VS, 0, 0);
d3dContext_->PSSetShader(PS, 0, 0);
d3dContext_->PSSetSamplers(0, 1, &samplerState);
d3dContext_->VSSetConstantBuffers(0, 1, &cbMatricesBuffer);
d3dContext_->PSSetConstantBuffers(1, 1, &cbParametersBuffer);
switch (DX11Game::gameState)
{
case GAME_STATE::GAME_STATE_PLAY:
d3dContext_->ClearRenderTargetView(backBufferTarget_, backColor);
DrawAsteroids();
DrawShip();
DrawUserBullets();
DrawEnemies();
DrawGUI();
break;
case GAME_STATE::GAME_STATE_GAMEOVER:
d3dContext_->ClearRenderTargetView(backBufferTarget_, backColor);
DrawAsteroids();
DrawShip();
DrawUserBullets();
DrawGameOver();
break;
case GAME_STATE::GAME_STATE_SCORES:
break;
}
swapChain_->Present(0, 0);
}
And here is one of the methods that draws game objects. Others Draws is similar to this:
void DX11Game::DrawAsteroids()
{
std::vector<std::shared_ptr<Asteroid>>* asteroids = gameLogic->GetAsteroids();
std::vector<std::shared_ptr<Asteroid>>::iterator it = asteroids->begin();
while (it != asteroids->end())
{
float SIZE = *((*it)->GetSize());
float* temp = (*it)->GetTexCoord();
XMFLOAT4 texBounds = XMFLOAT4( temp[0], temp[1], temp[2], temp[3] );
vertices[0].pos = XMFLOAT3( SIZE, SIZE, 1.0f );
vertices[0].tex0 = XMFLOAT2( texBounds.x + texBounds.z, texBounds.y );
vertices[1].pos = XMFLOAT3( SIZE, -SIZE, 1.0f );
vertices[1].tex0 = XMFLOAT2( texBounds.x + texBounds.z, texBounds.y + texBounds.w );
vertices[2].pos = XMFLOAT3( -SIZE, -SIZE, 1.0f );
vertices[2].tex0 =XMFLOAT2( texBounds.x,texBounds.y + texBounds.w );
vertices[3].pos = XMFLOAT3( -SIZE, -SIZE, 1.0f );
vertices[3].tex0 =XMFLOAT2( texBounds.x, texBounds.y + texBounds.w );
vertices[4].pos = XMFLOAT3( -SIZE, SIZE, 1.0f );
vertices[4].tex0 = XMFLOAT2( texBounds.x, texBounds.y );
vertices[5].pos = XMFLOAT3( SIZE, SIZE, 1.0f );
vertices[5].tex0 =XMFLOAT2( texBounds.x +texBounds.z, texBounds.y );
XMMATRIX world = XMMatrixTranslation(
( *it )->GetPosition()[0],
( *it )->GetPosition()[1],
0.0f );
world = XMMatrixTranspose(world);
d3dContext_->PSSetShaderResources(0, 1, &texGameSprites);
d3dContext_->UpdateSubresource(vertexBuffer, 0, 0, vertices, 0, 0);
CB_Matrices matrices;
matrices.World = world;
matrices.Projection = proj;
d3dContext_->UpdateSubresource(cbMatricesBuffer, 0, 0, &matrices, 0, 0);
CB_Parameters params;
params.isTextured = true;
params.color[0] = 1.0f; params.color[1] = 1.0f;
params.color[2] = 0.0f; params.color[3] = 1.0f;
d3dContext_->UpdateSubresource(cbParametersBuffer, 0, 0, ¶ms, 0, 0);
d3dContext_->Draw(6, 0);
it++;
}
}
So what I have to do to optimize my game?
Thanks!
I want to draw a thick Arc(something like colored segment of analog dial) using polygon. For that i have added vertices in polygon and its working fine for the outer circumference BUT its joining the ends for inner circumference(the concave side).
The same logic works fine if I add those vertices in Line, but that creates an empty/non-filled arc.
My logic of adding vertices is :
for( float i = m_segmentVertex.size() - 1; i < vCount; i++ )
{
float x1 = (m_segmentVertex[ i ].x ) * cosA - m_segmentVertex[ i ].y * sinA;
float y1 = (m_segmentVertex[ i ].x ) * sinA + m_segmentVertex[ i ].y * cosA;
addVertex( vec3( x1, y1, 0.0f ) );
}
Be aware that GL_POLYGON only works with convex polygons.
You'll have to triangulate concave polygons.
Try using a triangle fan and making the center of your dial the first point.
Possibly addVertex( vec3( 0.0f, 0.0f, 0.0f ) ); before your loop.
I'd also recommend making i an int or unsigned int, a float here doesn't make sense.
This is how I created the polygon dynamically by triangulating it :
//create thick colored segments
void CreateArcMesh( float sAngle, float eAngle, vec4 color, int thickness, int radius )
{
ObjectMeshDynamic meshObj = new ObjectMeshDynamic();
vec3 vertex[0];
float dAngle = ( ( eAngle - sAngle ) / ( VERTEX_COUNT / 2.0f ) );
float cosA = cos( DEG2RAD * dAngle );
float sinA = sin( DEG2RAD * dAngle );
meshObj.setMaterial( "material_base", "*" );
meshObj.setProperty( "surface_base", "*" );
meshObj.setMaterialParameter( "diffuse_color", color, 0 );
//Add the material on both side as the indices for Triangle strip start from last vertex added
Material material = meshObj.getMaterialInherit(0);
material.setTwoSided( 1 );
meshObj.addTriangleStrip( VERTEX_COUNT + 2 );
vec3 startPos = vec3( radius * cos( DEG2RAD * sAngle ), radius * sin( DEG2RAD * sAngle ), 0.0f );
vertex.append( startPos );
vec3 secondPos = vec3( ( radius - thickness ) * cos( DEG2RAD * sAngle ), ( radius - thickness ) * sin( DEG2RAD * sAngle ), 0.0f );
vertex.append( secondPos );
float x1 = startPos.x * cosA - startPos.y * sinA;
float y1 = startPos.x * sinA + startPos.y * cosA;
vertex.append( vec3( x1, y1, 0.0f ) );
x1 = secondPos.x * cosA - secondPos.y * sinA;
y1 = secondPos.x * sinA + secondPos.y * cosA;
vertex.append( vec3( x1, y1, 0.0f ) );
forloop( int k = 0 ; VERTEX_COUNT + 2 )
{
x1 = ( vertex[ vertex.size() - 2 ].x ) * cosA - vertex[ vertex.size() - 2 ].y * sinA;
y1 = ( vertex[ vertex.size() - 2 ].x ) * sinA + vertex[ vertex.size() - 2 ].y * cosA;
vertex.append( vec3( x1, y1, 0.0f ) );
meshObj.addVertex( vertex[k] );
}
vertex.clear();
meshObj.updateBounds();
meshObj.flush();
}
As per my question on Math Stackexchange:
I am working on a project for my 3D Graphics class. The project is built with C++ and OpenGL / Glut. Basically, I create a horizontal rectangle window, subdivided into two squares. On the left, I have a two dimensional coordinate plane, which allows the users to point and click and define a profile 'curve'. I then need to wrap this curve around the Y-axis n number of times.
So, would anyone be able to guide me as to how I would use Trigonometry to calculate the X and Z values of the successive points? If for example, a user clicks and creates the point:
(1, 1, 0)
And their sweep resolution (n) is set to, say, 10, then I need to redraw that point every 36 (360/10) degrees around the Y-axis.
Am I correct in assuming that Trigonometry will help me here? If so, can someone please enlighten me a bit as to how to calculate the location of a translated point in 3D space? It's been a while since I took Trig, and I don't believe we ever left 2D space.
EDIT: I attempted to use:
x'=xcos(theta)-zsin(theta)
y'=y
z'=xsin(theta)+zcos(theta)
, as per my understanding of AMPerrine's answer, and I don't think it worked as I'd hoped:
// this is in a loop
// setup the new angle
double angle = i>0 ? (360/sweepResolutionMod)*i : 0;
angle = angle * (M_PI/180);
// for each point...
for( int i=0; i<clickedPoints.size(); i++ )
{
// initial point, normalized
GLfloat tempX = (clickedPoints[i].x-250)/250;
GLfloat tempY = (clickedPoints[i].y-250)/250;
GLfloat tempZ = 0.0;
// log the initial point
cout << "(" << tempX << ", " << tempY << ", 0.0) by " << angle << " radians = ";
// generate the new point
GLfloat newX = (tempX * cos(angle)) - (tempZ * sin(angle));
GLfloat newY = tempY;
GLfloat newZ = (tempX * sin(angle)) - (tempZ * cos(angle));
// log the new point
cout << "(" << newX << ", " << newY << ", " << newZ << ")\n";
// render the new point
glVertex3d(newX, newY, newZ);
}
This produces no screen output, but console output of:
(0.048, -0.296, 0.0) by 0 radians = (0.048, -0.296, 0)
(0.376, -0.508, 0.0) by 0 radians = (0.376, -0.508, 0)
(0.72, -0.204, 0.0) by 0 radians = (0.72, -0.204, 0)
(0.652, 0.176, 0.0) by 0 radians = (0.652, 0.176, 0)
(0.368, 0.504, 0.0) by 0 radians = (0.368, 0.504, 0)
(0.048, -0.296, 0.0) by 0.628319 radians = (0.0388328, -0.296, 0.0282137)
(0.376, -0.508, 0.0) by 0.628319 radians = (0.30419, -0.508, 0.221007)
(0.72, -0.204, 0.0) by 0.628319 radians = (0.582492, -0.204, 0.423205)
(0.652, 0.176, 0.0) by 0.628319 radians = (0.527479, 0.176, 0.383236)
(0.368, 0.504, 0.0) by 0.628319 radians = (0.297718, 0.504, 0.216305)
(0.048, -0.296, 0.0) by 1.25664 radians = (0.0148328, -0.296, 0.0456507)
(0.376, -0.508, 0.0) by 1.25664 radians = (0.11619, -0.508, 0.357597)
(0.72, -0.204, 0.0) by 1.25664 radians = (0.222492, -0.204, 0.684761)
(0.652, 0.176, 0.0) by 1.25664 radians = (0.201479, 0.176, 0.620089)
(0.368, 0.504, 0.0) by 1.25664 radians = (0.113718, 0.504, 0.349989)
...
(0.048, -0.296, 0.0) by 6.28319 radians = (0.048, -0.296, -1.17566e-17)
(0.376, -0.508, 0.0) by 6.28319 radians = (0.376, -0.508, -9.20934e-17)
(0.72, -0.204, 0.0) by 6.28319 radians = (0.72, -0.204, -1.76349e-16)
(0.652, 0.176, 0.0) by 6.28319 radians = (0.652, 0.176, -1.59694e-16)
(0.368, 0.504, 0.0) by 6.28319 radians = (0.368, 0.504, -9.0134e-17)
I'm not sure what exactly is going on here, but I'm having a terrible time trying to figure it out, so please don't think I'm trying to get double reputation or anything, I'm just really stuck.
EDIT 2: Here is my whole display routine for my perspective subview:
void displayPersp(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
gluLookAt (-2.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0);
// draw the axis
glBegin(GL_LINES);
// x
glVertex3f(500.0, 0.0, 0.0);
glVertex3f(-500.0, 0.0, 0.0);
// y
glVertex3f(0.0, -500.0, 0.0);
glVertex3f(0.0, 500.0, 0.0);
// z
glVertex3f(0.0, 0.0, -500.0);
glVertex3f(0.0, 0.0, 500.0);
glEnd();
cout << endl;
// loop as many number of times as we are going to draw the points around the Y-Axis
for( int i=0; i<=sweepResolutionMod; i++ )
{
cout << endl;
// setup the new angle
double angle = i>0 ? (360/sweepResolutionMod)*i : 0;
angle = angle * (M_PI/180);
// for each point...
for( int i=0; i<clickedPoints.size(); i++ )
{
GLfloat tempX = (clickedPoints[i].x-250)/250;
GLfloat tempY = (clickedPoints[i].y-250)/250;
GLfloat tempZ = 0.0;
cout << "(" << tempX << ", " << tempY << ", 0.0) by " << angle << " degrees = ";
GLfloat newX = (tempX * cos(angle)) - (tempZ * sin(angle));
GLfloat newY = tempY;
GLfloat newZ = (tempX * sin(angle)) - (tempZ * cos(angle));
cout << "(" << newX << ", " << newY << ", " << newZ << ")\n";
glVertex3d(newX, newY, newZ);
}
// the following was my old solution, using OpenGL's rotate(), but that
// didn't allow me to get back the new point's coordinates.
/*
glRotatef(angle, 0.0, 1.0, 0.0);
// draw a line?
if( clickedPoints.size() > 1 )
{
glBegin(GL_LINE_STRIP);
for(int i=0; i<clickedPoints.size(); i++ )
{
glVertex3f((clickedPoints[i].x-250)/250, (clickedPoints[i].y-250)/250, 0.0);
}
glEnd();
}
// everyone gets points
glBegin(GL_POINTS);
for(int i=0; i<clickedPoints.size(); i++ )
{
glVertex3f((clickedPoints[i].x-250)/250, (clickedPoints[i].y-250)/250, 0.0);
}
glEnd();
*/
}
glutSwapBuffers();
}
EDIT 3: Here is a terrible illustration that illustrates what I need to do. I know the perspective seems off, but what I'm attempting to acquire is the green 'horizontals' in the right subview (this is using the commented out glRotatef() code above):
FINAL EDIT (for future generations!):
Here is what I finally got working, after discussing some linear algebra with a teacher at college:
void displayPersp(void)
{
glClear(GL_COLOR_BUFFER_BIT);
gluLookAt (-2.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
// draw the axis
glBegin(GL_LINES);
// x
glVertex3f(500.0, 0.0, 0.0);
glVertex3f(-500.0, 0.0, 0.0);
// y
glVertex3f(0.0, -500.0, 0.0);
glVertex3f(0.0, 500.0, 0.0);
// z
glVertex3f(0.0, 0.0, -500.0);
glVertex3f(0.0, 0.0, 500.0);
glEnd();
cout << endl;
double previousTheta = 0.0;
for( int i=0; i<=sweepResolutionMod; i++ )
{
double theta = i>0 ? (360/sweepResolutionMod)*i : 0;
theta = theta * (M_PI/180);
if( clickedPoints.size() > 1 )
{
// the 'vertical' piece
glBegin(GL_LINE_STRIP);
for(int i=0; i<clickedPoints.size(); i++ )
{
// normalize
GLfloat tempX = (clickedPoints[i].x-250)/250;
GLfloat tempY = (clickedPoints[i].y-250)/250;
GLfloat tempZ = 0.0;
// new points
GLfloat newX = ( tempX * cos(theta) ) + ( tempZ * sin(theta) );
GLfloat newY = tempY;
GLfloat newZ = ( tempZ * cos(theta) ) - ( tempX * sin(theta) );
glVertex3f(newX, newY, newZ);
}
glEnd();
// the 'horizontal' piece
if( previousTheta != theta )
{
glBegin(GL_LINES);
for(int i=0; i<clickedPoints.size(); i++ )
{
// normalize
GLfloat tempX = (clickedPoints[i].x-250)/250;
GLfloat tempY = (clickedPoints[i].y-250)/250;
GLfloat tempZ = 0.0;
// new points
GLfloat newX = ( tempX * cos(theta) ) + ( tempZ * sin(theta) );
GLfloat newY = tempY;
GLfloat newZ = ( tempZ * cos(theta) ) - ( tempX * sin(theta) );
// previous points
GLfloat previousX = ( tempX * cos(previousTheta) ) + ( tempZ * sin(previousTheta) );
GLfloat previousY = tempY;
GLfloat previousZ = ( tempZ * cos(previousTheta) ) - ( tempX * sin(previousTheta) );
// horizontal component
glVertex3f(newX, newY, newZ);
glVertex3f(previousX, previousY, previousZ);
}
glEnd();
}
}
previousTheta = theta;
}
glutSwapBuffers();
}
Edit 2: Okay, I see the problem you're running into -- it's a limitation I'd forgotten about (so the code I'd posted previously was dead wrong and wouldn't work at all). The problem is that you're not allowed to call glRotate between a glBegin/glEnd pair -- if you do, it'll set an error flag, and no more drawing will be done.
That does mean you pretty much have to handle the rotation yourself. Fortunately, that's a bit simpler than you've tried to make it:
static const double pi = 3.1416;
for (int point=0; point<NUM_POINTS; point++) {
glBegin(GL_LINE_STRIP);
for (double theta = 0.0; theta < 2.0 * pi; theta += pi/6.0) {
double x = cos(theta);
double z = sin(theta);
glVertex3d(points[point][0]*x, points[point][1], -1.0-points[point][0]*z);
}
glEnd();
}
As-is, this code uses -1.0 along the Z axis as the center of rotation. You can obviously move that where you wish, though anything outside your clipping frustum obviously won't display.
Also note that to get a wireframe, you'll have to draw both your "vertical", and your "horizontal" lines separately, so the code will look something like this:
for (int point=0; point<NUM_POINTS; point++) {
glBegin(GL_LINE_STRIP);
for (double theta = 0.0; theta < 2.0 * pi; theta += pi/6.0) {
double x = cos(theta);
double z = sin(theta);
glVertex3d(points[point][0]*x, points[point][1], -1.0 - points[point][0]*z);
}
glEnd();
}
for (double theta = 0.0; theta < 2.0 * pi; theta += pi/6.0) {
glBegin(GL_LINE_STRIP);
for (int point=0; point<NUM_POINTS; point++) {
double x = cos(theta);
double z = sin(theta);
glVertex3d(points[point][0]*x, points[point][1], -1.0 - points[point][0]*z);
}
glEnd();
}
The trig functions take angles in radians, not degrees.
I also suspect that your viewport isn't setup properly, which explains why you can't see anything on the screen. Typically when I think stuff isn't rendering, it usually is, however, I haven't configured the camera, lighting and other stuff correctly.
Looks like you're trying to construct a surface of revolution/solid of revolution/"lathe object".
A working example:
#include <GL/glut.h>
#include <glm/glm.hpp>
#include <vector>
#include <cmath>
using namespace std;
using namespace glm;
struct Vertex
{
Vertex( const vec3& position, const vec3& normal )
: position( position )
, normal( normal )
{}
vec3 position;
vec3 normal;
};
// spin the pts array around the Z axis.
// pts.x will become the radius, and pts.y will become the height
// pts should be sorted by y-coordinate
vector< Vertex > Lathe( const vector< vec2 >& pts, unsigned int segments = 32 )
{
// precalculate circle points
vector< vec2 > circlePts;
for( unsigned int i = 0; i <= segments; ++i )
{
float angle = ( i / (float)segments ) * 3.14159f * 2.0f;
circlePts.push_back( vec2( cos( angle ), sin( angle ) ) );
}
// fill each layer
typedef vector< vec3 > Layer;
typedef vector< Layer > Layers;
Layers layers( pts.size(), Layer( circlePts.size() ) );
for( size_t i = 0; i < pts.size(); ++i )
{
for( unsigned int j = 0; j < circlePts.size(); ++j )
{
layers[i][j] = vec3( circlePts[j] * pts[i].x, pts[i].y );
}
}
// move through layers generating triangles
vector< Vertex > verts;
for( size_t i = 1; i < layers.size(); ++i )
{
const Layer& prvLayer = layers[ i-1 ];
const Layer& curLayer = layers[ i-0 ];
for( size_t j = 1; j < circlePts.size(); ++j )
{
// upper = cur layer
// UL -- UR
// left | 0 / | right
// = j-1 | / 1 | = j-0
// LL -- LR
// lower = prv layer
const vec3& LL = prvLayer[ j-1 ]; // lower-left
const vec3& LR = prvLayer[ j-0 ]; // lower-right
const vec3& UL = curLayer[ j-1 ]; // upper-left
const vec3& UR = curLayer[ j-0 ]; // upper-right
// triangle0: LL -> UR -> UL
const vec3 normal0 = normalize( cross( UR - LL, UL - LL ) );
verts.push_back( Vertex( LL, normal0 ) );
verts.push_back( Vertex( UR, normal0 ) );
verts.push_back( Vertex( UL, normal0 ) );
// triangle1: LL -> LR -> UR
const vec3 normal1 = normalize( cross( LR - LL, UL - LL ) );
verts.push_back( Vertex( LL, normal1 ) );
verts.push_back( Vertex( LR, normal1 ) );
verts.push_back( Vertex( UR, normal1 ) );
}
}
return verts;
}
// mouse state
int btn;
ivec2 startMouse;
ivec2 startRot, curRot;
void mouse(int button, int state, int x, int y )
{
if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
{
btn = button;
startMouse = ivec2( x, glutGet( GLUT_WINDOW_HEIGHT ) - y );
startRot = curRot;
}
}
void motion( int x, int y )
{
ivec2 curMouse( x, glutGet( GLUT_WINDOW_HEIGHT ) - y );
if( btn == GLUT_LEFT_BUTTON )
{
curRot = startRot + ( curMouse - startMouse );
}
glutPostRedisplay();
}
vector< Vertex > model;
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
double ar = w / h;
gluPerspective( 60, ar, 0.1, 40 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0, 0, -10 );
glPushMatrix();
glRotatef( curRot.x % 360, 0, 1, 0 );
glRotatef( -curRot.y % 360, 1, 0, 0 );
// draw model
if( !model.empty() )
{
glColor3ub( 255, 0, 0 );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_NORMAL_ARRAY );
glVertexPointer( 3, GL_FLOAT, sizeof(Vertex), &model[0].position );
glNormalPointer( GL_FLOAT, sizeof(Vertex), &model[0].normal );
glDrawArrays( GL_TRIANGLES, 0, model.size() );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_NORMAL_ARRAY );
}
// draw bounding cube
glDisable( GL_LIGHTING );
glColor3ub( 255, 255, 255 );
glutWireCube( 7 );
glEnable( GL_LIGHTING );
glPopMatrix();
glutSwapBuffers();
}
int main( int argc, char **argv )
{
vector< vec2 > pts;
pts.push_back( vec2( 0.1, -3 ) );
pts.push_back( vec2( 2, -2 ) );
pts.push_back( vec2( 3, -1 ) );
pts.push_back( vec2( 1, 0 ) );
pts.push_back( vec2( 3, 1 ) );
pts.push_back( vec2( 4, 2 ) );
pts.push_back( vec2( 4, 3 ) );
model = Lathe( pts );
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutMouseFunc( mouse );
glutMotionFunc( motion );
glEnable( GL_DEPTH_TEST );
// set up lighting
glShadeModel( GL_SMOOTH );
glEnable( GL_COLOR_MATERIAL );
glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE );
glEnable( GL_LIGHTING );
// set up "headlamp"-like light
glEnable( GL_LIGHT0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
GLfloat position[] = { 0, 0, 1, 0 };
glLightfv( GL_LIGHT0, GL_POSITION, position );
glPolygonMode( GL_FRONT, GL_FILL );
glPolygonMode( GL_BACK, GL_LINE );
glutMainLoop();
return 0;
}