Skip to content
This repository has been archived by the owner on Sep 4, 2019. It is now read-only.

Commit

Permalink
Added OpenGL ES 2.0 Programming Guide samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
Darryl Gough committed Jan 12, 2012
1 parent befb5d0 commit f1576f2
Show file tree
Hide file tree
Showing 59 changed files with 11,199 additions and 0 deletions.
574 changes: 574 additions & 0 deletions OpenGLES2-ProgrammingGuide/Ch10_MultiTexture/.cproject

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions OpenGLES2-ProgrammingGuide/Ch10_MultiTexture/.project
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Ch10_MultiTexture</name>
<comment></comment>
<projects>
<project>Common</project>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
<dictionary>
<key>?name?</key>
<value></value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.append_environment</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildArguments</key>
<value></value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildCommand</key>
<value>make</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildLocation</key>
<value>${workspace_loc:/Ch10_MultiTexture/Simulator-Debug}</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.contents</key>
<value>org.eclipse.cdt.make.core.activeConfigSettings</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableAutoBuild</key>
<value>false</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableCleanBuild</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableFullBuild</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.stopOnError</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key>
<value>true</value>
</dictionary>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.qnx.tools.bbt.xml.core.bbtXMLValidationBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
<nature>com.qnx.tools.ide.bbt.core.bbtnature</nature>
</natures>
</projectDescription>
107 changes: 107 additions & 0 deletions OpenGLES2-ProgrammingGuide/Ch10_MultiTexture/bar-descriptor.xml
@@ -0,0 +1,107 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<qnx xmlns="http://www.qnx.com/schemas/application/1.0">

<!-- BlackBerry Tablet OS application descriptor file.
Specifies parameters for identifying, installing, and launching native applications on BlackBerry Tablet OS.
-->

<!-- A universally unique application identifier. Must be unique across all BlackBerry Tablet OS applications.
Using a reverse DNS-style name as the id is recommended. (Eg. com.example.ExampleApplication.) Required. -->
<id>com.example.Ch10_MultiTexture</id>

<!-- The name that is displayed in the BlackBerry Tablet OS application installer.
May have multiple values for each language. See samples or xsd schema file. Optional. -->
<name>Ch10_MultiTexture</name>

<!-- A string value of the format <0-999>.<0-999>.<0-999> that represents application version which can be used to check for application upgrade.
Values can also be 1-part or 2-part. It is not necessary to have a 3-part value.
An updated version of application must have a versionNumber value higher than the previous version. Required. -->
<versionNumber>1.0.0</versionNumber>

<!-- Fourth digit segment of the package version. First three segments are taken from the
<versionNumber> element. Must be an integer from 0 to 2^16-1 -->
<buildId>1</buildId>

<!-- A string value (such as "v1", "2.5", or "Alpha 1") that represents the version of the application, as it should be shown to users. Optional. -->
<!-- <versionLabel></versionLabel> -->

<!-- Description, displayed in the BlackBerry Tablet OS application installer.
May have multiple values for each language. See samples or xsd schema file. Optional. -->
<description>The Ch10_MultiTexture application</description>

<!-- Copyright information. Optional. -->
<!-- <copyright></copyright> -->

<!-- Name of author which is used for signing. Must match the developer name of your development certificate. -->
<author>Example Inc.</author>

<!-- Unique author ID assigned by signing authority. Required if using debug tokens. -->
<!-- <authorId>ABC1234YjsnUk235h</authorId> -->

<initialWindow>
<systemChrome>none</systemChrome>
<transparent>false</transparent>
</initialWindow>

