<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>confabulus &#187; OpenGL</title>
	<atom:link href="http://blog.confabulus.com/category/opengl/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.confabulus.com</link>
	<description>rails, coding, and the goings on at confabulus</description>
	<lastBuildDate>Tue, 26 Jul 2011 20:40:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Simplicity Itself</title>
		<link>http://blog.confabulus.com/2010/02/18/simplicity-itself/</link>
		<comments>http://blog.confabulus.com/2010/02/18/simplicity-itself/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 03:33:41 +0000</pubDate>
		<dc:creator>gaffo</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[OpenGL|ES]]></category>
		<category><![CDATA[Palm Pre]]></category>

		<guid isPermaLink="false">http://blog.confabulus.com/?p=159</guid>
		<description><![CDATA[ I just wanted to share what it took to draw a simple yellow triangle on a black background in OpenGL&#124;ES. I hope it will give my Ruby friends and Haskell friends an aneurysm. 
To show this:

I had to do this:

//The headers
#include &#60;SDL/SDL.h&#62;
#include &#60;SDL/SDL_opengles.h&#62;

//Screen attributes
const int SCREEN_WIDTH = 480;
const int SCREEN_HEIGHT = 320;
const int SCREEN_BPP [...]]]></description>
			<content:encoded><![CDATA[<p> I just wanted to share what it took to draw a simple yellow triangle on a black background in OpenGL|ES. I hope it will give my Ruby friends and Haskell friends an aneurysm. </p>
<p>To show this:<br />
<img src="http://blog.confabulus.com/wp-content/uploads/2010/02/HelloTriangle1.png" alt="Hello Triangle" /></p>
<p>I had to do this:</p>
<pre class="brush: cpp">
//The headers
#include &lt;SDL/SDL.h&gt;
#include &lt;SDL/SDL_opengles.h&gt;

//Screen attributes
const int SCREEN_WIDTH = 480;
const int SCREEN_HEIGHT = 320;
const int SCREEN_BPP = 32;

SDL_Event event;
GLuint programObject;

GLuint LoadShader ( GLenum type, const char *shaderSrc )
{
   GLuint shader;
   GLint compiled;

   // Create the shader object
   shader = glCreateShader ( type );

   if ( shader == 0 )
   	return 0;

   // Load the shader source
   glShaderSource ( shader, 1, &amp;amp;amp;amp;amp;amp;shaderSrc, NULL );

   // Compile the shader
   glCompileShader ( shader );

   // Check the compile status
   glGetShaderiv ( shader, GL_COMPILE_STATUS, &amp;amp;amp;amp;amp;amp;compiled );

   return shader;

}

bool init_GL() {

	const char* vShaderStr = &quot;attribute vec4 vPosition;    \n&quot;
		&quot;void main()                  \n&quot;
		&quot;{                            \n&quot;
		&quot;   gl_Position = vPosition;  \n&quot;
		&quot;}                            \n&quot;;

	const char* fShaderStr = &quot;precision mediump float;\n&quot;
		&quot;void main()                                  \n&quot;
		&quot;{                                            \n&quot;
		&quot;  gl_FragColor = vec4 ( 1.0, 1.0, 0.0, 1.0 );\n&quot;
		&quot;}                                            \n&quot;;

	GLuint vertexShader;
	GLuint fragmentShader;
	GLint linked;

	// Load the vertex/fragment shaders
	vertexShader = LoadShader(GL_VERTEX_SHADER, vShaderStr);
	fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fShaderStr);

	// Create the program object
	programObject = glCreateProgram();

	if (programObject == 0)
		return 0;

	glAttachShader(programObject, vertexShader);
	glAttachShader(programObject, fragmentShader);

	// Bind vPosition to attribute 0
	glBindAttribLocation(programObject, 0, &quot;vPosition&quot;);

	// Link the program
	glLinkProgram(programObject);

	// Check the link status
	glGetProgramiv(programObject, GL_LINK_STATUS, &amp;amp;amp;amp;amp;amp;linked);

	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	return true;
}

bool init() {
	//Initialize SDL
	if (SDL_Init(SDL_INIT_EVERYTHING) &lt; 0) {
//		return false;
	}

	//Create Window
	if (SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL)
			== NULL) {
		return false;
	}

	//Initialize OpenGL
	if (init_GL() == false) {
		return false;
	}

	//Set caption
	SDL_WM_SetCaption(&quot;OpenGL Test&quot;, NULL);

	return true;
}

void clean_up() {
	//Quit SDL
	SDL_Quit();
}

void Draw() {
	GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, -0.5f,
			0.0f };

	// Set the viewport
	glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

	// Clear the color buffer
	glClear(GL_COLOR_BUFFER_BIT);

	// Use the program object
	glUseProgram(programObject);

	// Load the vertex data
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
	glEnableVertexAttribArray(0);

	glDrawArrays(GL_TRIANGLES, 0, 3);
}

int main(int argc, char *argv[]) {
	//Quit flag
	bool quit = false;

	//Initialize
	if (init() == false) {
		return 1;
	}

	//Wait for user exit
	while (quit == false) {

		//While there are events to handle
		while (SDL_PollEvent(&amp;amp;amp;amp;amp;amp;event)) {
			//Handle key presses

			if (event.type == SDL_QUIT) {
				quit = true;
			}
		}
		Draw();
		SDL_GL_SwapBuffers();
	}

	//Clean up
	clean_up();

	return 0;
}
</pre>
<p>You will all be happy to know that one of the first things I did (after re-learning make) was dive into google test and get a testing framework working. My last several projects have been with Ruby and Java, and in my spare time I have been building Palm Pre apps in Javascript and messing around with Erlang.</p>
<p>So I thought it was funny that many of my friends have been going to more and more abstract languages, while I, oddly, have decided to go lower. I have been playing around with doing OpenGL|ES and C++ lately in my spare time, hopefully in prep of the release of the Native SDK (PDK) for the Palm Pre.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.confabulus.com/2010/02/18/simplicity-itself/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

