OpenGL: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added locally-hosted Triangle image)
m (Switch to header template)
Line 5: Line 5:
[[image:Triangle.png|right|thumb|150px|Triangle created using C example compiled with [[GCC]] 4.1.2 and [[freeglut3]].]]
[[image:Triangle.png|right|thumb|150px|Triangle created using C example compiled with [[GCC]] 4.1.2 and [[freeglut3]].]]


==[[C]]==
=={{header|C}}==
[[Category:C]]

'''Compiler:''' [[GCC]] 3.3.3
'''Compiler:''' [[GCC]] 3.3.3


Line 64: Line 62:
</pre>
</pre>


==[[Haskell]]==
=={{header|Haskell}}==
[[Category:Haskell]]

import Graphics.Rendering.OpenGL
import Graphics.Rendering.OpenGL
import Graphics.UI.GLUT
import Graphics.UI.GLUT
Line 93: Line 89:
vertex (Vertex2 x y :: Vertex2 GLfloat)
vertex (Vertex2 x y :: Vertex2 GLfloat)


==[[MAXScript]]==
=={{header|MAXScript}}==
[[Category:MAXScript]]
The choice of OpenGL or D3D in MAX is a user configuration setting. All MAXScript code is platform independent.
The choice of OpenGL or D3D in MAX is a user configuration setting. All MAXScript code is platform independent.
newMesh = mesh numVerts:3 numFaces:1
newMesh = mesh numVerts:3 numFaces:1
Line 108: Line 103:
viewport.SetRenderLevel #smoothhighlights
viewport.SetRenderLevel #smoothhighlights


==[[Perl]]==
=={{header|Perl}}==
[[Category:Perl]]
use OpenGL;
use OpenGL;

Revision as of 05:08, 13 November 2007

Task
OpenGL
You are encouraged to solve this task according to the task description, using any language you may know.

In this task, the goal is to display a smooth shaded triangle with OpenGL.

Triangle created using C example compiled with GCC 4.1.2 and freeglut3.

C

Compiler: GCC 3.3.3

Library: GLUT

In this example, we use GLUT to create a window and handle the main loop in a portable way. Windowing systems like MS Windows and X11 have their own platform-specific ways of handling these things.

#include<GL/gl.h>
#include<GL/glut.h>

void paint(void)
{
	glClearColor(0.3,0.3,0.3,0.0);
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	glShadeModel(GL_SMOOTH);

	glLoadIdentity();
	glTranslatef(-15.0, -15.0, 0.0);

	glBegin(GL_TRIANGLES);
	glColor3f(1.0, 0.0, 0.0);
	glVertex2f(0.0, 0.0);
	glColor3f(0.0, 1.0, 0.0);
	glVertex2f(30.0, 0.0);
	glColor3f(0.0, 0.0, 1.0);
	glVertex2f(0.0, 30.0);
	glEnd();

	glFlush();
}

void reshape(int width, int height)
{
	glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-30.0, 30.0, -30.0, 30.0, -30.0, 30.0);
	glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitWindowSize(640, 480);
	glutCreateWindow("Triangle");

	glutDisplayFunc(paint);
	glutReshapeFunc(reshape);

	glutMainLoop();

	return 0;
}

Haskell

import Graphics.Rendering.OpenGL
import Graphics.UI.GLUT

main = do
  getArgsAndInitialize
   createWindow "Triangle"
  displayCallback $= display
      
  matrixMode $= Projection
  loadIdentity
  ortho2D 0 30 0 30
  matrixMode $= Modelview 0
  
  mainLoop

display = do
  clear [ColorBuffer]
  renderPrimitive Triangles $ do
    corner 1 0 0 5 5
    corner 0 1 0 25 5
    corner 0 0 1 5 25
  swapBuffers
       
corner r g b x y = do color  (Color3  r g b :: Color3  GLfloat)
                      vertex (Vertex2 x y   :: Vertex2 GLfloat)

MAXScript

The choice of OpenGL or D3D in MAX is a user configuration setting. All MAXScript code is platform independent.

newMesh = mesh numVerts:3 numFaces:1
setMesh newMesh vertices:#([-100, -100, 0], [100, -100, 0], [-100, 100, 0]) faces:#([1, 2, 3])
defaultVCFaces newMesh
setVertColor newMesh 1 red
setVertColor newMesh 2 green
setVertColor newMesh 3 blue
setCVertMode newMesh true
update newMesh
viewport.setType #view_top
max tool maximize
viewport.SetRenderLevel #smoothhighlights

Perl

use OpenGL;

sub triangle {
    glBegin GL_TRIANGLES;
    glColor3f 1.0, 0.0, 0.0;
    glVertex2f 5.0, 5.0;
    glColor3f 0.0, 1.0, 0.0;
    glVertex2f 25.0, 5.0;
    glColor3f 0.0, 0.0, 1.0;
    glVertex2f 5.0, 25.0;
    glEnd;
};

glpOpenWindow;
glMatrixMode GL_PROJECTION;
glLoadIdentity;
gluOrtho2D 0.0, 30.0, 0.0, 30.0;
glMatrixMode GL_MODELVIEW;

glClear GL_COLOR_BUFFER_BIT;
triangle;
glpFlush;

glpMainLoop;