<!-- The category where the application appears. Either core.games or core.media. -->
<category>core.games</category>
<asset path="icon.png">icon.png</asset>
<asset path="res/textures/basemap.tga">res/textures/basemap.tga</asset>
<asset path="res/textures/lightmap.tga">res/textures/lightmap.tga</asset>
<configuration id="com.qnx.qcc.configuration.exe.debug.1987038463" name="Device-Debug">
<platformArchitecture>armle-v7</platformArchitecture>
<asset path="Device-Debug/Ch10_MultiTexture" entry="true" type="Qnx/Elf">Ch10_MultiTexture</asset>
</configuration>
<configuration id="com.qnx.qcc.configuration.exe.release.206455987" name="Device-Release">
<platformArchitecture>armle-v7</platformArchitecture>
<asset path="Device-Release/Ch10_MultiTexture" entry="true" type="Qnx/Elf">Ch10_MultiTexture</asset>
</configuration>
<configuration id="com.qnx.qcc.configuration.exe.profile.319483952" name="Device-Profile">
<platformArchitecture>armle-v7</platformArchitecture>
<asset path="Device-Profile/Ch10_MultiTexture" entry="true" type="Qnx/Elf">Ch10_MultiTexture</asset>
</configuration>
<configuration id="com.qnx.qcc.configuration.exe.profile.coverage.1379089284" name="Device-Coverage">
<platformArchitecture>armle-v7</platformArchitecture>
<asset path="Device-Coverage/Ch10_MultiTexture" entry="true" type="Qnx/Elf">Ch10_MultiTexture</asset>
</configuration>
<configuration id="com.qnx.qcc.configuration.exe.debug.1632401311" name="Simulator-Debug">
<platformArchitecture>x86</platformArchitecture>
<asset path="Simulator-Debug/Ch10_MultiTexture" entry="true" type="Qnx/Elf">Ch10_MultiTexture</asset>
</configuration>
<configuration id="com.qnx.qcc.configuration.exe.profile.377816019" name="Simulator-Profile">
<platformArchitecture>x86</platformArchitecture>
<asset path="Simulator-Profile/Ch10_MultiTexture" entry="true" type="Qnx/Elf">Ch10_MultiTexture</asset>
</configuration>
<configuration id="com.qnx.qcc.configuration.exe.profile.coverage.761276608" name="Simulator-Coverage">
<platformArchitecture>x86</platformArchitecture>
<asset path="Simulator-Coverage/Ch10_MultiTexture" entry="true" type="Qnx/Elf">Ch10_MultiTexture</asset>
</configuration>

<!-- The icon for the application, which should be 86x86. -->
<icon>
<image>icon.png</image>
</icon>

<!-- The splash screen that will appear when your application is launching. Should be 1024x600. -->
<!-- <splashscreen></splashscreen> -->

<!-- Request permission to execute native code. Required for native applications. -->
<action system="true">run_native</action>

<!-- The permissions requested by your application. -->
<!-- <action>access_shared</action> -->
<!-- <action>record_audio</action> -->
<!-- <action>read_geolocation</action> -->
<!-- <action>use_camera</action> -->
<!-- <action>access_internet</action> -->
<!-- <action>play_audio</action> -->
<!-- <action>post_notification</action> -->
<!-- <action>set_audio_volume</action> -->
<!-- <action>read_device_identifying_information</action> -->

<!-- Ensure that shared libraries in the package are found at run-time. -->
<env var="LD_LIBRARY_PATH" value="app/native/lib"/>

</qnx>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
212 changes: 212 additions & 0 deletions OpenGLES2-ProgrammingGuide/Ch10_MultiTexture/src/MultiTexture.c
@@ -0,0 +1,212 @@
//
// Book: OpenGL(R) ES 2.0 Programming Guide
// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
// ISBN-10: 0321502795
// ISBN-13: 9780321502797
// Publisher: Addison-Wesley Professional
// URLs: http://safari.informit.com/9780321563835
// http://www.opengles-book.com
// Additional contributions copyright (c) 2011 Research In Motion Limited

// MultiTexture.c
//
// This is an example that draws a quad with a basemap and
// lightmap to demonstrate multitexturing.
//
#include <stdlib.h>
#include "esUtil.h"

typedef struct
{
// Handle to a program object
GLuint programObject;

// Attribute locations
GLint positionLoc;
GLint texCoordLoc;

// Sampler locations
GLint baseMapLoc;
GLint lightMapLoc;

// Texture handle
GLuint baseMapTexId;
GLuint lightMapTexId;

} UserData;


///
// Load texture from disk
//
GLuint LoadTexture(char *fileName)
{
int width, height;
char *buffer = esLoadTGA(fileName, &width, &height);

GLuint texId;

if (buffer == NULL)
{
esLogMessage("Error loading (%s) image.\n", fileName);
return 0;
}

glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

free(buffer);

return texId;
}

///
// Initialize the shader and program object
//
int Init(ESContext *esContext)
{
UserData *userData = esContext->userData;
GLchar vShaderStr[] =
"attribute vec4 a_position; \n"
"attribute vec2 a_texCoord; \n"
"varying vec2 v_texCoord; \n"
"void main() \n"
"{ \n"
" gl_Position = a_position; \n"
" v_texCoord = a_texCoord; \n"
"} \n";

GLchar fShaderStr[] =
"precision mediump float; \n"
"varying vec2 v_texCoord; \n"
"uniform sampler2D s_baseMap; \n"
"uniform sampler2D s_lightMap; \n"
"void main() \n"
"{ \n"
" vec4 baseColor; \n"
" vec4 lightColor; \n"
" \n"
" baseColor = texture2D( s_baseMap, v_texCoord ); \n"
" lightColor = texture2D( s_lightMap, v_texCoord ); \n"
" gl_FragColor = baseColor * (lightColor + 0.25); \n"
"} \n";

// Load the shaders and get a linked program object
userData->programObject = esLoadProgram(vShaderStr, fShaderStr);

// Get the attribute locations
userData->positionLoc = glGetAttribLocation(userData->programObject, "a_position");
userData->texCoordLoc = glGetAttribLocation(userData->programObject, "a_texCoord");

// Get the sampler location
userData->baseMapLoc = glGetUniformLocation(userData->programObject, "s_baseMap");
userData->lightMapLoc = glGetUniformLocation(userData->programObject, "s_lightMap");

// Load the textures
userData->baseMapTexId = LoadTexture("res/textures/basemap.tga");
userData->lightMapTexId = LoadTexture("res/textures/lightmap.tga");

if (userData->baseMapTexId == 0 || userData->lightMapTexId == 0)
return FALSE;

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

///
// Draw a triangle using the shader pair created in Init()
//
void Draw(ESContext *esContext)
{
UserData *userData = esContext->userData;
GLfloat vVertices[] = {
-0.5f, 0.5f, 0.0f, // Position 0
0.0f, 0.0f, // TexCoord 0
-0.5f, -0.5f, 0.0f, // Position 1
0.0f, 1.0f, // TexCoord 1
0.5f, -0.5f, 0.0f, // Position 2
1.0f, 1.0f, // TexCoord 2
0.5f, 0.5f, 0.0f, // Position 3
1.0f, 0.0f // TexCoord 3
};
GLushort indices[] =
{ 0, 1, 2, 0, 2, 3 };

// Set the viewport
glViewport(0, 0, esContext->width, esContext->height);

// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);

// Use the program object
glUseProgram(userData->programObject);

// Load the vertex position
glVertexAttribPointer(userData->positionLoc, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vVertices);
// Load the texture coordinate
glVertexAttribPointer(userData->texCoordLoc, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3]);

glEnableVertexAttribArray(userData->positionLoc);
glEnableVertexAttribArray(userData->texCoordLoc);

// Bind the base map
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, userData->baseMapTexId);

// Set the base map sampler to texture unit to 0
glUniform1i(userData->baseMapLoc, 0);

// Bind the light map
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, userData->lightMapTexId);

// Set the light map sampler to texture unit 1
glUniform1i(userData->lightMapLoc, 1);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);

eglSwapBuffers(esContext->eglDisplay, esContext->eglSurface);
}

///
// Cleanup
//
void ShutDown(ESContext *esContext)
{
UserData *userData = esContext->userData;

// Delete texture object
glDeleteTextures(1, &userData->baseMapTexId);
glDeleteTextures(1, &userData->lightMapTexId);

// Delete program object
glDeleteProgram(userData->programObject);
}

int main(int argc, char *argv[])
{
ESContext esContext;
UserData userData;

esInitContext(&esContext);
esContext.userData = &userData;

esCreateWindow(&esContext, "MultiTexture", 1024, 600, ES_WINDOW_RGB);

if (!Init(&esContext))
return 0;

esRegisterDrawFunc(&esContext, Draw);

esMainLoop(&esContext);

ShutDown(&esContext);

return 0;
}

0 comments on commit f1576f2

Please sign in to comment.